summaryrefslogtreecommitdiff
path: root/xps/xpsfont.c
blob: fa3e0ca3f0b73e0dd546817382895f58bfd30a61 (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
#include "ghostxps.h"

/*
 * Big-endian memory accessor functions
 */

static inline int s16(byte *p)
{
    return (signed short)( (p[0] << 8) | p[1] );
}

static inline int u16(byte *p)
{
    return (p[0] << 8) | p[1];
}

static inline int u24(byte *p)
{
    return (p[0] << 16) | (p[1] << 8) | p[2];
}

static inline int u32(byte *p)
{
    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}


int xps_init_font_cache(xps_context_t *ctx)
{
    const int smax = 50;         /* number of scaled fonts */
    const int bmax = 500000;     /* space for cached chars */
    const int mmax = 200;        /* number of cached font/matrix pairs */
    const int cmax = 5000;       /* number of cached chars */
    const int upper = 32000;     /* max size of a single cached char */

    ctx->fontdir = gs_font_dir_alloc2_limits(ctx->memory, ctx->memory, smax, bmax, mmax, cmax, upper);
    if (!ctx->fontdir)
	return gs_throw(-1, "cannot gs_font_dir_alloc2_limits()");

    gs_setaligntopixels(ctx->fontdir, 0); /* no subpixels */
    gs_setgridfittt(ctx->fontdir, 0); /* see gx_ttf_outline in gxttfn.c for values */

    return gs_okay;
}

xps_font_t *
xps_new_font(xps_context_t *ctx, char *buf, int buflen, int index)
{
    xps_font_t *font;
    int code;

    font = xps_alloc(ctx, sizeof(xps_font_t));
    if (!font)
    {
	gs_throw(-1, "out of memory");
	return NULL;
    }

    font->data = (byte*)buf;
    font->length = buflen;
    font->font = NULL;

    font->subfontid = index;
    font->cmaptable = 0;
    font->cmapsubcount = 0;
    font->cmapsubtable = 0;
    font->usepua = 0;

    font->cffdata = 0;
    font->cffend = 0;
    font->gsubrs = 0;
    font->subrs = 0;
    font->charstrings = 0;

    if (memcmp(font->data, "OTTO", 4) == 0)
	code = xps_init_postscript_font(ctx, font);
    else if (memcmp(font->data, "\0\1\0\0", 4) == 0)
	code = xps_init_truetype_font(ctx, font);
    else if (memcmp(font->data, "true", 4) == 0)
	code = xps_init_truetype_font(ctx, font);
    else if (memcmp(font->data, "ttcf", 4) == 0)
	code = xps_init_truetype_font(ctx, font);
    else
    {
	xps_free_font(ctx, font);
	gs_throw(-1, "not an opentype font");
	return NULL;
    }

    if (code < 0)
    {
	xps_free_font(ctx, font);
	gs_rethrow(-1, "cannot init font");
	return NULL;
    }

    code = xps_load_sfnt_cmap(font);
    if (code < 0)
    {
	errprintf("warning: no cmap table found in font\n");
    }

    return font;
}

void
xps_free_font(xps_context_t *ctx, xps_font_t *font)
{
    dputs("calling xps_free_font!\n");
    if (font->font)
    {
	gs_font_finalize(font->font);
	gs_free_object(ctx->memory, font->font, "font object");
    }
    xps_free(ctx, font);
}


/*
 * Find the offset and length of an SFNT table.
 * Return -1 if no table by the specified name is found.
 */

int
xps_find_sfnt_table(xps_font_t *font, char *name, int *lengthp)
{
    byte *directory;
    int offset;
    int ntables;
    int i;

    if (font->length < 12)
	return -1;

    if (!memcmp(font->data, "ttcf", 4))
    {
	int nfonts = u32(font->data + 8);
	if (font->subfontid < 0 || font->subfontid >= nfonts)
	    return gs_throw(-1, "Invalid subfont ID");
	offset = u32(font->data + 12 + font->subfontid * 4);
    }
    else
    {
	offset = 0;
    }

    ntables = u16(font->data + offset + 4);
    if (font->length < offset + 12 + ntables * 16)
	return -1;

    for (i = 0; i < ntables; i++)
    {
	byte *entry = font->data + offset + 12 + i * 16;
	if (!memcmp(entry, name, 4))
	{
	    if (lengthp)
		*lengthp = u32(entry + 12);
	    return u32(entry + 8);
	}
    }

    return -1;
}

/*
 * Get the windows truetype font file name - position 4 in the name table.
 */
int xps_load_sfnt_name(xps_font_t *font, char *namep)
{
    byte *namedata;
    int offset, length;
    int format, count, stringoffset;
    int i;

    strcpy(namep, "Unknown");

    offset = xps_find_sfnt_table(font, "name", &length);
    if (offset < 0)
	return -1;
    if (length < 6)
	return -1;

    namedata = font->data + offset;

    format = u16(namedata + 0);
    count = u16(namedata + 2);
    stringoffset = u16(namedata + 4);

    for (i = 0; i < count; i++)
    {
	byte *record = namedata + 6 + i * 12;
	int pid = u16(record + 0);
	int eid = u16(record + 2);
	int langid = u16(record + 4);
	int nameid = u16(record + 6);
	length = u16(record + 8);
	offset = u16(record + 10);

	/* Mac Roman English */
	if (pid == 1 && eid == 0 && langid == 0)
	{
	    /* Full font name or postscript name */
	    if (nameid == 4 || nameid == 6)
	    {
		memcpy(namep, namedata + stringoffset + offset, length);
		namep[length] = 0;
	    }
	}
    }

    return 0;
}

/*
 * Locate the 'cmap' table and count the number of subtables.
 */

int
xps_load_sfnt_cmap(xps_font_t *font)
{
    byte *cmapdata;
    int offset, length;
    int nsubtables;

    offset = xps_find_sfnt_table(font, "cmap", &length);
    if (offset < 0)
	return -1;

    if (length < 4)
	return -1;

    cmapdata = font->data + offset;

    nsubtables = u16(cmapdata + 2);
    if (nsubtables < 0)
	return -1;
    if (length < 4 + nsubtables * 8)
	return -1;

    font->cmaptable = offset;
    font->cmapsubcount = nsubtables;
    font->cmapsubtable = 0;

    return 0;
}

/*
 * Return the number of cmap subtables.
 */

int
xps_count_font_encodings(xps_font_t *font)
{
    return font->cmapsubcount;
}

/*
 * Extract PlatformID and EncodingID for a cmap subtable.
 */

int
xps_identify_font_encoding(xps_font_t *font, int idx, int *pid, int *eid)
{
    byte *cmapdata, *entry;
    if (idx < 0 || idx >= font->cmapsubcount)
	return -1;
    cmapdata = font->data + font->cmaptable;
    entry = cmapdata + 4 + idx * 8;
    *pid = u16(entry + 0);
    *eid = u16(entry + 2);
    return 0;
}

/*
 * Select a cmap subtable for use with encoding functions.
 */

int
xps_select_font_encoding(xps_font_t *font, int idx)
{
    byte *cmapdata, *entry;
    int pid, eid;
    if (idx < 0 || idx >= font->cmapsubcount)
	return -1;
    cmapdata = font->data + font->cmaptable;
    entry = cmapdata + 4 + idx * 8;
    pid = u16(entry + 0);
    eid = u16(entry + 2);
    font->cmapsubtable = font->cmaptable + u32(entry + 4);
    font->usepua = (pid == 3 && eid == 0);
    return 0;
}


/*
 * Encode a character using the selected cmap subtable.
 * TODO: extend this to cover more cmap formats.
 */

static int
xps_encode_font_char_int(xps_font_t *font, int code)
{
    byte *table;

    /* no cmap selected: return identity */
    if (font->cmapsubtable <= 0)
	return code;

    table = font->data + font->cmapsubtable;

    switch (u16(table))
    {
    case 0: /* Apple standard 1-to-1 mapping. */
	return table[code + 6];

    case 4: /* Microsoft/Adobe segmented mapping. */
	{
	    int segCount2 = u16(table + 6);
	    byte *endCount = table + 14;
	    byte *startCount = endCount + segCount2 + 2;
	    byte *idDelta = startCount + segCount2;
	    byte *idRangeOffset = idDelta + segCount2;
	    int i2;

	    for (i2 = 0; i2 < segCount2 - 3; i2 += 2)
	    {
		int delta, roff;
		int start = u16(startCount + i2);
		int glyph;

		if ( code < start )
		    return 0;
		if ( code > u16(endCount + i2) )
		    continue;
		delta = s16(idDelta + i2);
		roff = s16(idRangeOffset + i2);
		if ( roff == 0 )
		{
		    return ( code + delta ) & 0xffff; /* mod 65536 */
		    return 0;
		}
		glyph = u16(idRangeOffset + i2 + roff + ((code - start) << 1));
		return (glyph == 0 ? 0 : glyph + delta);
	    }

	    /*
	     * The TrueType documentation says that the last range is
	     * always supposed to end with 0xffff, so this shouldn't
	     * happen; however, in some real fonts, it does.
	     */
	    return 0;
	}

    case 6: /* Single interval lookup. */
	{
	    int firstCode = u16(table + 6);
	    int entryCount = u16(table + 8);
	    if ( code < firstCode || code >= firstCode + entryCount )
		return 0;
	    return u16(table + 10 + ((code - firstCode) << 1));
	}

    case 10: /* Trimmed array (like 6) */
	{
	    int startCharCode = u32(table + 12);
	    int numChars = u32(table + 16);
	    if ( code < startCharCode || code >= startCharCode + numChars )
		return 0;
	    return u32(table + 20 + (code - startCharCode) * 4);
	}

    case 12: /* Segmented coverage. (like 4) */
	{
	    int nGroups = u32(table + 12);
	    byte *group = table + 16;
	    int i;

	    for (i = 0; i < nGroups; i++)
	    {
		int startCharCode = u32(group + 0);
		int endCharCode = u32(group + 4);
		int startGlyphID = u32(group + 8);
		if ( code < startCharCode )
		    return 0;
		if ( code <= endCharCode )
		    return startGlyphID + (code - startCharCode);
		group += 12;
	    }

	    return 0;
	}

    case 2: /* High-byte mapping through table. */
    case 8: /* Mixed 16-bit and 32-bit coverage (like 2) */
    default:
	errprintf("error: unknown cmap format: %d\n", u16(table));
	return 0;
    }

    return 0;
}

int
xps_encode_font_char(xps_font_t *font, int code)
{
    int gid = xps_encode_font_char_int(font, code);
    if (gid == 0 && font->usepua)
	gid = xps_encode_font_char_int(font, 0xF000 | code);
    return gid;
}

/*
 * Get glyph metrics by parsing TTF tables manually.
 * XPS needs more and different metrics than postscript/ghostscript
 * use so the native ghostscript functions are not adequate.
 */

int xps_measure_font_glyph(xps_context_t *ctx, xps_font_t *font, int gid, xps_glyph_metrics_t *mtx)
{

    int head, format, loca, glyf;
    int ofs, len;
    int idx, i, n;
    int hadv, vadv, vorg;
    int vtop, ymax, desc;
    int scale;

    /* some insane defaults */

    scale = 1000; /* units-per-em */
    hadv = 500;
    vadv = -1000;
    vorg = 1000;

    /*
     * Horizontal metrics are easy.
     */

    ofs = xps_find_sfnt_table(font, "hhea", &len);
    if (ofs < 0)
	return gs_throw(-1, "cannot find hhea table");

    if (len < 2 * 18)
	return gs_throw(-1, "hhea table is too short");

    vorg = s16(font->data + ofs + 4); /* ascender is default vorg */
    desc = s16(font->data + ofs + 6); /* descender */
    if (desc < 0)
	desc = -desc;
    n = u16(font->data + ofs + 17 * 2);

    ofs = xps_find_sfnt_table(font, "hmtx", &len);
    if (ofs < 0)
	return gs_throw(-1, "cannot find hmtx table");

    idx = gid;
    if (idx > n - 1)
	idx = n - 1;

    hadv = u16(font->data + ofs + idx * 4);
    vadv = 0;

    /*
     * Vertical metrics are hairy (with missing tables).
     */

    head = xps_find_sfnt_table(font, "head", &len);
    if (head > 0)
    {
	scale = u16(font->data + head + 18); /* units per em */
    }

    ofs = xps_find_sfnt_table(font, "OS/2", &len);
    if (ofs > 0 && len > 70)
    {
	vorg = s16(font->data + ofs + 68); /* sTypoAscender */
	desc = s16(font->data + ofs + 70); /* sTypoDescender */
	if (desc < 0)
	    desc = -desc;
    }

    ofs = xps_find_sfnt_table(font, "vhea", &len);
    if (ofs > 0)
    {
	if (len < 2 * 18)
	    return gs_throw(-1, "vhea table is too short");

	n = u16(font->data + ofs + 17 * 2);

	ofs = xps_find_sfnt_table(font, "vmtx", &len);
	if (ofs < 0)
	    return gs_throw(-1, "cannot find vmtx table");

	idx = gid;
	if (idx > n - 1)
	    idx = n - 1;

	vadv = u16(font->data + ofs + idx * 4);
	vtop = u16(font->data + ofs + idx * 4 + 2);

	glyf = xps_find_sfnt_table(font, "glyf", &len);
	loca = xps_find_sfnt_table(font, "loca", &len);
	if (head > 0 && glyf > 0 && loca > 0)
	{
	    format = u16(font->data + head + 50); /* indexToLocaFormat */

	    if (format == 0)
		ofs = u16(font->data + loca + gid * 2) * 2;
	    else
		ofs = u32(font->data + loca + gid * 4);

	    ymax = u16(font->data + glyf + ofs + 8); /* yMax */

	    vorg = ymax + vtop;
	}
    }

    ofs = xps_find_sfnt_table(font, "VORG", &len);
    if (ofs > 0)
    {
	vorg = u16(font->data + ofs + 6);
	n = u16(font->data + ofs + 6);
	for (i = 0; i < n; i++)
	{
	    if (u16(font->data + ofs + 8 + 4 * i) == gid)
	    {
		vorg = s16(font->data + ofs + 8 + 4 * i + 2);
		break;
	    }
	}
    }

    if (vadv == 0)
	vadv = vorg + desc;

    mtx->hadv = hadv / (float) scale;
    mtx->vadv = vadv / (float) scale;
    mtx->vorg = vorg / (float) scale;

    return 0;
}