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