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

/*
 * Some extra TTF parsing magic that isn't covered by the graphics library.
 */

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

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

extern ulong tt_find_table(gs_font_type42 *pfont, const char *tname, uint *plen);

static const char *pl_mac_names[258] = {
    ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl",
    "numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft",
    "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash",
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
    "nine", "colon", "semicolon", "less", "equal", "greater", "question", "at",
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
    "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
    "backslash", "bracketright", "asciicircum", "underscore", "grave", "a",
    "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
    "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar",
    "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute",
    "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex",
    "adieresis", "atilde", "aring", "ccedilla", "eacute", "egrave",
    "ecircumflex", "edieresis", "iacute", "igrave", "icircumflex", "idieresis",
    "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde",
    "uacute", "ugrave", "ucircumflex", "udieresis", "dagger", "degree", "cent",
    "sterling", "section", "bullet", "paragraph", "germandbls", "registered",
    "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash",
    "infinity", "plusminus", "lessequal", "greaterequal", "yen", "mu",
    "partialdiff", "summation", "product", "pi", "integral", "ordfeminine",
    "ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown",
    "logicalnot", "radical", "florin", "approxequal", "Delta", "guillemotleft",
    "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde",
    "Otilde", "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright",
    "quoteleft", "quoteright", "divide", "lozenge", "ydieresis", "Ydieresis",
    "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl",
    "daggerdbl", "periodcentered", "quotesinglbase", "quotedblbase",
    "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis",
    "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute",
    "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave",
    "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring",
    "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", "Scaron",
    "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute",
    "yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior",
    "twosuperior", "threesuperior", "onehalf", "onequarter", "threequarters",
    "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla",
    "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"
};

/*
 * A bunch of callback functions that the ghostscript
 * font machinery will call. The most important one
 * is the build_char function. These are specific to
 * truetype (loca/glyf) flavored opentypes.
 */

static gs_glyph
xps_true_callback_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t spc)
{
    xps_font_t *font = pfont->client_data;
    int value;
    value = xps_encode_font_char(font, chr);
    if (value == 0)
	return gs_no_glyph;
    return value;
}

static gs_char
xps_true_callback_decode_glyph(gs_font *p42, gs_glyph glyph)
{
    return GS_NO_CHAR;
}

static int
xps_true_callback_glyph_name(gs_font *pfont, gs_glyph glyph, gs_const_string *pstr)
{
    /* This funciton is copied verbatim from plfont.c */

    uint table_length;
    ulong table_offset;

    ulong format;
    uint numGlyphs;
    uint glyph_name_index;
    const byte *postp; /* post table pointer */

    /* guess if the font type is not truetype */
    if ( pfont->FontType != ft_TrueType )
    {
	glyph -= 29;
	if ( glyph >= 0 && glyph < 258 )
	{
	    pstr->data = pl_mac_names[glyph];
	    pstr->size = strlen(pstr->data);
	    return 0;
	}
	else
	{
	    return gs_throw1(-1, "glyph index %d out of range", glyph); 
	}
    }

    table_offset = tt_find_table((gs_font_type42 *)pfont, "post", &table_length);

    /* no post table */
    if ( table_offset == 0 )
	return gs_throw(-1, "no post table");

    /* this shoudn't happen but... */
    if ( table_length == 0 )
	return gs_throw(-1, "zero-size post table");

    ((gs_font_type42 *)pfont)->data.string_proc((gs_font_type42 *)pfont,
						table_offset, table_length, &postp);
    format = u32(postp);

    /* Format 1.0 (mac encoding) is a simple table see the TT spec.
       We don't implement this because we don't see it in practice. */
    if ( format == 0x10000 )
	return -1;

    /* Format 3.0 means that there is no post data in the font file.  */
    if ( format == 0x30000 )
	return -1;

    if ( format != 0x20000 )
	return gs_throw1(-1, "unknown post table format 0x%x", format);

    /* skip over the post header */
    numGlyphs = u16(postp + 32);
    if ( glyph < 0 || glyph > numGlyphs - 1)
    {
	return gs_throw1(-1, "glyph index %d out of range", glyph);
    }

    /* glyph name index starts at post + 34 each entry is 2 bytes */
    glyph_name_index = u16(postp + 34 + (glyph * 2));

    /* this shouldn't happen */
    if ( glyph_name_index < 0 && glyph_name_index > 0x7fff )
	return gs_throw(-1, "post table format error");

    /* mac easy */
    if ( glyph_name_index < 258 )
    {
	dprintf2("glyph name (mac) %d = %s\n", glyph, pl_mac_names[glyph_name_index]);
	pstr->data = pl_mac_names[glyph_name_index];
	pstr->size = strlen(pstr->data);
	return 0;
    }

    /* not mac */
    else
    {
	char *mydata;

	/* and here's the tricky part */
	const byte *pascal_stringp = postp + 34 + (numGlyphs * 2);

	/* 0 - 257 lives in the mac table above */
	glyph_name_index -= 258;

	/* The string we want is the index'th pascal string,
	   so we "hop" to each length byte "index" times. */
	while (glyph_name_index > 0)
	{
	    pascal_stringp += ((int)(*pascal_stringp)+1);
	    glyph_name_index--;
	}

	/* length byte */
	pstr->size = (int)(*pascal_stringp);

	/* + 1 is for the length byte */
	pstr->data = pascal_stringp + 1;

	/* sanity check */
	if ( pstr->data + pstr->size > postp + table_length || pstr->data - 1 < postp)
	    return gs_throw(-1, "data out of range");

	/* sigh - we have to allocate a copy of the data - by the
	   time a high level device makes use of it the font data
	   may be freed.  This is a necessary leak. */
	mydata = gs_alloc_bytes(pfont->memory, pstr->size + 1, "glyph to name");
	if ( mydata == 0 )
	    return -1;
	memcpy(mydata, pascal_stringp + 1, pstr->size);
	pstr->data = mydata;

	mydata[pstr->size] = 0;
	dprintf2("glyph name (tbl) %d = %s\n", glyph, pstr->data);

	return 0;
    }
}

static int
xps_true_callback_string_proc(gs_font_type42 *p42, ulong offset, uint length, const byte **pdata)
{
    /* NB bounds check offset + length - use gs_object_size for memory buffers - if file read should fail */
    xps_font_t *font = p42->client_data;
    *pdata = font->data + offset;
    return 0;
}

static int
xps_true_callback_build_char(gs_text_enum_t *ptextenum, gs_state *pgs, gs_font *pfont,
	gs_char chr, gs_glyph glyph)
{
    gs_show_enum *penum = (gs_show_enum*)ptextenum;
    gs_font_type42 *p42 = (gs_font_type42*)pfont;
    const gs_rect *pbbox;
    float sbw[4], w2[6];
    int code;

    // dprintf1("build char ttf %d\n", glyph);

    code = gs_type42_get_metrics(p42, glyph, sbw);
    if (code < 0)
	return code;

    w2[0] = sbw[2];
    w2[1] = sbw[3];

    pbbox =  &p42->FontBBox;
    w2[2] = pbbox->p.x;
    w2[3] = pbbox->p.y;
    w2[4] = pbbox->q.x;
    w2[5] = pbbox->q.y;

    /* Expand the bbox when stroking */
    if ( pfont->PaintType )
    {
	float expand = max(1.415, gs_currentmiterlimit(pgs)) * gs_currentlinewidth(pgs) / 2;
	w2[2] -= expand, w2[3] -= expand;
	w2[4] += expand, w2[5] += expand;
    }

    if ( (code = gs_moveto(pgs, 0.0, 0.0)) < 0 )
	return code;

    if ( (code = gs_setcachedevice(penum, pgs, w2)) < 0 )
	return code;

    code = gs_type42_append(glyph, pgs,
	    gx_current_path(pgs),
	    ptextenum, (gs_font*)p42,
	    gs_show_in_charpath(penum) != cpm_show);
    if (code < 0)
	return code;

    code = (pfont->PaintType ? gs_stroke(pgs) : gs_fill(pgs));
    if (code < 0)
	return code;

    return 0;
}

/*
 * Initialize the ghostscript font machinery for a truetype
 * (type42 in postscript terminology) font.
 */

int xps_init_truetype_font(xps_context_t *ctx, xps_font_t *font)
{
    font->font = (void*) gs_alloc_struct(ctx->memory, gs_font_type42, &st_gs_font_type42, "xps_font type42");
    if (!font->font)
	return gs_throw(-1, "out of memory");

    /* no shortage of things to initialize */
    {
	gs_font_type42 *p42 = (gs_font_type42*) font->font;

	/* Common to all fonts: */

	p42->next = 0;
	p42->prev = 0;
	p42->memory = ctx->memory;

	p42->dir = ctx->fontdir; /* NB also set by gs_definefont later */
	p42->base = font->font; /* NB also set by gs_definefont later */
	p42->is_resource = false;
	gs_notify_init(&p42->notify_list, gs_memory_stable(ctx->memory));
	p42->id = gs_next_ids(ctx->memory, 1);

	p42->client_data = font; /* that's us */

	/* this is overwritten in grid_fit() */
	gs_make_identity(&p42->FontMatrix);
	gs_make_identity(&p42->orig_FontMatrix); /* NB ... original or zeroes? */

	p42->FontType = ft_TrueType;
	p42->BitmapWidths = true;
	p42->ExactSize = fbit_use_outlines;
	p42->InBetweenSize = fbit_use_outlines;
	p42->TransformedChar = fbit_use_outlines;
	p42->WMode = 0;
	p42->PaintType = 0;
	p42->StrokeWidth = 0;

	p42->procs.init_fstack = gs_default_init_fstack;
	p42->procs.next_char_glyph = gs_default_next_char_glyph;
	p42->procs.glyph_name = xps_true_callback_glyph_name;
	p42->procs.decode_glyph = xps_true_callback_decode_glyph;
	p42->procs.define_font = gs_no_define_font;
	p42->procs.make_font = gs_no_make_font;
	p42->procs.font_info = gs_default_font_info;
	p42->procs.glyph_info = gs_default_glyph_info;
	p42->procs.glyph_outline = gs_no_glyph_outline;
	p42->procs.encode_char = xps_true_callback_encode_char;
	p42->procs.build_char = xps_true_callback_build_char;

	p42->font_name.size = 0;
	p42->key_name.size = 0;

	/* Base font specific: */

	p42->FontBBox.p.x = 0;
	p42->FontBBox.p.y = 0;
	p42->FontBBox.q.x = 0;
	p42->FontBBox.q.y = 0;

	uid_set_UniqueID(&p42->UID, p42->id);

	p42->encoding_index = ENCODING_INDEX_UNKNOWN;
	p42->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;

	p42->FAPI = 0;
	p42->FAPI_font_data = 0;

	/* Type 42 specific: */

	p42->data.string_proc = xps_true_callback_string_proc;
	p42->data.proc_data = font;
	gs_type42_font_init(p42, font->subfontid);
    }

    gs_definefont(ctx->fontdir, font->font);

    return 0;
}