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"cxx . . $ `<file>/2dlib/gr.h`/
89 #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/
90 #include "renderer.h"cxx . . $ `<file>/lib/renderer.h`/
91
92 #define CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`! (vp_InitLeft + vp_Left)
93 #define CLIP_TOPcxx . . $ `2dlib/pentext.cpp:93:9`! (vp_InitTop + vp_Top)
94 #define CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`! (vp_InitLeft + vp_Right)
95 #define CLIP_BOTTOMcxx . . $ `2dlib/pentext.cpp:95:9`! (vp_InitTop + vp_Bottom)
96
97 #define STR_BUF_SIZEcxx . . $ `2dlib/pentext.cpp:97:9`! 512
98 static char Str_bufcxx . . $ Str_buf.[STR_BUF_SIZEcxx . . $ `2dlib/pentext.cpp:97:9`!]; // String buffer for variable arguments
99 static char Draw_str_bufcxx . . $ Draw_str_buf.[STR_BUF_SIZEcxx . . $ `2dlib/pentext.cpp:97:9`!]; // used to buffer words in a string
100
101 // ---------------------------------------------------------------------------
102 // Viewport font and text funcitons
103 // ---------------------------------------------------------------------------
104
105 void grViewportcxx . . $ grViewport#::set_fontcxx . . $ grViewport#set_font(85c52e162fed56f9).(const char *fontname) { text_Fontcxx . . $ grViewport#text_Font..initcxx . . $ grFont#init(85c52e162fed56f9).(fontname); }
106
107 void grViewportcxx . . $ grViewport#::get_fontcxx . . $ grViewport#get_font(a07d8cccb7673f5a).(char *fontname, int size) {
108 ASSERTcxx . . $ `misc/pserror.h:246:9`!(size > (int)strlen(text_Font.get_name()));
109 strcpy(fontname, text_Fontcxx . . $ grViewport#text_Font..get_namecxx . . $ grFont#get_name(e7e5aa338b9d5147).());
110 }
111
112 // ---------------------------------------------------------------------------
113 // Standard text functions
114 // ---------------------------------------------------------------------------
115
116 int grViewportcxx . . $ grViewport#::printfcxx . . $ grViewport#printf(d6cf461368ff6cc0).(grTextAligncxx . . $ grTextAlign# align, int x, int y, char *fmt, ...) {
117 stdcxx . . $ std/::va_listcxx . . $ std/va_list# arglist;
118 int len;
119
120 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(arglist, fmt);
121 len = stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(Str_bufcxx . . $ Str_buf., STR_BUF_SIZEcxx . . $ `2dlib/pentext.cpp:97:9`!, fmt, arglist);
122 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(arglist);
123 if (len < 0)
124 return 0;
125
126 return grViewportcxx . . $ grViewport#::putscxx . . $ grViewport#puts(76a9c7ba36c5c57d).(align, x, y, Str_bufcxx . . $ Str_buf.);
127 }
128
129 int grViewportcxx . . $ grViewport#::printfcxx . . $ grViewport#printf(13a7a0696f737ebf).(int x, int y, char *fmt, ...) {
130 stdcxx . . $ std/::va_listcxx . . $ std/va_list# arglist;
131 int len;
132
133 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(arglist, fmt);
134 len = stdcxx . . $ std/::vsnprintfcxx . . $ std/vsnprintf(348eb892742d8727).(Str_bufcxx . . $ Str_buf., STR_BUF_SIZEcxx . . $ `2dlib/pentext.cpp:97:9`!, fmt, arglist);
135 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(arglist);
136 if (len < 0)
137 return 0;
138
139 return grViewportcxx . . $ grViewport#::putscxx . . $ grViewport#puts(76a9c7ba36c5c57d).(grTextUnjustifiedcxx . . $ grTextUnjustified., x, y, Str_bufcxx . . $ Str_buf.);
140 }
141
142 int grViewportcxx . . $ grViewport#::putscxx . . $ grViewport#puts(56759362374d8228).(int x, int y, char *str) { return grViewportcxx . . $ grViewport#::putscxx . . $ grViewport#puts(76a9c7ba36c5c57d).(grTextUnjustifiedcxx . . $ grTextUnjustified., x, y, str); }
143
144 // Ultimately all text output string functions call this monolith which
145 // calls the ddgr interface.
146
147 int grViewportcxx . . $ grViewport#::putscxx . . $ grViewport#puts(76a9c7ba36c5c57d).(grTextAligncxx . . $ grTextAlign# align, int x, int y, char *str) {
148 int cur_x, cur_y, gx, gy;
149 char *line, *save_pos;
150 tCharPropertiescxx . . $ tCharProperties# old_props;
151
152 // setup rendering of text.
153 rend_SetTextureTypecxx . . $ rend_SetTextureType(cccd9781d96f97ff).(TT_LINEARcxx . . $ TT_LINEAR.);
154 rend_SetOverlayTypecxx . . $ rend_SetOverlayType(ac50b47256ae7414).(OT_NONEcxx . . $ `lib/renderer.h:333:9`!);
155 rend_SetFilteringcxx . . $ rend_SetFiltering(4af4fd0e2b26c4ac).(0);
156 rend_SetLightingcxx . . $ rend_SetLighting(e55526c9d3dd1656).(LS_GOURAUDcxx . . $ LS_GOURAUD.);
157 rend_SetAlphaTypecxx . . $ rend_SetAlphaType(4af4fd0e2b26c4ac).(ATF_TEXTUREcxx . . $ `lib/renderer.h:395:9`! + ATF_CONSTANTcxx . . $ `lib/renderer.h:394:9`!);
158 rend_SetColorModelcxx . . $ rend_SetColorModel(e3c5062d5af601c7).(CM_RGBcxx . . $ CM_RGB.);
159 rend_SetZBufferStatecxx . . $ rend_SetZBufferState(4af4fd0e2b26c4ac).(0);
160 rend_SetAlphaValuecxx . . $ rend_SetAlphaValue(ac50b47256ae7414).(char_Propscxx . . $ grViewport#char_Props..alphacxx . . $ tCharProperties#alpha.);
161
162 get_char_propertiescxx . . $ grViewport#get_char_properties(9bd9ddac0cef7fd2).(&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_widthcxx . . $ grViewport#get_text_line_width(1ef8e6ec083aabb9).(line);
179
180 if (align == grTextCenteredcxx . . $ grTextCentered.)
181 cur_x = (((vp_Rightcxx . . $ grViewport#vp_Right. - vp_Leftcxx . . $ grViewport#vp_Left.) + 1) / 2) - (line_width / 2);
182 else if (align == grTextLeftcxx . . $ grTextLeft.)
183 cur_x = 0;
184
185 gy = global_ycxx . . $ grViewport#global_y(7ccb52b1bfe01045).(cur_y);
186 gx = global_xcxx . . $ grViewport#global_x(7ccb52b1bfe01045).(cur_x); // Clip function needs global coords.
187
188 clipped = 0;
189 if ((CLIP_TOPcxx . . $ `2dlib/pentext.cpp:93:9`! > (gy + text_Fontcxx . . $ grViewport#text_Font..heightcxx . . $ grFont#height(455f465bc33b4cdf).())) || (CLIP_BOTTOMcxx . . $ `2dlib/pentext.cpp:95:9`! < gy))
190 clipped = 2;
191 else if ((CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`! > (gx + line_width)) || (CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`! < gx))
192 clipped = 2;
193
194 if (clipped != 2) {
195 if (CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`! > gx || CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`! < (gx + line_width))
196 clipped = 1;
197 if (CLIP_TOPcxx . . $ `2dlib/pentext.cpp:93:9`! > gy || CLIP_BOTTOMcxx . . $ `2dlib/pentext.cpp:95:9`! < (gy + text_Fontcxx . . $ grViewport#text_Font..heightcxx . . $ grFont#height(455f465bc33b4cdf).()))
198 clipped = 1;
199
200 if (clipped == 0)
201 draw_text_linecxx . . $ grViewport#draw_text_line(1839e3b9b1397d59).(gx, gy, line);
202 else if (clipped == 1)
203 draw_text_line_clipcxx . . $ grViewport#draw_text_line_clip(1839e3b9b1397d59).(gx, gy, line);
204 }
205 cur_y += (get_text_line_spacingcxx . . $ grViewport#get_text_line_spacing(455f465bc33b4cdf).() + text_Fontcxx . . $ grViewport#text_Font..heightcxx . . $ grFont#height(455f465bc33b4cdf).());
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_propertiescxx . . $ grViewport#set_char_properties(5133d185afc50666).(GR_VPCP_ALLcxx . . $ `2dlib/gr.h:140:9`!, &old_props);
217
218 rend_SetFilteringcxx . . $ rend_SetFiltering(4af4fd0e2b26c4ac).(1);
219 rend_SetZBufferStatecxx . . $ rend_SetZBufferState(4af4fd0e2b26c4ac).(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 grViewportcxx . . $ grViewport#::get_text_line_widthcxx . . $ grViewport#get_text_line_width(1ef8e6ec083aabb9).(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_CHARcxx . . $ `lib/grdefs.h:82:9`!)
243 rgb_define_mode = 1;
244 if (!rgb_define_mode) {
245 if (str[i] == '\t') { // tab char
246 int space_width;
247 text_Fontcxx . . $ grViewport#text_Font..get_char_infocxx . . $ grFont#get_char_info(ac48f4a6ffa9e770).(' ', &space_width);
248 space_width += text_Spacingcxx . . $ grViewport#text_Spacing.;
249 line_width = (line_width + space_width - 1) / space_width * space_width;
250 } else {
251 text_Fontcxx . . $ grViewport#text_Font..get_char_infocxx . . $ grFont#get_char_info(ac48f4a6ffa9e770).((int)str[i], &width);
252 line_width += (width + get_text_spacingcxx . . $ grViewport#get_text_spacing(455f465bc33b4cdf).());
253 }
254 } else
255 rgb_define_mode++;
256 }
257
258 return line_width - get_text_spacingcxx . . $ grViewport#get_text_spacing(455f465bc33b4cdf).();
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 grViewportcxx . . $ grViewport#::draw_text_line_clipcxx . . $ grViewport#draw_text_line_clip(1839e3b9b1397d59).(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_Fontcxx . . $ grViewport#text_Font..heightcxx . . $ grFont#height(455f465bc33b4cdf).();
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_TOPcxx . . $ `2dlib/pentext.cpp:93:9`! >= y) {
280 ch_y = CLIP_TOPcxx . . $ `2dlib/pentext.cpp:93:9`! - y;
281 draw_y = CLIP_TOPcxx . . $ `2dlib/pentext.cpp:93:9`!;
282 }
283 if (CLIP_BOTTOMcxx . . $ `2dlib/pentext.cpp:95:9`! < (y + text_Fontcxx . . $ grViewport#text_Font..heightcxx . . $ grFont#height(455f465bc33b4cdf).())) {
284 ch_h = CLIP_BOTTOMcxx . . $ `2dlib/pentext.cpp:95:9`! - 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_tcxx . . $ uint8_t# ch;
291 int w;
292 ch = (uint8_tcxx . . $ uint8_t#)str[i];
293 text_Fontcxx . . $ grViewport#text_Font..get_char_infocxx . . $ grFont#get_char_info(ac48f4a6ffa9e770).((int)ch, &w);
294
295 if (ch == GR_COLOR_CHARcxx . . $ `lib/grdefs.h:82:9`!) {
296 if ((i + 3) >= (int)strlen(str))
297 Int3cxx . . $ `misc/pserror.h:247:9`!(); // This shouldn't happen! bad string!
298 set_text_colorcxx . . $ grViewport#set_text_color(cb2f890c2fdad230).(GR_RGBcxx . . $ GR_RGB(530956271a9518a5).(str[i + 1], str[i + 2], str[i + 3]));
299 i += 4;
300 if (i >= (int)strlen(str))
301 Int3cxx . . $ `misc/pserror.h:247:9`!(); // This shouldn't happen too.
302 ch = (uint8_tcxx . . $ uint8_t#)str[i];
303 } else if (ch == '\t') { // tab char
304 int space_width;
305 text_Fontcxx . . $ grViewport#text_Font..get_char_infocxx . . $ grFont#get_char_info(ac48f4a6ffa9e770).(' ', &space_width);
306 space_width += text_Spacingcxx . . $ grViewport#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_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`!) || (cur_x > CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`!)) {
312 cur_x += (text_Spacingcxx . . $ grViewport#text_Spacing. + w);
313 } else {
314 ch_x = 0;
315 ch_w = w;
316 draw_x = cur_x;
317 if (CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`! > cur_x) {
318 ch_x = CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`! - cur_x;
319 draw_x = CLIP_LEFTcxx . . $ `2dlib/pentext.cpp:92:9`!;
320 }
321 if (CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`! < (cur_x + w)) {
322 ch_w = CLIP_RIGHTcxx . . $ `2dlib/pentext.cpp:94:9`! - 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_Fontcxx . . $ grViewport#text_Font..draw_charcxx . . $ grFont#draw_char(222b2b3cf8cd66cf).(sf_Parentcxx . . $ grViewport#sf_Parent., draw_x, draw_y, ch, &char_Propscxx . . $ grViewport#char_Props.);
328 else
329 cur_x = text_Fontcxx . . $ grViewport#text_Font..draw_charcxx . . $ grFont#draw_char(9867803b38a2387f).(sf_Parentcxx . . $ grViewport#sf_Parent., draw_x, draw_y, ch, ch_x, ch_y, ch_w, ch_h, &char_Propscxx . . $ grViewport#char_Props.);
330
331 cur_x += text_Spacingcxx . . $ grViewport#text_Spacing.;
332 }
333 }
334 }
335
336 void grViewportcxx . . $ grViewport#::draw_text_linecxx . . $ grViewport#draw_text_line(1839e3b9b1397d59).(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_tcxx . . $ uint8_t# ch;
345
346 ch = (uint8_tcxx . . $ uint8_t#)str[i];
347
348 if (ch == GR_COLOR_CHARcxx . . $ `lib/grdefs.h:82:9`!) {
349 if ((i + 3) >= (int)strlen(str))
350 Int3cxx . . $ `misc/pserror.h:247:9`!(); // This shouldn't happen! bad string!
351 set_text_colorcxx . . $ grViewport#set_text_color(cb2f890c2fdad230).(GR_RGBcxx . . $ GR_RGB(530956271a9518a5).(str[i + 1], str[i + 2], str[i + 3]));
352 i += 4;
353 if (i >= (int)strlen(str))
354 Int3cxx . . $ `misc/pserror.h:247:9`!(); // This shouldn't happen too.
355 ch = (uint8_tcxx . . $ uint8_t#)str[i];
356 } else if (ch == '\t') { // tab char
357 int space_width;
358 text_Fontcxx . . $ grViewport#text_Font..get_char_infocxx . . $ grFont#get_char_info(ac48f4a6ffa9e770).(' ', &space_width);
359 space_width += text_Spacingcxx . . $ grViewport#text_Spacing.;
360 cur_x = (cur_x + space_width - 1) / space_width * space_width;
361 } else {
362 cur_x = text_Fontcxx . . $ grViewport#text_Font..draw_charcxx . . $ grFont#draw_char(222b2b3cf8cd66cf).(sf_Parentcxx . . $ grViewport#sf_Parent., cur_x, y, ch, &char_Propscxx . . $ grViewport#char_Props.);
363 cur_x += text_Spacingcxx . . $ grViewport#text_Spacing.;
364 }
365 }
366 }