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