1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment *
5 * Holger Schemel *
6 * Detmolder Strasse 189 *
7 * 33604 Bielefeld *
8 * Germany *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
11 * text.c *
12 ***********************************************************/
13
14 #include <stdio.h>
15 #include <stdarg.h>
16
17 #include "text.h"
18 #include "misc.h"
19
20
21 /* ========================================================================= */
22 /* font functions */
23 /* ========================================================================= */
24
25 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
26 static GC font_clip_gc = None;
27
InitFontClipmasks()28 static void InitFontClipmasks()
29 {
30 XGCValues clip_gc_values;
31 unsigned int clip_gc_valuemask;
32 GC copy_clipmask_gc;
33 int i, j;
34
35 /* This stuff is needed because X11 (XSetClipOrigin(), to be precise) is
36 often very slow when preparing a masked XCopyArea() for big Pixmaps.
37 To prevent this, create small (tile-sized) mask Pixmaps which will then
38 be set much faster with XSetClipOrigin() and speed things up a lot. */
39
40 clip_gc_values.graphics_exposures = False;
41 clip_gc_valuemask = GCGraphicsExposures;
42 font_clip_gc = XCreateGC(display, window->drawable,
43 clip_gc_valuemask, &clip_gc_values);
44
45 /* create graphic context structures needed for clipping */
46 clip_gc_values.graphics_exposures = False;
47 clip_gc_valuemask = GCGraphicsExposures;
48 copy_clipmask_gc = XCreateGC(display,
49 gfx.font_bitmap_info[0].bitmap->clip_mask,
50 clip_gc_valuemask, &clip_gc_values);
51
52 /* create only those clipping Pixmaps we really need */
53 for (i = 0; i < gfx.num_fonts; i++)
54 {
55 if (gfx.font_bitmap_info[i].bitmap == NULL)
56 continue;
57
58 gfx.font_bitmap_info[i].clip_mask =
59 checked_calloc(gfx.font_bitmap_info[i].num_chars * sizeof(Pixmap));
60
61 for (j = 0; j < gfx.font_bitmap_info[i].num_chars; j++)
62 {
63 Bitmap *src_bitmap = gfx.font_bitmap_info[i].bitmap;
64 Pixmap src_pixmap = src_bitmap->clip_mask;
65 int xpos = j % gfx.font_bitmap_info[i].num_chars_per_line;
66 int ypos = j / gfx.font_bitmap_info[i].num_chars_per_line;
67 int width = gfx.font_bitmap_info[i].width;
68 int height = gfx.font_bitmap_info[i].height;
69 int src_x = gfx.font_bitmap_info[i].src_x + xpos * width;
70 int src_y = gfx.font_bitmap_info[i].src_y + ypos * height;
71
72 gfx.font_bitmap_info[i].clip_mask[j] =
73 XCreatePixmap(display, window->drawable, width, height, 1);
74
75 XCopyArea(display, src_pixmap, gfx.font_bitmap_info[i].clip_mask[j],
76 copy_clipmask_gc, src_x, src_y, width, height, 0, 0);
77 }
78 }
79
80 XFreeGC(display, copy_clipmask_gc);
81 }
82
FreeFontClipmasks()83 static void FreeFontClipmasks()
84 {
85 int i, j;
86
87 if (gfx.num_fonts == 0 || gfx.font_bitmap_info[0].bitmap == NULL)
88 return;
89
90 for (i = 0; i < gfx.num_fonts; i++)
91 {
92 if (gfx.font_bitmap_info[i].clip_mask)
93 {
94 for (j = 0; j < gfx.font_bitmap_info[i].num_chars; j++)
95 XFreePixmap(display, gfx.font_bitmap_info[i].clip_mask[j]);
96 free(gfx.font_bitmap_info[i].clip_mask);
97 }
98
99 gfx.font_bitmap_info[i].clip_mask = NULL;
100 gfx.font_bitmap_info[i].num_chars = 0;
101 }
102
103 if (font_clip_gc)
104 XFreeGC(display, font_clip_gc);
105 font_clip_gc = None;
106 }
107 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
108
InitFontInfo(struct FontBitmapInfo * font_bitmap_info,int num_fonts,int (* select_font_function)(int),int (* get_font_from_token_function)(char *))109 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
110 int (*select_font_function)(int),
111 int (*get_font_from_token_function)(char *))
112 {
113 gfx.num_fonts = num_fonts;
114 gfx.font_bitmap_info = font_bitmap_info;
115 gfx.select_font_function = select_font_function;
116 gfx.get_font_from_token_function = get_font_from_token_function;
117
118 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
119 InitFontClipmasks();
120 #endif
121 }
122
FreeFontInfo(struct FontBitmapInfo * font_bitmap_info)123 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
124 {
125 if (font_bitmap_info == NULL)
126 return;
127
128 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
129 FreeFontClipmasks();
130 #endif
131
132 free(font_bitmap_info);
133 }
134
getFontBitmapInfo(int font_nr)135 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
136 {
137 int font_bitmap_id = gfx.select_font_function(font_nr);
138
139 return &gfx.font_bitmap_info[font_bitmap_id];
140 }
141
getFontWidth(int font_nr)142 int getFontWidth(int font_nr)
143 {
144 int font_bitmap_id = gfx.select_font_function(font_nr);
145
146 return gfx.font_bitmap_info[font_bitmap_id].width;
147 }
148
getFontHeight(int font_nr)149 int getFontHeight(int font_nr)
150 {
151 int font_bitmap_id = gfx.select_font_function(font_nr);
152
153 return gfx.font_bitmap_info[font_bitmap_id].height;
154 }
155
getTextWidth(char * text,int font_nr)156 int getTextWidth(char *text, int font_nr)
157 {
158 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
159 }
160
getFontCharPosition(int font_nr,char c)161 static int getFontCharPosition(int font_nr, char c)
162 {
163 int font_bitmap_id = gfx.select_font_function(font_nr);
164 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
165 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
166 int font_pos = (unsigned char)c - 32;
167
168 /* map some special characters to their ascii values in default font */
169 if (default_font)
170 font_pos = MAP_FONT_ASCII(c) - 32;
171
172 /* this allows dynamic special characters together with special font */
173 if (font_pos < 0 || font_pos >= font->num_chars)
174 font_pos = 0;
175
176 return font_pos;
177 }
178
getFontCharSource(int font_nr,char c,Bitmap ** bitmap,int * x,int * y)179 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
180 {
181 int font_bitmap_id = gfx.select_font_function(font_nr);
182 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
183 int font_pos = getFontCharPosition(font_nr, c);
184
185 *bitmap = font->bitmap;
186 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
187 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
188 }
189
190
191 /* ========================================================================= */
192 /* text string helper functions */
193 /* ========================================================================= */
194
maxWordLengthInString(char * text)195 int maxWordLengthInString(char *text)
196 {
197 char *text_ptr;
198 int max_word_len = 0;
199
200 for (text_ptr = text; *text_ptr; text_ptr++)
201 max_word_len = (*text_ptr != ' ' ? max_word_len + 1 : 0);
202
203 return max_word_len;
204 }
205
206
207 /* ========================================================================= */
208 /* simple text drawing functions */
209 /* ========================================================================= */
210
DrawInitTextExt(char * text,int ypos,int font_nr,boolean force)211 void DrawInitTextExt(char *text, int ypos, int font_nr, boolean force)
212 {
213 static unsigned int progress_delay = 0;
214 unsigned int progress_delay_value = 100; /* (in milliseconds) */
215
216 UPDATE_BUSY_STATE();
217
218 if (!force && !DelayReached(&progress_delay, progress_delay_value))
219 return;
220
221 if (window != NULL &&
222 gfx.draw_init_text &&
223 gfx.num_fonts > 0 &&
224 gfx.font_bitmap_info[font_nr].bitmap != NULL)
225 {
226 int x = (video.width - getTextWidth(text, font_nr)) / 2;
227 int y = ypos;
228 int width = video.width;
229 int height = getFontHeight(font_nr);
230
231 ClearRectangle(drawto, 0, y, width, height);
232 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
233
234 /* this makes things significantly faster than directly drawing to window */
235 BlitBitmap(drawto, window, 0, y, width, height, 0, y);
236 }
237 }
238
DrawInitText(char * text,int ypos,int font_nr)239 void DrawInitText(char *text, int ypos, int font_nr)
240 {
241 DrawInitTextExt(text, ypos, font_nr, TRUE);
242 }
243
DrawInitTextIfNeeded(char * text,int ypos,int font_nr)244 void DrawInitTextIfNeeded(char *text, int ypos, int font_nr)
245 {
246 DrawInitTextExt(text, ypos, font_nr, FALSE);
247 }
248
DrawTextF(int x,int y,int font_nr,char * format,...)249 void DrawTextF(int x, int y, int font_nr, char *format, ...)
250 {
251 char buffer[MAX_OUTPUT_LINESIZE + 1];
252 va_list ap;
253
254 va_start(ap, format);
255 vsprintf(buffer, format, ap);
256 va_end(ap);
257
258 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
259 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
260
261 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
262 }
263
DrawTextFCentered(int y,int font_nr,char * format,...)264 void DrawTextFCentered(int y, int font_nr, char *format, ...)
265 {
266 char buffer[MAX_OUTPUT_LINESIZE + 1];
267 va_list ap;
268
269 va_start(ap, format);
270 vsprintf(buffer, format, ap);
271 va_end(ap);
272
273 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
274 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
275
276 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
277 gfx.sy + y, buffer, font_nr);
278 }
279
DrawTextS(int x,int y,int font_nr,char * text)280 void DrawTextS(int x, int y, int font_nr, char *text)
281 {
282 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
283 }
284
DrawTextSCentered(int y,int font_nr,char * text)285 void DrawTextSCentered(int y, int font_nr, char *text)
286 {
287 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
288 gfx.sy + y, text, font_nr);
289 }
290
DrawTextCentered(int y,int font_nr,char * text)291 void DrawTextCentered(int y, int font_nr, char *text)
292 {
293 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
294 }
295
DrawTextSAligned(int x,int y,char * text,int font_nr,int align)296 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
297 {
298 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
299 gfx.sx + y, text, font_nr);
300 }
301
DrawTextAligned(int x,int y,char * text,int font_nr,int align)302 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
303 {
304 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
305 y, text, font_nr);
306 }
307
DrawText(int x,int y,char * text,int font_nr)308 void DrawText(int x, int y, char *text, int font_nr)
309 {
310 int mask_mode = BLIT_OPAQUE;
311
312 if (DrawingOnBackground(x, y))
313 mask_mode = BLIT_ON_BACKGROUND;
314
315 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
316
317 if (x < gfx.dx)
318 redraw_mask |= REDRAW_FIELD;
319 else if (y < gfx.vy || gfx.vy == 0)
320 redraw_mask |= REDRAW_DOOR_1;
321 }
322
DrawTextExt(DrawBuffer * dst_bitmap,int dst_x,int dst_y,char * text,int font_nr,int mask_mode)323 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
324 int font_nr, int mask_mode)
325 {
326 #if 1
327 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
328 #else
329 int font_bitmap_id = gfx.select_font_function(font_nr);
330 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
331 #endif
332 int font_width = getFontWidth(font_nr);
333 int font_height = getFontHeight(font_nr);
334 #if 0
335 int border_1 = gfx.sx + gfx.sxsize;
336 int border_2 = gfx.dx + gfx.dxsize;
337 int dst_x_start = dst_x;
338 #endif
339 Bitmap *src_bitmap;
340 int src_x, src_y;
341 char *text_ptr = text;
342
343 if (font->bitmap == NULL)
344 return;
345
346 /* skip text to be printed outside the window (left/right will be clipped) */
347 if (dst_y < 0 || dst_y + font_height > video.height)
348 return;
349
350 /* add offset for drawing font characters */
351 dst_x += font->draw_xoffset;
352 dst_y += font->draw_yoffset;
353
354 while (*text_ptr)
355 {
356 char c = *text_ptr++;
357
358 if (c == '\n')
359 c = ' '; /* print space instead of newline */
360
361 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
362
363 /* clip text at the left side of the window */
364 if (dst_x < 0)
365 {
366 dst_x += font_width;
367
368 continue;
369 }
370
371 /* clip text at the right side of the window */
372 #if 1
373 if (dst_x + font_width > video.width)
374 break;
375 #else
376 /* (this does not work well when trying to print text to whole screen) */
377 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
378 (dst_x_start < border_2 && dst_x + font_width > border_2))
379 break;
380 #endif
381
382 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
383 {
384 /* first step: draw solid colored rectangle (use "cursor" character) */
385 if (strlen(text) == 1) /* only one char inverted => draw cursor */
386 {
387 Bitmap *cursor_bitmap;
388 int cursor_x, cursor_y;
389
390 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
391 &cursor_x, &cursor_y);
392
393 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
394 font_width, font_height, dst_x, dst_y);
395 }
396
397 #if defined(TARGET_SDL)
398 /* second step: draw masked inverted character */
399 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
400 font_width, font_height, dst_x, dst_y);
401 #else
402 /* second step: draw masked black rectangle (use "space" character) */
403 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
404 dst_x - src_x, dst_y - src_y);
405 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
406 font_width, font_height, dst_x, dst_y);
407 #endif
408 }
409 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
410 {
411 if (mask_mode == BLIT_ON_BACKGROUND)
412 {
413 /* clear font character background */
414 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
415 font_width, font_height);
416 }
417
418 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
419 /* use special font tile clipmasks */
420 {
421 int font_pos = getFontCharPosition(font_nr, c);
422
423 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
424 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
425 }
426 #else
427 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
428 dst_x - src_x, dst_y - src_y);
429 #endif
430
431 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
432 font_width, font_height, dst_x, dst_y);
433 }
434 else /* normal, non-masked font blitting */
435 {
436 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
437 font_width, font_height, dst_x, dst_y);
438 }
439
440 dst_x += font_width;
441 }
442 }
443
444
445 /* ========================================================================= */
446 /* text buffer drawing functions */
447 /* ========================================================================= */
448
449 #define MAX_LINES_FROM_FILE 1024
450
GetTextBufferFromFile(char * filename,int max_lines)451 char *GetTextBufferFromFile(char *filename, int max_lines)
452 {
453 FILE *file;
454 char *buffer;
455 int num_lines = 0;
456
457 if (filename == NULL)
458 return NULL;
459
460 if (!(file = fopen(filename, MODE_READ)))
461 return NULL;
462
463 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
464
465 while (!feof(file) && num_lines < max_lines)
466 {
467 char line[MAX_LINE_LEN];
468
469 /* read next line of input file */
470 if (!fgets(line, MAX_LINE_LEN, file))
471 break;
472
473 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
474
475 strcat(buffer, line);
476
477 num_lines++;
478 }
479
480 fclose(file);
481
482 return buffer;
483 }
484
DrawTextToTextArea_OLD(int x,int y,char * text,int font_nr,int line_length,int area_xsize,int area_ysize,int mask_mode)485 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
486 int area_xsize, int area_ysize, int mask_mode)
487 {
488 int area_line = 0;
489 int font_height = getFontHeight(font_nr);
490
491 if (text == NULL)
492 return;
493
494 while (*text && area_line < area_ysize)
495 {
496 char buffer[MAX_OUTPUT_LINESIZE + 1];
497 int i;
498
499 for (i = 0; i < line_length && *text; i++)
500 if ((buffer[i] = *text++) == '\n')
501 break;
502 buffer[MIN(i, area_xsize)] = '\0';
503
504 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
505 mask_mode);
506
507 area_line++;
508 }
509
510 redraw_mask |= REDRAW_FIELD;
511 }
512
RenderLineToBuffer(char ** src_buffer_ptr,char * dst_buffer,int * dst_buffer_len,int line_length,boolean last_line_was_empty)513 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
514 int *dst_buffer_len, int line_length,
515 boolean last_line_was_empty)
516 {
517 char *text_ptr = *src_buffer_ptr;
518 char *buffer = dst_buffer;
519 int buffer_len = *dst_buffer_len;
520 boolean buffer_filled = FALSE;
521
522 while (*text_ptr)
523 {
524 char *word_ptr;
525 int word_len;
526
527 /* skip leading whitespaces */
528 while (*text_ptr == ' ' || *text_ptr == '\t')
529 text_ptr++;
530
531 word_ptr = text_ptr;
532 word_len = 0;
533
534 /* look for end of next word */
535 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
536 {
537 word_ptr++;
538 word_len++;
539 }
540
541 if (word_len == 0)
542 {
543 continue;
544 }
545 else if (*text_ptr == '\n') /* special case: force empty line */
546 {
547 if (buffer_len == 0)
548 text_ptr++;
549
550 /* prevent printing of multiple empty lines */
551 if (buffer_len > 0 || !last_line_was_empty)
552 buffer_filled = TRUE;
553 }
554 else if (word_len < line_length - buffer_len)
555 {
556 /* word fits into text buffer -- add word */
557
558 if (buffer_len > 0)
559 buffer[buffer_len++] = ' ';
560
561 strncpy(&buffer[buffer_len], text_ptr, word_len);
562 buffer_len += word_len;
563 buffer[buffer_len] = '\0';
564 text_ptr += word_len;
565 }
566 else if (buffer_len > 0)
567 {
568 /* not enough space left for word in text buffer -- print buffer */
569
570 buffer_filled = TRUE;
571 }
572 else
573 {
574 /* word does not fit at all into empty text buffer -- cut word */
575
576 strncpy(buffer, text_ptr, line_length);
577 buffer[line_length] = '\0';
578 text_ptr += line_length;
579 buffer_filled = TRUE;
580 }
581
582 if (buffer_filled)
583 break;
584 }
585
586 *src_buffer_ptr = text_ptr;
587 *dst_buffer_len = buffer_len;
588
589 return buffer_filled;
590 }
591
592 #if 0
593 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
594 int max_lines)
595 {
596 char *text_ptr = text;
597 int current_line = 0;
598 int font_height = getFontHeight(font_nr);
599
600 while (*text_ptr && current_line < max_lines)
601 {
602 char buffer[line_length + 1];
603 int buffer_len = 0;
604
605 buffer[0] = '\0';
606
607 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
608
609 DrawText(x, y + current_line * font_height, buffer, font_nr);
610 current_line++;
611 }
612 }
613 #endif
614
615 #if 0
616 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
617 int line_length, int max_lines, boolean wrap_text)
618 {
619 int font_height = getFontHeight(font_nr);
620 char line[MAX_LINE_LEN];
621 char buffer[line_length + 1];
622 int buffer_len;
623 int current_line = 0;
624 FILE *file;
625
626 if (current_line >= max_lines)
627 return 0;
628
629 if (filename == NULL)
630 return 0;
631
632 if (!(file = fopen(filename, MODE_READ)))
633 return 0;
634
635 buffer[0] = '\0';
636 buffer_len = 0;
637
638 while (!feof(file) && current_line < max_lines)
639 {
640 char *line_ptr;
641 boolean last_line_was_empty = TRUE;
642
643 /* read next line of input file */
644 if (!fgets(line, MAX_LINE_LEN, file))
645 break;
646
647 /* skip comments (lines directly beginning with '#') */
648 if (line[0] == '#')
649 continue;
650
651 /* cut trailing newline from input line */
652 for (line_ptr = line; *line_ptr; line_ptr++)
653 {
654 if (*line_ptr == '\n' || *line_ptr == '\r')
655 {
656 *line_ptr = '\0';
657 break;
658 }
659 }
660
661 if (strlen(line) == 0) /* special case: force empty line */
662 strcpy(line, "\n");
663
664 line_ptr = line;
665
666 while (*line_ptr && current_line < max_lines)
667 {
668 #if 1
669 boolean buffer_filled;
670
671 if (wrap_text)
672 {
673 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
674 line_length, last_line_was_empty);
675 }
676 else
677 {
678 if (strlen(line_ptr) <= line_length)
679 {
680 buffer_len = strlen(line_ptr);
681 strcpy(buffer, line_ptr);
682 }
683 else
684 {
685 buffer_len = line_length;
686 strncpy(buffer, line_ptr, line_length);
687 }
688
689 buffer[buffer_len] = '\0';
690 line_ptr += buffer_len;
691
692 buffer_filled = TRUE;
693 }
694 #else
695 boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
696 line_length, last_line_was_empty);
697 #endif
698
699 if (buffer_filled)
700 {
701 DrawText(x, y + current_line * font_height, buffer, font_nr);
702 current_line++;
703
704 last_line_was_empty = (buffer_len == 0);
705
706 buffer[0] = '\0';
707 buffer_len = 0;
708 }
709 }
710 }
711
712 fclose(file);
713
714 if (buffer_len > 0 && current_line < max_lines)
715 {
716 DrawText(x, y + current_line * font_height, buffer, font_nr);
717 current_line++;
718 }
719
720 return current_line;
721 }
722 #endif
723
getCheckedTokenValueFromString(char * string,char ** token,char ** value)724 static boolean getCheckedTokenValueFromString(char *string, char **token,
725 char **value)
726 {
727 char *ptr;
728
729 if (!getTokenValueFromString(string, token, value))
730 return FALSE;
731
732 if (**token != '.') /* token should begin with dot */
733 return FALSE;
734
735 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
736 if (*ptr == ' ' || *ptr == '\t')
737 return FALSE;
738
739 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
740 if (*ptr == ' ' || *ptr == '\t')
741 return FALSE;
742
743 return TRUE;
744 }
745
DrawTextBuffer_Flush(int x,int y,char * buffer,int font_nr,int line_length,int cut_length,int line_spacing,int mask_mode,boolean centered,int current_line)746 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
747 int line_length, int cut_length,
748 int line_spacing, int mask_mode,
749 boolean centered, int current_line)
750 {
751 int buffer_len = strlen(buffer);
752 int font_width = getFontWidth(font_nr);
753 int font_height = getFontHeight(font_nr);
754 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
755 int offset_xsize =
756 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
757 int final_cut_length = MAX(0, cut_length - offset_chars);
758 int xx = x + offset_xsize;
759 int yy = y + current_line * (font_height + line_spacing);
760
761 buffer[final_cut_length] = '\0';
762
763 if (mask_mode != -1)
764 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
765 else
766 DrawText(xx, yy, buffer, font_nr);
767 }
768
DrawTextBuffer(int x,int y,char * text_buffer,int font_nr,int line_length,int cut_length,int max_lines,int line_spacing,int mask_mode,boolean autowrap,boolean centered,boolean parse_comments)769 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
770 int line_length, int cut_length, int max_lines,
771 int line_spacing, int mask_mode, boolean autowrap,
772 boolean centered, boolean parse_comments)
773 {
774 #if 0
775 int font_width = getFontWidth(font_nr);
776 int font_height = getFontHeight(font_nr);
777 #endif
778 char buffer[line_length + 1];
779 int buffer_len;
780 int current_line = 0;
781
782 if (text_buffer == NULL || *text_buffer == '\0')
783 return 0;
784
785 if (current_line >= max_lines)
786 return 0;
787
788 if (cut_length == -1)
789 cut_length = line_length;
790
791 buffer[0] = '\0';
792 buffer_len = 0;
793
794 while (*text_buffer && current_line < max_lines)
795 {
796 char line[MAX_LINE_LEN + 1];
797 char *line_ptr;
798 boolean last_line_was_empty = TRUE;
799 #if 1
800 int num_line_chars = MAX_LINE_LEN;
801 #else
802 int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
803 #endif
804 int i;
805
806 /* copy next line from text buffer to line buffer (nearly fgets() style) */
807 for (i = 0; i < num_line_chars && *text_buffer; i++)
808 if ((line[i] = *text_buffer++) == '\n')
809 break;
810 line[i] = '\0';
811
812 /* prevent 'num_line_chars' sized lines to cause additional empty line */
813 if (i == num_line_chars && *text_buffer == '\n')
814 text_buffer++;
815
816 /* skip comments (lines directly beginning with '#') */
817 if (line[0] == '#' && parse_comments)
818 {
819 char *token, *value;
820
821 /* try to read generic token/value pair definition after comment sign */
822 if (getCheckedTokenValueFromString(line + 1, &token, &value))
823 {
824 /* if found, flush the current buffer, if non-empty */
825 if (buffer_len > 0 && current_line < max_lines)
826 {
827 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
828 line_spacing, mask_mode, centered, current_line);
829
830 current_line++;
831
832 buffer[0] = '\0';
833 buffer_len = 0;
834 }
835
836 if (strEqual(token, ".font"))
837 font_nr = gfx.get_font_from_token_function(value);
838 else if (strEqual(token, ".autowrap"))
839 autowrap = get_boolean_from_string(value);
840 else if (strEqual(token, ".centered"))
841 centered = get_boolean_from_string(value);
842 else if (strEqual(token, ".parse_comments"))
843 parse_comments = get_boolean_from_string(value);
844 }
845
846 continue;
847 }
848
849 /* cut trailing newline and carriage return from input line */
850 for (line_ptr = line; *line_ptr; line_ptr++)
851 {
852 if (*line_ptr == '\n' || *line_ptr == '\r')
853 {
854 *line_ptr = '\0';
855 break;
856 }
857 }
858
859 if (strlen(line) == 0) /* special case: force empty line */
860 strcpy(line, "\n");
861
862 line_ptr = line;
863
864 while (*line_ptr && current_line < max_lines)
865 {
866 boolean buffer_filled;
867
868 if (autowrap)
869 {
870 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
871 line_length, last_line_was_empty);
872 }
873 else
874 {
875 if (strlen(line_ptr) <= line_length)
876 {
877 buffer_len = strlen(line_ptr);
878 strcpy(buffer, line_ptr);
879 }
880 else
881 {
882 buffer_len = line_length;
883 strncpy(buffer, line_ptr, line_length);
884 }
885
886 buffer[buffer_len] = '\0';
887 line_ptr += buffer_len;
888
889 buffer_filled = TRUE;
890 }
891
892 if (buffer_filled)
893 {
894 #if 1
895 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
896 line_spacing, mask_mode, centered, current_line);
897 #else
898 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
899 int offset_xsize =
900 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
901 int final_cut_length = MAX(0, cut_length - offset_chars);
902 int xx = x + offset_xsize;
903
904 buffer[final_cut_length] = '\0';
905
906 if (mask_mode != -1)
907 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
908 font_nr, mask_mode);
909 else
910 DrawText(xx, y + current_line * font_height, buffer, font_nr);
911 #endif
912
913 current_line++;
914
915 last_line_was_empty = (buffer_len == 0);
916
917 buffer[0] = '\0';
918 buffer_len = 0;
919 }
920 }
921 }
922
923 if (buffer_len > 0 && current_line < max_lines)
924 {
925 #if 1
926 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
927 line_spacing, mask_mode, centered, current_line);
928 #else
929 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
930 int offset_xsize =
931 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
932 int final_cut_length = MAX(0, cut_length - offset_chars);
933 int xx = x + offset_xsize;
934
935 buffer[final_cut_length] = '\0';
936
937 if (mask_mode != -1)
938 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
939 font_nr, mask_mode);
940 else
941 DrawText(xx, y + current_line * font_height, buffer, font_nr);
942 #endif
943
944 current_line++;
945 }
946
947 return current_line;
948 }
949
DrawTextFile(int x,int y,char * filename,int font_nr,int line_length,int cut_length,int max_lines,int line_spacing,int mask_mode,boolean autowrap,boolean centered,boolean parse_comments)950 int DrawTextFile(int x, int y, char *filename, int font_nr,
951 int line_length, int cut_length, int max_lines,
952 int line_spacing, int mask_mode, boolean autowrap,
953 boolean centered, boolean parse_comments)
954 {
955 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
956 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
957 line_length, cut_length, max_lines,
958 line_spacing, mask_mode, autowrap,
959 centered, parse_comments);
960 checked_free(text_buffer);
961
962 return num_lines_printed;
963 }
964
965 #if 0
966 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
967 int max_lines)
968 {
969 DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
970 FALSE, FALSE);
971 }
972
973 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
974 int cut_length, int max_lines, int mask_mode)
975 {
976 DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
977 mask_mode, FALSE, FALSE, FALSE);
978 }
979 #endif
980