Debian Codegraph @

texk/xdvipsk/otflib/tt_table.c

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and
7 Shunsaku Hirata, the dvipdfmx project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 #include <config.h>
25
26 #include <stdio.h>
27
28 #include "system.h"
29 #include "error.h"
30 #include "mem.h"
31 #include "mfileio.h"
32
33 #include "sfnt.h"
34 #include "tt_table.h"
35
36 /*
37 tables contains information refered by other tables
38 maxp->numGlyphs, etc --> loca, etc
39 hhea->numOfLongHorMetrics --> hmtx
40 head->indexToLocFormat --> loca
41 head->glyphDataFormat --> glyf
42 */
43
44 char *tt_pack_head_table (struct tt_head_table *table)
45 {
46 int i;
47 char *p, *data;
48
49 if (table == NULL)
50 ERROR("passed NULL pointer\n");
51
52 p = data = NEW(TT_HEAD_TABLE_SIZE, char);
53 p += sfnt_put_ulong(p, table->version);
54 p += sfnt_put_ulong(p, table->fontRevision);
55 p += sfnt_put_ulong(p, table->checkSumAdjustment);
56 p += sfnt_put_ulong(p, table->magicNumber);
57 p += sfnt_put_ushort(p, table->flags);
58 p += sfnt_put_ushort(p, table->unitsPerEm);
59 for (i=0; i<8; i++) {
60 *(p++) = (table->created)[i];
61 }
62 for (i=0; i<8; i++) {
63 *(p++) = (table->modified)[i];
64 }
65 p += sfnt_put_short(p, table->xMin);
66 p += sfnt_put_short(p, table->yMin);
67 p += sfnt_put_short(p, table->xMax);
68 p += sfnt_put_short(p, table->yMax);
69 p += sfnt_put_ushort(p, table->macStyle);
70 p += sfnt_put_ushort(p, table->lowestRecPPEM);
71 p += sfnt_put_short(p, table->fontDirectionHint);
72 p += sfnt_put_short(p, table->indexToLocFormat);
73 p += sfnt_put_short(p, table->glyphDataFormat);
74
75 return data;
76 }
77
78 struct tt_head_table *tt_read_head_table (sfnt *sfont)
79 {
80 int i;
81 struct tt_head_table *table = NEW(1, struct tt_head_table);
82
83 sfnt_locate_table(sfont, "head");
84
85 table->version = sfnt_get_ulong(sfont);
86 table->fontRevision = sfnt_get_ulong(sfont);
87 table->checkSumAdjustment = sfnt_get_ulong(sfont);
88 table->magicNumber = sfnt_get_ulong(sfont);
89 table->flags = sfnt_get_ushort(sfont);
90 table->unitsPerEm = sfnt_get_ushort(sfont);
91 for (i=0; i<8; i++) {
92 (table->created)[i] = sfnt_get_byte (sfont);
93 }
94 for (i=0; i<8; i++) {
95 (table->modified)[i] = sfnt_get_byte (sfont);
96 }
97 table->xMin = sfnt_get_short(sfont);
98 table->yMin = sfnt_get_short(sfont);
99 table->xMax = sfnt_get_short(sfont);
100 table->yMax = sfnt_get_short(sfont);
101 table->macStyle = sfnt_get_short(sfont);
102 table->lowestRecPPEM = sfnt_get_short(sfont);
103 table->fontDirectionHint = sfnt_get_short(sfont);
104 table->indexToLocFormat = sfnt_get_short(sfont);
105 table->glyphDataFormat = sfnt_get_short(sfont);
106
107 return table;
108 }
109
110 char *tt_pack_maxp_table (struct tt_maxp_table *table)
111 {
112 char *p, *data;
113
114 p = data = NEW(TT_MAXP_TABLE_SIZE, char);
115 p += sfnt_put_ulong(p, table->version);
116 p += sfnt_put_ushort(p, table->numGlyphs);
117 p += sfnt_put_ushort(p, table->maxPoints);
118 p += sfnt_put_ushort(p, table->maxContours);
119 p += sfnt_put_ushort(p, table->maxComponentPoints);
120 p += sfnt_put_ushort(p, table->maxComponentContours);
121 p += sfnt_put_ushort(p, table->maxZones);
122 p += sfnt_put_ushort(p, table->maxTwilightPoints);
123 p += sfnt_put_ushort(p, table->maxStorage);
124 p += sfnt_put_ushort(p, table->maxFunctionDefs);
125 p += sfnt_put_ushort(p, table->maxInstructionDefs);
126 p += sfnt_put_ushort(p, table->maxStackElements);
127 p += sfnt_put_ushort(p, table->maxSizeOfInstructions);
128 p += sfnt_put_ushort(p, table->maxComponentElements);
129 p += sfnt_put_ushort(p, table->maxComponentDepth);
130
131 return data;
132 }
133
134 struct tt_maxp_table *tt_read_maxp_table (sfnt *sfont)
135 {
136 struct tt_maxp_table *table = NEW(1, struct tt_maxp_table);
137
138 sfnt_locate_table(sfont, "maxp");
139
140 table->version = sfnt_get_ulong(sfont);
141 table->numGlyphs = sfnt_get_ushort(sfont);
142 table->maxPoints = sfnt_get_ushort(sfont);
143 table->maxContours = sfnt_get_ushort(sfont);
144 table->maxComponentPoints = sfnt_get_ushort(sfont);
145 table->maxComponentContours = sfnt_get_ushort(sfont);
146 table->maxZones = sfnt_get_ushort(sfont);
147 table->maxTwilightPoints = sfnt_get_ushort(sfont);
148 table->maxStorage = sfnt_get_ushort(sfont);
149 table->maxFunctionDefs = sfnt_get_ushort(sfont);
150 table->maxInstructionDefs = sfnt_get_ushort(sfont);
151 table->maxStackElements = sfnt_get_ushort(sfont);
152 table->maxSizeOfInstructions = sfnt_get_ushort(sfont);
153 table->maxComponentElements = sfnt_get_ushort(sfont);
154 table->maxComponentDepth = sfnt_get_ushort(sfont);
155
156 return table;
157 }
158
159 char *tt_pack_hhea_table (struct tt_hhea_table *table)
160 {
161 int i;
162 char *p, *data;
163
164 p = data = NEW(TT_HHEA_TABLE_SIZE, char);
165 p += sfnt_put_ulong(p, table->version);
166 p += sfnt_put_short(p, table->ascent);
167 p += sfnt_put_short(p, table->descent);
168 p += sfnt_put_short(p, table->lineGap);
169 p += sfnt_put_ushort(p, table->advanceWidthMax);
170 p += sfnt_put_short(p, table->minLeftSideBearing);
171 p += sfnt_put_short(p, table->minRightSideBearing);
172 p += sfnt_put_short(p, table->xMaxExtent);
173 p += sfnt_put_short(p, table->caretSlopeRise);
174 p += sfnt_put_short(p, table->caretSlopeRun);
175 p += sfnt_put_short(p, table->caretOffset);
176 for (i = 0; i < 4; i++) {
177 p += sfnt_put_short(p, table->reserved[i]);
178 }
179 p += sfnt_put_short(p, table->metricDataFormat);
180 p += sfnt_put_ushort(p, table->numOfLongHorMetrics);
181
182 return data;
183 }
184
185 struct tt_hhea_table *
186 tt_read_hhea_table (sfnt *sfont)
187 {
188 int i;
189 ULONG len;
190 struct tt_hhea_table *table = NEW(1, struct tt_hhea_table);
191
192 sfnt_locate_table(sfont, "hhea");
193
194 table->version = sfnt_get_ulong(sfont);
195 table->ascent = sfnt_get_short (sfont);
196 table->descent = sfnt_get_short(sfont);
197 table->lineGap = sfnt_get_short(sfont);
198 table->advanceWidthMax = sfnt_get_ushort(sfont);
199 table->minLeftSideBearing = sfnt_get_short(sfont);
200 table->minRightSideBearing = sfnt_get_short(sfont);
201 table->xMaxExtent = sfnt_get_short(sfont);
202 table->caretSlopeRise = sfnt_get_short(sfont);
203 table->caretSlopeRun = sfnt_get_short(sfont);
204 table->caretOffset = sfnt_get_short(sfont);
205 for(i = 0; i < 4; i++) {
206 table->reserved[i] = sfnt_get_short(sfont);
207 }
208 table->metricDataFormat = sfnt_get_short(sfont);
209 if (table->metricDataFormat != 0)
210 ERROR("unknown metricDataFormat");
211 table->numOfLongHorMetrics = sfnt_get_ushort(sfont);
212
213 len = sfnt_find_table_len(sfont, "hmtx");
214 table->numOfExSideBearings = (USHORT)((len - table->numOfLongHorMetrics * 4) / 2);
215
216 return table;
217 }
218
219 /* vhea */
220 #if 0
221 char *
222 tt_pack_vhea_table (struct tt_vhea_table *table)
223 {
224 int i;
225 char *p, *data;
226
227 p = data = NEW(TT_VHEA_TABLE_SIZE, char);
228 p += sfnt_put_ulong(p, table->version);
229 p += sfnt_put_short(p, table->vertTypoAscender);
230 p += sfnt_put_short(p, table->vertTypoDescender);
231 p += sfnt_put_short(p, table->vertTypoLineGap);
232 p += sfnt_put_short(p, table->advanceHeightMax); /* ushort ? */
233 p += sfnt_put_short(p, table->minTopSideBearing);
234 p += sfnt_put_short(p, table->minBottomSideBearing);
235 p += sfnt_put_short(p, table->yMaxExtent);
236 p += sfnt_put_short(p, table->caretSlopeRise);
237 p += sfnt_put_short(p, table->caretSlopeRun);
238 p += sfnt_put_short(p, table->caretOffset);
239 for(i = 0; i < 4; i++) {
240 p += sfnt_put_short(p, table->reserved[i]);
241 }
242 p += sfnt_put_short(p, table->metricDataFormat);
243 p += sfnt_put_ushort(p, table->numOfLongVerMetrics);
244
245 return data;
246 }
247 #endif
248
249 struct tt_vhea_table *tt_read_vhea_table (sfnt *sfont)
250 {
251 int i;
252 ULONG len;
253 struct tt_vhea_table *table = NEW(1, struct tt_vhea_table);
254
255 sfnt_locate_table(sfont, "vhea");
256
257 table->version = sfnt_get_ulong(sfont);
258 table->vertTypoAscender = sfnt_get_short (sfont);
259 table->vertTypoDescender = sfnt_get_short(sfont);
260 table->vertTypoLineGap = sfnt_get_short(sfont);
261 table->advanceHeightMax = sfnt_get_short(sfont); /* ushort ? */
262 table->minTopSideBearing = sfnt_get_short(sfont);
263 table->minBottomSideBearing = sfnt_get_short(sfont);
264 table->yMaxExtent = sfnt_get_short(sfont);
265 table->caretSlopeRise = sfnt_get_short(sfont);
266 table->caretSlopeRun = sfnt_get_short(sfont);
267 table->caretOffset = sfnt_get_short(sfont);
268 for(i = 0; i < 4; i++) {
269 (table->reserved)[i] = sfnt_get_short(sfont);
270 }
271 table->metricDataFormat = sfnt_get_short(sfont);
272 table->numOfLongVerMetrics = sfnt_get_ushort(sfont);
273
274 len = sfnt_find_table_len(sfont, "vmtx");
275 table->numOfExSideBearings = (USHORT)((len - table->numOfLongVerMetrics * 4) / 2);
276
277 return table;
278 }
279
280
281 struct tt_VORG_table *
282 tt_read_VORG_table (sfnt *sfont)
283 {
284 struct tt_VORG_table *vorg;
285 ULONG offset;
286 USHORT i;
287
288 offset = sfnt_find_table_pos(sfont, "VORG");
289
290 if (offset > 0) {
291 vorg = NEW(1, struct tt_VORG_table);
292
293 sfnt_locate_table(sfont, "VORG");
294 if (sfnt_get_ushort(sfont) != 1 ||
295 sfnt_get_ushort(sfont) != 0)
296 ERROR("Unsupported VORG version.");
297
298 vorg->defaultVertOriginY = sfnt_get_short(sfont);
299 vorg->numVertOriginYMetrics = sfnt_get_ushort(sfont);
300 vorg->vertOriginYMetrics = NEW(vorg->numVertOriginYMetrics,
301 struct tt_vertOriginYMetrics);
302 /*
303 * The vertOriginYMetrics array must be sorted in increasing
304 * glyphIndex order.
305 */
306 for (i = 0;
307 i < vorg->numVertOriginYMetrics; i++) {
308 vorg->vertOriginYMetrics[i].glyphIndex = sfnt_get_ushort(sfont);
309 vorg->vertOriginYMetrics[i].vertOriginY = sfnt_get_short(sfont);
310 }
311 } else {
312 vorg = NULL;
313 }
314
315 return vorg;
316 }
317
318 /*
319 * hmtx and vmtx
320 *
321 * Reading/writing hmtx and vmtx depend on other tables, maxp and hhea/vhea.
322 */
323
324 struct tt_longMetrics *
325 tt_read_longMetrics (sfnt *sfont, USHORT numGlyphs, USHORT numLongMetrics, USHORT numExSideBearings)
326 {
327 struct tt_longMetrics *m;
328 USHORT gid, last_adv = 0;
329 SHORT last_esb = 0;
330
331 m = NEW(numGlyphs, struct tt_longMetrics);
332 for (gid = 0; gid < numGlyphs; gid++) {
333 if (gid < numLongMetrics)
334 last_adv = sfnt_get_ushort(sfont);
335 if (gid < numLongMetrics + numExSideBearings)
336 last_esb = sfnt_get_short(sfont);
337 m[gid].advance = last_adv;
338 m[gid].sideBearing = last_esb;
339 }
340
341 return m;
342 }
343
344 /* OS/2 table */
345 /* this table may not exist */
346 struct tt_os2__table *
347 tt_read_os2__table (sfnt *sfont)
348 {
349 struct tt_os2__table *table = NULL;
350 int i;
351
352 table = NEW(1, struct tt_os2__table);
353
354 if (sfnt_find_table_pos(sfont, "OS/2") > 0) {
355 sfnt_locate_table(sfont, "OS/2");
356 table->version = sfnt_get_ushort(sfont);
357 table->xAvgCharWidth = sfnt_get_short(sfont);
358 table->usWeightClass = sfnt_get_ushort(sfont);
359 table->usWidthClass = sfnt_get_ushort(sfont);
360 table->fsType = sfnt_get_short(sfont);
361 table->ySubscriptXSize = sfnt_get_short(sfont);
362 table->ySubscriptYSize = sfnt_get_short(sfont);
363 table->ySubscriptXOffset = sfnt_get_short(sfont);
364 table->ySubscriptYOffset = sfnt_get_short(sfont);
365 table->ySuperscriptXSize = sfnt_get_short(sfont);
366 table->ySuperscriptYSize = sfnt_get_short(sfont);
367 table->ySuperscriptXOffset = sfnt_get_short(sfont);
368 table->ySuperscriptYOffset = sfnt_get_short(sfont);
369 table->yStrikeoutSize = sfnt_get_short(sfont);
370 table->yStrikeoutPosition = sfnt_get_short(sfont);
371 table->sFamilyClass = sfnt_get_short(sfont);
372 for (i = 0; i < 10; i++) {
373 table->panose[i] = sfnt_get_byte(sfont);
374 }
375 table->ulUnicodeRange1 = sfnt_get_ulong(sfont);
376 table->ulUnicodeRange2 = sfnt_get_ulong(sfont);
377 table->ulUnicodeRange3 = sfnt_get_ulong(sfont);
378 table->ulUnicodeRange4 = sfnt_get_ulong(sfont);
379 for (i = 0; i < 4; i++) {
380 table->achVendID[i] = sfnt_get_char(sfont);
381 }
382 table->fsSelection = sfnt_get_ushort(sfont);
383 table->usFirstCharIndex = sfnt_get_ushort(sfont);
384 table->usLastCharIndex = sfnt_get_ushort(sfont);
385 if (sfnt_find_table_len(sfont, "OS/2") >= 78) {
386 /* these fields are not present in the original Apple spec (68-byte table),
387 but Microsoft's version of "format 0" does include them... grr! */
388 table->sTypoAscender = sfnt_get_short(sfont);
389 table->sTypoDescender = sfnt_get_short(sfont);
390 table->sTypoLineGap = sfnt_get_short(sfont);
391 table->usWinAscent = sfnt_get_ushort(sfont);
392 table->usWinDescent = sfnt_get_ushort(sfont);
393 if (table->version > 0) {
394 /* format 1 adds the following 2 fields */
395 table->ulCodePageRange1 = sfnt_get_ulong(sfont);
396 table->ulCodePageRange2 = sfnt_get_ulong(sfont);
397 if (table->version > 1) {
398 /* and formats 2 and 3 (current) include 5 more.... these share the
399 same fields, only the precise definition of some was changed */
400 table->sxHeight = sfnt_get_short(sfont);
401 table->sCapHeight = sfnt_get_short(sfont);
402 table->usDefaultChar = sfnt_get_ushort(sfont);
403 table->usBreakChar = sfnt_get_ushort(sfont);
404 table->usMaxContext = sfnt_get_ushort(sfont);
405 }
406 }
407 }
408 } else {
409
410 /* used in add_CIDVMetrics() of cidtype0.c */
411 table->sTypoAscender = 880;
412 table->sTypoDescender = -120;
413 /* used in tt_get_fontdesc() of tt_aux.c */
414 table->usWeightClass = 400U; /* Normal(Regular) */
415 table->xAvgCharWidth = 0; /* ignore */
416 table->version = 0; /* TrueType rev 1.5 */
417 table->fsType = 0; /* Installable Embedding */
418 table->fsSelection = 0U; /* All undefined */
419 table->sFamilyClass = 0; /* No Classification */
420 for (i = 0; i < 10; i++) {
421 table->panose[i] = 0; /* All Any */
422 }
423 }
424
425 return table;
426 }
427
428 USHORT
429 tt_get_name (sfnt *sfont, char *dest, USHORT destlen,
430 USHORT plat_id, USHORT enco_id,
431 USHORT lang_id, USHORT name_id)
432 {
433 USHORT length = 0;
434 USHORT num_names, string_offset;
435 ULONG name_offset;
436 int i;
437
438 name_offset = sfnt_locate_table (sfont, "name");
439
440 if (sfnt_get_ushort(sfont))
441 ERROR ("Expecting zero");
442
443 num_names = sfnt_get_ushort(sfont);
444 string_offset = sfnt_get_ushort(sfont);
445 for (i=0;i<num_names;i++) {
446 USHORT p_id, e_id, n_id, l_id;
447 USHORT offset;
448
449 p_id = sfnt_get_ushort(sfont);
450 e_id = sfnt_get_ushort(sfont);
451 l_id = sfnt_get_ushort(sfont);
452 n_id = sfnt_get_ushort(sfont);
453 length = sfnt_get_ushort(sfont);
454 offset = sfnt_get_ushort(sfont);
455 /* language ID value 0xffffu for `accept any language ID' */
456 if ((p_id == plat_id) && (e_id == enco_id) &&
457 (lang_id == 0xffffu || l_id == lang_id) && (n_id == name_id)) {
458 if (length > destlen - 1) {
459 WARN ("Name string too long (%u), truncating to %u", length, destlen);
460 length = destlen - 1;
461 }
462 sfnt_seek_set (sfont, name_offset+string_offset+offset);
463 sfnt_read((unsigned char*)dest, length, sfont);
464 dest[length] = '\0';
465 break;
466 }
467 }
468 if (i == num_names) {
469 length = 0;
470 }
471
472 return length;
473 }
474
475 USHORT
476 tt_get_ps_fontname (sfnt *sfont, char *dest, USHORT destlen)
477 {
478 USHORT namelen = 0;
479
480 /* First try Mac-Roman PS name and then Win-Unicode PS name */
481 if ((namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0, 6)) != 0 ||
482 (namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 6)) != 0 ||
483 (namelen = tt_get_name(sfont, dest, destlen, 3, 5, 0x412u, 6)) != 0)
484 return namelen;
485
486 WARN ("No valid PostScript name available");
487 /*
488 Workaround for some bad TTfonts:
489 Language ID value 0xffffu for `accept any language ID'
490 */
491 if ((namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0xffffu, 6)) == 0) {
492 /*
493 Finally falling back to Mac Roman name field.
494 Warning: Some bad Japanese TTfonts using SJIS encoded string in the
495 Mac Roman name field.
496 */
497 namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0, 1);
498 }
499
500 return namelen;
501 }