added debug output showing which font token is used for which screen text
[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 //                  https://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                   char * (*get_token_from_font_function)(int))
27 {
28   gfx.num_fonts = num_fonts;
29   gfx.font_bitmap_info = font_bitmap_info;
30   gfx.select_font_function = select_font_function;
31   gfx.get_font_from_token_function = get_font_from_token_function;
32   gfx.get_token_from_font_function = get_token_from_font_function;
33 }
34
35 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
36 {
37   if (font_bitmap_info == NULL)
38     return;
39
40   free(font_bitmap_info);
41 }
42
43 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
44 {
45   int font_bitmap_id = gfx.select_font_function(font_nr);
46
47   return &gfx.font_bitmap_info[font_bitmap_id];
48 }
49
50 int getFontWidth(int font_nr)
51 {
52   int font_bitmap_id = gfx.select_font_function(font_nr);
53
54   return gfx.font_bitmap_info[font_bitmap_id].width;
55 }
56
57 int getFontHeight(int font_nr)
58 {
59   int font_bitmap_id = gfx.select_font_function(font_nr);
60
61   return gfx.font_bitmap_info[font_bitmap_id].height;
62 }
63
64 int getFontDrawOffsetX(int font_nr)
65 {
66   int font_bitmap_id = gfx.select_font_function(font_nr);
67
68   return gfx.font_bitmap_info[font_bitmap_id].draw_xoffset;
69 }
70
71 int getFontDrawOffsetY(int font_nr)
72 {
73   int font_bitmap_id = gfx.select_font_function(font_nr);
74
75   return gfx.font_bitmap_info[font_bitmap_id].draw_yoffset;
76 }
77
78 int getTextWidth(char *text, int font_nr)
79 {
80   return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
81 }
82
83 static int getFontCharPosition(int font_nr, char c)
84 {
85   int font_bitmap_id = gfx.select_font_function(font_nr);
86   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
87   boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
88   int font_pos = (unsigned char)c - 32;
89
90   // map some special characters to their ascii values in default font
91   if (default_font)
92     font_pos = MAP_FONT_ASCII(c) - 32;
93
94   // this allows dynamic special characters together with special font
95   if (font_pos < 0 || font_pos >= font->num_chars)
96     font_pos = 0;
97
98   return font_pos;
99 }
100
101 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
102 {
103   int font_bitmap_id = gfx.select_font_function(font_nr);
104   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
105   int font_pos = getFontCharPosition(font_nr, c);
106   int offset_x = (font->offset_x != 0 ? font->offset_x : font->width);
107   int offset_y = (font->offset_y != 0 ? font->offset_y : font->height);
108
109   *bitmap = font->bitmap;
110   *x = font->src_x + (font_pos % font->num_chars_per_line) * offset_x;
111   *y = font->src_y + (font_pos / font->num_chars_per_line) * offset_y;
112 }
113
114
115 // ============================================================================
116 // text string helper functions
117 // ============================================================================
118
119 int maxWordLengthInRequestString(char *text)
120 {
121   char *text_ptr;
122   int word_len = 0, max_word_len = 0;
123
124   for (text_ptr = text; *text_ptr; text_ptr++)
125   {
126     word_len = (*text_ptr != ' ' &&
127                 *text_ptr != '?' &&
128                 *text_ptr != '!' ? word_len + 1 : 0);
129
130     max_word_len = MAX(word_len, max_word_len);
131   }
132
133   return max_word_len;
134 }
135
136
137 // ============================================================================
138 // simple text drawing functions
139 // ============================================================================
140
141 static void DrawInitTextExt(char *text, int ypos, int font_nr, boolean update)
142 {
143   LimitScreenUpdates(TRUE);
144
145   UPDATE_BUSY_STATE();
146
147   if (window != NULL &&
148       gfx.draw_init_text &&
149       gfx.num_fonts > 0 &&
150       gfx.font_bitmap_info[font_nr].bitmap != NULL)
151   {
152     int x = (video.width - getTextWidth(text, font_nr)) / 2;
153     int y = ypos;
154     int width = video.width;
155     int height = getFontHeight(font_nr);
156
157     ClearRectangleOnBackground(drawto, 0, y, width, height);
158     DrawTextExt(drawto, x, y, text, font_nr, BLIT_MASKED);
159
160     if (update)
161       BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
162   }
163 }
164
165 void DrawInitText(char *text, int ypos, int font_nr)
166 {
167   DrawInitTextExt(text, ypos, font_nr, FALSE);
168 }
169
170 void DrawInitTextHead(char *text)
171 {
172   DrawInitTextExt(text, 120, FC_GREEN, FALSE);
173 }
174
175 void DrawInitTextItem(char *text)
176 {
177   DrawInitTextExt(text, 150, FC_YELLOW, TRUE);
178 }
179
180 void DrawTextF(int x, int y, int font_nr, char *format, ...)
181 {
182   char buffer[MAX_OUTPUT_LINESIZE + 1];
183   va_list ap;
184
185   va_start(ap, format);
186   vsprintf(buffer, format, ap);
187   va_end(ap);
188
189   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
190     Fail("string too long in DrawTextF() -- aborting");
191
192   DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
193 }
194
195 void DrawTextFCentered(int y, int font_nr, char *format, ...)
196 {
197   char buffer[MAX_OUTPUT_LINESIZE + 1];
198   va_list ap;
199
200   va_start(ap, format);
201   vsprintf(buffer, format, ap);
202   va_end(ap);
203
204   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
205     Fail("string too long in DrawTextFCentered() -- aborting");
206
207   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
208            gfx.sy + y, buffer, font_nr);
209 }
210
211 void DrawTextS(int x, int y, int font_nr, char *text)
212 {
213   DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
214 }
215
216 void DrawTextSCentered(int y, int font_nr, char *text)
217 {
218   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
219            gfx.sy + y, text, font_nr);
220 }
221
222 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
223 {
224   DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
225            gfx.sy + y, text, font_nr);
226 }
227
228 void DrawText(int x, int y, char *text, int font_nr)
229 {
230   int mask_mode = BLIT_OPAQUE;
231
232   if (DrawingOnBackground(x, y))
233     mask_mode = BLIT_ON_BACKGROUND;
234
235   DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
236
237   if (IN_GFX_FIELD_FULL(x, y))
238     redraw_mask |= REDRAW_FIELD;
239   else if (IN_GFX_DOOR_1(x, y))
240     redraw_mask |= REDRAW_DOOR_1;
241   else if (IN_GFX_DOOR_2(x, y))
242     redraw_mask |= REDRAW_DOOR_2;
243   else if (IN_GFX_DOOR_3(x, y))
244     redraw_mask |= REDRAW_DOOR_3;
245   else
246     redraw_mask |= REDRAW_ALL;
247 }
248
249 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
250                  int font_nr, int mask_mode)
251 {
252   struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
253   int font_width = getFontWidth(font_nr);
254   int font_height = getFontHeight(font_nr);
255   Bitmap *src_bitmap;
256   int src_x, src_y;
257   char *text_ptr = text;
258
259 #if DEBUG
260   Debug("font:token", "'%s' / '%s'",
261         gfx.get_token_from_font_function(font_nr), text);
262 #endif
263
264   if (font->bitmap == NULL)
265     return;
266
267   // skip text to be printed outside the window (left/right will be clipped)
268   if (dst_y < 0 || dst_y + font_height > video.height)
269     return;
270
271   // add offset for drawing font characters
272   dst_x += font->draw_xoffset;
273   dst_y += font->draw_yoffset;
274
275   while (*text_ptr)
276   {
277     char c = *text_ptr++;
278
279     if (c == '\n')
280       c = ' ';          // print space instead of newline
281
282     getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
283
284     // clip text at the left side of the window
285     if (dst_x < 0)
286     {
287       dst_x += font_width;
288
289       continue;
290     }
291
292     // clip text at the right side of the window
293     if (dst_x + font_width > video.width)
294       break;
295
296     if (mask_mode == BLIT_INVERSE)      // special mode for text gadgets
297     {
298       // first step: draw solid colored rectangle (use "cursor" character)
299       if (strlen(text) == 1)    // only one char inverted => draw cursor
300       {
301         Bitmap *cursor_bitmap;
302         int cursor_x, cursor_y;
303
304         getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
305                           &cursor_x, &cursor_y);
306
307         BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
308                    font_width, font_height, dst_x, dst_y);
309       }
310
311       // second step: draw masked inverted character
312       SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
313                            font_width, font_height, dst_x, dst_y);
314     }
315     else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
316     {
317       if (mask_mode == BLIT_ON_BACKGROUND)
318       {
319         // clear font character background
320         ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
321                                    font_width, font_height);
322       }
323
324       BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
325                        font_width, font_height, dst_x, dst_y);
326     }
327     else        // normal, non-masked font blitting
328     {
329       BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
330                  font_width, font_height, dst_x, dst_y);
331     }
332
333     dst_x += font_width;
334   }
335 }
336
337
338 // ============================================================================
339 // text buffer drawing functions
340 // ============================================================================
341
342 #define MAX_LINES_FROM_FILE             1024
343
344 char *GetTextBufferFromFile(char *filename, int max_lines)
345 {
346   File *file;
347   char *buffer;
348   int num_lines = 0;
349
350   if (filename == NULL)
351     return NULL;
352
353   if (!(file = openFile(filename, MODE_READ)))
354     return NULL;
355
356   buffer = checked_calloc(1);   // start with valid, but empty text buffer
357
358   while (!checkEndOfFile(file) && num_lines < max_lines)
359   {
360     char line[MAX_LINE_LEN];
361     char *line_ptr;
362     int line_len;
363
364     // read next line of input file
365     if (!getStringFromFile(file, line, MAX_LINE_LEN))
366       break;
367
368     line_len = strlen(line);
369
370     // cut trailing line break (this can be newline and/or carriage return)
371     for (line_ptr = &line[line_len]; line_ptr >= line; line_ptr--)
372       if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
373         *line_ptr = '\0';
374
375     // re-add newline (so the result is terminated by newline, but not CR/LF)
376     if (strlen(line) != line_len)
377       strcat(line, "\n");
378
379     buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
380
381     strcat(buffer, line);
382
383     num_lines++;
384   }
385
386   closeFile(file);
387
388   if (getTextEncoding(buffer) == TEXT_ENCODING_UTF_8)
389   {
390     char *body_latin1 = getLatin1FromUTF8(buffer);
391
392     checked_free(buffer);
393
394     buffer = body_latin1;
395   }
396
397   return buffer;
398 }
399
400 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
401                                   int *dst_buffer_len, int line_length,
402                                   boolean last_line_was_empty)
403 {
404   char *text_ptr = *src_buffer_ptr;
405   char *buffer = dst_buffer;
406   int buffer_len = *dst_buffer_len;
407   boolean buffer_filled = FALSE;
408
409   while (*text_ptr)
410   {
411     char *word_ptr;
412     int word_len;
413
414     if (strEqual(text_ptr, "  "))       // special case: force line break
415       buffer_filled = TRUE;
416
417     // skip leading whitespaces
418     while (*text_ptr == ' ' || *text_ptr == '\t')
419       text_ptr++;
420
421     word_ptr = text_ptr;
422     word_len = 0;
423
424     // look for end of next word
425     while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
426     {
427       word_ptr++;
428       word_len++;
429     }
430
431     if (word_len == 0)
432     {
433       continue;
434     }
435     else if (*text_ptr == '\n')         // special case: force empty line
436     {
437       if (buffer_len == 0)
438         text_ptr++;
439
440       // prevent printing of multiple empty lines
441       if (buffer_len > 0 || !last_line_was_empty)
442         buffer_filled = TRUE;
443     }
444     else if (word_len < line_length - buffer_len)
445     {
446       // word fits into text buffer -- add word
447
448       if (buffer_len > 0)
449         buffer[buffer_len++] = ' ';
450
451       strncpy(&buffer[buffer_len], text_ptr, word_len);
452       buffer_len += word_len;
453       buffer[buffer_len] = '\0';
454       text_ptr += word_len;
455     }
456     else if (buffer_len > 0)
457     {
458       // not enough space left for word in text buffer -- print buffer
459
460       buffer_filled = TRUE;
461     }
462     else
463     {
464       // word does not fit at all into empty text buffer -- cut word
465
466       strncpy(buffer, text_ptr, line_length);
467       buffer[line_length] = '\0';
468       text_ptr += line_length;
469       buffer_filled = TRUE;
470     }
471
472     if (buffer_filled)
473       break;
474   }
475
476   *src_buffer_ptr = text_ptr;
477   *dst_buffer_len = buffer_len;
478
479   return buffer_filled;
480 }
481
482 static boolean getCheckedTokenValueFromString(char *string, char **token,
483                                               char **value)
484 {
485   char *ptr;
486
487   if (!getTokenValueFromString(string, token, value))
488     return FALSE;
489
490   if (**token != '.')                   // token should begin with dot
491     return FALSE;
492
493   for (ptr = *token; *ptr; ptr++)       // token should contain no whitespace
494     if (*ptr == ' ' || *ptr == '\t')
495       return FALSE;
496
497   for (ptr = *value; *ptr; ptr++)       // value should contain no whitespace
498     if (*ptr == ' ' || *ptr == '\t')
499       return FALSE;
500
501   return TRUE;
502 }
503
504 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
505                                  int line_length, int cut_length,
506                                  int mask_mode, boolean centered,
507                                  int current_ypos)
508 {
509   int buffer_len = strlen(buffer);
510   int font_width = getFontWidth(font_nr);
511   int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
512   int offset_xsize =
513     (centered ? font_width * (line_length - buffer_len) / 2 : 0);
514   int final_cut_length = MAX(0, cut_length - offset_chars);
515   int xx = x + offset_xsize;
516   int yy = y + current_ypos;
517
518   buffer[final_cut_length] = '\0';
519
520   if (mask_mode != -1)
521     DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
522   else
523     DrawText(xx, yy, buffer, font_nr);
524 }
525
526 static int DrawTextBufferExt(int x, int y, char *text_buffer, int font_nr,
527                              int line_length, int cut_length, int max_lines,
528                              int line_spacing, int mask_mode, boolean autowrap,
529                              boolean centered, boolean parse_comments,
530                              boolean is_text_area)
531 {
532   char buffer[line_length + 1];
533   int buffer_len;
534   int font_height = getFontHeight(font_nr);
535   int line_height = font_height + line_spacing;
536   int current_line = 0;
537   int current_ypos = 0;
538   int max_ysize = max_lines * line_height;
539
540   if (text_buffer == NULL || *text_buffer == '\0')
541     return 0;
542
543   if (current_line >= max_lines)
544     return 0;
545
546   if (cut_length == -1)
547     cut_length = line_length;
548
549   buffer[0] = '\0';
550   buffer_len = 0;
551
552   while (*text_buffer && current_ypos < max_ysize)
553   {
554     char line[MAX_LINE_LEN + 1];
555     char *line_ptr;
556     boolean last_line_was_empty = TRUE;
557     int num_line_chars = MAX_LINE_LEN;
558     int i;
559
560     // copy next line from text buffer to line buffer (nearly fgets() style)
561     for (i = 0; i < num_line_chars && *text_buffer; i++)
562     {
563       if ((line[i] = *text_buffer++) == '\n')
564       {
565         // in text areas, 'line_length' sized lines cause additional empty line
566         if (i == line_length && is_text_area)
567           text_buffer--;
568
569         break;
570       }
571     }
572     line[i] = '\0';
573
574     // prevent 'num_line_chars' sized lines to cause additional empty line
575     if (i == num_line_chars && *text_buffer == '\n')
576       text_buffer++;
577
578     // skip comments (lines directly beginning with '#')
579     if (line[0] == '#' && parse_comments)
580     {
581       char *token, *value;
582
583       // try to read generic token/value pair definition after comment sign
584       if (getCheckedTokenValueFromString(line + 1, &token, &value))
585       {
586         // if found, flush the current buffer, if non-empty
587         if (buffer_len > 0 && current_ypos < max_ysize)
588         {
589           DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
590                                mask_mode, centered, current_ypos);
591           current_ypos += line_height;
592           current_line++;
593
594           buffer[0] = '\0';
595           buffer_len = 0;
596         }
597
598         if (strEqual(token, ".font"))
599           font_nr = gfx.get_font_from_token_function(value);
600         else if (strEqual(token, ".autowrap"))
601           autowrap = get_boolean_from_string(value);
602         else if (strEqual(token, ".centered"))
603           centered = get_boolean_from_string(value);
604         else if (strEqual(token, ".parse_comments"))
605           parse_comments = get_boolean_from_string(value);
606
607         // if font has changed, depending values need to be updated as well
608         font_height = getFontHeight(font_nr);
609         line_height = font_height + line_spacing;
610       }
611
612       continue;
613     }
614
615     // cut trailing newline and carriage return from input line
616     for (line_ptr = line; *line_ptr; line_ptr++)
617     {
618       if (*line_ptr == '\n' || *line_ptr == '\r')
619       {
620         *line_ptr = '\0';
621         break;
622       }
623     }
624
625     if (strlen(line) == 0)              // special case: force empty line
626       strcpy(line, "\n");
627
628     line_ptr = line;
629
630     while (*line_ptr && current_ypos < max_ysize)
631     {
632       boolean buffer_filled;
633
634       if (autowrap)
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
658       if (buffer_filled)
659       {
660         DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
661                              mask_mode, centered, current_ypos);
662         current_ypos += line_height;
663         current_line++;
664
665         last_line_was_empty = (buffer_len == 0);
666
667         buffer[0] = '\0';
668         buffer_len = 0;
669       }
670     }
671   }
672
673   if (buffer_len > 0 && current_ypos < max_ysize)
674   {
675     DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
676                          mask_mode, centered, current_ypos);
677     current_ypos += line_height;
678     current_line++;
679   }
680
681   return current_line;
682 }
683
684 int DrawTextArea(int x, int y, char *text_buffer, int font_nr,
685                  int line_length, int cut_length, int max_lines,
686                  int line_spacing, int mask_mode, boolean autowrap,
687                  boolean centered, boolean parse_comments)
688 {
689   return DrawTextBufferExt(x, y, text_buffer, font_nr,
690                            line_length, cut_length, max_lines,
691                            line_spacing, mask_mode, autowrap,
692                            centered, parse_comments, TRUE);
693 }
694
695 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
696                    int line_length, int cut_length, int max_lines,
697                    int line_spacing, int mask_mode, boolean autowrap,
698                    boolean centered, boolean parse_comments)
699 {
700   return DrawTextBufferExt(x, y, text_buffer, font_nr,
701                            line_length, cut_length, max_lines,
702                            line_spacing, mask_mode, autowrap,
703                            centered, parse_comments, FALSE);
704 }
705
706 int DrawTextBufferS(int x, int y, char *text_buffer, int font_nr,
707                     int line_length, int cut_length, int max_lines,
708                     int line_spacing, int mask_mode, boolean autowrap,
709                     boolean centered, boolean parse_comments)
710 {
711   return DrawTextBuffer(gfx.sx + x, gfx.sy + y, text_buffer, font_nr,
712                         line_length, cut_length, max_lines,
713                         line_spacing, mask_mode, autowrap,
714                         centered, parse_comments);
715 }
716
717 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
718                      int line_length, int cut_length, int max_lines,
719                      int line_spacing, int mask_mode, boolean autowrap,
720                      boolean centered, boolean parse_comments)
721 {
722   char text_buffer[MAX_OUTPUT_LINESIZE];
723   int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
724
725   if (text_length >= MAX_OUTPUT_LINESIZE)
726     Warn("string too long in DrawTextBufferVA() -- truncated");
727
728   int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
729                                          line_length, cut_length, max_lines,
730                                          line_spacing, mask_mode, autowrap,
731                                          centered, parse_comments);
732   return num_lines_printed;
733 }
734
735 int DrawTextFile(int x, int y, char *filename, int font_nr,
736                  int line_length, int cut_length, int max_lines,
737                  int line_spacing, int mask_mode, boolean autowrap,
738                  boolean centered, boolean parse_comments)
739 {
740   char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
741   int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
742                                          line_length, cut_length, max_lines,
743                                          line_spacing, mask_mode, autowrap,
744                                          centered, parse_comments);
745   checked_free(text_buffer);
746
747   return num_lines_printed;
748 }