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