summaryrefslogtreecommitdiff
path: root/riscos/font.c
blob: aa6c14307272d35eac3d5f7022b805c669fb6fe2 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 * Copyright 2004 John Tytgat <John.Tytgat@aaug.net>
 */

/** \file
 * Font handling (RISC OS implementation).
 *
 * The Font Manager is used to handle and render fonts.
 */

#include <assert.h>
#include <stdio.h>
#include "oslib/font.h"
#include "netsurf/css/css.h"
#include "netsurf/desktop/gui.h"
#include "netsurf/render/font.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/options.h"
#include "netsurf/riscos/ufont.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

#define FONT_FAMILIES 5 /* Number of families */
#define FONT_FACES 8    /* Number of faces */

/* Font Variants */
#define FONT_SMALLCAPS 4

/* Font Styles */
#define FONT_BOLD 2
#define FONT_SLANTED 1

/* Font families */
#define FONT_SANS_SERIF (0 * FONT_FACES)
#define FONT_SERIF      (1 * FONT_FACES)
#define FONT_MONOSPACE  (2 * FONT_FACES)
#define FONT_CURSIVE    (3 * FONT_FACES)
#define FONT_FANTASY    (4 * FONT_FACES)

/* a font_set is just a linked list of font_data for each face for now */
struct font_set {
	struct font_data *font[FONT_FAMILIES * FONT_FACES];
};

static os_error *nsfont_open_ufont(const char *fontNameP, const char *fbFontNameP, int size, int *handleP, bool *using_fb);
static os_error *nsfont_open_standard(const char *fontNameP, const char *fbFontNameP, int size, int *handleP, bool *using_fb);

/** Table of font names for UFont and an UTF-8 capable FontManager.
 *
 * font id = font family * 8 + smallcaps * 4 + bold * 2 + slanted
 *
 * font family: 0 = sans-serif, 1 = serif, 2 = monospace, 3 = cursive,
 * 4 = fantasy.
 * Font family 0 must be available as it is the replacement font when
 * the other font families can not be found.
 */
static const char * const ufont_table[FONT_FAMILIES * FONT_FACES] = {
	/* sans-serif */
/*0*/	"Homerton.Medium",
/*1*/	"Homerton.Medium.Oblique",
/*2*/	"Homerton.Bold",
/*3*/	"Homerton.Bold.Oblique",
	"Homerton.Medium.SmallCaps",
	"Homerton.Medium.Oblique.SmallCaps",
	"Homerton.Bold.SmallCaps",
	"Homerton.Bold.Oblique.SmallCaps",
	/* serif */
/*8*/	"Trinity.Medium",
/*9*/	"Trinity.Medium.Italic",
/*10*/	"Trinity.Bold",
/*11*/	"Trinity.Bold.Italic",
	"Trinity.Medium.SmallCaps",
	"Trinity.Medium.Italic.SmallCaps",
	"Trinity.Bold.SmallCaps",
	"Trinity.Bold.Italic.SmallCaps",
	/* monospace */
/*16*/	"Corpus.Medium",
/*17*/	"Corpus.Medium.Oblique",
/*18*/	"Corpus.Bold",
/*19*/	"Corpus.Bold.Oblique",
	"Corpus.Medium.SmallCaps",
	"Corpus.Medium.Oblique.SmallCaps",
	"Corpus.Bold.SmallCaps",
	"Corpus.Bold.Oblique.SmallCaps",
	/* cursive */
/*24*/	"Churchill.Medium",
/*25*/	"Churchill.Medium.Oblique",
/*26*/	"Churchill.Bold",
/*27*/	"Churchill.Bold.Oblique",
	"Churchill.Medium.SmallCaps",
	"Churchill.Medium.Oblique.SmallCaps",
	"Churchill.Bold.SmallCaps",
	"Churchill.Bold.Oblique.SmallCaps",
	/* fantasy */
/*32*/	"Sassoon.Primary",
/*33*/	"Sassoon.Primary.Oblique",
/*34*/	"Sassoon.Primary.Bold",
/*35*/	"Sassoon.Primary.Bold.Oblique",
	"Sassoon.Primary.SmallCaps",
	"Sassoon.Primary.Oblique.SmallCaps",
	"Sassoon.Primary.Bold.SmallCaps",
	"Sassoon.Primary.Bold.Oblique.SmallCaps",
};

/** Table of Latin1 encoded font names for a pre-UTF-8 capable FontManager.
 *
 * font id = font family * 8 + smallcaps * 4 + bold * 2 + slanted
 *
 * font family: 0 = sans-serif, 1 = serif, 2 = monospace, 3 = cursive,
 * 4 = fantasy.
 * Font family 0 must be available as it is the replacement font when
 * the other font families can not be found.
 */
static const char * const font_table[FONT_FAMILIES * FONT_FACES] = {
	/* sans-serif */
/*0*/	"Homerton.Medium\\ELatin1",
/*1*/	"Homerton.Medium.Oblique\\ELatin1",
/*2*/	"Homerton.Bold\\ELatin1",
/*3*/	"Homerton.Bold.Oblique\\ELatin1",
	"Homerton.Medium.SmallCaps\\ELatin1",
	"Homerton.Medium.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	"Homerton.Bold.SmallCaps\\ELatin1",
	"Homerton.Bold.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	/* serif */
/*8*/	"Trinity.Medium\\ELatin1",
/*9*/	"Trinity.Medium.Italic\\ELatin1",
/*10*/	"Trinity.Bold\\ELatin1",
/*11*/	"Trinity.Bold.Italic\\ELatin1",
	"Trinity.Medium.SmallCaps\\ELatin1",
	"Trinity.Medium.Italic.SmallCaps\\ELatin1",
	"Trinity.Bold.SmallCaps\\ELatin1",
	"Trinity.Bold.Italic.SmallCaps\\ELatin1",
	/* monospace */
/*16*/	"Corpus.Medium\\ELatin1",
/*17*/	"Corpus.Medium.Oblique\\ELatin1",
/*18*/	"Corpus.Bold\\ELatin1",
/*19*/	"Corpus.Bold.Oblique\\ELatin1",
	"Corpus.Medium.SmallCaps\\ELatin1",
	"Corpus.Medium.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	"Corpus.Bold.SmallCaps\\ELatin1",
	"Corpus.Bold.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	/* cursive */
/*24*/	"Churchill.Medium\\ELatin1",
/*25*/	"Churchill.Medium\\ELatin1\\M65536 0 13930 65536 0 0",
/*26*/	"Churchill.Bold\\ELatin1",
/*27*/	"Churchill.Bold\\ELatin1\\M65536 0 13930 65536 0 0",
	"Churchill.Medium.SmallCaps\\ELatin1",
	"Churchill.Medium.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	"Churchill.Bold.SmallCaps\\ELatin1",
	"Churchill.Bold.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	/* fantasy */
/*32*/	"Sassoon.Primary\\ELatin1",
/*33*/	"Sassoon.Primary\\ELatin1\\M65536 0 13930 65536 0 0",
/*34*/	"Sassoon.Primary.Bold\\ELatin1",
/*35*/	"Sassoon.Primary.Bold\\ELatin1\\M65536 0 13930 65536 0 0",
	"Sassoon.Primary.SmallCaps\\ELatin1",
	"Sassoon.Primary.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
	"Sassoon.Primary.Bold.SmallCaps\\ELatin1",
	"Sassoon.Primary.Bold.SmallCaps\\ELatin1\\M65536 0 13930 65536 0 0",
};

/**
 * Create an empty font_set.
 *
 * \return an opaque struct font_set.
 */
struct font_set *nsfont_new_set(void)
{
	struct font_set *set;
	unsigned int i;

	LOG(("nsfont_new_set()\n"));

	if ((set = malloc(sizeof(*set))) == NULL)
		return NULL;

	for (i = 0; i < FONT_FAMILIES * FONT_FACES; i++)
		set->font[i] = NULL;

	return set;
}

/**
 * Open a font for use based on a css_style.
 *
 * \param set a font_set, as returned by nsfont_new_set()
 * \param style a css_style which describes the font
 * \return a struct font_data, with an opaque font handle in handle
 *
 * The set is updated to include the font, if it was not present.
 */
struct font_data *nsfont_open(struct font_set *set, struct css_style *style)
{
	struct font_data *data;
	unsigned int size = option_font_size * 1.6;
	unsigned int f = 0;
	int fhandle;
	os_error *error;
	bool using_fb;

	assert(set != NULL);
	assert(style != NULL);

	if (style->font_size.size == CSS_FONT_SIZE_LENGTH)
		size = len(&style->font_size.value.length, style) *
				72.0 / 90.0 * 16;
	if (size < option_font_min_size * 1.6)
		size = option_font_min_size * 1.6;
	if (1600 < size)
		size = 1600;

	switch (style->font_family) {
		case CSS_FONT_FAMILY_SANS_SERIF:
			f += FONT_SANS_SERIF;
			break;
		case CSS_FONT_FAMILY_SERIF:
			f += FONT_SERIF;
			break;
		case CSS_FONT_FAMILY_MONOSPACE:
			f += FONT_MONOSPACE;
			break;
		case CSS_FONT_FAMILY_CURSIVE:
			f += FONT_CURSIVE;
			break;
		case CSS_FONT_FAMILY_FANTASY:
			f += FONT_FANTASY;
			break;
		default:
			break;
	}

	switch (style->font_variant) {
		case CSS_FONT_VARIANT_SMALL_CAPS:
			f += FONT_SMALLCAPS;
			break;
		default:
			break;
	}

	switch (style->font_weight) {
		case CSS_FONT_WEIGHT_BOLD:
		case CSS_FONT_WEIGHT_600:
		case CSS_FONT_WEIGHT_700:
		case CSS_FONT_WEIGHT_800:
		case CSS_FONT_WEIGHT_900:
			f += FONT_BOLD;
			break;
		default:
			break;
	}

	switch (style->font_style) {
		case CSS_FONT_STYLE_ITALIC:
		case CSS_FONT_STYLE_OBLIQUE:
			f += FONT_SLANTED;
			break;
		default:
			break;
	}

	for (data = set->font[f]; data != NULL; data = data->next)
		if (data->size == size)
			return data;

	if ((data = malloc(sizeof(*data))) == NULL)
		return NULL;

	/* Strategy : first try the UFont font code with given font name
	 * or the default font name if the former fails.
	 * If this still fails, try the use the default RISC OS font open
	 * in UTF-8 encoding (again first with given font name, then with
	 * the default font name).
	 * If this still fails, we repeat the previous step but now using
	 * the Latin 1 encoding.
	 */
	if ((error = nsfont_open_ufont(ufont_table[f], ufont_table[f % 4], (int)size, &fhandle, &using_fb)) != NULL) {
		char fontName1[128], fontName2[128];
		/* Go for the UTF-8 encoding with standard FontManager */
		strcpy(fontName1, ufont_table[f]);
		strcat(fontName1, "\\EUTF8");
		strcpy(fontName2, ufont_table[f % 4]);
		strcat(fontName2, "\\EUTF8");
		if ((error = nsfont_open_standard(fontName1, fontName2, (int)size, &fhandle, &using_fb)) != NULL) {
			/* All UTF-8 font methods failed, only support Latin 1 */
			if ((error = nsfont_open_standard(font_table[f], font_table[f % 4], (int)size, &fhandle, &using_fb)) != NULL) {
				LOG(("(u)font_find_font failed : %s\n", error->errmess));
				die("(u)font_find_font failed");
			}
			data->ftype = FONTTYPE_STANDARD_LATIN1;
		} else
			data->ftype = FONTTYPE_STANDARD_UTF8ENC;
	} else
		data->ftype = FONTTYPE_UFONT;

	data->id = (using_fb) ? f % 4 : f;
	data->handle = fhandle;
	data->size = size;
	data->space_width = nsfont_width(data, " ", sizeof(" ")-1);

	data->next = set->font[f];
	set->font[f] = data;

	return data;
}


/**
 * Open font via UFont code.
 *
 * \param fontNameP UFont font name
 * \param fbFontNameP fallback UFont font name
 * \param size font size
 * \param handle returning UFont handle in case there isn't an error.
 * \param using_fb returning whether the fallback font was used or not.
 * \return error in case there was one.
 */
static os_error *nsfont_open_ufont(const char *fontNameP, const char *fbFontNameP, int size, int *handleP, bool *using_fb)
{
	os_error *errorP;
	*handleP = 0; *using_fb = false;
	if ((errorP = xufont_find_font(fontNameP, size, size, 0, 0, (ufont_f *)handleP, NULL, NULL)) == NULL)
		return NULL;
	LOG(("ufont_find_font(<%s>) failed <%s> (case 1)", fontNameP, errorP->errmess));
	/* If the fallback font is the same as the first font name, return */
	if (strcmp(fontNameP, fbFontNameP) == 0)
		return errorP;
	*using_fb = true;
	if ((errorP = xufont_find_font(fbFontNameP, size, size, 0, 0, (ufont_f *)handleP, NULL, NULL)) == NULL)
		return NULL;
	LOG(("ufont_find_font(<%s>) failed <%s> (case 2)", fbFontNameP, errorP->errmess));
	return errorP;
}


/**
 * Open font via standard FontManager.
 *
 * \param fontNameP RISC OS font name
 * \param fbFontNameP fallback RISC OS font name
 * \param size font size
 * \param handle RISC OS handle in case there isn't an error.
 * \param using_fb returning whether the fallback font was used or not.
 * \return error in case there was one.
 */
static os_error *nsfont_open_standard(const char *fontNameP, const char *fbFontNameP, int size, int *handleP, bool *using_fb)
{
	os_error *errorP;
	*handleP = 0; *using_fb = false;
	if ((errorP = xfont_find_font(fontNameP, size, size, 0, 0, (font_f *)handleP, NULL, NULL)) == NULL)
		return NULL;
	LOG(("font_find_font(<%s>) failed <%s> (case 1)", fontNameP, errorP->errmess));
	/* If the fallback font is the same as the first font name, return */
	if (strcmp(fontNameP, fbFontNameP) == 0)
		return errorP;
	*using_fb = true;
	if ((errorP = xfont_find_font(fbFontNameP, size, size, 0, 0, (font_f *)handleP, NULL, NULL)) == NULL)
		return NULL;
	LOG(("font_find_font(<%s>) failed <%s> (case 2)", fbFontNameP, errorP->errmess));
	return errorP;
}


/**
 * Frees all the fonts in a font_set.
 *
 * \param set a font_set as returned by nsfont_new_set()
 */
void nsfont_free_set(struct font_set *set)
{
	unsigned int i;

	LOG(("nsfont_free_set()\n"));
	assert(set != NULL);

	for (i = 0; i < FONT_FAMILIES * FONT_FACES; i++) {
		struct font_data *data, *next;
		for (data = set->font[i]; data != NULL; data = next) {
			os_error *error;
			next = data->next;
			switch (data->ftype) {
				case FONTTYPE_UFONT:
					error = xufont_lose_font((ufont_f)data->handle);
					break;
				case FONTTYPE_STANDARD_UTF8ENC:
				case FONTTYPE_STANDARD_LATIN1:
					error = xfont_lose_font((font_f)data->handle);
					break;
				default:
					assert(0);
					break;
			}
			if (error != NULL)
				LOG(("(u)font_lose_font() failed : 0x%x <%s>\n", error->errnum, error->errmess));
			free(data);
		}
	}

	free(set);
}


/**
 * Find the width of some text in a font.
 *
 * \param font a font_data, as returned by nsfont_open()
 * \param text string to measure
 * \param length length of text
 * \return width of text in pixels
 */
unsigned long nsfont_width(struct font_data *font, const char *text,
		unsigned int length)
{
	int width;
	os_error *error;

	assert(font != NULL && text != NULL);

	if (length == 0)
		return 0;

	switch (font->ftype) {
		case FONTTYPE_UFONT:
			error = xufont_scan_string((ufont_f)font->handle,
					text,
					font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					0x7fffffff, 0x7fffffff,
					NULL,
					NULL, (int)length,
					NULL, &width, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_UTF8ENC:
			error = xfont_scan_string((font_f)font->handle,
					text,
					font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					0x7fffffff, 0x7fffffff,
					NULL,
					NULL, (int)length,
					NULL, &width, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_LATIN1: {
			const char *loc_text = cnv_strn_local_enc(text, length, NULL);
			error = xfont_scan_string((font_f)font->handle,
					loc_text,
					font_GIVEN_FONT
						| font_KERN,
					0x7fffffff, 0x7fffffff,
					NULL,
					NULL, 0,
					NULL, &width, NULL, NULL);
			free((void *)loc_text);
			break;
		}
		default:
			assert(0);
			break;
	}
	if (error != NULL) {
		LOG(("(u)font_scan_string failed : %s\n", error->errmess));
		die("nsfont_width: (u)font_scan_string failed");
	}

	return width / 800;
}


/**
 * Find where in a string a x coordinate falls.
 *
 * For example, used to find where to position the caret in response to mouse
 * click.
 *
 * \param font a font_data, as returned by nsfont_open()
 * \param text a string
 * \param length length of text
 * \param x horizontal position in pixels
 * \param char_offset updated to give the offset in the string
 * \param pixel_offset updated to give the coordinate of the character in pixels
 */
void nsfont_position_in_string(struct font_data *font, const char *text,
		unsigned int length, unsigned long x,
		int *char_offset, int *pixel_offset)
{
	os_error *error;
	font_scan_block block;
	const char *split;
	int x_out;

	assert(font != NULL && text != NULL);

	block.space.x = block.space.y = block.letter.x = block.letter.y = 0;
	block.split_char = -1;

	switch (font->ftype) {
		case FONTTYPE_UFONT:
			error = xufont_scan_string((ufont_f)font->handle,
					text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN
						| font_RETURN_CARET_POS
						| font_GIVEN_LENGTH,
					x * 2 * 400, 0x7fffffff,
					&block, NULL, (int)length,
					&split, &x_out, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_UTF8ENC:
			error = xfont_scan_string((font_f)font->handle,
					text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN
						| font_RETURN_CARET_POS
						| font_GIVEN_LENGTH,
					x * 2 * 400, 0x7fffffff,
					&block, NULL, (int)length,
					&split, &x_out, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_LATIN1: {
			const ptrdiff_t *back_mapP;
			const char *loc_text = cnv_strn_local_enc(text, length, &back_mapP);
			error = xfont_scan_string((font_f)font->handle,
					loc_text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN
						| font_RETURN_CARET_POS,
					x * 2 * 400, 0x7fffffff,
					&block, NULL, 0,
					&split, &x_out, NULL, NULL);
			split = &text[back_mapP[split - loc_text]];
			free((void *)loc_text); free((void *)back_mapP);
			break;
		}
		default:
			assert(0);
			break;
	}
	if (error != NULL) {
		LOG(("(u)font_scan_string failed : %s\n", error->errmess));
		die("nsfont_position_in_string: (u)font_scan_string failed");
	}

	*char_offset = (int)(split - text);
	*pixel_offset = x_out / 800;
}


/**
 * Find where to split a string to fit in a width.
 *
 * For example, used when wrapping paragraphs.
 *
 * \param font a font_data, as returned by nsfont_open()
 * \param text string to split
 * \param length length of text
 * \param width available width
 * \param used_width updated to actual width used
 * \return pointer to character which does not fit
 */
char *nsfont_split(struct font_data *font, const char *text,
		unsigned int length,
		unsigned int width, unsigned int *used_width)
{
	os_error *error;
	font_scan_block block;
	const char *split;

	assert(font != NULL && text != NULL);

	block.space.x = block.space.y = block.letter.x = block.letter.y = 0;
	block.split_char = ' ';

	switch (font->ftype) {
		case FONTTYPE_UFONT:
			error = xufont_scan_string((ufont_f)font->handle,
					text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					width * 2 * 400, 0x7fffffff,
					&block,
					NULL,
					(int)length,
					&split,
					used_width, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_UTF8ENC:
			error = xfont_scan_string((font_f)font->handle,
					text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					width * 2 * 400, 0x7fffffff,
					&block,
					NULL,
					(int)length,
					&split,
					used_width, NULL, NULL);
			break;
		case FONTTYPE_STANDARD_LATIN1: {
			const ptrdiff_t *back_mapP;
			const char *loc_text = cnv_strn_local_enc(text, length, &back_mapP);
			error = xfont_scan_string((font_f)font->handle,
					loc_text,
					font_GIVEN_BLOCK
						| font_GIVEN_FONT
						| font_KERN,
					width * 2 * 400, 0x7fffffff,
					&block,
					NULL,
					0,
					&split,
					used_width, NULL, NULL);
			split = &text[back_mapP[split - loc_text]];
			free((void *)loc_text); free((void *)back_mapP);
			break;
		}
		default:
			assert(0);
			break;
	}
	if (error != NULL) {
		LOG(("(u)font_scan_string failed : %s\n", error->errmess));
		die("nsfont_split: (u)font_scan_string failed");
	}

	assert(split == &text[length] || *split == ' ');

	*used_width = *used_width / 2 / 400;

	return split;
}


void nsfont_paint(struct font_data *data, const char *text,
		int xpos, int ypos, void *trfm, int length)
{
	os_error *error;
	unsigned int flags;

	flags = font_OS_UNITS | font_GIVEN_FONT | font_KERN;
	if (trfm != NULL)
		flags |= font_GIVEN_TRFM;

	/* font background blending (RO3.7+) */
	if (option_background_blending) {
		int version;

		/* Font manager versions below 3.35 complain
		 * about this flag being set.
		 */
		error = xfont_cache_addr(&version, 0, 0);
		/**\todo should we do anything else on error? */
		if (!error && version >= 335)
			flags |= font_BLEND_FONT;
	}

	assert(data != NULL);
	assert(text != NULL);

	switch (data->ftype) {
		case FONTTYPE_UFONT:
			flags |= font_GIVEN_LENGTH;
			error = xufont_paint((ufont_f)data->handle, text,
					flags, xpos, ypos, NULL,
					trfm, length);
			break;
		case FONTTYPE_STANDARD_UTF8ENC:
			flags |= font_GIVEN_LENGTH;
			error = xfont_paint((font_f)data->handle, text,
					flags, xpos, ypos, NULL,
					trfm, length);
			break;
		case FONTTYPE_STANDARD_LATIN1: {
			const char *loc_text = cnv_strn_local_enc(text, length, NULL);
			error = xfont_paint((font_f)data->handle, loc_text,
					flags, xpos, ypos, NULL,
					trfm, 0);
			free((void *)loc_text);
			break;
		}
		default:
			assert(0);
			break;
	}
	if (error != NULL) {
		LOG(("(u)font_paint failed : %s\n", error->errmess));
		die("nsfont_paint: (u)font_paint failed");
	}
}


/**
 * Given a text line, return the number of bytes which can be set using
 * one RISC OS font and the bounding box fitting that part of the text
 * only.
 *
 * \param font a font_data, as returned by nsfont_open()
 * \param text string text.  Does not have to be NUL terminated.
 * \param length length in bytes of the text to consider.
 * \param width returned width of the text which can be set with one RISC OS font. If 0, then error happened or initial text length was 0.
 * \param rofontname returned name of the RISC OS font which can be used to set the text. If NULL, then error happened or initial text length was 0.
 * \param rotext returned string containing the characters in returned RISC OS font. Not necessary NUL terminated. free() after use.  If NULL, then error happened or initial text length was 0.
 * \param rolength length of return rotext string. If 0, then error happened or initial text length was 0.
 * \param consumed number of bytes of the given text which can be set with one RISC OS font. If 0, then error happened or initial text length was 0.
 */
void nsfont_txtenum(struct font_data *font, const char *text,
		unsigned int length,
		unsigned int *width,
		const char **rofontname,
		const char **rotext,
		unsigned int *rolength,
		unsigned int *consumed)
{
	assert(font != NULL && text != NULL && rofontname != NULL && rotext != NULL && rolength != NULL && consumed != NULL);

	*rotext = *rofontname = NULL;
	*consumed = *rolength = *width = 0;

	if (length == 0)
		return;

	switch (font->ftype) {
		case FONTTYPE_UFONT:
			(void)xufont_txtenum((ufont_f)font->handle,
					text,
					font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					length,
					(int *)width,
					rofontname,
					rotext,
					rolength,
					consumed);
			*width /= 800;
			break;
		case FONTTYPE_STANDARD_UTF8ENC: {
			static char *fontname[128]; /** /todo: not nice */
			int rowidth;
			os_error *error;

			error = xfont_scan_string((font_f)font->handle,
					text,
					font_GIVEN_FONT
						| font_KERN
						| font_GIVEN_LENGTH,
					0x7fffffff, 0x7fffffff,
					NULL,
					NULL, (int)length,
					NULL, &rowidth, NULL, NULL);
			if (error != NULL)
				return;

			strcpy(fontname, ufont_table[font->id]);
			strcat(fontname, "\\EUTF8");
			if ((*rotext = strndup(text, length)) == NULL)
				return;
			*rolength = length;
			*rofontname = fontname;
			*consumed = length;
			*width = (unsigned int)rowidth / 800;
			break;
		}
		case FONTTYPE_STANDARD_LATIN1: {
			int rowidth;
			os_error *error;

			if ((*rotext = cnv_strn_local_enc(text, length, NULL)) == NULL)
				return;

			error = xfont_scan_string((font_f)font->handle,
					*rotext,
					font_GIVEN_FONT
						| font_KERN,
					0x7fffffff, 0x7fffffff,
					NULL,
					NULL, 0,
					NULL, &rowidth, NULL, NULL);
			if (error != NULL) {
				free((void *)*rotext); *rotext = NULL;
				return;
			}
			*rolength = strlen(*rotext);
			*rofontname = font_table[font->id];
			*consumed = length;
			*width = (unsigned int)rowidth / 800;
			break;
		}
		default:
			assert(0);
			break;
	}
}