1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://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);
105 *bitmap = font->bitmap;
106 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
107 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
111 /* ========================================================================= */
112 /* text string helper functions */
113 /* ========================================================================= */
115 int maxWordLengthInString(char *text)
118 int word_len = 0, max_word_len = 0;
120 for (text_ptr = text; *text_ptr; text_ptr++)
122 word_len = (*text_ptr != ' ' ? word_len + 1 : 0);
124 max_word_len = MAX(word_len, max_word_len);
131 /* ========================================================================= */
132 /* simple text drawing functions */
133 /* ========================================================================= */
135 void DrawInitText(char *text, int ypos, int font_nr)
137 LimitScreenUpdates(TRUE);
141 if (window != NULL &&
142 gfx.draw_init_text &&
144 gfx.font_bitmap_info[font_nr].bitmap != NULL)
146 int x = (video.width - getTextWidth(text, font_nr)) / 2;
148 int width = video.width;
149 int height = getFontHeight(font_nr);
151 ClearRectangle(drawto, 0, y, width, height);
152 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
154 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
158 void DrawTextF(int x, int y, int font_nr, char *format, ...)
160 char buffer[MAX_OUTPUT_LINESIZE + 1];
163 va_start(ap, format);
164 vsprintf(buffer, format, ap);
167 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
168 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
170 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
173 void DrawTextFCentered(int y, int font_nr, char *format, ...)
175 char buffer[MAX_OUTPUT_LINESIZE + 1];
178 va_start(ap, format);
179 vsprintf(buffer, format, ap);
182 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
183 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
185 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
186 gfx.sy + y, buffer, font_nr);
189 void DrawTextS(int x, int y, int font_nr, char *text)
191 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
194 void DrawTextSCentered(int y, int font_nr, char *text)
196 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
197 gfx.sy + y, text, font_nr);
200 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
202 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
203 gfx.sx + y, text, font_nr);
206 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
208 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
212 void DrawText(int x, int y, char *text, int font_nr)
214 int mask_mode = BLIT_OPAQUE;
216 if (DrawingOnBackground(x, y))
217 mask_mode = BLIT_ON_BACKGROUND;
219 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
221 if (IN_GFX_FIELD_FULL(x, y))
222 redraw_mask |= REDRAW_FIELD;
223 else if (IN_GFX_DOOR_1(x, y))
224 redraw_mask |= REDRAW_DOOR_1;
225 else if (IN_GFX_DOOR_2(x, y))
226 redraw_mask |= REDRAW_DOOR_2;
227 else if (IN_GFX_DOOR_3(x, y))
228 redraw_mask |= REDRAW_DOOR_3;
230 redraw_mask |= REDRAW_ALL;
233 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
234 int font_nr, int mask_mode)
236 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
237 int font_width = getFontWidth(font_nr);
238 int font_height = getFontHeight(font_nr);
241 char *text_ptr = text;
243 if (font->bitmap == NULL)
246 /* skip text to be printed outside the window (left/right will be clipped) */
247 if (dst_y < 0 || dst_y + font_height > video.height)
250 /* add offset for drawing font characters */
251 dst_x += font->draw_xoffset;
252 dst_y += font->draw_yoffset;
256 char c = *text_ptr++;
259 c = ' '; /* print space instead of newline */
261 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
263 /* clip text at the left side of the window */
271 /* clip text at the right side of the window */
272 if (dst_x + font_width > video.width)
275 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
277 /* first step: draw solid colored rectangle (use "cursor" character) */
278 if (strlen(text) == 1) /* only one char inverted => draw cursor */
280 Bitmap *cursor_bitmap;
281 int cursor_x, cursor_y;
283 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
284 &cursor_x, &cursor_y);
286 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
287 font_width, font_height, dst_x, dst_y);
290 /* second step: draw masked inverted character */
291 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
292 font_width, font_height, dst_x, dst_y);
294 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
296 if (mask_mode == BLIT_ON_BACKGROUND)
298 /* clear font character background */
299 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
300 font_width, font_height);
303 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
304 font_width, font_height, dst_x, dst_y);
306 else /* normal, non-masked font blitting */
308 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
309 font_width, font_height, dst_x, dst_y);
317 /* ========================================================================= */
318 /* text buffer drawing functions */
319 /* ========================================================================= */
321 #define MAX_LINES_FROM_FILE 1024
323 char *GetTextBufferFromFile(char *filename, int max_lines)
329 if (filename == NULL)
332 if (!(file = openFile(filename, MODE_READ)))
335 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
337 while (!checkEndOfFile(file) && num_lines < max_lines)
339 char line[MAX_LINE_LEN];
341 /* read next line of input file */
342 if (!getStringFromFile(file, line, MAX_LINE_LEN))
345 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
347 strcat(buffer, line);
357 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
358 int *dst_buffer_len, int line_length,
359 boolean last_line_was_empty)
361 char *text_ptr = *src_buffer_ptr;
362 char *buffer = dst_buffer;
363 int buffer_len = *dst_buffer_len;
364 boolean buffer_filled = FALSE;
371 /* skip leading whitespaces */
372 while (*text_ptr == ' ' || *text_ptr == '\t')
378 /* look for end of next word */
379 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
389 else if (*text_ptr == '\n') /* special case: force empty line */
394 /* prevent printing of multiple empty lines */
395 if (buffer_len > 0 || !last_line_was_empty)
396 buffer_filled = TRUE;
398 else if (word_len < line_length - buffer_len)
400 /* word fits into text buffer -- add word */
403 buffer[buffer_len++] = ' ';
405 strncpy(&buffer[buffer_len], text_ptr, word_len);
406 buffer_len += word_len;
407 buffer[buffer_len] = '\0';
408 text_ptr += word_len;
410 else if (buffer_len > 0)
412 /* not enough space left for word in text buffer -- print buffer */
414 buffer_filled = TRUE;
418 /* word does not fit at all into empty text buffer -- cut word */
420 strncpy(buffer, text_ptr, line_length);
421 buffer[line_length] = '\0';
422 text_ptr += line_length;
423 buffer_filled = TRUE;
430 *src_buffer_ptr = text_ptr;
431 *dst_buffer_len = buffer_len;
433 return buffer_filled;
436 static boolean getCheckedTokenValueFromString(char *string, char **token,
441 if (!getTokenValueFromString(string, token, value))
444 if (**token != '.') /* token should begin with dot */
447 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
448 if (*ptr == ' ' || *ptr == '\t')
451 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
452 if (*ptr == ' ' || *ptr == '\t')
458 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
459 int line_length, int cut_length,
460 int mask_mode, boolean centered,
463 int buffer_len = strlen(buffer);
464 int font_width = getFontWidth(font_nr);
465 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
467 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
468 int final_cut_length = MAX(0, cut_length - offset_chars);
469 int xx = x + offset_xsize;
470 int yy = y + current_ypos;
472 buffer[final_cut_length] = '\0';
475 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
477 DrawText(xx, yy, buffer, font_nr);
480 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
481 int line_length, int cut_length, int max_lines,
482 int line_spacing, int mask_mode, boolean autowrap,
483 boolean centered, boolean parse_comments)
485 char buffer[line_length + 1];
487 int font_height = getFontHeight(font_nr);
488 int line_height = font_height + line_spacing;
489 int current_line = 0;
490 int current_ypos = 0;
491 int max_ysize = max_lines * line_height;
493 if (text_buffer == NULL || *text_buffer == '\0')
496 if (current_line >= max_lines)
499 if (cut_length == -1)
500 cut_length = line_length;
505 while (*text_buffer && current_ypos < max_ysize)
507 char line[MAX_LINE_LEN + 1];
509 boolean last_line_was_empty = TRUE;
510 int num_line_chars = MAX_LINE_LEN;
513 /* copy next line from text buffer to line buffer (nearly fgets() style) */
514 for (i = 0; i < num_line_chars && *text_buffer; i++)
515 if ((line[i] = *text_buffer++) == '\n')
519 /* prevent 'num_line_chars' sized lines to cause additional empty line */
520 if (i == num_line_chars && *text_buffer == '\n')
523 /* skip comments (lines directly beginning with '#') */
524 if (line[0] == '#' && parse_comments)
528 /* try to read generic token/value pair definition after comment sign */
529 if (getCheckedTokenValueFromString(line + 1, &token, &value))
531 /* if found, flush the current buffer, if non-empty */
532 if (buffer_len > 0 && current_ypos < max_ysize)
534 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
535 mask_mode, centered, current_ypos);
536 current_ypos += line_height;
543 if (strEqual(token, ".font"))
544 font_nr = gfx.get_font_from_token_function(value);
545 else if (strEqual(token, ".autowrap"))
546 autowrap = get_boolean_from_string(value);
547 else if (strEqual(token, ".centered"))
548 centered = get_boolean_from_string(value);
549 else if (strEqual(token, ".parse_comments"))
550 parse_comments = get_boolean_from_string(value);
552 // if font has changed, depending values need to be updated as well
553 font_height = getFontHeight(font_nr);
554 line_height = font_height + line_spacing;
560 /* cut trailing newline and carriage return from input line */
561 for (line_ptr = line; *line_ptr; line_ptr++)
563 if (*line_ptr == '\n' || *line_ptr == '\r')
570 if (strlen(line) == 0) /* special case: force empty line */
575 while (*line_ptr && current_ypos < max_ysize)
577 boolean buffer_filled;
581 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
582 line_length, last_line_was_empty);
586 if (strlen(line_ptr) <= line_length)
588 buffer_len = strlen(line_ptr);
589 strcpy(buffer, line_ptr);
593 buffer_len = line_length;
594 strncpy(buffer, line_ptr, line_length);
597 buffer[buffer_len] = '\0';
598 line_ptr += buffer_len;
600 buffer_filled = TRUE;
605 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
606 mask_mode, centered, current_ypos);
607 current_ypos += line_height;
610 last_line_was_empty = (buffer_len == 0);
618 if (buffer_len > 0 && current_ypos < max_ysize)
620 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
621 mask_mode, centered, current_ypos);
622 current_ypos += line_height;
629 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
630 int line_length, int cut_length, int max_lines,
631 int line_spacing, int mask_mode, boolean autowrap,
632 boolean centered, boolean parse_comments)
634 char text_buffer[MAX_OUTPUT_LINESIZE];
635 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
637 if (text_length >= MAX_OUTPUT_LINESIZE)
638 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
640 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
641 line_length, cut_length, max_lines,
642 line_spacing, mask_mode, autowrap,
643 centered, parse_comments);
644 return num_lines_printed;
647 int DrawTextFile(int x, int y, char *filename, int font_nr,
648 int line_length, int cut_length, int max_lines,
649 int line_spacing, int mask_mode, boolean autowrap,
650 boolean centered, boolean parse_comments)
652 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
653 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
654 line_length, cut_length, max_lines,
655 line_spacing, mask_mode, autowrap,
656 centered, parse_comments);
657 checked_free(text_buffer);
659 return num_lines_printed;