1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
19 // ============================================================================
20 // static font variables
21 // ============================================================================
23 boolean text_drawing_enabled = TRUE;
26 // ============================================================================
28 // ============================================================================
30 void EnableDrawingText(void)
32 text_drawing_enabled = TRUE;
35 void DisableDrawingText(void)
37 text_drawing_enabled = FALSE;
40 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
41 int (*select_font_function)(int),
42 int (*get_font_from_token_function)(char *),
43 char * (*get_token_from_font_function)(int))
45 gfx.num_fonts = num_fonts;
46 gfx.font_bitmap_info = font_bitmap_info;
47 gfx.select_font_function = select_font_function;
48 gfx.get_font_from_token_function = get_font_from_token_function;
49 gfx.get_token_from_font_function = get_token_from_font_function;
52 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
54 if (font_bitmap_info == NULL)
57 free(font_bitmap_info);
60 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
62 int font_bitmap_id = gfx.select_font_function(font_nr);
64 return &gfx.font_bitmap_info[font_bitmap_id];
67 int getFontWidth(int font_nr)
69 int font_bitmap_id = gfx.select_font_function(font_nr);
71 return gfx.font_bitmap_info[font_bitmap_id].width;
74 int getFontHeight(int font_nr)
76 int font_bitmap_id = gfx.select_font_function(font_nr);
78 return gfx.font_bitmap_info[font_bitmap_id].height;
81 int getFontDrawOffsetX(int font_nr)
83 int font_bitmap_id = gfx.select_font_function(font_nr);
85 return gfx.font_bitmap_info[font_bitmap_id].draw_xoffset;
88 int getFontDrawOffsetY(int font_nr)
90 int font_bitmap_id = gfx.select_font_function(font_nr);
92 return gfx.font_bitmap_info[font_bitmap_id].draw_yoffset;
95 int getTextWidth(char *text, int font_nr)
97 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
100 static int getFontCharPosition(int font_nr, char c)
102 int font_bitmap_id = gfx.select_font_function(font_nr);
103 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
104 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
105 int font_pos = (unsigned char)c - 32;
107 // map some special characters to their ascii values in default font
109 font_pos = MAP_FONT_ASCII(c) - 32;
111 // this allows dynamic special characters together with special font
112 if (font_pos < 0 || font_pos >= font->num_chars)
118 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
120 int font_bitmap_id = gfx.select_font_function(font_nr);
121 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
122 int font_pos = getFontCharPosition(font_nr, c);
123 int offset_x = (font->offset_x != 0 ? font->offset_x : font->width);
124 int offset_y = (font->offset_y != 0 ? font->offset_y : font->height);
126 *bitmap = font->bitmap;
127 *x = font->src_x + (font_pos % font->num_chars_per_line) * offset_x;
128 *y = font->src_y + (font_pos / font->num_chars_per_line) * offset_y;
132 // ============================================================================
133 // text string helper functions
134 // ============================================================================
136 int maxWordLengthInRequestString(char *text)
139 int word_len = 0, max_word_len = 0;
141 for (text_ptr = text; *text_ptr; text_ptr++)
143 word_len = (*text_ptr != ' ' &&
145 *text_ptr != '!' ? word_len + 1 : 0);
147 max_word_len = MAX(word_len, max_word_len);
154 // ============================================================================
155 // simple text drawing functions
156 // ============================================================================
158 static void DrawInitTextExt(char *text, int ypos, int font_nr, boolean update)
160 LimitScreenUpdates(TRUE);
164 if (!text_drawing_enabled)
167 if (window != NULL &&
168 gfx.draw_init_text &&
170 gfx.font_bitmap_info[font_nr].bitmap != NULL)
172 int x = (video.width - getTextWidth(text, font_nr)) / 2;
174 int width = video.width;
175 int height = getFontHeight(font_nr);
177 ClearRectangleOnBackground(drawto, 0, y, width, height);
178 DrawTextExt(drawto, x, y, text, font_nr, BLIT_MASKED);
181 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
185 void DrawInitText(char *text, int ypos, int font_nr)
187 DrawInitTextExt(text, ypos, font_nr, FALSE);
190 void DrawInitTextHead(char *text)
192 DrawInitTextExt(text, 120, FC_GREEN, FALSE);
195 void DrawInitTextItem(char *text)
197 DrawInitTextExt(text, 150, FC_YELLOW, TRUE);
200 void DrawTextF(int x, int y, int font_nr, char *format, ...)
202 char buffer[MAX_OUTPUT_LINESIZE + 1];
205 va_start(ap, format);
206 vsprintf(buffer, format, ap);
209 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
210 Fail("string too long in DrawTextF() -- aborting");
212 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
215 void DrawTextFCentered(int y, int font_nr, char *format, ...)
217 char buffer[MAX_OUTPUT_LINESIZE + 1];
220 va_start(ap, format);
221 vsprintf(buffer, format, ap);
224 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
225 Fail("string too long in DrawTextFCentered() -- aborting");
227 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
228 gfx.sy + y, buffer, font_nr);
231 void DrawTextS(int x, int y, int font_nr, char *text)
233 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
236 void DrawTextSCentered(int y, int font_nr, char *text)
238 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
239 gfx.sy + y, text, font_nr);
242 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
244 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
245 gfx.sy + y, text, font_nr);
248 void DrawText(int x, int y, char *text, int font_nr)
250 int mask_mode = BLIT_OPAQUE;
252 if (DrawingOnBackground(x, y))
253 mask_mode = BLIT_ON_BACKGROUND;
255 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
257 if (IN_GFX_FIELD_FULL(x, y))
258 redraw_mask |= REDRAW_FIELD;
259 else if (IN_GFX_DOOR_1(x, y))
260 redraw_mask |= REDRAW_DOOR_1;
261 else if (IN_GFX_DOOR_2(x, y))
262 redraw_mask |= REDRAW_DOOR_2;
263 else if (IN_GFX_DOOR_3(x, y))
264 redraw_mask |= REDRAW_DOOR_3;
266 redraw_mask |= REDRAW_ALL;
269 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
270 int font_nr, int mask_mode)
272 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
273 int font_width = getFontWidth(font_nr);
274 int font_height = getFontHeight(font_nr);
277 char *text_ptr = text;
279 if (!text_drawing_enabled)
283 Debug("font:token", "'%s' / '%s'",
284 gfx.get_token_from_font_function(font_nr), text);
287 if (font->bitmap == NULL)
290 // skip text to be printed outside the window (left/right will be clipped)
291 if (dst_y < 0 || dst_y + font_height > video.height)
294 // add offset for drawing font characters
295 dst_x += font->draw_xoffset;
296 dst_y += font->draw_yoffset;
300 char c = *text_ptr++;
303 c = ' '; // print space instead of newline
305 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
307 // clip text at the left side of the window
315 // clip text at the right side of the window
316 if (dst_x + font_width > video.width)
319 if (mask_mode == BLIT_INVERSE) // special mode for text gadgets
321 // first step: draw solid colored rectangle (use "cursor" character)
322 if (strlen(text) == 1) // only one char inverted => draw cursor
324 Bitmap *cursor_bitmap;
325 int cursor_x, cursor_y;
327 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
328 &cursor_x, &cursor_y);
330 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
331 font_width, font_height, dst_x, dst_y);
334 // second step: draw masked inverted character
335 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
336 font_width, font_height, dst_x, dst_y);
338 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
340 if (mask_mode == BLIT_ON_BACKGROUND)
342 // clear font character background
343 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
344 font_width, font_height);
347 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
348 font_width, font_height, dst_x, dst_y);
350 else // normal, non-masked font blitting
352 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
353 font_width, font_height, dst_x, dst_y);
361 // ============================================================================
362 // text buffer drawing functions
363 // ============================================================================
365 #define MAX_LINES_FROM_FILE 1024
367 char *GetTextBufferFromFile(char *filename, int max_lines)
373 if (filename == NULL)
376 if (!(file = openFile(filename, MODE_READ)))
379 buffer = checked_calloc(1); // start with valid, but empty text buffer
381 while (!checkEndOfFile(file) && num_lines < max_lines)
383 char line[MAX_LINE_LEN];
387 // read next line of input file
388 if (!getStringFromFile(file, line, MAX_LINE_LEN))
391 line_len = strlen(line);
393 // cut trailing line break (this can be newline and/or carriage return)
394 for (line_ptr = &line[line_len]; line_ptr >= line; line_ptr--)
395 if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
398 // re-add newline (so the result is terminated by newline, but not CR/LF)
399 if (strlen(line) != line_len)
402 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
404 strcat(buffer, line);
411 if (getTextEncoding(buffer) == TEXT_ENCODING_UTF_8)
413 char *body_latin1 = getLatin1FromUTF8(buffer);
415 checked_free(buffer);
417 buffer = body_latin1;
423 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
424 int *dst_buffer_len, int line_length,
425 boolean last_line_was_empty)
427 char *text_ptr = *src_buffer_ptr;
428 char *buffer = dst_buffer;
429 int buffer_len = *dst_buffer_len;
430 boolean buffer_filled = FALSE;
437 if (strEqual(text_ptr, " ")) // special case: force line break
438 buffer_filled = TRUE;
440 // skip leading whitespaces
441 while (*text_ptr == ' ' || *text_ptr == '\t')
447 // look for end of next word
448 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
458 else if (*text_ptr == '\n') // special case: force empty line
463 // prevent printing of multiple empty lines
464 if (buffer_len > 0 || !last_line_was_empty)
465 buffer_filled = TRUE;
467 else if (word_len < line_length - buffer_len)
469 // word fits into text buffer -- add word
472 buffer[buffer_len++] = ' ';
474 strncpy(&buffer[buffer_len], text_ptr, word_len);
475 buffer_len += word_len;
476 buffer[buffer_len] = '\0';
477 text_ptr += word_len;
479 else if (buffer_len > 0)
481 // not enough space left for word in text buffer -- print buffer
483 buffer_filled = TRUE;
487 // word does not fit at all into empty text buffer -- cut word
489 strncpy(buffer, text_ptr, line_length);
490 buffer[line_length] = '\0';
491 text_ptr += line_length;
492 buffer_filled = TRUE;
499 *src_buffer_ptr = text_ptr;
500 *dst_buffer_len = buffer_len;
502 return buffer_filled;
505 static boolean getCheckedTokenValueFromString(char *string, char **token,
510 if (!getTokenValueFromString(string, token, value))
513 if (**token != '.') // token should begin with dot
516 for (ptr = *token; *ptr; ptr++) // token should contain no whitespace
517 if (*ptr == ' ' || *ptr == '\t')
520 for (ptr = *value; *ptr; ptr++) // value should contain no whitespace
521 if (*ptr == ' ' || *ptr == '\t')
527 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
528 int line_length, int cut_length,
529 int mask_mode, boolean centered,
532 int buffer_len = strlen(buffer);
533 int font_width = getFontWidth(font_nr);
534 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
536 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
537 int final_cut_length = MAX(0, cut_length - offset_chars);
538 int xx = x + offset_xsize;
539 int yy = y + current_ypos;
541 buffer[final_cut_length] = '\0';
544 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
546 DrawText(xx, yy, buffer, font_nr);
549 static int DrawTextBufferExt(int x, int y, char *text_buffer, int font_nr,
550 int line_length, int cut_length, int max_lines,
551 int line_spacing, int mask_mode, boolean autowrap,
552 boolean centered, boolean parse_comments,
553 boolean is_text_area)
555 char buffer[line_length + 1];
557 int font_height = getFontHeight(font_nr);
558 int line_height = font_height + line_spacing;
559 int current_line = 0;
560 int current_ypos = 0;
561 int max_ysize = max_lines * line_height;
563 if (text_buffer == NULL || *text_buffer == '\0')
566 if (current_line >= max_lines)
569 if (cut_length == -1)
570 cut_length = line_length;
575 while (*text_buffer && current_ypos < max_ysize)
577 char line[MAX_LINE_LEN + 1];
579 boolean last_line_was_empty = TRUE;
580 int num_line_chars = MAX_LINE_LEN;
583 // copy next line from text buffer to line buffer (nearly fgets() style)
584 for (i = 0; i < num_line_chars && *text_buffer; i++)
586 if ((line[i] = *text_buffer++) == '\n')
588 // in text areas, 'line_length' sized lines cause additional empty line
589 if (i == line_length && is_text_area)
597 // prevent 'num_line_chars' sized lines to cause additional empty line
598 if (i == num_line_chars && *text_buffer == '\n')
601 // skip comments (lines directly beginning with '#')
602 if (line[0] == '#' && parse_comments)
606 // try to read generic token/value pair definition after comment sign
607 if (getCheckedTokenValueFromString(line + 1, &token, &value))
609 // if found, flush the current buffer, if non-empty
610 if (buffer_len > 0 && current_ypos < max_ysize)
612 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
613 mask_mode, centered, current_ypos);
614 current_ypos += line_height;
621 if (strEqual(token, ".font"))
622 font_nr = gfx.get_font_from_token_function(value);
623 else if (strEqual(token, ".autowrap"))
624 autowrap = get_boolean_from_string(value);
625 else if (strEqual(token, ".centered"))
626 centered = get_boolean_from_string(value);
627 else if (strEqual(token, ".parse_comments"))
628 parse_comments = get_boolean_from_string(value);
630 // if font has changed, depending values need to be updated as well
631 font_height = getFontHeight(font_nr);
632 line_height = font_height + line_spacing;
638 // cut trailing newline and carriage return from input line
639 for (line_ptr = line; *line_ptr; line_ptr++)
641 if (*line_ptr == '\n' || *line_ptr == '\r')
648 if (strlen(line) == 0) // special case: force empty line
653 while (*line_ptr && current_ypos < max_ysize)
655 boolean buffer_filled;
659 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
660 line_length, last_line_was_empty);
664 if (strlen(line_ptr) <= line_length)
666 buffer_len = strlen(line_ptr);
667 strcpy(buffer, line_ptr);
671 buffer_len = line_length;
672 strncpy(buffer, line_ptr, line_length);
675 buffer[buffer_len] = '\0';
676 line_ptr += buffer_len;
678 buffer_filled = TRUE;
683 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
684 mask_mode, centered, current_ypos);
685 current_ypos += line_height;
688 last_line_was_empty = (buffer_len == 0);
696 if (buffer_len > 0 && current_ypos < max_ysize)
698 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
699 mask_mode, centered, current_ypos);
700 current_ypos += line_height;
707 int DrawTextArea(int x, int y, char *text_buffer, int font_nr,
708 int line_length, int cut_length, int max_lines,
709 int line_spacing, int mask_mode, boolean autowrap,
710 boolean centered, boolean parse_comments)
712 return DrawTextBufferExt(x, y, text_buffer, font_nr,
713 line_length, cut_length, max_lines,
714 line_spacing, mask_mode, autowrap,
715 centered, parse_comments, TRUE);
718 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
719 int line_length, int cut_length, int max_lines,
720 int line_spacing, int mask_mode, boolean autowrap,
721 boolean centered, boolean parse_comments)
723 return DrawTextBufferExt(x, y, text_buffer, font_nr,
724 line_length, cut_length, max_lines,
725 line_spacing, mask_mode, autowrap,
726 centered, parse_comments, FALSE);
729 int DrawTextBufferS(int x, int y, char *text_buffer, int font_nr,
730 int line_length, int cut_length, int max_lines,
731 int line_spacing, int mask_mode, boolean autowrap,
732 boolean centered, boolean parse_comments)
734 return DrawTextBuffer(gfx.sx + x, gfx.sy + y, text_buffer, font_nr,
735 line_length, cut_length, max_lines,
736 line_spacing, mask_mode, autowrap,
737 centered, parse_comments);
740 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
741 int line_length, int cut_length, int max_lines,
742 int line_spacing, int mask_mode, boolean autowrap,
743 boolean centered, boolean parse_comments)
745 char text_buffer[MAX_OUTPUT_LINESIZE];
746 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
748 if (text_length >= MAX_OUTPUT_LINESIZE)
749 Warn("string too long in DrawTextBufferVA() -- truncated");
751 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
752 line_length, cut_length, max_lines,
753 line_spacing, mask_mode, autowrap,
754 centered, parse_comments);
755 return num_lines_printed;
758 int DrawTextFile(int x, int y, char *filename, int font_nr,
759 int line_length, int cut_length, int max_lines,
760 int line_spacing, int mask_mode, boolean autowrap,
761 boolean centered, boolean parse_comments)
763 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
764 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
765 line_length, cut_length, max_lines,
766 line_spacing, mask_mode, autowrap,
767 centered, parse_comments);
768 checked_free(text_buffer);
770 return num_lines_printed;