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 *),
26 char * (*get_token_from_font_function)(int))
28 gfx.num_fonts = num_fonts;
29 gfx.font_bitmap_info = font_bitmap_info;
30 gfx.select_font_function = select_font_function;
31 gfx.get_font_from_token_function = get_font_from_token_function;
32 gfx.get_token_from_font_function = get_token_from_font_function;
35 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
37 if (font_bitmap_info == NULL)
40 free(font_bitmap_info);
43 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
45 int font_bitmap_id = gfx.select_font_function(font_nr);
47 return &gfx.font_bitmap_info[font_bitmap_id];
50 int getFontWidth(int font_nr)
52 int font_bitmap_id = gfx.select_font_function(font_nr);
54 return gfx.font_bitmap_info[font_bitmap_id].width;
57 int getFontHeight(int font_nr)
59 int font_bitmap_id = gfx.select_font_function(font_nr);
61 return gfx.font_bitmap_info[font_bitmap_id].height;
64 int getFontDrawOffsetX(int font_nr)
66 int font_bitmap_id = gfx.select_font_function(font_nr);
68 return gfx.font_bitmap_info[font_bitmap_id].draw_xoffset;
71 int getFontDrawOffsetY(int font_nr)
73 int font_bitmap_id = gfx.select_font_function(font_nr);
75 return gfx.font_bitmap_info[font_bitmap_id].draw_yoffset;
78 int getTextWidth(char *text, int font_nr)
80 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
83 static int getFontCharPosition(int font_nr, char c)
85 int font_bitmap_id = gfx.select_font_function(font_nr);
86 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
87 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
88 int font_pos = (unsigned char)c - 32;
90 // map some special characters to their ascii values in default font
92 font_pos = MAP_FONT_ASCII(c) - 32;
94 // this allows dynamic special characters together with special font
95 if (font_pos < 0 || font_pos >= font->num_chars)
101 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
103 int font_bitmap_id = gfx.select_font_function(font_nr);
104 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
105 int font_pos = getFontCharPosition(font_nr, c);
106 int offset_x = (font->offset_x != 0 ? font->offset_x : font->width);
107 int offset_y = (font->offset_y != 0 ? font->offset_y : font->height);
109 *bitmap = font->bitmap;
110 *x = font->src_x + (font_pos % font->num_chars_per_line) * offset_x;
111 *y = font->src_y + (font_pos / font->num_chars_per_line) * offset_y;
115 // ============================================================================
116 // text string helper functions
117 // ============================================================================
119 int maxWordLengthInRequestString(char *text)
122 int word_len = 0, max_word_len = 0;
124 for (text_ptr = text; *text_ptr; text_ptr++)
126 word_len = (*text_ptr != ' ' &&
128 *text_ptr != '!' ? word_len + 1 : 0);
130 max_word_len = MAX(word_len, max_word_len);
137 // ============================================================================
138 // simple text drawing functions
139 // ============================================================================
141 static void DrawInitTextExt(char *text, int ypos, int font_nr, boolean update)
143 LimitScreenUpdates(TRUE);
147 if (window != NULL &&
148 gfx.draw_init_text &&
150 gfx.font_bitmap_info[font_nr].bitmap != NULL)
152 int x = (video.width - getTextWidth(text, font_nr)) / 2;
154 int width = video.width;
155 int height = getFontHeight(font_nr);
157 ClearRectangleOnBackground(drawto, 0, y, width, height);
158 DrawTextExt(drawto, x, y, text, font_nr, BLIT_MASKED);
161 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
165 void DrawInitText(char *text, int ypos, int font_nr)
167 DrawInitTextExt(text, ypos, font_nr, FALSE);
170 void DrawInitTextHead(char *text)
172 DrawInitTextExt(text, 120, FC_GREEN, FALSE);
175 void DrawInitTextItem(char *text)
177 DrawInitTextExt(text, 150, FC_YELLOW, TRUE);
180 void DrawTextF(int x, int y, int font_nr, char *format, ...)
182 char buffer[MAX_OUTPUT_LINESIZE + 1];
185 va_start(ap, format);
186 vsprintf(buffer, format, ap);
189 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
190 Fail("string too long in DrawTextF() -- aborting");
192 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
195 void DrawTextFCentered(int y, int font_nr, char *format, ...)
197 char buffer[MAX_OUTPUT_LINESIZE + 1];
200 va_start(ap, format);
201 vsprintf(buffer, format, ap);
204 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
205 Fail("string too long in DrawTextFCentered() -- aborting");
207 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
208 gfx.sy + y, buffer, font_nr);
211 void DrawTextS(int x, int y, int font_nr, char *text)
213 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
216 void DrawTextSCentered(int y, int font_nr, char *text)
218 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
219 gfx.sy + y, text, font_nr);
222 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
224 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
225 gfx.sy + y, text, font_nr);
228 void DrawText(int x, int y, char *text, int font_nr)
230 int mask_mode = BLIT_OPAQUE;
232 if (DrawingOnBackground(x, y))
233 mask_mode = BLIT_ON_BACKGROUND;
235 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
237 if (IN_GFX_FIELD_FULL(x, y))
238 redraw_mask |= REDRAW_FIELD;
239 else if (IN_GFX_DOOR_1(x, y))
240 redraw_mask |= REDRAW_DOOR_1;
241 else if (IN_GFX_DOOR_2(x, y))
242 redraw_mask |= REDRAW_DOOR_2;
243 else if (IN_GFX_DOOR_3(x, y))
244 redraw_mask |= REDRAW_DOOR_3;
246 redraw_mask |= REDRAW_ALL;
249 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
250 int font_nr, int mask_mode)
252 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
253 int font_width = getFontWidth(font_nr);
254 int font_height = getFontHeight(font_nr);
257 char *text_ptr = text;
260 Debug("font:token", "'%s' / '%s'",
261 gfx.get_token_from_font_function(font_nr), text);
264 if (font->bitmap == NULL)
267 // skip text to be printed outside the window (left/right will be clipped)
268 if (dst_y < 0 || dst_y + font_height > video.height)
271 // add offset for drawing font characters
272 dst_x += font->draw_xoffset;
273 dst_y += font->draw_yoffset;
277 char c = *text_ptr++;
280 c = ' '; // print space instead of newline
282 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
284 // clip text at the left side of the window
292 // clip text at the right side of the window
293 if (dst_x + font_width > video.width)
296 if (mask_mode == BLIT_INVERSE) // special mode for text gadgets
298 // first step: draw solid colored rectangle (use "cursor" character)
299 if (strlen(text) == 1) // only one char inverted => draw cursor
301 Bitmap *cursor_bitmap;
302 int cursor_x, cursor_y;
304 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
305 &cursor_x, &cursor_y);
307 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
308 font_width, font_height, dst_x, dst_y);
311 // second step: draw masked inverted character
312 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
313 font_width, font_height, dst_x, dst_y);
315 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
317 if (mask_mode == BLIT_ON_BACKGROUND)
319 // clear font character background
320 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
321 font_width, font_height);
324 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
325 font_width, font_height, dst_x, dst_y);
327 else // normal, non-masked font blitting
329 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
330 font_width, font_height, dst_x, dst_y);
338 // ============================================================================
339 // text buffer drawing functions
340 // ============================================================================
342 #define MAX_LINES_FROM_FILE 1024
344 char *GetTextBufferFromFile(char *filename, int max_lines)
350 if (filename == NULL)
353 if (!(file = openFile(filename, MODE_READ)))
356 buffer = checked_calloc(1); // start with valid, but empty text buffer
358 while (!checkEndOfFile(file) && num_lines < max_lines)
360 char line[MAX_LINE_LEN];
364 // read next line of input file
365 if (!getStringFromFile(file, line, MAX_LINE_LEN))
368 line_len = strlen(line);
370 // cut trailing line break (this can be newline and/or carriage return)
371 for (line_ptr = &line[line_len]; line_ptr >= line; line_ptr--)
372 if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
375 // re-add newline (so the result is terminated by newline, but not CR/LF)
376 if (strlen(line) != line_len)
379 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
381 strcat(buffer, line);
388 if (getTextEncoding(buffer) == TEXT_ENCODING_UTF_8)
390 char *body_latin1 = getLatin1FromUTF8(buffer);
392 checked_free(buffer);
394 buffer = body_latin1;
400 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
401 int *dst_buffer_len, int line_length,
402 boolean last_line_was_empty)
404 char *text_ptr = *src_buffer_ptr;
405 char *buffer = dst_buffer;
406 int buffer_len = *dst_buffer_len;
407 boolean buffer_filled = FALSE;
414 if (strEqual(text_ptr, " ")) // special case: force line break
415 buffer_filled = TRUE;
417 // skip leading whitespaces
418 while (*text_ptr == ' ' || *text_ptr == '\t')
424 // look for end of next word
425 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
435 else if (*text_ptr == '\n') // special case: force empty line
440 // prevent printing of multiple empty lines
441 if (buffer_len > 0 || !last_line_was_empty)
442 buffer_filled = TRUE;
444 else if (word_len < line_length - buffer_len)
446 // word fits into text buffer -- add word
449 buffer[buffer_len++] = ' ';
451 strncpy(&buffer[buffer_len], text_ptr, word_len);
452 buffer_len += word_len;
453 buffer[buffer_len] = '\0';
454 text_ptr += word_len;
456 else if (buffer_len > 0)
458 // not enough space left for word in text buffer -- print buffer
460 buffer_filled = TRUE;
464 // word does not fit at all into empty text buffer -- cut word
466 strncpy(buffer, text_ptr, line_length);
467 buffer[line_length] = '\0';
468 text_ptr += line_length;
469 buffer_filled = TRUE;
476 *src_buffer_ptr = text_ptr;
477 *dst_buffer_len = buffer_len;
479 return buffer_filled;
482 static boolean getCheckedTokenValueFromString(char *string, char **token,
487 if (!getTokenValueFromString(string, token, value))
490 if (**token != '.') // token should begin with dot
493 for (ptr = *token; *ptr; ptr++) // token should contain no whitespace
494 if (*ptr == ' ' || *ptr == '\t')
497 for (ptr = *value; *ptr; ptr++) // value should contain no whitespace
498 if (*ptr == ' ' || *ptr == '\t')
504 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
505 int line_length, int cut_length,
506 int mask_mode, boolean centered,
509 int buffer_len = strlen(buffer);
510 int font_width = getFontWidth(font_nr);
511 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
513 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
514 int final_cut_length = MAX(0, cut_length - offset_chars);
515 int xx = x + offset_xsize;
516 int yy = y + current_ypos;
518 buffer[final_cut_length] = '\0';
521 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
523 DrawText(xx, yy, buffer, font_nr);
526 static int DrawTextBufferExt(int x, int y, char *text_buffer, int font_nr,
527 int line_length, int cut_length, int max_lines,
528 int line_spacing, int mask_mode, boolean autowrap,
529 boolean centered, boolean parse_comments,
530 boolean is_text_area)
532 char buffer[line_length + 1];
534 int font_height = getFontHeight(font_nr);
535 int line_height = font_height + line_spacing;
536 int current_line = 0;
537 int current_ypos = 0;
538 int max_ysize = max_lines * line_height;
540 if (text_buffer == NULL || *text_buffer == '\0')
543 if (current_line >= max_lines)
546 if (cut_length == -1)
547 cut_length = line_length;
552 while (*text_buffer && current_ypos < max_ysize)
554 char line[MAX_LINE_LEN + 1];
556 boolean last_line_was_empty = TRUE;
557 int num_line_chars = MAX_LINE_LEN;
560 // copy next line from text buffer to line buffer (nearly fgets() style)
561 for (i = 0; i < num_line_chars && *text_buffer; i++)
563 if ((line[i] = *text_buffer++) == '\n')
565 // in text areas, 'line_length' sized lines cause additional empty line
566 if (i == line_length && is_text_area)
574 // prevent 'num_line_chars' sized lines to cause additional empty line
575 if (i == num_line_chars && *text_buffer == '\n')
578 // skip comments (lines directly beginning with '#')
579 if (line[0] == '#' && parse_comments)
583 // try to read generic token/value pair definition after comment sign
584 if (getCheckedTokenValueFromString(line + 1, &token, &value))
586 // if found, flush the current buffer, if non-empty
587 if (buffer_len > 0 && current_ypos < max_ysize)
589 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
590 mask_mode, centered, current_ypos);
591 current_ypos += line_height;
598 if (strEqual(token, ".font"))
599 font_nr = gfx.get_font_from_token_function(value);
600 else if (strEqual(token, ".autowrap"))
601 autowrap = get_boolean_from_string(value);
602 else if (strEqual(token, ".centered"))
603 centered = get_boolean_from_string(value);
604 else if (strEqual(token, ".parse_comments"))
605 parse_comments = get_boolean_from_string(value);
607 // if font has changed, depending values need to be updated as well
608 font_height = getFontHeight(font_nr);
609 line_height = font_height + line_spacing;
615 // cut trailing newline and carriage return from input line
616 for (line_ptr = line; *line_ptr; line_ptr++)
618 if (*line_ptr == '\n' || *line_ptr == '\r')
625 if (strlen(line) == 0) // special case: force empty line
630 while (*line_ptr && current_ypos < max_ysize)
632 boolean buffer_filled;
636 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
637 line_length, last_line_was_empty);
641 if (strlen(line_ptr) <= line_length)
643 buffer_len = strlen(line_ptr);
644 strcpy(buffer, line_ptr);
648 buffer_len = line_length;
649 strncpy(buffer, line_ptr, line_length);
652 buffer[buffer_len] = '\0';
653 line_ptr += buffer_len;
655 buffer_filled = TRUE;
660 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
661 mask_mode, centered, current_ypos);
662 current_ypos += line_height;
665 last_line_was_empty = (buffer_len == 0);
673 if (buffer_len > 0 && current_ypos < max_ysize)
675 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
676 mask_mode, centered, current_ypos);
677 current_ypos += line_height;
684 int DrawTextArea(int x, int y, char *text_buffer, int font_nr,
685 int line_length, int cut_length, int max_lines,
686 int line_spacing, int mask_mode, boolean autowrap,
687 boolean centered, boolean parse_comments)
689 return DrawTextBufferExt(x, y, text_buffer, font_nr,
690 line_length, cut_length, max_lines,
691 line_spacing, mask_mode, autowrap,
692 centered, parse_comments, TRUE);
695 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
696 int line_length, int cut_length, int max_lines,
697 int line_spacing, int mask_mode, boolean autowrap,
698 boolean centered, boolean parse_comments)
700 return DrawTextBufferExt(x, y, text_buffer, font_nr,
701 line_length, cut_length, max_lines,
702 line_spacing, mask_mode, autowrap,
703 centered, parse_comments, FALSE);
706 int DrawTextBufferS(int x, int y, char *text_buffer, int font_nr,
707 int line_length, int cut_length, int max_lines,
708 int line_spacing, int mask_mode, boolean autowrap,
709 boolean centered, boolean parse_comments)
711 return DrawTextBuffer(gfx.sx + x, gfx.sy + y, text_buffer, font_nr,
712 line_length, cut_length, max_lines,
713 line_spacing, mask_mode, autowrap,
714 centered, parse_comments);
717 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
718 int line_length, int cut_length, int max_lines,
719 int line_spacing, int mask_mode, boolean autowrap,
720 boolean centered, boolean parse_comments)
722 char text_buffer[MAX_OUTPUT_LINESIZE];
723 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
725 if (text_length >= MAX_OUTPUT_LINESIZE)
726 Warn("string too long in DrawTextBufferVA() -- truncated");
728 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
729 line_length, cut_length, max_lines,
730 line_spacing, mask_mode, autowrap,
731 centered, parse_comments);
732 return num_lines_printed;
735 int DrawTextFile(int x, int y, char *filename, int font_nr,
736 int line_length, int cut_length, int max_lines,
737 int line_spacing, int mask_mode, boolean autowrap,
738 boolean centered, boolean parse_comments)
740 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
741 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
742 line_length, cut_length, max_lines,
743 line_spacing, mask_mode, autowrap,
744 centered, parse_comments);
745 checked_free(text_buffer);
747 return num_lines_printed;