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