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