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