| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/grtext/textaux.cpp $ |
| 21 | * $Revision: 6 $ |
| 22 | * $Date: 5/19/99 10:00p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Auxillary Text functions (helper function, not necessarily belonging to grtext) |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/grtext/textaux.cpp $ |
| 28 | * |
| 29 | * 6 5/19/99 10:00p Jeff |
| 30 | * fixed word-wrap not to wrap words that are bigger than the line |
| 31 | * |
| 32 | * 5 11/03/98 7:04p Samir |
| 33 | * made Grtext_spacing global so word wrapper could access it. |
| 34 | * |
| 35 | * 4 11/03/98 3:12p Jeff |
| 36 | * fixed wordwrap so it doesn't modify source buffer |
| 37 | * |
| 38 | * 3 7/14/98 2:47p Jeff |
| 39 | * added textaux_ClipString |
| 40 | * |
| 41 | * 2 7/14/98 11:53a Samir |
| 42 | * moved textaux to it's own libtary again. |
| 43 | * |
| 44 | * 1 7/13/98 4:41p Samir |
| 45 | * |
| 46 | * 2 7/11/98 9:14p Jeff |
| 47 | * initial creation (moved from TelCom) |
| 48 | * |
| 49 | * $NoKeywords: $ |
| 50 | */ |
| 51 | |
| 52 | #include <cstdio> |
| 53 | #include <cstring> |
| 54 | |
| 55 | #include "grtext.h"cxx . . $ `<file>/grtext/grtext.h`/ |
| 56 | |
| 57 | // textaux_WordWrap |
| 58 | // |
| 59 | // Given a buffer of text, and an empty buffer of same size |
| 60 | // it will break the source buffer up into lines (seperated by /n) of size width or smaller (in pixels). |
| 61 | // All /n within the source buffer are preserved. Source buffer is also not changed. |
| 62 | void textaux_WordWrapcxx . . $ textaux_WordWrap(2480230cce71d9d7).(const char *src, char *dest, int width, int font) { |
| 63 | // make sure we are kosher with the values past in |
| 64 | if ((!src) || (!dest)) |
| 65 | return; |
| 66 | if (src[0] == '\0') |
| 67 | return; |
| 68 | |
| 69 | char *last_word; // points to the character after the last complete word |
| 70 | char *start_line; // points to the starting char of the line |
| 71 | // char *destpos; //points to the current position of the dest buffer |
| 72 | int curr_width; // current width |
| 73 | int index; // index into source string |
| 74 | bool done = false; // if we are done adjusting for word-wrap |
| 75 | |
| 76 | // copy the source buffer into the destination buffer, where all work will go |
| 77 | strcpy(dest, src); |
| 78 | |
| 79 | // initialize vars |
| 80 | last_word = start_line = dest; |
| 81 | curr_width = 0; |
| 82 | index = 0; |
| 83 | |
| 84 | int num_words_on_line = 0; |
| 85 | |
| 86 | while (!done) { |
| 87 | curr_width = 0; |
| 88 | index = 0; |
| 89 | num_words_on_line = 0; |
| 90 | |
| 91 | while (curr_width <= width || num_words_on_line == 0) { |
| 92 | switch (start_line[index]) { |
| 93 | case '\0': |
| 94 | // we are done with everything |
| 95 | done = true; |
| 96 | case '\n': |
| 97 | // we are done with this line |
| 98 | curr_width = width + 1; |
| 99 | case ' ': |
| 100 | // update last_word |
| 101 | last_word = &start_line[index]; |
| 102 | num_words_on_line++; |
| 103 | break; |
| 104 | } |
| 105 | if ((start_line[index] != '\0') && (start_line[index] != '\n')) |
| 106 | curr_width += (grfont_GetCharWidthcxx . . $ grfont_GetCharWidth(9b79fb6aee4c0440).(font, start_line[index]) + Grtext_spacingcxx . . $ Grtext_spacing.); |
| 107 | index++; |
| 108 | } |
| 109 | // put a newline char |
| 110 | if (!done) { |
| 111 | last_word[0] = '\n'; |
| 112 | } |
| 113 | |
| 114 | // copy from start_line->last_word into the dest buffer |
| 115 | start_line += (last_word - start_line + 1); |
| 116 | last_word = start_line; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // textaux_CopyTextLine |
| 121 | // |
| 122 | // This function goes hand-in-hand with textaux_WordWrap(). Given a buffer of data it will fill in |
| 123 | // the dest buffer until it hits a /n or /0. It returns a pointer to the start position of the next line, |
| 124 | // or NULL if it's done with the buffer (it hit a /0). |
| 125 | const char *textaux_CopyTextLinecxx . . $ textaux_CopyTextLine(327ecc8d2b0bb221).(const char *src, char *dest) { |
| 126 | // make sure src and dest are allocated |
| 127 | if (!src) { |
| 128 | if (dest) |
| 129 | dest[0] = '\0'; |
| 130 | return nullptr; |
| 131 | } |
| 132 | if (!dest) |
| 133 | return nullptr; |
| 134 | // see if we are at the end of the src |
| 135 | if (src[0] == '\0') { |
| 136 | dest[0] = '\0'; |
| 137 | return nullptr; |
| 138 | } |
| 139 | int i; |
| 140 | i = 0; |
| 141 | // find the end |
| 142 | while ((src[i] != '\n') && (src[i] != '\0')) |
| 143 | i++; |
| 144 | if (src[i] == '\0') { |
| 145 | // no more lines left after this |
| 146 | strncpy(dest, src, i); |
| 147 | dest[i] = '\0'; |
| 148 | return nullptr; |
| 149 | } else { |
| 150 | // we hit a newline char |
| 151 | strncpy(dest, src, i); |
| 152 | dest[i] = '\0'; |
| 153 | i++; |
| 154 | return &src[i]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // textaux_ClipString |
| 159 | // Given a width (in pixels), and a string, this function will truncate the string |
| 160 | // to at most width pixels. If the end parameter is not 0, then that char is attached to |
| 161 | // the end of the string if it has to clip(the char's width is taken into consideration). |
| 162 | // It is based off the current font. if horizont_ratio is given it is used to correct for |
| 163 | // possible different sized hud fonts. For instance, if this string is going to be printed on |
| 164 | // the hud, then you should always pass (DEFAULT_HUD_WIDTH/((float)*Game_window_w)) as the |
| 165 | // horizont_ratio paramter. |
| 166 | void textaux_ClipStringcxx . . $ textaux_ClipString(2c11c8157ca6455b).(int width, char *string, float horizont_ratio, char end) { |
| 167 | if (!string) |
| 168 | return; |
| 169 | |
| 170 | float ratio = horizont_ratio; |
| 171 | |
| 172 | int string_length = strlen(string); |
| 173 | char arrow_string[2]; |
| 174 | int arrow_length = 0; |
| 175 | |
| 176 | if (end) { |
| 177 | snprintf(arrow_string, sizeof(arrow_string), "%c", end); |
| 178 | arrow_length = (int)(ratio * ((float)grtext_GetTextLineWidthcxx . . $ grtext_GetTextLineWidth(5c4306d8553b3696).(arrow_string))); |
| 179 | } |
| 180 | |
| 181 | if (width < arrow_length) { |
| 182 | // Nothing can fit |
| 183 | strcpy(string, ""); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // adjust width based on the arrow |
| 188 | width -= arrow_length; |
| 189 | |
| 190 | int size = 0; |
| 191 | char save = string[0]; |
| 192 | while (size < string_length) { |
| 193 | string[size] = '\0'; |
| 194 | if ((((float)grtext_GetTextLineWidthcxx . . $ grtext_GetTextLineWidth(5c4306d8553b3696).(string)) * ratio) >= width) { |
| 195 | // We have to clip |
| 196 | size--; |
| 197 | if (end) { |
| 198 | string[size] = end; |
| 199 | string[size + 1] = '\0'; |
| 200 | } |
| 201 | return; |
| 202 | } |
| 203 | // replace the char and move to the next |
| 204 | string[size] = save; |
| 205 | size++; |
| 206 | save = string[size]; |
| 207 | } |
| 208 | // The string didn't need to be clipped |
| 209 | } |