1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
21 /* ========================================================================= */
23 /* ========================================================================= */
25 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
26 static GC font_clip_gc = None;
28 static void InitFontClipmasks()
30 XGCValues clip_gc_values;
31 unsigned int clip_gc_valuemask;
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. */
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);
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);
52 /* create only those clipping Pixmaps we really need */
53 for (i = 0; i < gfx.num_fonts; i++)
55 if (gfx.font_bitmap_info[i].bitmap == NULL)
58 gfx.font_bitmap_info[i].clip_mask =
59 checked_calloc(gfx.font_bitmap_info[i].num_chars * sizeof(Pixmap));
61 for (j = 0; j < gfx.font_bitmap_info[i].num_chars; j++)
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;
72 gfx.font_bitmap_info[i].clip_mask[j] =
73 XCreatePixmap(display, window->drawable, width, height, 1);
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);
80 XFreeGC(display, copy_clipmask_gc);
83 static void FreeFontClipmasks()
87 if (gfx.num_fonts == 0 || gfx.font_bitmap_info[0].bitmap == NULL)
90 for (i = 0; i < gfx.num_fonts; i++)
92 if (gfx.font_bitmap_info[i].clip_mask)
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);
99 gfx.font_bitmap_info[i].clip_mask = NULL;
100 gfx.font_bitmap_info[i].num_chars = 0;
104 XFreeGC(display, font_clip_gc);
107 #endif /* TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND */
109 void InitFontInfo(struct FontBitmapInfo *font_bitmap_info, int num_fonts,
110 int (*select_font_function)(int),
111 int (*get_font_from_token_function)(char *))
113 gfx.num_fonts = num_fonts;
114 gfx.font_bitmap_info = font_bitmap_info;
115 gfx.select_font_function = select_font_function;
116 gfx.get_font_from_token_function = get_font_from_token_function;
118 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
123 void FreeFontInfo(struct FontBitmapInfo *font_bitmap_info)
125 if (font_bitmap_info == NULL)
128 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
132 free(font_bitmap_info);
135 struct FontBitmapInfo *getFontBitmapInfo(int font_nr)
137 int font_bitmap_id = gfx.select_font_function(font_nr);
139 return &gfx.font_bitmap_info[font_bitmap_id];
142 int getFontWidth(int font_nr)
144 int font_bitmap_id = gfx.select_font_function(font_nr);
146 return gfx.font_bitmap_info[font_bitmap_id].width;
149 int getFontHeight(int font_nr)
151 int font_bitmap_id = gfx.select_font_function(font_nr);
153 return gfx.font_bitmap_info[font_bitmap_id].height;
156 int getTextWidth(char *text, int font_nr)
158 return (text != NULL ? strlen(text) * getFontWidth(font_nr) : 0);
161 static int getFontCharPosition(int font_nr, char c)
163 int font_bitmap_id = gfx.select_font_function(font_nr);
164 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
165 boolean default_font = (font->num_chars == DEFAULT_NUM_CHARS_PER_FONT);
166 int font_pos = (unsigned char)c - 32;
168 /* map some special characters to their ascii values in default font */
170 font_pos = MAP_FONT_ASCII(c) - 32;
172 /* this allows dynamic special characters together with special font */
173 if (font_pos < 0 || font_pos >= font->num_chars)
179 void getFontCharSource(int font_nr, char c, Bitmap **bitmap, int *x, int *y)
181 int font_bitmap_id = gfx.select_font_function(font_nr);
182 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
183 int font_pos = getFontCharPosition(font_nr, c);
185 *bitmap = font->bitmap;
186 *x = font->src_x + (font_pos % font->num_chars_per_line) * font->width;
187 *y = font->src_y + (font_pos / font->num_chars_per_line) * font->height;
191 /* ========================================================================= */
192 /* text string helper functions */
193 /* ========================================================================= */
195 int maxWordLengthInString(char *text)
198 int word_len = 0, max_word_len = 0;
200 for (text_ptr = text; *text_ptr; text_ptr++)
202 word_len = (*text_ptr != ' ' ? word_len + 1 : 0);
204 max_word_len = MAX(word_len, max_word_len);
211 /* ========================================================================= */
212 /* simple text drawing functions */
213 /* ========================================================================= */
215 void DrawInitTextExt(char *text, int ypos, int font_nr, boolean force)
219 static unsigned int progress_delay = 0;
220 unsigned int progress_delay_value = 100; /* (in milliseconds) */
223 // LimitScreenUpdates(TRUE); // (ignore "force" for now)
224 // LimitScreenUpdates(!force);
225 LimitScreenUpdates(TRUE);
230 if (!force && !DelayReached(&progress_delay, progress_delay_value))
234 if (window != NULL &&
235 gfx.draw_init_text &&
237 gfx.font_bitmap_info[font_nr].bitmap != NULL)
239 int x = (video.width - getTextWidth(text, font_nr)) / 2;
241 int width = video.width;
242 int height = getFontHeight(font_nr);
244 ClearRectangle(drawto, 0, y, width, height);
245 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
247 BlitBitmap(drawto, window, 0, 0, video.width, video.height, 0, 0);
250 static unsigned int progress_delay = 0;
251 unsigned int progress_delay_value = 100; /* (in milliseconds) */
253 // LimitScreenUpdates(TRUE); // (ignore "force" for now)
254 LimitScreenUpdates(!force);
258 if (!force && !DelayReached(&progress_delay, progress_delay_value))
261 if (window != NULL &&
262 gfx.draw_init_text &&
264 gfx.font_bitmap_info[font_nr].bitmap != NULL)
266 int x = (video.width - getTextWidth(text, font_nr)) / 2;
268 int width = video.width;
269 int height = getFontHeight(font_nr);
271 ClearRectangle(drawto, 0, y, width, height);
272 DrawTextExt(drawto, x, y, text, font_nr, BLIT_OPAQUE);
274 /* this makes things significantly faster than directly drawing to window */
275 BlitBitmap(drawto, window, 0, y, width, height, 0, y);
280 void DrawInitText(char *text, int ypos, int font_nr)
282 // DrawInitTextExt(text, ypos, font_nr, TRUE);
283 DrawInitTextExt(text, ypos, font_nr, FALSE);
286 void DrawInitTextAlways(char *text, int ypos, int font_nr)
288 DrawInitTextExt(text, ypos, font_nr, TRUE);
291 void DrawInitTextIfNeeded(char *text, int ypos, int font_nr)
293 DrawInitTextExt(text, ypos, font_nr, FALSE);
296 void DrawTextF(int x, int y, int font_nr, char *format, ...)
298 char buffer[MAX_OUTPUT_LINESIZE + 1];
301 va_start(ap, format);
302 vsprintf(buffer, format, ap);
305 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
306 Error(ERR_EXIT, "string too long in DrawTextF() -- aborting");
308 DrawText(gfx.sx + x, gfx.sy + y, buffer, font_nr);
311 void DrawTextFCentered(int y, int font_nr, char *format, ...)
313 char buffer[MAX_OUTPUT_LINESIZE + 1];
316 va_start(ap, format);
317 vsprintf(buffer, format, ap);
320 if (strlen(buffer) > MAX_OUTPUT_LINESIZE)
321 Error(ERR_EXIT, "string too long in DrawTextFCentered() -- aborting");
323 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(buffer, font_nr)) / 2,
324 gfx.sy + y, buffer, font_nr);
327 void DrawTextS(int x, int y, int font_nr, char *text)
329 DrawText(gfx.sx + x, gfx.sy + y, text, font_nr);
332 void DrawTextSCentered(int y, int font_nr, char *text)
334 DrawText(gfx.sx + (gfx.sxsize - getTextWidth(text, font_nr)) / 2,
335 gfx.sy + y, text, font_nr);
338 void DrawTextCentered(int y, int font_nr, char *text)
340 DrawText((gfx.sxsize - getTextWidth(text, font_nr)) / 2, y, text, font_nr);
343 void DrawTextSAligned(int x, int y, char *text, int font_nr, int align)
345 DrawText(gfx.sx + ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
346 gfx.sx + y, text, font_nr);
349 void DrawTextAligned(int x, int y, char *text, int font_nr, int align)
351 DrawText(ALIGNED_XPOS(x, getTextWidth(text, font_nr), align),
355 void DrawText(int x, int y, char *text, int font_nr)
357 int mask_mode = BLIT_OPAQUE;
359 if (DrawingOnBackground(x, y))
360 mask_mode = BLIT_ON_BACKGROUND;
362 DrawTextExt(drawto, x, y, text, font_nr, mask_mode);
365 if (IN_GFX_FIELD_FULL(x, y))
366 redraw_mask |= REDRAW_FIELD;
367 else if (IN_GFX_DOOR_1(x, y))
368 redraw_mask |= REDRAW_DOOR_1;
369 else if (IN_GFX_DOOR_2(x, y))
370 redraw_mask |= REDRAW_DOOR_2;
371 else if (IN_GFX_DOOR_3(x, y))
372 redraw_mask |= REDRAW_DOOR_3;
374 redraw_mask |= REDRAW_ALL;
377 redraw_mask |= REDRAW_FIELD;
378 else if (y < gfx.vy || gfx.vy == 0)
379 redraw_mask |= REDRAW_DOOR_1;
383 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
384 int font_nr, int mask_mode)
387 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
389 int font_bitmap_id = gfx.select_font_function(font_nr);
390 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
392 int font_width = getFontWidth(font_nr);
393 int font_height = getFontHeight(font_nr);
395 int border_1 = gfx.sx + gfx.sxsize;
396 int border_2 = gfx.dx + gfx.dxsize;
397 int dst_x_start = dst_x;
401 char *text_ptr = text;
403 if (font->bitmap == NULL)
406 /* skip text to be printed outside the window (left/right will be clipped) */
407 if (dst_y < 0 || dst_y + font_height > video.height)
410 /* add offset for drawing font characters */
411 dst_x += font->draw_xoffset;
412 dst_y += font->draw_yoffset;
416 char c = *text_ptr++;
419 c = ' '; /* print space instead of newline */
421 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
423 /* clip text at the left side of the window */
431 /* clip text at the right side of the window */
433 if (dst_x + font_width > video.width)
436 /* (this does not work well when trying to print text to whole screen) */
437 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
438 (dst_x_start < border_2 && dst_x + font_width > border_2))
442 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
444 /* first step: draw solid colored rectangle (use "cursor" character) */
445 if (strlen(text) == 1) /* only one char inverted => draw cursor */
447 Bitmap *cursor_bitmap;
448 int cursor_x, cursor_y;
450 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
451 &cursor_x, &cursor_y);
453 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
454 font_width, font_height, dst_x, dst_y);
457 #if defined(TARGET_SDL)
458 /* second step: draw masked inverted character */
459 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
460 font_width, font_height, dst_x, dst_y);
462 /* second step: draw masked black rectangle (use "space" character) */
463 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
464 dst_x - src_x, dst_y - src_y);
465 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
466 font_width, font_height, dst_x, dst_y);
469 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
471 if (mask_mode == BLIT_ON_BACKGROUND)
473 /* clear font character background */
474 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
475 font_width, font_height);
478 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
479 /* use special font tile clipmasks */
481 int font_pos = getFontCharPosition(font_nr, c);
483 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
484 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
487 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
488 dst_x - src_x, dst_y - src_y);
491 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
492 font_width, font_height, dst_x, dst_y);
494 else /* normal, non-masked font blitting */
496 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
497 font_width, font_height, dst_x, dst_y);
505 /* ========================================================================= */
506 /* text buffer drawing functions */
507 /* ========================================================================= */
509 #define MAX_LINES_FROM_FILE 1024
513 char *GetTextBufferFromFile(char *filename, int max_lines)
519 if (filename == NULL)
522 if (!(file = openFile(filename, MODE_READ)))
525 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
527 while (!checkEndOfFile(file) && num_lines < max_lines)
529 char line[MAX_LINE_LEN];
531 /* read next line of input file */
532 if (!getStringFromFile(file, line, MAX_LINE_LEN))
535 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
537 strcat(buffer, line);
549 char *GetTextBufferFromFile(char *filename, int max_lines)
555 if (filename == NULL)
558 if (!(file = fopen(filename, MODE_READ)))
561 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
563 while (!feof(file) && num_lines < max_lines)
565 char line[MAX_LINE_LEN];
567 /* read next line of input file */
568 if (!fgets(line, MAX_LINE_LEN, file))
571 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
573 strcat(buffer, line);
585 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
586 int area_xsize, int area_ysize, int mask_mode)
589 int font_height = getFontHeight(font_nr);
594 while (*text && area_line < area_ysize)
596 char buffer[MAX_OUTPUT_LINESIZE + 1];
599 for (i = 0; i < line_length && *text; i++)
600 if ((buffer[i] = *text++) == '\n')
602 buffer[MIN(i, area_xsize)] = '\0';
604 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
610 redraw_mask |= REDRAW_FIELD;
613 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
614 int *dst_buffer_len, int line_length,
615 boolean last_line_was_empty)
617 char *text_ptr = *src_buffer_ptr;
618 char *buffer = dst_buffer;
619 int buffer_len = *dst_buffer_len;
620 boolean buffer_filled = FALSE;
627 /* skip leading whitespaces */
628 while (*text_ptr == ' ' || *text_ptr == '\t')
634 /* look for end of next word */
635 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
645 else if (*text_ptr == '\n') /* special case: force empty line */
650 /* prevent printing of multiple empty lines */
651 if (buffer_len > 0 || !last_line_was_empty)
652 buffer_filled = TRUE;
654 else if (word_len < line_length - buffer_len)
656 /* word fits into text buffer -- add word */
659 buffer[buffer_len++] = ' ';
661 strncpy(&buffer[buffer_len], text_ptr, word_len);
662 buffer_len += word_len;
663 buffer[buffer_len] = '\0';
664 text_ptr += word_len;
666 else if (buffer_len > 0)
668 /* not enough space left for word in text buffer -- print buffer */
670 buffer_filled = TRUE;
674 /* word does not fit at all into empty text buffer -- cut word */
676 strncpy(buffer, text_ptr, line_length);
677 buffer[line_length] = '\0';
678 text_ptr += line_length;
679 buffer_filled = TRUE;
686 *src_buffer_ptr = text_ptr;
687 *dst_buffer_len = buffer_len;
689 return buffer_filled;
693 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
696 char *text_ptr = text;
697 int current_line = 0;
698 int font_height = getFontHeight(font_nr);
700 while (*text_ptr && current_line < max_lines)
702 char buffer[line_length + 1];
707 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
709 DrawText(x, y + current_line * font_height, buffer, font_nr);
716 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
717 int line_length, int max_lines, boolean wrap_text)
719 int font_height = getFontHeight(font_nr);
720 char line[MAX_LINE_LEN];
721 char buffer[line_length + 1];
723 int current_line = 0;
726 if (current_line >= max_lines)
729 if (filename == NULL)
732 if (!(file = fopen(filename, MODE_READ)))
738 while (!feof(file) && current_line < max_lines)
741 boolean last_line_was_empty = TRUE;
743 /* read next line of input file */
744 if (!fgets(line, MAX_LINE_LEN, file))
747 /* skip comments (lines directly beginning with '#') */
751 /* cut trailing newline from input line */
752 for (line_ptr = line; *line_ptr; line_ptr++)
754 if (*line_ptr == '\n' || *line_ptr == '\r')
761 if (strlen(line) == 0) /* special case: force empty line */
766 while (*line_ptr && current_line < max_lines)
769 boolean buffer_filled;
773 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
774 line_length, last_line_was_empty);
778 if (strlen(line_ptr) <= line_length)
780 buffer_len = strlen(line_ptr);
781 strcpy(buffer, line_ptr);
785 buffer_len = line_length;
786 strncpy(buffer, line_ptr, line_length);
789 buffer[buffer_len] = '\0';
790 line_ptr += buffer_len;
792 buffer_filled = TRUE;
795 boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
796 line_length, last_line_was_empty);
801 DrawText(x, y + current_line * font_height, buffer, font_nr);
804 last_line_was_empty = (buffer_len == 0);
814 if (buffer_len > 0 && current_line < max_lines)
816 DrawText(x, y + current_line * font_height, buffer, font_nr);
824 static boolean getCheckedTokenValueFromString(char *string, char **token,
829 if (!getTokenValueFromString(string, token, value))
832 if (**token != '.') /* token should begin with dot */
835 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
836 if (*ptr == ' ' || *ptr == '\t')
839 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
840 if (*ptr == ' ' || *ptr == '\t')
846 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
847 int line_length, int cut_length,
848 int line_spacing, int mask_mode,
849 boolean centered, int current_line)
851 int buffer_len = strlen(buffer);
852 int font_width = getFontWidth(font_nr);
853 int font_height = getFontHeight(font_nr);
854 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
856 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
857 int final_cut_length = MAX(0, cut_length - offset_chars);
858 int xx = x + offset_xsize;
859 int yy = y + current_line * (font_height + line_spacing);
861 buffer[final_cut_length] = '\0';
864 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
866 DrawText(xx, yy, buffer, font_nr);
869 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
870 int line_length, int cut_length, int max_lines,
871 int line_spacing, int mask_mode, boolean autowrap,
872 boolean centered, boolean parse_comments)
875 int font_width = getFontWidth(font_nr);
876 int font_height = getFontHeight(font_nr);
878 char buffer[line_length + 1];
880 int current_line = 0;
882 if (text_buffer == NULL || *text_buffer == '\0')
885 if (current_line >= max_lines)
888 if (cut_length == -1)
889 cut_length = line_length;
894 while (*text_buffer && current_line < max_lines)
896 char line[MAX_LINE_LEN + 1];
898 boolean last_line_was_empty = TRUE;
900 int num_line_chars = MAX_LINE_LEN;
902 int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
906 /* copy next line from text buffer to line buffer (nearly fgets() style) */
907 for (i = 0; i < num_line_chars && *text_buffer; i++)
908 if ((line[i] = *text_buffer++) == '\n')
912 /* prevent 'num_line_chars' sized lines to cause additional empty line */
913 if (i == num_line_chars && *text_buffer == '\n')
916 /* skip comments (lines directly beginning with '#') */
917 if (line[0] == '#' && parse_comments)
921 /* try to read generic token/value pair definition after comment sign */
922 if (getCheckedTokenValueFromString(line + 1, &token, &value))
924 /* if found, flush the current buffer, if non-empty */
925 if (buffer_len > 0 && current_line < max_lines)
927 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
928 line_spacing, mask_mode, centered, current_line);
936 if (strEqual(token, ".font"))
937 font_nr = gfx.get_font_from_token_function(value);
938 else if (strEqual(token, ".autowrap"))
939 autowrap = get_boolean_from_string(value);
940 else if (strEqual(token, ".centered"))
941 centered = get_boolean_from_string(value);
942 else if (strEqual(token, ".parse_comments"))
943 parse_comments = get_boolean_from_string(value);
949 /* cut trailing newline and carriage return from input line */
950 for (line_ptr = line; *line_ptr; line_ptr++)
952 if (*line_ptr == '\n' || *line_ptr == '\r')
959 if (strlen(line) == 0) /* special case: force empty line */
964 while (*line_ptr && current_line < max_lines)
966 boolean buffer_filled;
970 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
971 line_length, last_line_was_empty);
975 if (strlen(line_ptr) <= line_length)
977 buffer_len = strlen(line_ptr);
978 strcpy(buffer, line_ptr);
982 buffer_len = line_length;
983 strncpy(buffer, line_ptr, line_length);
986 buffer[buffer_len] = '\0';
987 line_ptr += buffer_len;
989 buffer_filled = TRUE;
995 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
996 line_spacing, mask_mode, centered, current_line);
998 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
1000 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
1001 int final_cut_length = MAX(0, cut_length - offset_chars);
1002 int xx = x + offset_xsize;
1004 buffer[final_cut_length] = '\0';
1006 if (mask_mode != -1)
1007 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
1008 font_nr, mask_mode);
1010 DrawText(xx, y + current_line * font_height, buffer, font_nr);
1015 last_line_was_empty = (buffer_len == 0);
1023 if (buffer_len > 0 && current_line < max_lines)
1026 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
1027 line_spacing, mask_mode, centered, current_line);
1029 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
1031 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
1032 int final_cut_length = MAX(0, cut_length - offset_chars);
1033 int xx = x + offset_xsize;
1035 buffer[final_cut_length] = '\0';
1037 if (mask_mode != -1)
1038 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
1039 font_nr, mask_mode);
1041 DrawText(xx, y + current_line * font_height, buffer, font_nr);
1047 return current_line;
1050 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
1051 int line_length, int cut_length, int max_lines,
1052 int line_spacing, int mask_mode, boolean autowrap,
1053 boolean centered, boolean parse_comments)
1055 char text_buffer[MAX_OUTPUT_LINESIZE];
1056 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
1058 if (text_length >= MAX_OUTPUT_LINESIZE)
1059 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
1061 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
1062 line_length, cut_length, max_lines,
1063 line_spacing, mask_mode, autowrap,
1064 centered, parse_comments);
1065 return num_lines_printed;
1068 int DrawTextFile(int x, int y, char *filename, int font_nr,
1069 int line_length, int cut_length, int max_lines,
1070 int line_spacing, int mask_mode, boolean autowrap,
1071 boolean centered, boolean parse_comments)
1073 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
1074 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
1075 line_length, cut_length, max_lines,
1076 line_spacing, mask_mode, autowrap,
1077 centered, parse_comments);
1078 checked_free(text_buffer);
1080 return num_lines_printed;
1084 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
1087 DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
1091 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
1092 int cut_length, int max_lines, int mask_mode)
1094 DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
1095 mask_mode, FALSE, FALSE, FALSE);