aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/font.cpp
blob: 0b0abac7338384e7893ff6418fbbd5c852ae0268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/***************************************************************************
 font.c Copyright (C) 2000 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/


#include "sci/include/gfx_system.h"
#include "sci/include/gfx_resource.h"
#include "sci/include/gfx_tools.h"

int font_counter = 0;

void
gfxr_free_font(gfx_bitmap_font_t *font) {
	if (font->widths)
		free(font->widths);

	if (font->data)
		free(font->data);

	--font_counter;

	free(font);
}



void
scale_char(byte *dest, byte *src, int width, int height, int newwidth, int xfact, int yfact) {
	int x, y;

	for (y = 0; y < height; y++) {
		int yc;
		byte *bdest = dest;
		byte *bsrc = src;

		for (x = 0; x < width; x++) {
			int xbitc;
			int bits = 0;
			int value = 0;

			for (xbitc = 128; xbitc; xbitc >>= 1) {
				int xc;

				for (xc = 0; xc < xfact; xc++) {
					if (*bsrc & xbitc)
						value |= 1;
					value <<= 1;

					if (++bits == 8) {
						*bdest++ = value;
						bits = value = 0;
					}
				}
			}
			bsrc++;
		}

		src += width;
		for (yc = 1; yc < yfact; yc++) {
			memcpy(dest + newwidth, dest, newwidth);
			dest += newwidth;
		}
		dest += newwidth;
	}
}

gfx_bitmap_font_t *
gfxr_scale_font_unfiltered(gfx_bitmap_font_t *orig_font, gfx_mode_t *mode) {
	gfx_bitmap_font_t *font = (gfx_bitmap_font_t*)sci_malloc(sizeof(gfx_bitmap_font_t));
	int height = orig_font->height * mode->yfact;
	int width = 0;
	int byte_width;
	int i;

	font->chars_nr = orig_font->chars_nr;
	for (i = 0; i < font->chars_nr; i++)
		if (orig_font->widths[i] > width)
			width = orig_font->widths[i];

	width *= mode->xfact;
	byte_width = (width + 7) >> 3;
	if (byte_width == 3)
		byte_width = 4;
	if (byte_width > 4)
		byte_width = (byte_width + 3) & ~3;

	font->row_size = byte_width;
	font->height = height;
	font->line_height = orig_font->line_height * mode->yfact;

	font->widths = (int*)sci_malloc(sizeof(int) * orig_font->chars_nr);
	font->char_size = byte_width * height;
	font->data = (byte*)sci_malloc(font->chars_nr * font->char_size);

	for (i = 0; i < font->chars_nr; i++) {
		font->widths[i] = orig_font->widths[i] * mode->xfact;
		scale_char(font->data + font->char_size * i,
		           orig_font->data + orig_font->char_size * i,
		           orig_font->row_size, orig_font->height,
		           font->row_size,
		           mode->xfact, mode->yfact);
	}
	return font;
}


gfx_bitmap_font_t *
gfxr_scale_font(gfx_bitmap_font_t *orig_font, gfx_mode_t *mode, gfxr_font_scale_filter_t filter) {
	GFXWARN("This function hasn't been tested yet!\n");

	switch (filter) {

	case GFXR_FONT_SCALE_FILTER_NONE:
		return gfxr_scale_font_unfiltered(orig_font, mode);

	default:
		GFXERROR("Invalid font filter mode %d!\n", filter);
		return NULL;
	}
}




text_fragment_t *
gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width, const char *text,
                         int *width, int *height,
                         int *lines, int *line_height_p, int *last_offset_p,
                         int flags) {
	int est_char_width = font->widths[(font->chars_nr > 'M')? 'M' : font->chars_nr - 1];
	/* 'M' is typically among the widest chars */
	int fragments_nr;
	text_fragment_t *fragments;
	int lineheight = font->line_height;
	int maxheight = lineheight;
	int last_breakpoint = 0;
	int last_break_width = 0;
	int max_allowed_width = max_width;
	int maxwidth = 0, localmaxwidth = 0;
	int current_fragment = 1;
	const char *breakpoint_ptr = NULL;
	unsigned char foo;

	if (line_height_p)
		*line_height_p = lineheight;

	if (max_width > 1) fragments_nr = 3 + (strlen(text) * est_char_width) * 3 / (max_width << 1);
	else fragments_nr = 1;

	fragments = (text_fragment_t*)sci_calloc(sizeof(text_fragment_t), fragments_nr);


	fragments[0].offset = text;

	while ((foo = *text++)) {

		if (foo >= font->chars_nr) {
			GFXWARN("Invalid char 0x%02x (max. 0x%02x) encountered in text string '%s', font %04x\n",
			        foo, font->chars_nr, text, font->ID);
			if (font->chars_nr > ' ')
				foo = ' ';
			else {
				free(fragments);
				return NULL;
			}
		}

		if (((foo == '\n') || (foo == 0x0d))
		        && !(flags & GFXR_FONT_FLAG_NO_NEWLINES)) {

			fragments[current_fragment-1].length = text - 1 - fragments[current_fragment-1].offset;

			if (*text)
				maxheight += lineheight;

			if (foo == 0x0d && *text == '\n')
				text++; /* Interpret DOS-style CR LF as single NL */

			fragments[current_fragment++].offset = text;

			if (localmaxwidth > maxwidth)
				maxwidth = localmaxwidth;

			if (current_fragment == fragments_nr)
				fragments = (text_fragment_t*)sci_realloc(fragments, sizeof(text_fragment_t) * (fragments_nr <<= 1));

			localmaxwidth = 0;

		} else { /* foo != '\n' */
			localmaxwidth += font->widths[foo];

			if (localmaxwidth > max_allowed_width) {
				int blank_break = 1; /* break is at a blank char, i.e. not within a word */

				maxheight += lineheight;

				if (last_breakpoint == 0) { /* Text block too long and without whitespace? */
					last_breakpoint = localmaxwidth - font->widths[foo];
					last_break_width = 0;
					--text;
					blank_break = 0; /* non-blank break */
				} else {
					text = breakpoint_ptr + 1;
					assert(breakpoint_ptr);
				}

				if (last_breakpoint == 0) {
					GFXWARN("Warning: maxsize %d too small for '%s'\n",
					        max_allowed_width, text);
				}

				if (last_breakpoint > maxwidth)
					maxwidth = last_breakpoint;

				fragments[current_fragment-1].length = text - blank_break - fragments[current_fragment-1].offset;
				fragments[current_fragment++].offset = text;

				if (current_fragment == fragments_nr)
					fragments = (text_fragment_t*)sci_realloc(fragments, sizeof(text_fragment_t *) * (fragments_nr <<= 1));

				localmaxwidth = localmaxwidth - last_breakpoint;
				if (!(flags & GFXR_FONT_FLAG_COUNT_WHITESPACE))
					localmaxwidth -= last_break_width;
				last_breakpoint = localmaxwidth = 0;

			} else if (*text == ' ') {
				last_breakpoint = localmaxwidth;
				last_break_width = font->widths[foo];
				breakpoint_ptr = text;
			}

		}
	}

	if (localmaxwidth > maxwidth)
		*width = localmaxwidth;
	else
		*width = maxwidth;

	if (last_offset_p)
		*last_offset_p = localmaxwidth;

	if (height)
		*height = maxheight;
	if (lines)
		*lines = current_fragment;

	fragments[current_fragment-1].length = text - fragments[current_fragment-1].offset - 1;

	return fragments;
}


static inline void
render_char(byte *dest, byte *src, int width, int line_width, int lines, int bytes_per_src_line, int fg0, int fg1, int bg) {
	int x, y;

	for (y = 0; y < lines; y++) {
		int dat = 0;
		byte *vdest = dest;
		byte *vsrc = src;
		int xc = 0;

		for (x = 0; x < width; x++) {
			if (!xc) {
				dat = *vsrc++;
				xc = 8;
			}
			xc--;

			if (dat & 0x80)
				*vdest++ = ((xc ^ y) & 1) ? fg0 : fg1; /* dither */
			else
				*vdest++ = bg;

			dat <<= 1;
		}
		src += bytes_per_src_line;
		dest += line_width;
	}
}

gfx_pixmap_t *
gfxr_draw_font(gfx_bitmap_font_t *font, const char *stext, int characters,
               gfx_pixmap_color_t *fg0, gfx_pixmap_color_t *fg1, gfx_pixmap_color_t *bg) {
	unsigned char *text = (unsigned char *) stext;
	int height = font->height;
	int width = 0;
	gfx_pixmap_t *pxm;
	int fore_0, fore_1, back;
	int i;
	int hack = 0;
	gfx_pixmap_color_t dummy = {0, 0, 0, 0};
	byte *offset;

	for (i = 0; i < characters; i++) {
		int ch = (int) text[i];

		if (ch >= font->chars_nr) {
			GFXERROR("Invalid character 0x%02x encountered!\n", text[i]);
			return NULL;
		}

		width += font->widths[ch];
	}

	pxm = gfx_pixmap_alloc_index_data(gfx_new_pixmap(width, height, GFX_RESID_NONE, 0, 0));

	pxm->colors_nr = !!fg0 + !!fg1 + !!bg;
	if (pxm->colors_nr == 0) {
		GFXWARN("Pixmap would have zero colors, resetting!\n");
		pxm->colors_nr = 3;
		hack = 1;
		fg0 = fg1 = bg = &dummy;
	}
	pxm->colors = (gfx_pixmap_color_t*)sci_malloc(sizeof(gfx_pixmap_color_t) * pxm->colors_nr);
#ifdef SATISFY_PURIFY
	memset(pxm->colors, 0, sizeof(gfx_pixmap_color_t) * pxm->colors_nr);
#endif
	pxm->flags |= GFX_PIXMAP_FLAG_PALETTE_ALLOCATED | GFX_PIXMAP_FLAG_DONT_UNALLOCATE_PALETTE;

	i = 0;

	if (fg0 || hack) {
		memcpy(pxm->colors + i, fg0, sizeof(gfx_pixmap_color_t));
		fore_0 = i++;
	} else fore_0 = pxm->color_key;

	if (fg1 || hack) {
		memcpy(pxm->colors + i, fg1, sizeof(gfx_pixmap_color_t));
		fore_1 = i++;
	} else fore_1 = pxm->color_key;

	if (bg || hack) {
		memcpy(pxm->colors + i, bg, sizeof(gfx_pixmap_color_t));
		back = i++;
	} else back = pxm->color_key;

	offset = pxm->index_data;

	memset(pxm->index_data, back, pxm->index_xl * pxm->index_yl);
	for (i = 0; i < characters; i++) {
		unsigned char ch = text[i];
		width = font->widths[ch];

		render_char(offset, font->data + (ch * font->char_size), width,
		            pxm->index_xl, pxm->index_yl, font->row_size,
		            fore_0, fore_1, back);

		offset += width;
	}

	return pxm;
}