rnd-20070427-1-src
[rocksndiamonds.git] / src / libgame / text.c
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
28 static void InitFontClipmasks()
29 {
30   XGCValues clip_gc_values;
31   unsigned long 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
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
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
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
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
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
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
156 int getTextWidth(char *text, int font_nr)
157 {
158   return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
159 }
160
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
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 /* simple text drawing functions                                             */
193 /* ========================================================================= */
194
195 void DrawInitTextExt(char *text, int ypos, int font_nr, boolean force)
196 {
197   static unsigned long progress_delay = 0;
198   unsigned long progress_delay_value = 100;     /* (in milliseconds) */
199
200   UPDATE_BUSY_STATE();
201
202   if (!force && !DelayReached(&progress_delay, progress_delay_value))
203     return;
204
205   if (window != NULL &&
206       gfx.num_fonts > 0 &&
207       gfx.font_bitmap_info[font_nr].bitmap != NULL)
208   {
209     int x = (video.width - getTextWidth(text, font_nr)) / 2;
210     int y = ypos;
211     int width = video.width;
212     int height = getFontHeight(font_nr);
213
214     ClearRectangle(drawto, 0, y, width, height);
215     DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
216
217     /* this makes things significantly faster than directly drawing to window */
218     BlitBitmap(drawto, window, 0, y, width, height, 0, y);
219   }
220 }
221
222 void DrawInitText(char *text, int ypos, int font_nr)
223 {
224   DrawInitTextExt(text, ypos, font_nr, TRUE);
225 }
226
227 void DrawInitTextIfNeeded(char *text, int ypos, int font_nr)
228 {
229   DrawInitTextExt(text, ypos, font_nr, FALSE);
230 }
231
232 void DrawTextF(int x, int y, int font_nr, char *format, ...)
233 {
234   char buffer[MAX_OUTPUT_LINESIZE + 1];
235   va_list ap;
236
237   va_start(ap, format);
238   vsprintf(buffer, format, ap);
239   va_end(ap);
240
241   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
242     Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
243
244   DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
245 }
246
247 void DrawTextFCentered(int y, int font_nr, char *format, ...)
248 {
249   char buffer[MAX_OUTPUT_LINESIZE + 1];
250   va_list ap;
251
252   va_start(ap, format);
253   vsprintf(buffer, format, ap);
254   va_end(ap);
255
256   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
257     Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
258
259   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
260            gfx.sy + y, buffer, font_nr);
261 }
262
263 void DrawTextS(int x, int y, int font_nr, char *text)
264 {
265   DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
266 }
267
268 void DrawTextSCentered(int y, int font_nr, char *text)
269 {
270   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
271            gfx.sy + y, text, font_nr);
272 }
273
274 void DrawTextCentered(int y, int font_nr, char *text)
275 {
276   DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
277 }
278
279 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
280 {
281   DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
282            gfx.sx + y, text, font_nr);
283 }
284
285 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
286 {
287   DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
288            y, text, font_nr);
289 }
290
291 void DrawText(int x, int y, char *text, int font_nr)
292 {
293   int mask_mode = BLIT_OPAQUE;
294
295   if (DrawingOnBackground(x, y))
296     mask_mode = BLIT_ON_BACKGROUND;
297
298   DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
299
300   if (x < gfx.dx)
301     redraw_mask |= REDRAW_FIELD;
302   else if (y < gfx.vy || gfx.vy == 0)
303     redraw_mask |= REDRAW_DOOR_1;
304 }
305
306 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
307                  int font_nr, int mask_mode)
308 {
309 #if 1
310   struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
311 #else
312   int font_bitmap_id = gfx.select_font_function(font_nr);
313   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
314 #endif
315   int font_width = getFontWidth(font_nr);
316   int font_height = getFontHeight(font_nr);
317 #if 0
318   int border_1 = gfx.sx + gfx.sxsize;
319   int border_2 = gfx.dx + gfx.dxsize;
320   int dst_x_start = dst_x;
321 #endif
322   Bitmap *src_bitmap;
323   int src_x, src_y;
324   char *text_ptr = text;
325
326   if (font->bitmap == NULL)
327     return;
328
329   /* skip text to be printed outside the window (left/right will be clipped) */
330   if (dst_y < 0 || dst_y + font_height > video.height)
331     return;
332
333   /* add offset for drawing font characters */
334   dst_x += font->draw_xoffset;
335   dst_y += font->draw_yoffset;
336
337   while (*text_ptr)
338   {
339     char c = *text_ptr++;
340
341     if (c == '\n')
342       c = ' ';          /* print space instead of newline */
343
344     getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
345
346     /* clip text at the left side of the window */
347     if (dst_x < 0)
348     {
349       dst_x += font_width;
350
351       continue;
352     }
353
354     /* clip text at the right side of the window */
355 #if 1
356     if (dst_x + font_width > video.width)
357       break;
358 #else
359     /* (this does not work well when trying to print text to whole screen) */
360     if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
361         (dst_x_start < border_2 && dst_x + font_width > border_2))
362       break;
363 #endif
364
365     if (mask_mode == BLIT_INVERSE)      /* special mode for text gadgets */
366     {
367       /* first step: draw solid colored rectangle (use "cursor" character) */
368       if (strlen(text) == 1)    /* only one char inverted => draw cursor */
369       {
370         Bitmap *cursor_bitmap;
371         int cursor_x, cursor_y;
372
373         getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
374                           &cursor_x, &cursor_y);
375
376         BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
377                    font_width, font_height, dst_x, dst_y);
378       }
379
380 #if defined(TARGET_SDL)
381       /* second step: draw masked inverted character */
382       SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
383                            font_width, font_height, dst_x, dst_y);
384 #else
385       /* second step: draw masked black rectangle (use "space" character) */
386       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
387                     dst_x - src_x, dst_y - src_y);
388       BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
389                        font_width, font_height, dst_x, dst_y);
390 #endif
391     }
392     else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
393     {
394       if (mask_mode == BLIT_ON_BACKGROUND)
395       {
396         /* clear font character background */
397         ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
398                                    font_width, font_height);
399       }
400
401 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
402       /* use special font tile clipmasks */
403       {
404         int font_pos = getFontCharPosition(font_nr, c);
405
406         SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
407         SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
408       }
409 #else
410       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
411                     dst_x - src_x, dst_y - src_y);
412 #endif
413
414       BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
415                        font_width, font_height, dst_x, dst_y);
416     }
417     else        /* normal, non-masked font blitting */
418     {
419       BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
420                  font_width, font_height, dst_x, dst_y);
421     }
422
423     dst_x += font_width;
424   }
425 }
426
427
428 /* ========================================================================= */
429 /* text buffer drawing functions                                             */
430 /* ========================================================================= */
431
432 #define MAX_LINES_FROM_FILE             1024
433
434 char *GetTextBufferFromFile(char *filename, int max_lines)
435 {
436   FILE *file;
437   char *buffer;
438   int num_lines = 0;
439
440   if (filename == NULL)
441     return NULL;
442
443   if (!(file = fopen(filename, MODE_READ)))
444     return NULL;
445
446   buffer = checked_calloc(1);   /* start with valid, but empty text buffer */
447
448   while (!feof(file) && num_lines < max_lines)
449   {
450     char line[MAX_LINE_LEN];
451
452     /* read next line of input file */
453     if (!fgets(line, MAX_LINE_LEN, file))
454       break;
455
456     buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
457
458     strcat(buffer, line);
459
460     num_lines++;
461   }
462
463   fclose(file);
464
465   return buffer;
466 }
467
468 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
469                             int area_xsize, int area_ysize, int mask_mode)
470 {
471   int area_line = 0;
472   int font_height = getFontHeight(font_nr);
473
474   if (text == NULL)
475     return;
476
477   while (*text && area_line < area_ysize)
478   {
479     char buffer[MAX_OUTPUT_LINESIZE + 1];
480     int i;
481
482     for (i = 0; i < line_length && *text; i++)
483       if ((buffer[i] = *text++) == '\n')
484         break;
485     buffer[MIN(i, area_xsize)] = '\0';
486
487     DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
488                 mask_mode);
489
490     area_line++;
491   }
492
493   redraw_mask |= REDRAW_FIELD;
494 }
495
496 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
497                                   int *dst_buffer_len, int line_length,
498                                   boolean last_line_was_empty)
499 {
500   char *text_ptr = *src_buffer_ptr;
501   char *buffer = dst_buffer;
502   int buffer_len = *dst_buffer_len;
503   boolean buffer_filled = FALSE;
504
505   while (*text_ptr)
506   {
507     char *word_ptr;
508     int word_len;
509
510     /* skip leading whitespaces */
511     while (*text_ptr == ' ' || *text_ptr == '\t')
512       text_ptr++;
513
514     word_ptr = text_ptr;
515     word_len = 0;
516
517     /* look for end of next word */
518     while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
519     {
520       word_ptr++;
521       word_len++;
522     }
523
524     if (word_len == 0)
525     {
526       continue;
527     }
528     else if (*text_ptr == '\n')         /* special case: force empty line */
529     {
530       if (buffer_len == 0)
531         text_ptr++;
532
533       /* prevent printing of multiple empty lines */
534       if (buffer_len > 0 || !last_line_was_empty)
535         buffer_filled = TRUE;
536     }
537     else if (word_len < line_length - buffer_len)
538     {
539       /* word fits into text buffer -- add word */
540
541       if (buffer_len > 0)
542         buffer[buffer_len++] = ' ';
543
544       strncpy(&buffer[buffer_len], text_ptr, word_len);
545       buffer_len += word_len;
546       buffer[buffer_len] = '\0';
547       text_ptr += word_len;
548     }
549     else if (buffer_len > 0)
550     {
551       /* not enough space left for word in text buffer -- print buffer */
552
553       buffer_filled = TRUE;
554     }
555     else
556     {
557       /* word does not fit at all into empty text buffer -- cut word */
558
559       strncpy(buffer, text_ptr, line_length);
560       buffer[line_length] = '\0';
561       text_ptr += line_length;
562       buffer_filled = TRUE;
563     }
564
565     if (buffer_filled)
566       break;
567   }
568
569   *src_buffer_ptr = text_ptr;
570   *dst_buffer_len = buffer_len;
571
572   return buffer_filled;
573 }
574
575 #if 0
576 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
577                          int max_lines)
578 {
579   char *text_ptr = text;
580   int current_line = 0;
581   int font_height = getFontHeight(font_nr);
582
583   while (*text_ptr && current_line < max_lines)
584   {
585     char buffer[line_length + 1];
586     int buffer_len = 0;
587
588     buffer[0] = '\0';
589
590     RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
591
592     DrawText(x, y + current_line * font_height, buffer, font_nr);
593     current_line++;
594   }
595 }
596 #endif
597
598 #if 0
599 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
600                          int line_length, int max_lines, boolean wrap_text)
601 {
602   int font_height = getFontHeight(font_nr);
603   char line[MAX_LINE_LEN];
604   char buffer[line_length + 1];
605   int buffer_len;
606   int current_line = 0;
607   FILE *file;
608
609   if (current_line >= max_lines)
610     return 0;
611
612   if (filename == NULL)
613     return 0;
614
615   if (!(file = fopen(filename, MODE_READ)))
616     return 0;
617
618   buffer[0] = '\0';
619   buffer_len = 0;
620
621   while (!feof(file) && current_line < max_lines)
622   {
623     char *line_ptr;
624     boolean last_line_was_empty = TRUE;
625
626     /* read next line of input file */
627     if (!fgets(line, MAX_LINE_LEN, file))
628       break;
629
630     /* skip comments (lines directly beginning with '#') */
631     if (line[0] == '#')
632       continue;
633
634     /* cut trailing newline from input line */
635     for (line_ptr = line; *line_ptr; line_ptr++)
636     {
637       if (*line_ptr == '\n' || *line_ptr == '\r')
638       {
639         *line_ptr = '\0';
640         break;
641       }
642     }
643
644     if (strlen(line) == 0)              /* special case: force empty line */
645       strcpy(line, "\n");
646
647     line_ptr = line;
648
649     while (*line_ptr && current_line < max_lines)
650     {
651 #if 1
652       boolean buffer_filled;
653
654       if (wrap_text)
655       {
656         buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
657                                            line_length, last_line_was_empty);
658       }
659       else
660       {
661         if (strlen(line_ptr) <= line_length)
662         {
663           buffer_len = strlen(line_ptr);
664           strcpy(buffer, line_ptr);
665         }
666         else
667         {
668           buffer_len = line_length;
669           strncpy(buffer, line_ptr, line_length);
670         }
671
672         buffer[buffer_len] = '\0';
673         line_ptr += buffer_len;
674
675         buffer_filled = TRUE;
676       }
677 #else
678       boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
679                                                  line_length, last_line_was_empty);
680 #endif
681
682       if (buffer_filled)
683       {
684         DrawText(x, y + current_line * font_height, buffer, font_nr);
685         current_line++;
686
687         last_line_was_empty = (buffer_len == 0);
688
689         buffer[0] = '\0';
690         buffer_len = 0;
691       }
692     }
693   }
694
695   fclose(file);
696
697   if (buffer_len > 0 && current_line < max_lines)
698   {
699     DrawText(x, y + current_line * font_height, buffer, font_nr);
700     current_line++;
701   }
702
703   return current_line;
704 }
705 #endif
706
707 static boolean getCheckedTokenValueFromString(char *string, char **token,
708                                               char **value)
709 {
710   char *ptr;
711
712   if (!getTokenValueFromString(string, token, value))
713     return FALSE;
714
715   if (**token != '.')                   /* token should begin with dot */
716     return FALSE;
717
718   for (ptr = *token; *ptr; ptr++)       /* token should contain no whitespace */
719     if (*ptr == ' ' || *ptr == '\t')
720       return FALSE;
721
722   for (ptr = *value; *ptr; ptr++)       /* value should contain no whitespace */
723     if (*ptr == ' ' || *ptr == '\t')
724       return FALSE;
725
726   return TRUE;
727 }
728
729 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
730                                  int line_length, int cut_length, int mask_mode,
731                                  boolean centered, int current_line)
732 {
733   int buffer_len = strlen(buffer);
734   int font_width = getFontWidth(font_nr);
735   int font_height = getFontHeight(font_nr);
736   int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
737   int offset_xsize =
738     (centered ? font_width * (line_length - buffer_len) / 2 : 0);
739   int final_cut_length = MAX(0, cut_length - offset_chars);
740   int xx = x + offset_xsize;
741   int yy = y + current_line * font_height;
742
743   buffer[final_cut_length] = '\0';
744
745   if (mask_mode != -1)
746     DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
747   else
748     DrawText(xx, yy, buffer, font_nr);
749 }
750
751 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
752                    int line_length, int cut_length, int max_lines,
753                    int mask_mode, boolean autowrap, boolean centered,
754                    boolean parse_comments)
755 {
756 #if 0
757   int font_width = getFontWidth(font_nr);
758   int font_height = getFontHeight(font_nr);
759 #endif
760   char buffer[line_length + 1];
761   int buffer_len;
762   int current_line = 0;
763
764   if (text_buffer == NULL || *text_buffer == '\0')
765     return 0;
766
767   if (current_line >= max_lines)
768     return 0;
769
770   if (cut_length == -1)
771     cut_length = line_length;
772
773   buffer[0] = '\0';
774   buffer_len = 0;
775
776   while (*text_buffer && current_line < max_lines)
777   {
778     char line[MAX_LINE_LEN + 1];
779     char *line_ptr;
780     boolean last_line_was_empty = TRUE;
781 #if 1
782     int num_line_chars = MAX_LINE_LEN;
783 #else
784     int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
785 #endif
786     int i;
787
788     /* copy next line from text buffer to line buffer (nearly fgets() style) */
789     for (i = 0; i < num_line_chars && *text_buffer; i++)
790       if ((line[i] = *text_buffer++) == '\n')
791         break;
792     line[i] = '\0';
793
794     /* prevent 'num_line_chars' sized lines to cause additional empty line */
795     if (i == num_line_chars && *text_buffer == '\n')
796       text_buffer++;
797
798     /* skip comments (lines directly beginning with '#') */
799     if (line[0] == '#' && parse_comments)
800     {
801       char *token, *value;
802
803       /* try to read generic token/value pair definition after comment sign */
804       if (getCheckedTokenValueFromString(line + 1, &token, &value))
805       {
806         /* if found, flush the current buffer, if non-empty */
807         if (buffer_len > 0 && current_line < max_lines)
808         {
809           DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
810                                mask_mode, centered, current_line);
811
812           current_line++;
813
814           buffer[0] = '\0';
815           buffer_len = 0;
816         }
817
818         if (strEqual(token, ".font"))
819           font_nr = gfx.get_font_from_token_function(value);
820         else if (strEqual(token, ".autowrap"))
821           autowrap = get_boolean_from_string(value);
822         else if (strEqual(token, ".centered"))
823           centered = get_boolean_from_string(value);
824         else if (strEqual(token, ".parse_comments"))
825           parse_comments = get_boolean_from_string(value);
826       }
827
828       continue;
829     }
830
831     /* cut trailing newline and carriage return from input line */
832     for (line_ptr = line; *line_ptr; line_ptr++)
833     {
834       if (*line_ptr == '\n' || *line_ptr == '\r')
835       {
836         *line_ptr = '\0';
837         break;
838       }
839     }
840
841     if (strlen(line) == 0)              /* special case: force empty line */
842       strcpy(line, "\n");
843
844     line_ptr = line;
845
846     while (*line_ptr && current_line < max_lines)
847     {
848       boolean buffer_filled;
849
850       if (autowrap)
851       {
852         buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
853                                            line_length, last_line_was_empty);
854       }
855       else
856       {
857         if (strlen(line_ptr) <= line_length)
858         {
859           buffer_len = strlen(line_ptr);
860           strcpy(buffer, line_ptr);
861         }
862         else
863         {
864           buffer_len = line_length;
865           strncpy(buffer, line_ptr, line_length);
866         }
867
868         buffer[buffer_len] = '\0';
869         line_ptr += buffer_len;
870
871         buffer_filled = TRUE;
872       }
873
874       if (buffer_filled)
875       {
876 #if 1
877         DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
878                              mask_mode, centered, current_line);
879 #else
880         int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
881         int offset_xsize =
882           (centered ?  font_width * (line_length - buffer_len) / 2 : 0);
883         int final_cut_length = MAX(0, cut_length - offset_chars);
884         int xx = x + offset_xsize;
885
886         buffer[final_cut_length] = '\0';
887
888         if (mask_mode != -1)
889           DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
890                       font_nr, mask_mode);
891         else
892           DrawText(xx, y + current_line * font_height, buffer, font_nr);
893 #endif
894
895         current_line++;
896
897         last_line_was_empty = (buffer_len == 0);
898
899         buffer[0] = '\0';
900         buffer_len = 0;
901       }
902     }
903   }
904
905   if (buffer_len > 0 && current_line < max_lines)
906   {
907 #if 1
908     DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
909                          mask_mode, centered, current_line);
910 #else
911     int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
912         int offset_xsize =
913           (centered ?  font_width * (line_length - buffer_len) / 2 : 0);
914     int final_cut_length = MAX(0, cut_length - offset_chars);
915     int xx = x + offset_xsize;
916
917     buffer[final_cut_length] = '\0';
918
919     if (mask_mode != -1)
920       DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
921                   font_nr, mask_mode);
922     else
923       DrawText(xx, y + current_line * font_height, buffer, font_nr);
924 #endif
925
926     current_line++;
927   }
928
929   return current_line;
930 }
931
932 int DrawTextFile(int x, int y, char *filename, int font_nr,
933                  int line_length, int cut_length, int max_lines,
934                  int mask_mode, boolean autowrap, boolean centered,
935                  boolean parse_comments)
936 {
937   char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
938   int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
939                                          line_length, cut_length, max_lines,
940                                          mask_mode, autowrap, centered,
941                                          parse_comments);
942   checked_free(text_buffer);
943
944   return num_lines_printed;
945 }
946
947 #if 0
948 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
949                      int max_lines)
950 {
951   DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
952                  FALSE, FALSE);
953 }
954
955 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
956                         int cut_length, int max_lines, int mask_mode)
957 {
958   DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
959                  mask_mode, FALSE, FALSE, FALSE);
960 }
961 #endif