summaryrefslogtreecommitdiff
path: root/rufl_paint.c
blob: fa7403234434324fab35f8647f459984722923c0 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * This file is part of RUfl
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license
 * Copyright 2005 James Bursa <james@semichrome.net>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "oslib/font.h"
#include "rufl_internal.h"


typedef enum { rufl_PAINT, rufl_WIDTH } rufl_action;


static rufl_code rufl_process(rufl_action action,
		const char *font_family, rufl_style font_style,
		unsigned int font_size,
		const char *string, size_t length,
		int x, int y, int *width);
static int rufl_family_list_cmp(const void *keyval, const void *datum);
static rufl_code rufl_process_span(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font, unsigned int font_size, int *x, int y);
static rufl_code rufl_process_span_old(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font, unsigned int font_size, int *x, int y);
static int rufl_unicode_map_search_cmp(const void *keyval, const void *datum);
static rufl_code rufl_process_not_available(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font_size, int *x, int y);
static rufl_code rufl_place_in_cache(unsigned int font, unsigned int font_size,
		font_f f);


/**
 * Render Unicode text.
 */

rufl_code rufl_paint(const char *font_family, rufl_style font_style,
		unsigned int font_size,
		const char *string, size_t length,
		int x, int y)
{
	return rufl_process(rufl_PAINT,
			font_family, font_style, font_size, string,
			length, x, y, 0);
}


/**
 * Measure the width of Unicode text.
 */

rufl_code rufl_width(const char *font_family, rufl_style font_style,
		unsigned int font_size,
		const char *string, size_t length,
		int *width)
{
	return rufl_process(rufl_WIDTH,
			font_family, font_style, font_size, string,
			length, 0, 0, width);
}


/**
 * Render, measure, or split Unicode text.
 */

rufl_code rufl_process(rufl_action action,
		const char *font_family, rufl_style font_style,
		unsigned int font_size,
		const char *string, size_t length,
		int x, int y, int *width)
{
	unsigned short s[80];
	unsigned int font;
	unsigned int font0, font1;
	unsigned int n;
	unsigned int u;
	char **family;
	struct rufl_character_set *charset;
	rufl_code code;

	if (length == 0)
		return rufl_OK;

	family = bsearch(font_family, rufl_family_list,
			rufl_family_list_entries,
			sizeof rufl_family_list[0], rufl_family_list_cmp);
	if (!family)
		return rufl_FONT_NOT_FOUND;
	font = rufl_family_map[rufl_STYLES * (family - rufl_family_list) +
			font_style];
	charset = rufl_font_list[font].charset;

	rufl_utf8_read(string, length, u);
	if (rufl_character_set_test(charset, u))
		font1 = font;
	else
		font1 = rufl_substitution_lookup(u);
	do {
		s[0] = u;
		n = 1;
		font0 = font1;
		/* invariant: s[0..n) is in font font0 */
		while (0 < length && n < 70 && font1 == font0) {
			rufl_utf8_read(string, length, u);
			s[n] = u;
			if (rufl_character_set_test(charset, u))
				font1 = font;
			else
				font1 = rufl_substitution_lookup(u);
			if (font1 == font0)
				n++;
		}
		s[n] = 0;

		if (font0 == NOT_AVAILABLE)
			code = rufl_process_not_available(action, s, n,
					font_size, &x, y);
		else if (rufl_old_font_manager)
			code = rufl_process_span_old(action, s, n, font0,
					font_size, &x, y);
		else
			code = rufl_process_span(action, s, n, font0,
					font_size, &x, y);

		if (code != rufl_OK)
			return code;

	} while (!(length == 0 && font1 == font0));

	if (width)
		*width = x;

	return rufl_OK;
}


int rufl_family_list_cmp(const void *keyval, const void *datum)
{
	const char *key = keyval;
	const char * const *entry = datum;
	return strcmp(key, *entry);
}


/**
 * Render a string of characters from a single RISC OS font.
 */

rufl_code rufl_process_span(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font, unsigned int font_size, int *x, int y)
{
	char font_name[80];
	int x_out, y_out;
	unsigned int i;
	font_f f;
	rufl_code code;

	/* search cache */
	for (i = 0; i != rufl_CACHE_SIZE; i++) {
		if (rufl_cache[i].font == font &&
				rufl_cache[i].size == font_size)
			break;
	}
	if (i != rufl_CACHE_SIZE) {
		/* found in cache */
		f = rufl_cache[i].f;
		rufl_cache[i].last_used = rufl_cache_time++;
	} else {
		/* not found */
		snprintf(font_name, sizeof font_name, "%s\\EUTF8",
				rufl_font_list[font].identifier);
		rufl_fm_error = xfont_find_font(font_name,
				font_size, font_size, 0, 0, &f, 0, 0);
		if (rufl_fm_error)
			return rufl_FONT_MANAGER_ERROR;
		/* place in cache */
		code = rufl_place_in_cache(font, font_size, f);
		if (code != rufl_OK)
			return code;
	}

	if (action == rufl_PAINT) {
		/* paint span */
		rufl_fm_error = xfont_paint(f, (const char *) s,
				font_OS_UNITS | font_GIVEN_LENGTH |
				font_GIVEN_FONT | font_KERN | font_GIVEN16_BIT,
				*x, y, 0, 0, n * 2);
		if (rufl_fm_error) {
			xfont_lose_font(f);
			return rufl_FONT_MANAGER_ERROR;
		}
	}

	/* increment x by width of span */
	rufl_fm_error = xfont_scan_string(f, (const char *) s,
			font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN |
			font_GIVEN16_BIT,
			0x7fffffff, 0x7fffffff, 0, 0, n * 2,
			0, &x_out, &y_out, 0);
	if (rufl_fm_error) {
		xfont_lose_font(f);
		return rufl_FONT_MANAGER_ERROR;
	}
	*x += x_out / 400;

	return rufl_OK;
}


/**
 * Render a string of characters from a single RISC OS font  (old font manager
 * version).
 */

rufl_code rufl_process_span_old(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font, unsigned int font_size, int *x, int y)
{
	char s2[80];
	const char *font_name = rufl_font_list[font].identifier;
	int x_out, y_out;
	unsigned int i;
	font_f f;
	rufl_code code;
	struct rufl_unicode_map_entry *entry;

	/* search cache */
	for (i = 0; i != rufl_CACHE_SIZE; i++) {
		if (rufl_cache[i].font == font &&
				rufl_cache[i].size == font_size)
			break;
	}
	if (i != rufl_CACHE_SIZE) {
		/* found in cache */
		f = rufl_cache[i].f;
		rufl_cache[i].last_used = rufl_cache_time++;
	} else {
		/* not found */
		rufl_fm_error = xfont_find_font(font_name,
				font_size, font_size, 0, 0, &f, 0, 0);
		if (rufl_fm_error)
			return rufl_FONT_MANAGER_ERROR;
		/* place in cache */
		code = rufl_place_in_cache(font, font_size, f);
		if (code != rufl_OK)
			return code;
	}

	/* convert Unicode string into character string */
	for (i = 0; i != n; i++) {
		entry = bsearch(&s[i], rufl_font_list[font].umap->map,
				rufl_font_list[font].umap->entries,
				sizeof rufl_font_list[font].umap->map[0],
				rufl_unicode_map_search_cmp);
		s2[i] = entry->c;
	}
	s2[i] = 0;

	if (action == rufl_PAINT) {
		/* paint span */
		rufl_fm_error = xfont_paint(f, s2, font_OS_UNITS |
				font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN,
				*x, y, 0, 0, n);
		if (rufl_fm_error) {
			xfont_lose_font(f);
			return rufl_FONT_MANAGER_ERROR;
		}
	}

	/* increment x by width of span */
	rufl_fm_error = xfont_scan_string(f, s2,
			font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN,
			0x7fffffff, 0x7fffffff, 0, 0, n,
			0, &x_out, &y_out, 0);
	if (rufl_fm_error) {
		xfont_lose_font(f);
		return rufl_FONT_MANAGER_ERROR;
	}
	*x += x_out / 400;

	return rufl_OK;
}


int rufl_unicode_map_search_cmp(const void *keyval, const void *datum)
{
	const unsigned short *key = keyval;
	const struct rufl_unicode_map_entry *entry = datum;
	if (*key < entry->u)
		return -1;
	else if (entry->u < *key)
		return 1;
	return 0;
}


/**
 * Render a string of characters not available in any font as their hex code.
 */

rufl_code rufl_process_not_available(rufl_action action,
		unsigned short *s, unsigned int n,
		unsigned int font_size, int *x, int y)
{
	char missing[] = "0000";
	unsigned int i;
	font_f f;
	rufl_code code;

	if (action == rufl_WIDTH) {
		*x += 7 * font_size / 64;
		return rufl_OK;
	}

	/* search cache */
	for (i = 0; i != rufl_CACHE_SIZE; i++) {
		if (rufl_cache[i].font == rufl_CACHE_CORPUS &&
				rufl_cache[i].size == font_size)
			break;
	}
	if (i != rufl_CACHE_SIZE) {
		/* found in cache */
		f = rufl_cache[i].f;
		rufl_cache[i].last_used = rufl_cache_time++;
	} else {
		/* not found */
		rufl_fm_error = xfont_find_font("Corpus.Medium\\ELatin1",
				font_size / 2, font_size / 2, 0, 0,
				&f, 0, 0);
		if (rufl_fm_error)
			return rufl_FONT_MANAGER_ERROR;
		/* place in cache */
		code = rufl_place_in_cache(rufl_CACHE_CORPUS, font_size, f);
		if (code != rufl_OK)
			return code;
	}

	for (i = 0; i != n; i++) {
		missing[0] = "0123456789abcdef"[(s[i] >> 12) & 0xf];
		missing[1] = "0123456789abcdef"[(s[i] >> 8) & 0xf];
		missing[2] = "0123456789abcdef"[(s[i] >> 4) & 0xf];
		missing[3] = "0123456789abcdef"[(s[i] >> 0) & 0xf];

		/* first two characters in top row */
		rufl_fm_error = xfont_paint(f, missing, font_OS_UNITS |
				font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN,
				*x, y + 5 * font_size / 64,
				0, 0, 2);
		if (rufl_fm_error)
			return rufl_FONT_MANAGER_ERROR;

		/* last two characters underneath */
		rufl_fm_error = xfont_paint(f, missing + 2, font_OS_UNITS |
				font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN,
				*x, y, 0, 0, 2);
		if (rufl_fm_error)
			return rufl_FONT_MANAGER_ERROR;

		*x += 7 * font_size / 64;
	}

	return rufl_OK;
}


/**
 * Place a font into the recent-use cache, making space if necessary.
 */

rufl_code rufl_place_in_cache(unsigned int font, unsigned int font_size,
		font_f f)
{
	unsigned int i;
	unsigned int max_age = 0;
	unsigned int evict = 0;

	for (i = 0; i != rufl_CACHE_SIZE; i++) {
		if (rufl_cache[i].font == rufl_CACHE_NONE) {
			evict = i;
			break;
		} else if (max_age < rufl_cache_time -
				rufl_cache[i].last_used) {
			max_age = rufl_cache_time -
					rufl_cache[i].last_used;
			evict = i;
		}
	}
	rufl_fm_error = xfont_lose_font(rufl_cache[evict].f);
	if (rufl_fm_error)
		return rufl_FONT_MANAGER_ERROR;
	rufl_cache[evict].font = font;
	rufl_cache[evict].size = font_size;
	rufl_cache[evict].f = f;
	rufl_cache[evict].last_used = rufl_cache_time++;

	return rufl_OK;
}