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 redraw_mask |= REDRAW_FIELD;
366 else if (y < gfx.vy || gfx.vy == 0)
367 redraw_mask |= REDRAW_DOOR_1;
370 void DrawTextExt(DrawBuffer *dst_bitmap, int dst_x, int dst_y, char *text,
371 int font_nr, int mask_mode)
374 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
376 int font_bitmap_id = gfx.select_font_function(font_nr);
377 struct FontBitmapInfo *font = &gfx.font_bitmap_info[font_bitmap_id];
379 int font_width = getFontWidth(font_nr);
380 int font_height = getFontHeight(font_nr);
382 int border_1 = gfx.sx + gfx.sxsize;
383 int border_2 = gfx.dx + gfx.dxsize;
384 int dst_x_start = dst_x;
388 char *text_ptr = text;
390 if (font->bitmap == NULL)
393 /* skip text to be printed outside the window (left/right will be clipped) */
394 if (dst_y < 0 || dst_y + font_height > video.height)
397 /* add offset for drawing font characters */
398 dst_x += font->draw_xoffset;
399 dst_y += font->draw_yoffset;
403 char c = *text_ptr++;
406 c = ' '; /* print space instead of newline */
408 getFontCharSource(font_nr, c, &src_bitmap, &src_x, &src_y);
410 /* clip text at the left side of the window */
418 /* clip text at the right side of the window */
420 if (dst_x + font_width > video.width)
423 /* (this does not work well when trying to print text to whole screen) */
424 if ((dst_x_start < border_1 && dst_x + font_width > border_1) ||
425 (dst_x_start < border_2 && dst_x + font_width > border_2))
429 if (mask_mode == BLIT_INVERSE) /* special mode for text gadgets */
431 /* first step: draw solid colored rectangle (use "cursor" character) */
432 if (strlen(text) == 1) /* only one char inverted => draw cursor */
434 Bitmap *cursor_bitmap;
435 int cursor_x, cursor_y;
437 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &cursor_bitmap,
438 &cursor_x, &cursor_y);
440 BlitBitmap(cursor_bitmap, dst_bitmap, cursor_x, cursor_y,
441 font_width, font_height, dst_x, dst_y);
444 #if defined(TARGET_SDL)
445 /* second step: draw masked inverted character */
446 SDLCopyInverseMasked(src_bitmap, dst_bitmap, src_x, src_y,
447 font_width, font_height, dst_x, dst_y);
449 /* second step: draw masked black rectangle (use "space" character) */
450 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
451 dst_x - src_x, dst_y - src_y);
452 BlitBitmapMasked(src_bitmap, dst_bitmap, 0, 0,
453 font_width, font_height, dst_x, dst_y);
456 else if (mask_mode == BLIT_MASKED || mask_mode == BLIT_ON_BACKGROUND)
458 if (mask_mode == BLIT_ON_BACKGROUND)
460 /* clear font character background */
461 ClearRectangleOnBackground(dst_bitmap, dst_x, dst_y,
462 font_width, font_height);
465 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
466 /* use special font tile clipmasks */
468 int font_pos = getFontCharPosition(font_nr, c);
470 SetClipMask(src_bitmap, font_clip_gc, font->clip_mask[font_pos]);
471 SetClipOrigin(src_bitmap, font_clip_gc, dst_x, dst_y);
474 SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
475 dst_x - src_x, dst_y - src_y);
478 BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y,
479 font_width, font_height, dst_x, dst_y);
481 else /* normal, non-masked font blitting */
483 BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y,
484 font_width, font_height, dst_x, dst_y);
492 /* ========================================================================= */
493 /* text buffer drawing functions */
494 /* ========================================================================= */
496 #define MAX_LINES_FROM_FILE 1024
500 char *GetTextBufferFromFile(char *filename, int max_lines)
506 if (filename == NULL)
509 if (!(file = openFile(filename, MODE_READ)))
512 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
514 while (!checkEndOfFile(file) && num_lines < max_lines)
516 char line[MAX_LINE_LEN];
518 /* read next line of input file */
519 if (!getStringFromFile(file, line, MAX_LINE_LEN))
522 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
524 strcat(buffer, line);
536 char *GetTextBufferFromFile(char *filename, int max_lines)
542 if (filename == NULL)
545 if (!(file = fopen(filename, MODE_READ)))
548 buffer = checked_calloc(1); /* start with valid, but empty text buffer */
550 while (!feof(file) && num_lines < max_lines)
552 char line[MAX_LINE_LEN];
554 /* read next line of input file */
555 if (!fgets(line, MAX_LINE_LEN, file))
558 buffer = checked_realloc(buffer, strlen(buffer) + strlen(line) + 1);
560 strcat(buffer, line);
572 void DrawTextToTextArea_OLD(int x, int y, char *text, int font_nr, int line_length,
573 int area_xsize, int area_ysize, int mask_mode)
576 int font_height = getFontHeight(font_nr);
581 while (*text && area_line < area_ysize)
583 char buffer[MAX_OUTPUT_LINESIZE + 1];
586 for (i = 0; i < line_length && *text; i++)
587 if ((buffer[i] = *text++) == '\n')
589 buffer[MIN(i, area_xsize)] = '\0';
591 DrawTextExt(drawto, x, y + area_line * font_height, buffer, font_nr,
597 redraw_mask |= REDRAW_FIELD;
600 static boolean RenderLineToBuffer(char **src_buffer_ptr, char *dst_buffer,
601 int *dst_buffer_len, int line_length,
602 boolean last_line_was_empty)
604 char *text_ptr = *src_buffer_ptr;
605 char *buffer = dst_buffer;
606 int buffer_len = *dst_buffer_len;
607 boolean buffer_filled = FALSE;
614 /* skip leading whitespaces */
615 while (*text_ptr == ' ' || *text_ptr == '\t')
621 /* look for end of next word */
622 while (*word_ptr != ' ' && *word_ptr != '\t' && *word_ptr != '\0')
632 else if (*text_ptr == '\n') /* special case: force empty line */
637 /* prevent printing of multiple empty lines */
638 if (buffer_len > 0 || !last_line_was_empty)
639 buffer_filled = TRUE;
641 else if (word_len < line_length - buffer_len)
643 /* word fits into text buffer -- add word */
646 buffer[buffer_len++] = ' ';
648 strncpy(&buffer[buffer_len], text_ptr, word_len);
649 buffer_len += word_len;
650 buffer[buffer_len] = '\0';
651 text_ptr += word_len;
653 else if (buffer_len > 0)
655 /* not enough space left for word in text buffer -- print buffer */
657 buffer_filled = TRUE;
661 /* word does not fit at all into empty text buffer -- cut word */
663 strncpy(buffer, text_ptr, line_length);
664 buffer[line_length] = '\0';
665 text_ptr += line_length;
666 buffer_filled = TRUE;
673 *src_buffer_ptr = text_ptr;
674 *dst_buffer_len = buffer_len;
676 return buffer_filled;
680 void DrawTextWrapped_OLD(int x, int y, char *text, int font_nr, int line_length,
683 char *text_ptr = text;
684 int current_line = 0;
685 int font_height = getFontHeight(font_nr);
687 while (*text_ptr && current_line < max_lines)
689 char buffer[line_length + 1];
694 RenderLineToBuffer(&text_ptr, buffer, &buffer_len, line_length, TRUE);
696 DrawText(x, y + current_line * font_height, buffer, font_nr);
703 int DrawTextFromFile_OLD(int x, int y, char *filename, int font_nr,
704 int line_length, int max_lines, boolean wrap_text)
706 int font_height = getFontHeight(font_nr);
707 char line[MAX_LINE_LEN];
708 char buffer[line_length + 1];
710 int current_line = 0;
713 if (current_line >= max_lines)
716 if (filename == NULL)
719 if (!(file = fopen(filename, MODE_READ)))
725 while (!feof(file) && current_line < max_lines)
728 boolean last_line_was_empty = TRUE;
730 /* read next line of input file */
731 if (!fgets(line, MAX_LINE_LEN, file))
734 /* skip comments (lines directly beginning with '#') */
738 /* cut trailing newline from input line */
739 for (line_ptr = line; *line_ptr; line_ptr++)
741 if (*line_ptr == '\n' || *line_ptr == '\r')
748 if (strlen(line) == 0) /* special case: force empty line */
753 while (*line_ptr && current_line < max_lines)
756 boolean buffer_filled;
760 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
761 line_length, last_line_was_empty);
765 if (strlen(line_ptr) <= line_length)
767 buffer_len = strlen(line_ptr);
768 strcpy(buffer, line_ptr);
772 buffer_len = line_length;
773 strncpy(buffer, line_ptr, line_length);
776 buffer[buffer_len] = '\0';
777 line_ptr += buffer_len;
779 buffer_filled = TRUE;
782 boolean buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
783 line_length, last_line_was_empty);
788 DrawText(x, y + current_line * font_height, buffer, font_nr);
791 last_line_was_empty = (buffer_len == 0);
801 if (buffer_len > 0 && current_line < max_lines)
803 DrawText(x, y + current_line * font_height, buffer, font_nr);
811 static boolean getCheckedTokenValueFromString(char *string, char **token,
816 if (!getTokenValueFromString(string, token, value))
819 if (**token != '.') /* token should begin with dot */
822 for (ptr = *token; *ptr; ptr++) /* token should contain no whitespace */
823 if (*ptr == ' ' || *ptr == '\t')
826 for (ptr = *value; *ptr; ptr++) /* value should contain no whitespace */
827 if (*ptr == ' ' || *ptr == '\t')
833 static void DrawTextBuffer_Flush(int x, int y, char *buffer, int font_nr,
834 int line_length, int cut_length,
835 int line_spacing, int mask_mode,
836 boolean centered, int current_line)
838 int buffer_len = strlen(buffer);
839 int font_width = getFontWidth(font_nr);
840 int font_height = getFontHeight(font_nr);
841 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
843 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
844 int final_cut_length = MAX(0, cut_length - offset_chars);
845 int xx = x + offset_xsize;
846 int yy = y + current_line * (font_height + line_spacing);
848 buffer[final_cut_length] = '\0';
851 DrawTextExt(drawto, xx, yy, buffer, font_nr, mask_mode);
853 DrawText(xx, yy, buffer, font_nr);
856 int DrawTextBuffer(int x, int y, char *text_buffer, int font_nr,
857 int line_length, int cut_length, int max_lines,
858 int line_spacing, int mask_mode, boolean autowrap,
859 boolean centered, boolean parse_comments)
862 int font_width = getFontWidth(font_nr);
863 int font_height = getFontHeight(font_nr);
865 char buffer[line_length + 1];
867 int current_line = 0;
869 if (text_buffer == NULL || *text_buffer == '\0')
872 if (current_line >= max_lines)
875 if (cut_length == -1)
876 cut_length = line_length;
881 while (*text_buffer && current_line < max_lines)
883 char line[MAX_LINE_LEN + 1];
885 boolean last_line_was_empty = TRUE;
887 int num_line_chars = MAX_LINE_LEN;
889 int num_line_chars = (autowrap ? MAX_LINE_LEN : line_length);
893 /* copy next line from text buffer to line buffer (nearly fgets() style) */
894 for (i = 0; i < num_line_chars && *text_buffer; i++)
895 if ((line[i] = *text_buffer++) == '\n')
899 /* prevent 'num_line_chars' sized lines to cause additional empty line */
900 if (i == num_line_chars && *text_buffer == '\n')
903 /* skip comments (lines directly beginning with '#') */
904 if (line[0] == '#' && parse_comments)
908 /* try to read generic token/value pair definition after comment sign */
909 if (getCheckedTokenValueFromString(line + 1, &token, &value))
911 /* if found, flush the current buffer, if non-empty */
912 if (buffer_len > 0 && current_line < max_lines)
914 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
915 line_spacing, mask_mode, centered, current_line);
923 if (strEqual(token, ".font"))
924 font_nr = gfx.get_font_from_token_function(value);
925 else if (strEqual(token, ".autowrap"))
926 autowrap = get_boolean_from_string(value);
927 else if (strEqual(token, ".centered"))
928 centered = get_boolean_from_string(value);
929 else if (strEqual(token, ".parse_comments"))
930 parse_comments = get_boolean_from_string(value);
936 /* cut trailing newline and carriage return from input line */
937 for (line_ptr = line; *line_ptr; line_ptr++)
939 if (*line_ptr == '\n' || *line_ptr == '\r')
946 if (strlen(line) == 0) /* special case: force empty line */
951 while (*line_ptr && current_line < max_lines)
953 boolean buffer_filled;
957 buffer_filled = RenderLineToBuffer(&line_ptr, buffer, &buffer_len,
958 line_length, last_line_was_empty);
962 if (strlen(line_ptr) <= line_length)
964 buffer_len = strlen(line_ptr);
965 strcpy(buffer, line_ptr);
969 buffer_len = line_length;
970 strncpy(buffer, line_ptr, line_length);
973 buffer[buffer_len] = '\0';
974 line_ptr += buffer_len;
976 buffer_filled = TRUE;
982 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
983 line_spacing, mask_mode, centered, current_line);
985 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
987 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
988 int final_cut_length = MAX(0, cut_length - offset_chars);
989 int xx = x + offset_xsize;
991 buffer[final_cut_length] = '\0';
994 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
997 DrawText(xx, y + current_line * font_height, buffer, font_nr);
1002 last_line_was_empty = (buffer_len == 0);
1010 if (buffer_len > 0 && current_line < max_lines)
1013 DrawTextBuffer_Flush(x, y, buffer, font_nr, line_length, cut_length,
1014 line_spacing, mask_mode, centered, current_line);
1016 int offset_chars = (centered ? (line_length - buffer_len) / 2 : 0);
1018 (centered ? font_width * (line_length - buffer_len) / 2 : 0);
1019 int final_cut_length = MAX(0, cut_length - offset_chars);
1020 int xx = x + offset_xsize;
1022 buffer[final_cut_length] = '\0';
1024 if (mask_mode != -1)
1025 DrawTextExt(drawto, xx, y + current_line * font_height, buffer,
1026 font_nr, mask_mode);
1028 DrawText(xx, y + current_line * font_height, buffer, font_nr);
1034 return current_line;
1037 int DrawTextBufferVA(int x, int y, char *format, va_list ap, int font_nr,
1038 int line_length, int cut_length, int max_lines,
1039 int line_spacing, int mask_mode, boolean autowrap,
1040 boolean centered, boolean parse_comments)
1042 char text_buffer[MAX_OUTPUT_LINESIZE];
1043 int text_length = vsnprintf(text_buffer, MAX_OUTPUT_LINESIZE, format, ap);
1045 if (text_length >= MAX_OUTPUT_LINESIZE)
1046 Error(ERR_WARN, "string too long in DrawTextBufferVA() -- truncated");
1048 int num_lines_printed = DrawTextBuffer(x, y, text_buffer, font_nr,
1049 line_length, cut_length, max_lines,
1050 line_spacing, mask_mode, autowrap,
1051 centered, parse_comments);
1052 return num_lines_printed;
1055 int DrawTextFile(int x, int y, char *filename, int font_nr,
1056 int line_length, int cut_length, int max_lines,
1057 int line_spacing, int mask_mode, boolean autowrap,
1058 boolean centered, boolean parse_comments)
1060 char *text_buffer = GetTextBufferFromFile(filename, MAX_LINES_FROM_FILE);
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 checked_free(text_buffer);
1067 return num_lines_printed;
1071 void DrawTextWrapped(int x, int y, char *text, int font_nr, int line_length,
1074 DrawTextBuffer(x, y, text, font_nr, line_length, -1, max_lines, -1, TRUE,
1078 void DrawTextToTextArea(int x, int y, char *text, int font_nr, int line_length,
1079 int cut_length, int max_lines, int mask_mode)
1081 DrawTextBuffer(x, y, text, font_nr, line_length, cut_length, max_lines,
1082 mask_mode, FALSE, FALSE, FALSE);