rnd-20070203-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 char *GetTextBufferFromFile(char *filename, int max_lines)
408 {
409   FILE *file;
410   char *buffer;
411   int num_lines = 0;
412
413   if (filename == NULL)
414     return NULL;
415
416   if (!(file = fopen(filename, MODE_READ)))
417     return NULL;
418
419   buffer = checked_calloc(1);   /* start with valid, but empty text buffer */
420
421   while (!feof(file) && num_lines < max_lines)
422   {
423     char line[MAX_LINE_LEN];
424
425     /* read next line of input file */
426     if (!fgets(line, MAX_LINE_LEN, file))
427       break;
428
429     buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
430
431     strcat(buffer, line);
432
433     num_lines++;
434   }
435
436   fclose(file);
437
438   return buffer;
439 }
440
441 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
442                             int area_xsize, int area_ysize, int mask_mode)
443 {
444   int area_line = 0;
445   int font_height = getFontHeight(font_nr);
446
447   if (text == NULL)
448     return;
449
450   while (*text && area_line < area_ysize)
451   {
452     char buffer[MAX_OUTPUT_LINESIZE + 1];
453     int i;
454
455     for (i = 0; i < line_length && *text; i++)
456       if ((buffer[i] = *text++) == '\n')
457         break;
458     buffer[MIN(i, area_xsize)] = '\0';
459
460     DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
461                 mask_mode);
462
463     area_line++;
464   }
465
466   redraw_mask |= REDRAW_FIELD;
467 }
468
469 boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
470                            int *dst_buffer_len, boolean last_line_was_empty,
471                            int line_length)
472 {
473   char *text_ptr = *src_buffer_ptr;
474   char *buffer = dst_buffer;
475   int buffer_len = *dst_buffer_len;
476   boolean buffer_filled = FALSE;
477
478   while (*text_ptr)
479   {
480     char *word_ptr;
481     int word_len;
482
483     /* skip leading whitespaces */
484     while (*text_ptr == ' ' || *text_ptr == '\t')
485       text_ptr++;
486
487     word_ptr = text_ptr;
488     word_len = 0;
489
490     /* look for end of next word */
491     while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
492     {
493       word_ptr++;
494       word_len++;
495     }
496
497     if (word_len == 0)
498     {
499       continue;
500     }
501     else if (*text_ptr == '\n')         /* special case: force empty line */
502     {
503       if (buffer_len == 0)
504         text_ptr++;
505
506       /* prevent printing of multiple empty lines */
507       if (buffer_len > 0 || !last_line_was_empty)
508         buffer_filled = TRUE;
509     }
510     else if (word_len < line_length - buffer_len)
511     {
512       /* word fits into text buffer -- add word */
513
514       if (buffer_len > 0)
515         buffer[buffer_len++] = ' ';
516
517       strncpy(&buffer[buffer_len], text_ptr, word_len);
518       buffer_len += word_len;
519       buffer[buffer_len] = '\0';
520       text_ptr += word_len;
521     }
522     else if (buffer_len > 0)
523     {
524       /* not enough space left for word in text buffer -- print buffer */
525
526       buffer_filled = TRUE;
527     }
528     else
529     {
530       /* word does not fit at all into empty text buffer -- cut word */
531
532       strncpy(buffer, text_ptr, line_length);
533       buffer[line_length] = '\0';
534       text_ptr += line_length;
535       buffer_filled = TRUE;
536     }
537
538     if (buffer_filled)
539       break;
540   }
541
542   *src_buffer_ptr = text_ptr;
543   *dst_buffer_len = buffer_len;
544
545   return buffer_filled;
546 }
547
548 #if 0
549 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
550                          int max_lines)
551 {
552   char *text_ptr = text;
553   int current_line = 0;
554   int font_height = getFontHeight(font_nr);
555
556   while (*text_ptr && current_line < max_lines)
557   {
558     char buffer[line_length + 1];
559     int buffer_len = 0;
560
561     buffer[0] = '\0';
562
563     RenderLineToBuffer(&text_ptr, buffer, &buffer_len, TRUE, line_length);
564
565     DrawText(x, y + current_line * font_height, buffer, font_nr);
566     current_line++;
567   }
568 }
569 #endif
570
571 #if 0
572 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
573                          int line_length, int max_lines, boolean wrap_text)
574 {
575   int font_height = getFontHeight(font_nr);
576   char line[MAX_LINE_LEN];
577   char buffer[line_length + 1];
578   int buffer_len;
579   int current_line = 0;
580   FILE *file;
581
582   if (current_line >= max_lines)
583     return 0;
584
585   if (filename == NULL)
586     return 0;
587
588   if (!(file = fopen(filename, MODE_READ)))
589     return 0;
590
591   buffer[0] = '\0';
592   buffer_len = 0;
593
594   while (!feof(file) && current_line < max_lines)
595   {
596     char *line_ptr;
597     boolean last_line_was_empty = TRUE;
598
599     /* read next line of input file */
600     if (!fgets(line, MAX_LINE_LEN, file))
601       break;
602
603     /* skip comments (lines directly beginning with '#') */
604     if (line[0] == '#')
605       continue;
606
607     /* cut trailing newline from input line */
608     for (line_ptr = line; *line_ptr; line_ptr++)
609     {
610       if (*line_ptr == '\n' || *line_ptr == '\r')
611       {
612         *line_ptr = '\0';
613         break;
614       }
615     }
616
617     if (strlen(line) == 0)              /* special case: force empty line */
618       strcpy(line, "\n");
619
620     line_ptr = line;
621
622     while (*line_ptr && current_line < max_lines)
623     {
624 #if 1
625       boolean buffer_filled;
626
627       if (wrap_text)
628       {
629         buffer_filled = RenderLineToBuffer(&line_ptr,
630                                            buffer, &buffer_len,
631                                            last_line_was_empty,
632                                            line_length);
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,
654                                                  buffer, &buffer_len,
655                                                  last_line_was_empty,
656                                                  line_length);
657 #endif
658
659       if (buffer_filled)
660       {
661         DrawText(x, y + current_line * font_height, buffer, font_nr);
662         current_line++;
663
664         last_line_was_empty = (buffer_len == 0);
665
666         buffer[0] = '\0';
667         buffer_len = 0;
668       }
669     }
670   }
671
672   fclose(file);
673
674   if (buffer_len > 0 && current_line < max_lines)
675   {
676     DrawText(x, y + current_line * font_height, buffer, font_nr);
677     current_line++;
678   }
679
680   return current_line;
681 }
682 #endif
683
684 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
685                    int line_length, int cut_length, int max_lines,
686                    int mask_mode, boolean formatted, boolean centered)
687 {
688   int font_width = getFontWidth(font_nr);
689   int font_height = getFontHeight(font_nr);
690   char buffer[line_length + 1];
691   int buffer_len;
692   int current_line = 0;
693
694   if (text_buffer == NULL || *text_buffer == '\0')
695     return 0;
696
697   if (current_line >= max_lines)
698     return 0;
699
700   if (cut_length == -1)
701     cut_length = line_length;
702
703   buffer[0] = '\0';
704   buffer_len = 0;
705
706   while (*text_buffer && current_line < max_lines)
707   {
708     char line[MAX_LINE_LEN + 1];
709     char *line_ptr;
710     boolean last_line_was_empty = TRUE;
711     int num_line_chars = (formatted ? MAX_LINE_LEN : line_length);
712     int i;
713
714     /* copy next line from text buffer to line buffer (nearly fgets() style) */
715     for (i = 0; i < num_line_chars && *text_buffer; i++)
716       if ((line[i] = *text_buffer++) == '\n')
717         break;
718     line[i] = '\0';
719
720     /* skip comments (lines directly beginning with '#') */
721     if (line[0] == '#')
722       continue;
723
724     /* cut trailing newline and carriage return from input line */
725     for (line_ptr = line; *line_ptr; line_ptr++)
726     {
727       if (*line_ptr == '\n' || *line_ptr == '\r')
728       {
729         *line_ptr = '\0';
730         break;
731       }
732     }
733
734     if (strlen(line) == 0)              /* special case: force empty line */
735       strcpy(line, "\n");
736
737     line_ptr = line;
738
739     while (*line_ptr && current_line < max_lines)
740     {
741       boolean buffer_filled;
742
743       if (formatted)
744       {
745         buffer_filled = RenderLineToBuffer(&line_ptr,
746                                            buffer, &buffer_len,
747                                            last_line_was_empty,
748                                            line_length);
749       }
750       else
751       {
752         if (strlen(line_ptr) <= line_length)
753         {
754           buffer_len = strlen(line_ptr);
755           strcpy(buffer, line_ptr);
756         }
757         else
758         {
759           buffer_len = line_length;
760           strncpy(buffer, line_ptr, line_length);
761         }
762
763         buffer[buffer_len] = '\0';
764         line_ptr += buffer_len;
765
766         buffer_filled = TRUE;
767       }
768
769       if (buffer_filled)
770       {
771         int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
772         int offset_xsize =
773           (centered ?  font_width * (line_length - buffer_len) / 2 : 0);
774         int final_cut_length = MAX(0, cut_length - offset_chars);
775         int xx = x + offset_xsize;
776
777         buffer[final_cut_length] = '\0';
778
779         if (mask_mode != -1)
780           DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
781                       font_nr, mask_mode);
782         else
783           DrawText(xx, y + current_line * font_height, buffer, font_nr);
784
785         current_line++;
786
787         last_line_was_empty = (buffer_len == 0);
788
789         buffer[0] = '\0';
790         buffer_len = 0;
791       }
792     }
793   }
794
795   if (buffer_len > 0 && current_line < max_lines)
796   {
797     int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
798         int offset_xsize =
799           (centered ?  font_width * (line_length - buffer_len) / 2 : 0);
800     int final_cut_length = MAX(0, cut_length - offset_chars);
801     int xx = x + offset_xsize;
802
803     buffer[final_cut_length] = '\0';
804
805     if (mask_mode != -1)
806       DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
807                   font_nr, mask_mode);
808     else
809       DrawText(xx, y + current_line * font_height, buffer, font_nr);
810
811     current_line++;
812   }
813
814   return current_line;
815 }
816
817 int DrawTextFromFile(int x, int y, char *filename, int font_nr,
818                      int line_length, int max_lines, boolean formatted)
819 {
820   char *text_buffer = GetTextBufferFromFile(filename, 1000);
821   int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
822                                          line_length, -1, max_lines, -1,
823                                          formatted, FALSE);
824   checked_free(text_buffer);
825
826   return num_lines_printed;
827 }
828
829 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
830                      int max_lines)
831 {
832   DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
833                  FALSE);
834 }
835
836 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
837                         int cut_length, int max_lines, int mask_mode)
838 {
839   DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
840                  mask_mode, FALSE, FALSE);
841 }