rnd-20061230-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 void DrawInitText(char *text, int ypos, int font_nr)
189 {
190   if (window != NULL &&
191       gfx.num_fonts > 0 &&
192       gfx.font_bitmap_info[font_nr].bitmap != NULL)
193   {
194     int x = (video.width - getTextWidth(text, font_nr)) / 2;
195     int y = ypos;
196     int width = video.width;
197     int height = getFontHeight(font_nr);
198
199     ClearRectangle(drawto, 0, y, width, height);
200     DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
201
202     /* this makes things significantly faster than directly drawing to window */
203     BlitBitmap(drawto, window, 0, y, width, height, 0, y);
204   }
205 }
206
207 void DrawTextF(int x, int y, int font_nr, char *format, ...)
208 {
209   char buffer[MAX_OUTPUT_LINESIZE + 1];
210   va_list ap;
211
212   va_start(ap, format);
213   vsprintf(buffer, format, ap);
214   va_end(ap);
215
216   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
217     Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
218
219   DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
220 }
221
222 void DrawTextFCentered(int y, int font_nr, char *format, ...)
223 {
224   char buffer[MAX_OUTPUT_LINESIZE + 1];
225   va_list ap;
226
227   va_start(ap, format);
228   vsprintf(buffer, format, ap);
229   va_end(ap);
230
231   if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
232     Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
233
234   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
235            gfx.sy + y, buffer, font_nr);
236 }
237
238 void DrawTextS(int x, int y, int font_nr, char *text)
239 {
240   DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
241 }
242
243 void DrawTextSCentered(int y, int font_nr, char *text)
244 {
245   DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
246            gfx.sy + 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 (x < gfx.dx)
271     redraw_mask |= REDRAW_FIELD;
272   else if (y < gfx.vy || gfx.vy == 0)
273     redraw_mask |= REDRAW_DOOR_1;
274 }
275
276 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
277                  int font_nr, int mask_mode)
278 {
279 #if 1
280   struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
281 #else
282   int font_bitmap_id = gfx.select_font_function(font_nr);
283   struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
284 #endif
285   int font_width = getFontWidth(font_nr);
286   int font_height = getFontHeight(font_nr);
287 #if 0
288   int border_1 = gfx.sx + gfx.sxsize;
289   int border_2 = gfx.dx + gfx.dxsize;
290   int dst_x_start = dst_x;
291 #endif
292   Bitmap *src_bitmap;
293   int src_x, src_y;
294   char *text_ptr = text;
295
296   if (font->bitmap == NULL)
297     return;
298
299   /* skip text to be printed outside the window (left/right will be clipped) */
300   if (dst_y < 0 || dst_y + font_height > video.height)
301     return;
302
303   /* add offset for drawing font characters */
304   dst_x += font->draw_xoffset;
305   dst_y += font->draw_yoffset;
306
307   while (*text_ptr)
308   {
309     char c = *text_ptr++;
310
311     if (c == '\n')
312       c = ' ';          /* print space instead of newline */
313
314     getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
315
316     /* clip text at the left side of the window */
317     if (dst_x < 0)
318     {
319       dst_x += font_width;
320
321       continue;
322     }
323
324     /* clip text at the right side of the window */
325 #if 1
326     if (dst_x + font_width > video.width)
327       break;
328 #else
329     /* (this does not work well when trying to print text to whole screen) */
330     if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
331         (dst_x_start < border_2 && dst_x + font_width > border_2))
332       break;
333 #endif
334
335     if (mask_mode == BLIT_INVERSE)      /* special mode for text gadgets */
336     {
337       /* first step: draw solid colored rectangle (use "cursor" character) */
338       if (strlen(text) == 1)    /* only one char inverted => draw cursor */
339       {
340         Bitmap *cursor_bitmap;
341         int cursor_x, cursor_y;
342
343         getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
344                           &cursor_x, &cursor_y);
345
346         BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
347                    font_width, font_height, dst_x, dst_y);
348       }
349
350 #if defined(TARGET_SDL)
351       /* second step: draw masked inverted character */
352       SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
353                            font_width, font_height, dst_x, dst_y);
354 #else
355       /* second step: draw masked black rectangle (use "space" character) */
356       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
357                     dst_x - src_x, dst_y - src_y);
358       BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
359                        font_width, font_height, dst_x, dst_y);
360 #endif
361     }
362     else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
363     {
364       if (mask_mode == BLIT_ON_BACKGROUND)
365       {
366         /* clear font character background */
367         ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
368                                    font_width, font_height);
369       }
370
371 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
372       /* use special font tile clipmasks */
373       {
374         int font_pos = getFontCharPosition(font_nr, c);
375
376         SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
377         SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
378       }
379 #else
380       SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
381                     dst_x - src_x, dst_y - src_y);
382 #endif
383
384       BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
385                        font_width, font_height, dst_x, dst_y);
386     }
387     else        /* normal, non-masked font blitting */
388     {
389       BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
390                  font_width, font_height, dst_x, dst_y);
391     }
392
393     dst_x += font_width;
394   }
395 }
396
397 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
398                         int area_xsize, int area_ysize, int mask_mode)
399 {
400   int area_line = 0;
401   int font_height = getFontHeight(font_nr);
402
403   if (text == NULL)
404     return;
405
406   while (*text && area_line < area_ysize)
407   {
408     char buffer[MAX_OUTPUT_LINESIZE + 1];
409     int i;
410
411     for (i = 0; i < line_length && *text; i++)
412       if ((buffer[i] = *text++) == '\n')
413         break;
414     buffer[MIN(i, area_xsize)] = '\0';
415
416     DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
417                 mask_mode);
418
419     area_line++;
420   }
421
422   redraw_mask |= REDRAW_FIELD;
423 }
424
425 boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
426                            int *dst_buffer_len, boolean last_line_was_empty,
427                            int line_length)
428 {
429   char *text_ptr = *src_buffer_ptr;
430   char *buffer = dst_buffer;
431   int buffer_len = *dst_buffer_len;
432   boolean buffer_filled = FALSE;
433
434   while (*text_ptr)
435   {
436     char *word_ptr;
437     int word_len;
438
439     /* skip leading whitespaces */
440     while (*text_ptr == ' ' || *text_ptr == '\t')
441       text_ptr++;
442
443     word_ptr = text_ptr;
444     word_len = 0;
445
446     /* look for end of next word */
447     while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
448     {
449       word_ptr++;
450       word_len++;
451     }
452
453     if (word_len == 0)
454     {
455       continue;
456     }
457     else if (*text_ptr == '\n')         /* special case: force empty line */
458     {
459       if (buffer_len == 0)
460         text_ptr++;
461
462       /* prevent printing of multiple empty lines */
463       if (buffer_len > 0 || !last_line_was_empty)
464         buffer_filled = TRUE;
465     }
466     else if (word_len < line_length - buffer_len)
467     {
468       /* word fits into text buffer -- add word */
469
470       if (buffer_len > 0)
471         buffer[buffer_len++] = ' ';
472
473       strncpy(&buffer[buffer_len], text_ptr, word_len);
474       buffer_len += word_len;
475       buffer[buffer_len] = '\0';
476       text_ptr += word_len;
477     }
478     else if (buffer_len > 0)
479     {
480       /* not enough space left for word in text buffer -- print buffer */
481
482       buffer_filled = TRUE;
483     }
484     else
485     {
486       /* word does not fit at all into empty text buffer -- cut word */
487
488       strncpy(buffer, text_ptr, line_length);
489       buffer[line_length] = '\0';
490       text_ptr += line_length;
491       buffer_filled = TRUE;
492     }
493
494     if (buffer_filled)
495       break;
496   }
497
498   *src_buffer_ptr = text_ptr;
499   *dst_buffer_len = buffer_len;
500
501   return buffer_filled;
502 }
503
504 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
505                      int max_lines)
506 {
507   char *text_ptr = text;
508   char buffer[line_length + 1];
509   int buffer_len;
510   int current_line = 0;
511   int font_height = getFontHeight(font_nr);
512
513   while (*text_ptr && current_line < max_lines)
514   {
515     buffer[0] = '\0';
516     buffer_len = 0;
517
518     RenderLineToBuffer(&text_ptr, buffer, &buffer_len, TRUE, line_length);
519
520     DrawText(x, y + current_line * font_height, buffer, font_nr);
521     current_line++;
522   }
523 }
524
525 int DrawTextFromFile(int x, int y, char *filename, int font_nr,
526                      int line_length, int max_lines, boolean rewrap)
527 {
528   int font_height = getFontHeight(font_nr);
529   char line[MAX_LINE_LEN];
530   char buffer[line_length + 1];
531   int buffer_len;
532   int current_line = 0;
533   FILE *file;
534
535   if (current_line >= max_lines)
536     return 0;
537
538   if (filename == NULL)
539     return 0;
540
541   if (!(file = fopen(filename, MODE_READ)))
542     return 0;
543
544   buffer[0] = '\0';
545   buffer_len = 0;
546
547   while (!feof(file) && current_line < max_lines)
548   {
549     char *line_ptr;
550     boolean last_line_was_empty = TRUE;
551
552     /* read next line of input file */
553     if (!fgets(line, MAX_LINE_LEN, file))
554       break;
555
556     /* skip comments (lines directly beginning with '#') */
557     if (line[0] == '#')
558       continue;
559
560     /* cut trailing newline from input line */
561     for (line_ptr = line; *line_ptr; line_ptr++)
562     {
563       if (*line_ptr == '\n' || *line_ptr == '\r')
564       {
565         *line_ptr = '\0';
566         break;
567       }
568     }
569
570     if (strlen(line) == 0)              /* special case: force empty line */
571       strcpy(line, "\n");
572
573     line_ptr = line;
574
575     while (*line_ptr && current_line < max_lines)
576     {
577 #if 1
578       boolean buffer_filled;
579
580       if (rewrap)
581       {
582         buffer_filled = RenderLineToBuffer(&line_ptr,
583                                            buffer, &buffer_len,
584                                            last_line_was_empty,
585                                            line_length);
586       }
587       else
588       {
589         if (strlen(line_ptr) <= line_length)
590         {
591           buffer_len = strlen(line_ptr);
592           strcpy(buffer, line_ptr);
593         }
594         else
595         {
596           buffer_len = line_length;
597           strncpy(buffer, line_ptr, line_length);
598         }
599
600         buffer[buffer_len] = '\0';
601         line_ptr += buffer_len;
602
603         buffer_filled = TRUE;
604       }
605 #else
606       boolean buffer_filled = RenderLineToBuffer(&line_ptr,
607                                                  buffer, &buffer_len,
608                                                  last_line_was_empty,
609                                                  line_length);
610 #endif
611
612       if (buffer_filled)
613       {
614         DrawText(x, y + current_line * font_height, buffer, font_nr);
615         current_line++;
616
617         last_line_was_empty = (buffer_len == 0);
618
619         buffer[0] = '\0';
620         buffer_len = 0;
621       }
622     }
623   }
624
625   fclose(file);
626
627   if (buffer_len > 0 && current_line < max_lines)
628   {
629     DrawText(x, y + current_line * font_height, buffer, font_nr);
630     current_line++;
631   }
632
633   return current_line;
634 }