Debian Codegraph @

2dlib/pentext.cpp

raw symbols SCIP
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/2dlib/pentext.cpp $
21 * $Revision: 8 $
22 * $Date: 11/01/98 1:57a $
23 * $Author: Jeff $
24 *
25 * handles text drawing caps for viewports
26 *
27 * $Log: /DescentIII/Main/2dlib/pentext.cpp $
28 *
29 * 8 11/01/98 1:57a Jeff
30 * converted the vsprintf calls to use the Pvsprintf, which is a safe
31 * vsprintf, no buffer overflows allowed
32 *
33 * 7 12/19/97 12:31p Samir
34 * Better renderer support.
35 *
36 * 6 12/12/97 6:40p Samir
37 * Some viewport font functions.
38 *
39 * 5 11/17/97 3:46p Matt
40 * Made the tab character move the current text location to the next even
41 * multiple of the width of a space
42 *
43 * 4 11/16/97 5:06p Matt
44 * Fixed puts() to deal correctly with multiple blank likes in a string.
45 *
46 * 3 11/14/97 5:37p Matt
47 * Fixed small bug w/ printing multi-line strings
48 *
49 * 2 8/15/97 6:34p Samir
50 * Fixed bug when passing an empty string to puts.
51 *
52 * 13 3/04/97 12:06p Samir
53 * fixed bug in draw_clip_text_line.
54 *
55 * 12 2/13/97 3:44p Samir
56 * get_text_line_width works correctly with special color text formatting.
57 *
58 * 11 2/13/97 2:24p Samir
59 * Added mid-string color-coding text. Note that when
60 * done with text operation, the original color is restored.
61 *
62 * 10 2/06/97 6:33p Samir
63 * Untested clipping of text.
64 *
65 * 9 2/06/97 12:44p Samir
66 * Made change to draw_char call based off change of prototype.
67 *
68 * 8 2/05/97 10:35a Samir
69 * Fixed text drawing at wrong location problem
70 *
71 * 7 2/04/97 11:14a Samir
72 * Improved clipping system by adding a separate clipport based off of the
73 * viewport's rectangle in it's parent surface
74 *
75 * 6 2/03/97 3:00p Samir
76 * Fixed x updating problem in draw_text_line
77 *
78 * 5 1/30/97 7:01p Samir
79 * updated font stuff
80 *
81 * $NoKeywords: $
82 */
83
84 #include <cstdio>
85 #include <cstdarg>
86 #include <cstring>
87
88 #include "gr.h"
89 #include "mono.h"
90 #include "renderer.h"
91
92 #define CLIP_LEFT (vp_InitLeft + vp_Left)
93 #define CLIP_TOP (vp_InitTop + vp_Top)
94 #define CLIP_RIGHT (vp_InitLeft + vp_Right)
95 #define CLIP_BOTTOM (vp_InitTop + vp_Bottom)
96
97 #define STR_BUF_SIZE 512
98 static char Str_buf[STR_BUF_SIZE]; // String buffer for variable arguments
99 static char Draw_str_buf[STR_BUF_SIZE]; // used to buffer words in a string
100
101 // ---------------------------------------------------------------------------
102 // Viewport font and text funcitons
103 // ---------------------------------------------------------------------------
104
105 void grViewport::set_font(const char *fontname) { text_Font.init(fontname); }
106
107 void grViewport::get_font(char *fontname, int size) {
108 ASSERT(size > (int)strlen(text_Font.get_name()));
109 strcpy(fontname, text_Font.get_name());
110 }
111
112 // ---------------------------------------------------------------------------
113 // Standard text functions
114 // ---------------------------------------------------------------------------
115
116 int grViewport::printf(grTextAlign align, int x, int y, char *fmt, ...) {
117 std::va_list arglist;
118 int len;
119
120 va_start(arglist, fmt);
121 len = std::vsnprintf(Str_buf, STR_BUF_SIZE, fmt, arglist);
122 va_end(arglist);
123 if (len < 0)
124 return 0;
125
126 return grViewport::puts(align, x, y, Str_buf);
127 }
128
129 int grViewport::printf(int x, int y, char *fmt, ...) {
130 std::va_list arglist;
131 int len;
132
133 va_start(arglist, fmt);
134 len = std::vsnprintf(Str_buf, STR_BUF_SIZE, fmt, arglist);
135 va_end(arglist);
136 if (len < 0)
137 return 0;
138
139 return grViewport::puts(grTextUnjustified, x, y, Str_buf);
140 }
141
142 int grViewport::puts(int x, int y, char *str) { return grViewport::puts(grTextUnjustified, x, y, str); }
143
144 // Ultimately all text output string functions call this monolith which
145 // calls the ddgr interface.
146
147 int grViewport::puts(grTextAlign align, int x, int y, char *str) {
148 int cur_x, cur_y, gx, gy;
149 char *line, *save_pos;
150 tCharProperties old_props;
151
152 // setup rendering of text.
153 rend_SetTextureType(TT_LINEAR);
154 rend_SetOverlayType(OT_NONE);
155 rend_SetFiltering(0);
156 rend_SetLighting(LS_GOURAUD);
157 rend_SetAlphaType(ATF_TEXTURE + ATF_CONSTANT);
158 rend_SetColorModel(CM_RGB);
159 rend_SetZBufferState(0);
160 rend_SetAlphaValue(char_Props.alpha);
161
162 get_char_properties(&old_props);
163
164 cur_x = x;
165 cur_y = y;
166
167 line = str;
168
169 do {
170 // Look for newline, and if found, put in a 0 to terminate the line
171 save_pos = strchr(line, '\n');
172 if (save_pos) // found an newline
173 *save_pos = 0; // terminate the line
174
175 int line_width;
176 int clipped;
177
178 line_width = get_text_line_width(line);
179
180 if (align == grTextCentered)
181 cur_x = (((vp_Right - vp_Left) + 1) / 2) - (line_width / 2);
182 else if (align == grTextLeft)
183 cur_x = 0;
184
185 gy = global_y(cur_y);
186 gx = global_x(cur_x); // Clip function needs global coords.
187
188 clipped = 0;
189 if ((CLIP_TOP > (gy + text_Font.height())) || (CLIP_BOTTOM < gy))
190 clipped = 2;
191 else if ((CLIP_LEFT > (gx + line_width)) || (CLIP_RIGHT < gx))
192 clipped = 2;
193
194 if (clipped != 2) {
195 if (CLIP_LEFT > gx || CLIP_RIGHT < (gx + line_width))
196 clipped = 1;
197 if (CLIP_TOP > gy || CLIP_BOTTOM < (gy + text_Font.height()))
198 clipped = 1;
199
200 if (clipped == 0)
201 draw_text_line(gx, gy, line);
202 else if (clipped == 1)
203 draw_text_line_clip(gx, gy, line);
204 }
205 cur_y += (get_text_line_spacing() + text_Font.height());
206 cur_x = x;
207
208 // replace the newline, if there was one
209 if (save_pos)
210 *save_pos++ = '\n';
211
212 line = save_pos;
213
214 } while (line);
215
216 set_char_properties(GR_VPCP_ALL, &old_props);
217
218 rend_SetFiltering(1);
219 rend_SetZBufferState(1);
220
221 return strlen(str);
222 }
223
224 // ---------------------------------------------------------------------------
225 // Private functions
226 // ---------------------------------------------------------------------------
227
228 // get_text_line_width()
229 // returns length in pixels of text line using current text spacing settings
230
231 int grViewport::get_text_line_width(char *str) {
232 int line_width = 0;
233 int rgb_define_mode = 0;
234
235 for (int i = 0; i < (int)strlen(str); i++) {
236 int width;
237
238 // note that if we hit the GR_COLOR_CHAR then the next three values should
239 // not count when defining the width of the line.
240 if (rgb_define_mode == 3)
241 rgb_define_mode = 0;
242 else if (str[i] == GR_COLOR_CHAR)
243 rgb_define_mode = 1;
244 if (!rgb_define_mode) {
245 if (str[i] == '\t') { // tab char
246 int space_width;
247 text_Font.get_char_info(' ', &space_width);
248 space_width += text_Spacing;
249 line_width = (line_width + space_width - 1) / space_width * space_width;
250 } else {
251 text_Font.get_char_info((int)str[i], &width);
252 line_width += (width + get_text_spacing());
253 }
254 } else
255 rgb_define_mode++;
256 }
257
258 return line_width - get_text_spacing();
259 }
260
261 /* These internal routines just draw a line of text. depending on the function
262 we clip or don't clip
263 */
264
265 void grViewport::draw_text_line_clip(int x, int y, char *str) {
266 int ch_y, ch_x, ch_w, ch_h; // what part of the character to draw
267 int i, cur_x, draw_x, draw_y; // where to draw character section
268 int h;
269
270 /* by clipping, we should first determine what our vertical clipping is. then
271 go through each character in the line and determine what is totally clipped,
272 partially clipped and by how much, and not clipped at all and draw accordingly
273 */
274 h = text_Font.height();
275 ch_y = 0;
276 ch_h = h;
277 draw_y = y;
278 // determine each character bitmap y and height to blt.
279 if (CLIP_TOP >= y) {
280 ch_y = CLIP_TOP - y;
281 draw_y = CLIP_TOP;
282 }
283 if (CLIP_BOTTOM < (y + text_Font.height())) {
284 ch_h = CLIP_BOTTOM - y;
285 }
286 ch_h = ch_h - ch_y; // do this to clip both top and bottom
287
288 cur_x = x;
289 for (i = 0; i < (int)strlen(str); i++) {
290 uint8_t ch;
291 int w;
292 ch = (uint8_t)str[i];
293 text_Font.get_char_info((int)ch, &w);
294
295 if (ch == GR_COLOR_CHAR) {
296 if ((i + 3) >= (int)strlen(str))
297 Int3(); // This shouldn't happen! bad string!
298 set_text_color(GR_RGB(str[i + 1], str[i + 2], str[i + 3]));
299 i += 4;
300 if (i >= (int)strlen(str))
301 Int3(); // This shouldn't happen too.
302 ch = (uint8_t)str[i];
303 } else if (ch == '\t') { // tab char
304 int space_width;
305 text_Font.get_char_info(' ', &space_width);
306 space_width += text_Spacing;
307 cur_x = (cur_x + space_width - 1) / space_width * space_width;
308 }
309
310 // do horizontal clipping
311 if (((cur_x + w) < CLIP_LEFT) || (cur_x > CLIP_RIGHT)) {
312 cur_x += (text_Spacing + w);
313 } else {
314 ch_x = 0;
315 ch_w = w;
316 draw_x = cur_x;
317 if (CLIP_LEFT > cur_x) {
318 ch_x = CLIP_LEFT - cur_x;
319 draw_x = CLIP_LEFT;
320 }
321 if (CLIP_RIGHT < (cur_x + w)) {
322 ch_w = CLIP_RIGHT - cur_x;
323 }
324 ch_w = ch_w - ch_x;
325
326 if (ch_x == 0 && ch_w == w && ch_y == 0 && ch_h == h)
327 cur_x = text_Font.draw_char(sf_Parent, draw_x, draw_y, ch, &char_Props);
328 else
329 cur_x = text_Font.draw_char(sf_Parent, draw_x, draw_y, ch, ch_x, ch_y, ch_w, ch_h, &char_Props);
330
331 cur_x += text_Spacing;
332 }
333 }
334 }
335
336 void grViewport::draw_text_line(int x, int y, char *str) {
337 unsigned i, cur_x;
338
339 /* perform string drawing without viewport clipping.
340 supports bitmap fonts or color(alpha) mapped fonts.
341 */
342 cur_x = x;
343 for (i = 0; i < (int)strlen(str); i++) {
344 uint8_t ch;
345
346 ch = (uint8_t)str[i];
347
348 if (ch == GR_COLOR_CHAR) {
349 if ((i + 3) >= (int)strlen(str))
350 Int3(); // This shouldn't happen! bad string!
351 set_text_color(GR_RGB(str[i + 1], str[i + 2], str[i + 3]));
352 i += 4;
353 if (i >= (int)strlen(str))
354 Int3(); // This shouldn't happen too.
355 ch = (uint8_t)str[i];
356 } else if (ch == '\t') { // tab char
357 int space_width;
358 text_Font.get_char_info(' ', &space_width);
359 space_width += text_Spacing;
360 cur_x = (cur_x + space_width - 1) / space_width * space_width;
361 } else {
362 cur_x = text_Font.draw_char(sf_Parent, cur_x, y, ch, &char_Props);
363 cur_x += text_Spacing;
364 }
365 }
366 }