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