1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
19 // ============================================================================
21 // ============================================================================
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 *))
27 gfx.num_fonts = num_fonts;
28 gfx.font_bitmap_info = font_bitmap_info;
29 gfx.select_font_function = select_font_function;
30 gfx.get_font_from_token_function = get_font_from_token_function;
33 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
35 if (font_bitmap_info == NULL)
38 free(font_bitmap_info);
41 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
43 int font_bitmap_id = gfx.select_font_function(font_nr);
45 return &gfx.font_bitmap_info[font_bitmap_id];
48 int getFontWidth(int font_nr)
50 int font_bitmap_id = gfx.select_font_function(font_nr);
52 return gfx.font_bitmap_info[font_bitmap_id].width;
55 int getFontHeight(int font_nr)
57 int font_bitmap_id = gfx.select_font_function(font_nr);
59 return gfx.font_bitmap_info[font_bitmap_id].height;
62 int getFontDrawOffsetX(int font_nr)
64 int font_bitmap_id = gfx.select_font_function(font_nr);
66 return gfx.font_bitmap_info[font_bitmap_id].draw_xoffset;
69 int getFontDrawOffsetY(int font_nr)
71 int font_bitmap_id = gfx.select_font_function(font_nr);
73 return gfx.font_bitmap_info[font_bitmap_id].draw_yoffset;
76 int getTextWidth(char *text, int font_nr)
78 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
81 static int getFontCharPosition(int font_nr, char c)
83 int font_bitmap_id = gfx.select_font_function(font_nr);
84 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
85 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
86 int font_pos = (unsigned char)c - 32;
88 // map some special characters to their ascii values in default font
90 font_pos = MAP_FONT_ASCII(c) - 32;
92 // this allows dynamic special characters together with special font
93 if (font_pos < 0 || font_pos >= font->num_chars)
99 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
101 int font_bitmap_id = gfx.select_font_function(font_nr);
102 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
103 int font_pos = getFontCharPosition(font_nr, c);
104 int offset_x = (font->offset_x != 0 ? font->offset_x : font->width);
105 int offset_y = (font->offset_y != 0 ? font->offset_y : font->height);
107 *bitmap = font->bitmap;
108 *x = font->src_x + (font_pos % font->num_chars_per_line) * offset_x;
109 *y = font->src_y + (font_pos / font->num_chars_per_line) * offset_y;
113 // ============================================================================
114 // text string helper functions
115 // ============================================================================
117 int maxWordLengthInRequestString(char *text)
120 int word_len = 0, max_word_len = 0;
122 for (text_ptr = text; *text_ptr; text_ptr++)
124 word_len = (*text_ptr != ' ' &&
126 *text_ptr != '!' ? word_len + 1 : 0);
128 max_word_len = MAX(word_len, max_word_len);
135 // ============================================================================
136 // simple text drawing functions
137 // ============================================================================
139 static void DrawInitTextExt(char *text, int ypos, int font_nr, boolean update)
141 LimitScreenUpdates(TRUE);
145 if (window != NULL &&
146 gfx.draw_init_text &&
148 gfx.font_bitmap_info[font_nr].bitmap != NULL)
150 int x = (video.width - getTextWidth(text, font_nr)) / 2;
152 int width = video.width;
153 int height = getFontHeight(font_nr);
155 ClearRectangleOnBackground(drawto, 0, y, width, height);
156 DrawTextExt(drawto, x, y, text, font_nr, BLIT_MASKED);
159 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
163 void DrawInitText(char *text, int ypos, int font_nr)
165 DrawInitTextExt(text, ypos, font_nr, FALSE);
168 void DrawInitTextHead(char *text)
170 DrawInitTextExt(text, 120, FC_GREEN, FALSE);
173 void DrawInitTextItem(char *text)
175 DrawInitTextExt(text, 150, FC_YELLOW, TRUE);
178 void DrawTextF(int x, int y, int font_nr, char *format, ...)
180 char buffer[MAX_OUTPUT_LINESIZE + 1];
183 va_start(ap, format);
184 vsprintf(buffer, format, ap);
187 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
188 Fail("string too long in DrawTextF() -- aborting");
190 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
193 void DrawTextFCentered(int y, int font_nr, char *format, ...)
195 char buffer[MAX_OUTPUT_LINESIZE + 1];
198 va_start(ap, format);
199 vsprintf(buffer, format, ap);
202 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
203 Fail("string too long in DrawTextFCentered() -- aborting");
205 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
206 gfx.sy + y, buffer, font_nr);
209 void DrawTextS(int x, int y, int font_nr, char *text)
211 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
214 void DrawTextSCentered(int y, int font_nr, char *text)
216 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
217 gfx.sy + y, text, font_nr);
220 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
222 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
223 gfx.sy + y, text, font_nr);
226 void DrawText(int x, int y, char *text, int font_nr)
228 int mask_mode = BLIT_OPAQUE;
230 if (DrawingOnBackground(x, y))
231 mask_mode = BLIT_ON_BACKGROUND;
233 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
235 if (IN_GFX_FIELD_FULL(x, y))
236 redraw_mask |= REDRAW_FIELD;
237 else if (IN_GFX_DOOR_1(x, y))
238 redraw_mask |= REDRAW_DOOR_1;
239 else if (IN_GFX_DOOR_2(x, y))
240 redraw_mask |= REDRAW_DOOR_2;
241 else if (IN_GFX_DOOR_3(x, y))
242 redraw_mask |= REDRAW_DOOR_3;
244 redraw_mask |= REDRAW_ALL;
247 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
248 int font_nr, int mask_mode)
250 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
251 int font_width = getFontWidth(font_nr);
252 int font_height = getFontHeight(font_nr);
255 char *text_ptr = text;
257 if (font->bitmap == NULL)
260 // skip text to be printed outside the window (left/right will be clipped)
261 if (dst_y < 0 || dst_y + font_height > video.height)
264 // add offset for drawing font characters
265 dst_x += font->draw_xoffset;
266 dst_y += font->draw_yoffset;
270 char c = *text_ptr++;
273 c = ' '; // print space instead of newline
275 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
277 // clip text at the left side of the window
285 // clip text at the right side of the window
286 if (dst_x + font_width > video.width)
289 if (mask_mode == BLIT_INVERSE) // special mode for text gadgets
291 // first step: draw solid colored rectangle (use "cursor" character)
292 if (strlen(text) == 1) // only one char inverted => draw cursor
294 Bitmap *cursor_bitmap;
295 int cursor_x, cursor_y;
297 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
298 &cursor_x, &cursor_y);
300 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
301 font_width, font_height, dst_x, dst_y);
304 // second step: draw masked inverted character
305 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
306 font_width, font_height, dst_x, dst_y);
308 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
310 if (mask_mode == BLIT_ON_BACKGROUND)
312 // clear font character background
313 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
314 font_width, font_height);
317 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
318 font_width, font_height, dst_x, dst_y);
320 else // normal, non-masked font blitting
322 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
323 font_width, font_height, dst_x, dst_y);
331 // ============================================================================
332 // text buffer drawing functions
333 // ============================================================================
335 #define MAX_LINES_FROM_FILE 1024
337 char *GetTextBufferFromFile(char *filename, int max_lines)
343 if (filename == NULL)
346 if (!(file = openFile(filename, MODE_READ)))
349 buffer = checked_calloc(1); // start with valid, but empty text buffer
351 while (!checkEndOfFile(file) && num_lines < max_lines)
353 char line[MAX_LINE_LEN];
355 // read next line of input file
356 if (!getStringFromFile(file, line, MAX_LINE_LEN))
359 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
361 strcat(buffer, line);
368 if (getTextEncoding(buffer) == TEXT_ENCODING_UTF_8)
370 char *body_latin1 = getLatin1FromUTF8(buffer);
372 checked_free(buffer);
374 buffer = body_latin1;
380 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
381 int *dst_buffer_len, int line_length,
382 boolean last_line_was_empty)
384 char *text_ptr = *src_buffer_ptr;
385 char *buffer = dst_buffer;
386 int buffer_len = *dst_buffer_len;
387 boolean buffer_filled = FALSE;
394 // skip leading whitespaces
395 while (*text_ptr == ' ' || *text_ptr == '\t')
401 // look for end of next word
402 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
412 else if (*text_ptr == '\n') // special case: force empty line
417 // prevent printing of multiple empty lines
418 if (buffer_len > 0 || !last_line_was_empty)
419 buffer_filled = TRUE;
421 else if (word_len < line_length - buffer_len)
423 // word fits into text buffer -- add word
426 buffer[buffer_len++] = ' ';
428 strncpy(&buffer[buffer_len], text_ptr, word_len);
429 buffer_len += word_len;
430 buffer[buffer_len] = '\0';
431 text_ptr += word_len;
433 else if (buffer_len > 0)
435 // not enough space left for word in text buffer -- print buffer
437 buffer_filled = TRUE;
441 // word does not fit at all into empty text buffer -- cut word
443 strncpy(buffer, text_ptr, line_length);
444 buffer[line_length] = '\0';
445 text_ptr += line_length;
446 buffer_filled = TRUE;
453 *src_buffer_ptr = text_ptr;
454 *dst_buffer_len = buffer_len;
456 return buffer_filled;
459 static boolean getCheckedTokenValueFromString(char *string, char **token,
464 if (!getTokenValueFromString(string, token, value))
467 if (**token != '.') // token should begin with dot
470 for (ptr = *token; *ptr; ptr++) // token should contain no whitespace
471 if (*ptr == ' ' || *ptr == '\t')
474 for (ptr = *value; *ptr; ptr++) // value should contain no whitespace
475 if (*ptr == ' ' || *ptr == '\t')
481 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
482 int line_length, int cut_length,
483 int mask_mode, boolean centered,
486 int buffer_len = strlen(buffer);
487 int font_width = getFontWidth(font_nr);
488 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
490 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
491 int final_cut_length = MAX(0, cut_length - offset_chars);
492 int xx = x + offset_xsize;
493 int yy = y + current_ypos;
495 buffer[final_cut_length] = '\0';
498 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
500 DrawText(xx, yy, buffer, font_nr);
503 static int DrawTextBufferExt(int x, int y, char *text_buffer, int font_nr,
504 int line_length, int cut_length, int max_lines,
505 int line_spacing, int mask_mode, boolean autowrap,
506 boolean centered, boolean parse_comments,
507 boolean is_text_area)
509 char buffer[line_length + 1];
511 int font_height = getFontHeight(font_nr);
512 int line_height = font_height + line_spacing;
513 int current_line = 0;
514 int current_ypos = 0;
515 int max_ysize = max_lines * line_height;
517 if (text_buffer == NULL || *text_buffer == '\0')
520 if (current_line >= max_lines)
523 if (cut_length == -1)
524 cut_length = line_length;
529 while (*text_buffer && current_ypos < max_ysize)
531 char line[MAX_LINE_LEN + 1];
533 boolean last_line_was_empty = TRUE;
534 int num_line_chars = MAX_LINE_LEN;
537 // copy next line from text buffer to line buffer (nearly fgets() style)
538 for (i = 0; i < num_line_chars && *text_buffer; i++)
540 if ((line[i] = *text_buffer++) == '\n')
542 // in text areas, 'line_length' sized lines cause additional empty line
543 if (i == line_length && is_text_area)
551 // prevent 'num_line_chars' sized lines to cause additional empty line
552 if (i == num_line_chars && *text_buffer == '\n')
555 // skip comments (lines directly beginning with '#')
556 if (line[0] == '#' && parse_comments)
560 // try to read generic token/value pair definition after comment sign
561 if (getCheckedTokenValueFromString(line + 1, &token, &value))
563 // if found, flush the current buffer, if non-empty
564 if (buffer_len > 0 && current_ypos < max_ysize)
566 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
567 mask_mode, centered, current_ypos);
568 current_ypos += line_height;
575 if (strEqual(token, ".font"))
576 font_nr = gfx.get_font_from_token_function(value);
577 else if (strEqual(token, ".autowrap"))
578 autowrap = get_boolean_from_string(value);
579 else if (strEqual(token, ".centered"))
580 centered = get_boolean_from_string(value);
581 else if (strEqual(token, ".parse_comments"))
582 parse_comments = get_boolean_from_string(value);
584 // if font has changed, depending values need to be updated as well
585 font_height = getFontHeight(font_nr);
586 line_height = font_height + line_spacing;
592 // cut trailing newline and carriage return from input line
593 for (line_ptr = line; *line_ptr; line_ptr++)
595 if (*line_ptr == '\n' || *line_ptr == '\r')
602 if (strlen(line) == 0) // special case: force empty line
607 while (*line_ptr && current_ypos < max_ysize)
609 boolean buffer_filled;
613 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
614 line_length, last_line_was_empty);
618 if (strlen(line_ptr) <= line_length)
620 buffer_len = strlen(line_ptr);
621 strcpy(buffer, line_ptr);
625 buffer_len = line_length;
626 strncpy(buffer, line_ptr, line_length);
629 buffer[buffer_len] = '\0';
630 line_ptr += buffer_len;
632 buffer_filled = TRUE;
637 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
638 mask_mode, centered, current_ypos);
639 current_ypos += line_height;
642 last_line_was_empty = (buffer_len == 0);
650 if (buffer_len > 0 && current_ypos < max_ysize)
652 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
653 mask_mode, centered, current_ypos);
654 current_ypos += line_height;
661 int DrawTextArea(int x, int y, char *text_buffer, int font_nr,
662 int line_length, int cut_length, int max_lines,
663 int line_spacing, int mask_mode, boolean autowrap,
664 boolean centered, boolean parse_comments)
666 return DrawTextBufferExt(x, y, text_buffer, font_nr,
667 line_length, cut_length, max_lines,
668 line_spacing, mask_mode, autowrap,
669 centered, parse_comments, TRUE);
672 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
673 int line_length, int cut_length, int max_lines,
674 int line_spacing, int mask_mode, boolean autowrap,
675 boolean centered, boolean parse_comments)
677 return DrawTextBufferExt(x, y, text_buffer, font_nr,
678 line_length, cut_length, max_lines,
679 line_spacing, mask_mode, autowrap,
680 centered, parse_comments, FALSE);
683 int DrawTextBufferS(int x, int y, char *text_buffer, int font_nr,
684 int line_length, int cut_length, int max_lines,
685 int line_spacing, int mask_mode, boolean autowrap,
686 boolean centered, boolean parse_comments)
688 return DrawTextBuffer(gfx.sx + x, gfx.sy + y, text_buffer, font_nr,
689 line_length, cut_length, max_lines,
690 line_spacing, mask_mode, autowrap,
691 centered, parse_comments);
694 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
695 int line_length, int cut_length, int max_lines,
696 int line_spacing, int mask_mode, boolean autowrap,
697 boolean centered, boolean parse_comments)
699 char text_buffer[MAX_OUTPUT_LINESIZE];
700 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
702 if (text_length >= MAX_OUTPUT_LINESIZE)
703 Warn("string too long in DrawTextBufferVA() -- truncated");
705 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
706 line_length, cut_length, max_lines,
707 line_spacing, mask_mode, autowrap,
708 centered, parse_comments);
709 return num_lines_printed;
712 int DrawTextFile(int x, int y, char *filename, int font_nr,
713 int line_length, int cut_length, int max_lines,
714 int line_spacing, int mask_mode, boolean autowrap,
715 boolean centered, boolean parse_comments)
717 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
718 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
719 line_length, cut_length, max_lines,
720 line_spacing, mask_mode, autowrap,
721 centered, parse_comments);
722 checked_free(text_buffer);
724 return num_lines_printed;