Debian Codegraph @

texk/web2c/luatexdir/font/tt_table.c

raw
1 /*
2
3 Copyright 2002 by Jin-Hwan Cho and Shunsaku Hirata, the dvipdfmx project
4 team <[email protected]>
5 Copyright 2006-2010 Taco Hoekwater <[email protected]>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under the terms
10 of the GNU General Public License as published by the Free Software Foundation;
11 either version 2 of the License, or (at your option) any later version.
12
13 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "ptexlib.h"
23 #include <stdio.h>
24 #include "font/sfnt.h"
25 #include "font/tt_table.h"
26
27 /*tex
28
29 Tables contain information referred by other tables.
30
31 */
32
33 char *tt_pack_head_table(struct tt_head_table *table)
34 {
35 int i;
36 char *p, *data;
37 if (table == NULL)
38 normal_error("ttf","passed NULL pointer");
39 p = data = NEW(TT_HEAD_TABLE_SIZE, char);
40 p += sfnt_put_ulong(p, (LONG) table->version);
41 p += sfnt_put_ulong(p, (LONG) table->fontRevision);
42 p += sfnt_put_ulong(p, (LONG) table->checkSumAdjustment);
43 p += sfnt_put_ulong(p, (LONG) table->magicNumber);
44 p += sfnt_put_ushort(p, table->flags);
45 p += sfnt_put_ushort(p, table->unitsPerEm);
46 for (i = 0; i < 8; i++) {
47 *(p++) = (char) (table->created)[i];
48 }
49 for (i = 0; i < 8; i++) {
50 *(p++) = (char) (table->modified)[i];
51 }
52 p += sfnt_put_short(p, table->xMin);
53 p += sfnt_put_short(p, table->yMin);
54 p += sfnt_put_short(p, table->xMax);
55 p += sfnt_put_short(p, table->yMax);
56 p += sfnt_put_ushort(p, table->macStyle);
57 p += sfnt_put_ushort(p, table->lowestRecPPEM);
58 p += sfnt_put_short(p, table->fontDirectionHint);
59 p += sfnt_put_short(p, table->indexToLocFormat);
60 p += sfnt_put_short(p, table->glyphDataFormat);
61 return data;
62 }
63
64 struct tt_head_table *tt_read_head_table(sfnt * sfont)
65 {
66 int i;
67 struct tt_head_table *table = NULL;
68 table = NEW(1, struct tt_head_table);
69 sfnt_locate_table(sfont, "head");
70 table->version = sfnt_get_ulong(sfont);
71 table->fontRevision = sfnt_get_ulong(sfont);
72 table->checkSumAdjustment = sfnt_get_ulong(sfont);
73 table->magicNumber = sfnt_get_ulong(sfont);
74 table->flags = sfnt_get_ushort(sfont);
75 table->unitsPerEm = sfnt_get_ushort(sfont);
76 for (i = 0; i < 8; i++) {
77 (table->created)[i] = sfnt_get_byte(sfont);
78 }
79 for (i = 0; i < 8; i++) {
80 (table->modified)[i] = sfnt_get_byte(sfont);
81 }
82 table->xMin = sfnt_get_short(sfont);
83 table->yMin = sfnt_get_short(sfont);
84 table->xMax = sfnt_get_short(sfont);
85 table->yMax = sfnt_get_short(sfont);
86 table->macStyle = (USHORT) sfnt_get_short(sfont);
87 table->lowestRecPPEM = (USHORT) sfnt_get_short(sfont);
88 table->fontDirectionHint = sfnt_get_short(sfont);
89 table->indexToLocFormat = sfnt_get_short(sfont);
90 table->glyphDataFormat = sfnt_get_short(sfont);
91 return table;
92 }
93
94 char *tt_pack_maxp_table(struct tt_maxp_table *table)
95 {
96 char *p, *data;
97 p = data = NEW(TT_MAXP_TABLE_SIZE, char);
98 p += sfnt_put_ulong(p, (LONG) table->version);
99 p += sfnt_put_ushort(p, table->numGlyphs);
100 p += sfnt_put_ushort(p, table->maxPoints);
101 p += sfnt_put_ushort(p, table->maxContours);
102 p += sfnt_put_ushort(p, table->maxComponentPoints);
103 p += sfnt_put_ushort(p, table->maxComponentContours);
104 p += sfnt_put_ushort(p, table->maxZones);
105 p += sfnt_put_ushort(p, table->maxTwilightPoints);
106 p += sfnt_put_ushort(p, table->maxStorage);
107 p += sfnt_put_ushort(p, table->maxFunctionDefs);
108 p += sfnt_put_ushort(p, table->maxInstructionDefs);
109 p += sfnt_put_ushort(p, table->maxStackElements);
110 p += sfnt_put_ushort(p, table->maxSizeOfInstructions);
111 p += sfnt_put_ushort(p, table->maxComponentElements);
112 p += sfnt_put_ushort(p, table->maxComponentDepth);
113 return data;
114 }
115
116 struct tt_maxp_table *tt_read_maxp_table(sfnt * sfont)
117 {
118 struct tt_maxp_table *table = NULL;
119 table = NEW(1, struct tt_maxp_table);
120 sfnt_locate_table(sfont, "maxp");
121 table->version = sfnt_get_ulong(sfont);
122 table->numGlyphs = sfnt_get_ushort(sfont);
123 table->maxPoints = sfnt_get_ushort(sfont);
124 table->maxContours = sfnt_get_ushort(sfont);
125 table->maxComponentPoints = sfnt_get_ushort(sfont);
126 table->maxComponentContours = sfnt_get_ushort(sfont);
127 table->maxZones = sfnt_get_ushort(sfont);
128 table->maxTwilightPoints = sfnt_get_ushort(sfont);
129 table->maxStorage = sfnt_get_ushort(sfont);
130 table->maxFunctionDefs = sfnt_get_ushort(sfont);
131 table->maxInstructionDefs = sfnt_get_ushort(sfont);
132 table->maxStackElements = sfnt_get_ushort(sfont);
133 table->maxSizeOfInstructions = sfnt_get_ushort(sfont);
134 table->maxComponentElements = sfnt_get_ushort(sfont);
135 table->maxComponentDepth = sfnt_get_ushort(sfont);
136 return table;
137 }
138
139 char *tt_pack_hhea_table(struct tt_hhea_table *table)
140 {
141 int i;
142 char *p, *data;
143 p = data = NEW(TT_HHEA_TABLE_SIZE, char);
144 p += sfnt_put_ulong(p, (LONG) table->version);
145 p += sfnt_put_short(p, table->Ascender);
146 p += sfnt_put_short(p, table->Descender);
147 p += sfnt_put_short(p, table->LineGap);
148 p += sfnt_put_ushort(p, table->advanceWidthMax);
149 p += sfnt_put_short(p, table->minLeftSideBearing);
150 p += sfnt_put_short(p, table->minRightSideBearing);
151 p += sfnt_put_short(p, table->xMaxExtent);
152 p += sfnt_put_short(p, table->caretSlopeRise);
153 p += sfnt_put_short(p, table->caretSlopeRun);
154 for (i = 0; i < 5; i++) {
155 p += sfnt_put_short(p, table->reserved[i]);
156 }
157 p += sfnt_put_short(p, table->metricDataFormat);
158 p += sfnt_put_ushort(p, table->numberOfHMetrics);
159 return data;
160 }
161
162 struct tt_hhea_table *tt_read_hhea_table(sfnt * sfont)
163 {
164 int i;
165 struct tt_hhea_table *table = NULL;
166 table = NEW(1, struct tt_hhea_table);
167 sfnt_locate_table(sfont, "hhea");
168 table->version = sfnt_get_ulong(sfont);
169 table->Ascender = sfnt_get_short(sfont);
170 table->Descender = sfnt_get_short(sfont);
171 table->LineGap = sfnt_get_short(sfont);
172 table->advanceWidthMax = sfnt_get_ushort(sfont);
173 table->minLeftSideBearing = sfnt_get_short(sfont);
174 table->minRightSideBearing = sfnt_get_short(sfont);
175 table->xMaxExtent = sfnt_get_short(sfont);
176 table->caretSlopeRise = sfnt_get_short(sfont);
177 table->caretSlopeRun = sfnt_get_short(sfont);
178 for (i = 0; i < 5; i++) {
179 table->reserved[i] = sfnt_get_short(sfont);
180 }
181 table->metricDataFormat = sfnt_get_short(sfont);
182 if (table->metricDataFormat != 0)
183 normal_error("ttf","unknown metricDaraFormat");
184 table->numberOfHMetrics = sfnt_get_ushort(sfont);
185 return table;
186 }
187
188 char *tt_pack_vhea_table(struct tt_vhea_table *table)
189 {
190 int i;
191 char *p, *data;
192 p = data = NEW(TT_VHEA_TABLE_SIZE, char);
193 p += sfnt_put_ulong(p, (LONG) table->version);
194 p += sfnt_put_short(p, table->vertTypoAscender);
195 p += sfnt_put_short(p, table->vertTypoDescender);
196 p += sfnt_put_short(p, table->vertTypoLineGap);
197 p += sfnt_put_short(p, table->advanceHeightMax);
198 p += sfnt_put_short(p, table->minTopSideBearing);
199 p += sfnt_put_short(p, table->minBottomSideBearing);
200 p += sfnt_put_short(p, table->yMaxExtent);
201 p += sfnt_put_short(p, table->caretSlopeRise);
202 p += sfnt_put_short(p, table->caretSlopeRun);
203 p += sfnt_put_short(p, table->caretOffset);
204 for (i = 0; i < 5; i++) {
205 p += sfnt_put_short(p, table->reserved[i]);
206 }
207 p += sfnt_put_ushort(p, table->numOfLongVerMetrics);
208 return data;
209 }
210
211 struct tt_vhea_table *tt_read_vhea_table(sfnt * sfont)
212 {
213 int i;
214 struct tt_vhea_table *table = NULL;
215 table = NEW(1, struct tt_vhea_table);
216 sfnt_locate_table(sfont, "vhea");
217 table->version = sfnt_get_ulong(sfont);
218 table->vertTypoAscender = sfnt_get_short(sfont);
219 table->vertTypoDescender = sfnt_get_short(sfont);
220 table->vertTypoLineGap = sfnt_get_short(sfont);
221 table->advanceHeightMax = sfnt_get_short(sfont);
222 table->minTopSideBearing = sfnt_get_short(sfont);
223 table->minBottomSideBearing = sfnt_get_short(sfont);
224 table->yMaxExtent = sfnt_get_short(sfont);
225 table->caretSlopeRise = sfnt_get_short(sfont);
226 table->caretSlopeRun = sfnt_get_short(sfont);
227 table->caretOffset = sfnt_get_short(sfont);
228 for (i = 0; i < 5; i++) {
229 (table->reserved)[i] = sfnt_get_short(sfont);
230 }
231 table->numOfLongVerMetrics = sfnt_get_ushort(sfont);
232 return table;
233 }
234
235 struct tt_VORG_table *tt_read_VORG_table(sfnt * sfont)
236 {
237 struct tt_VORG_table *vorg;
238 ULONG offset;
239 USHORT i;
240 offset = sfnt_find_table_pos(sfont, "VORG");
241 if (offset > 0) {
242 vorg = NEW(1, struct tt_VORG_table);
243 sfnt_locate_table(sfont, "VORG");
244 if (sfnt_get_ushort(sfont) != 1 || sfnt_get_ushort(sfont) != 0)
245 normal_error("ttf","unsupported VORG version");
246 vorg->defaultVertOriginY = sfnt_get_short(sfont);
247 vorg->numVertOriginYMetrics = sfnt_get_ushort(sfont);
248 vorg->vertOriginYMetrics = NEW(vorg->numVertOriginYMetrics, struct tt_vertOriginYMetrics);
249 /*tex
250 The |vertOriginYMetrics| array must be sorted in increasing |glyphIndex| order.
251 */
252 for (i = 0; i < vorg->numVertOriginYMetrics; i++) {
253 vorg->vertOriginYMetrics[i].glyphIndex = sfnt_get_ushort(sfont);
254 vorg->vertOriginYMetrics[i].vertOriginY = sfnt_get_short(sfont);
255 }
256 } else {
257 vorg = NULL;
258 }
259 return vorg;
260 }
261
262 /*tex
263
264 Reading and writing |hmtx| and |vmtx| depends on other tables, like |maxp|,
265 |hhea| and |vhea|.
266
267 */
268
269 struct tt_longMetrics *tt_read_longMetrics(sfnt * sfont, USHORT numGlyphs, USHORT numLongMetrics)
270 {
271 struct tt_longMetrics *m;
272 USHORT gid, last_adv = 0;
273 m = NEW(numGlyphs, struct tt_longMetrics);
274 for (gid = 0; gid < numGlyphs; gid++) {
275 if (gid < numLongMetrics)
276 last_adv = sfnt_get_ushort(sfont);
277 m[gid].sideBearing = sfnt_get_short(sfont);
278 m[gid].advance = last_adv;
279 }
280 return m;
281 }
282
283 /*tex
284
285 The |OS/2| table may not exist.
286
287 */
288
289 struct tt_os2__table *tt_read_os2__table(sfnt * sfont)
290 {
291 struct tt_os2__table *table = NULL;
292 int i;
293 if (sfnt_find_table_pos(sfont, "OS/2") == 0)
294 return NULL;
295 sfnt_locate_table(sfont, "OS/2");
296 table = NEW(1, struct tt_os2__table);
297 table->version = sfnt_get_ushort(sfont);
298 table->xAvgCharWidth = sfnt_get_short(sfont);
299 table->usWeightClass = sfnt_get_ushort(sfont);
300 table->usWidthClass = sfnt_get_ushort(sfont);
301 table->fsType = sfnt_get_short(sfont);
302 table->ySubscriptXSize = sfnt_get_short(sfont);
303 table->ySubscriptYSize = sfnt_get_short(sfont);
304 table->ySubscriptXOffset = sfnt_get_short(sfont);
305 table->ySubscriptYOffset = sfnt_get_short(sfont);
306 table->ySuperscriptXSize = sfnt_get_short(sfont);
307 table->ySuperscriptYSize = sfnt_get_short(sfont);
308 table->ySuperscriptXOffset = sfnt_get_short(sfont);
309 table->ySuperscriptYOffset = sfnt_get_short(sfont);
310 table->yStrikeoutSize = sfnt_get_short(sfont);
311 table->yStrikeoutPosition = sfnt_get_short(sfont);
312 table->sFamilyClass = sfnt_get_short(sfont);
313 for (i = 0; i < 10; i++) {
314 table->panose[i] = sfnt_get_byte(sfont);
315 }
316 table->ulUnicodeRange1 = sfnt_get_ulong(sfont);
317 table->ulUnicodeRange2 = sfnt_get_ulong(sfont);
318 table->ulUnicodeRange3 = sfnt_get_ulong(sfont);
319 table->ulUnicodeRange4 = sfnt_get_ulong(sfont);
320 for (i = 0; i < 4; i++) {
321 table->achVendID[i] = sfnt_get_char(sfont);
322 }
323 table->fsSelection = sfnt_get_ushort(sfont);
324 table->usFirstCharIndex = sfnt_get_ushort(sfont);
325 table->usLastCharIndex = sfnt_get_ushort(sfont);
326 table->sTypoAscender = sfnt_get_short(sfont);
327 table->sTypoDescender = sfnt_get_short(sfont);
328 table->sTypoLineGap = sfnt_get_short(sfont);
329 table->usWinAscent = sfnt_get_ushort(sfont);
330 table->usWinDescent = sfnt_get_ushort(sfont);
331 if (table->version >= 0x0001) {
332 table->ulCodePageRange1 = sfnt_get_ulong(sfont);
333 table->ulCodePageRange2 = sfnt_get_ulong(sfont);
334 }
335 if (table->version >= 0x0002) {
336 table->sxHeight = sfnt_get_short(sfont);
337 table->sCapHeight = sfnt_get_short(sfont);
338 table->usDefaultChar = sfnt_get_ushort(sfont);
339 table->usBreakChar = sfnt_get_ushort(sfont);
340 table->usMaxContext = sfnt_get_ushort(sfont);
341 }
342 /*
343 https://docs.microsoft.com/en-us/typography/opentype/spec/os2
344
345 There are now 5 versions but we don't read the few additional
346 fields as we don't use them. Some changes are cosmetical (names).
347 */
348 return table;
349 }
350
351 USHORT tt_get_name(sfnt * sfont, char *dest, USHORT destlen,
352 USHORT plat_id, USHORT enco_id, USHORT lang_id, USHORT name_id)
353 {
354 USHORT length = 0;
355 USHORT num_names, string_offset;
356 ULONG name_offset;
357 int i;
358 name_offset = sfnt_locate_table(sfont, "name");
359 if (sfnt_get_ushort(sfont))
360 normal_error("ttf","expecting zero");
361 num_names = sfnt_get_ushort(sfont);
362 string_offset = sfnt_get_ushort(sfont);
363 for (i = 0; i < num_names; i++) {
364 USHORT p_id, e_id, n_id, l_id;
365 USHORT offset;
366 p_id = sfnt_get_ushort(sfont);
367 e_id = sfnt_get_ushort(sfont);
368 l_id = sfnt_get_ushort(sfont);
369 n_id = sfnt_get_ushort(sfont);
370 length = sfnt_get_ushort(sfont);
371 offset = sfnt_get_ushort(sfont);
372 /*tex The language |ID| value |0xffffu| stands for ``accept any language ID''. */
373 if ((p_id == plat_id) && (e_id == enco_id) &&
374 (lang_id == 0xffffu || l_id == lang_id) && (n_id == name_id)) {
375 if (length > destlen - 1) {
376 normal_warning("ttf","truncating a very long name");
377 length = (USHORT) (destlen - 1);
378 }
379 sfnt_seek_set(sfont, (long) (name_offset + string_offset + offset));
380 sfnt_read((unsigned char *) dest, length, sfont);
381 dest[length] = '\0';
382 break;
383 }
384 }
385 if (i == num_names) {
386 length = 0;
387 }
388 return length;
389 }
390
391 USHORT tt_get_ps_fontname(sfnt * sfont, char *dest, USHORT destlen)
392 {
393 USHORT namelen = 0;
394 /*tex First try Mac-Roman PS name and then Win-Unicode PS name. */
395 if ((namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0, 6)) != 0 ||
396 (namelen = tt_get_name(sfont, dest, destlen, 3, 1, 0x409u, 6)) != 0 ||
397 (namelen = tt_get_name(sfont, dest, destlen, 3, 5, 0x412u, 6)) != 0)
398 return namelen;
399 normal_warning("ttf","no valid PostScript name available");
400 /*tex
401 This is a workaround for some bad TTfonts: the language ID value |0xffffu|
402 indicates ``accept any language ID''.
403 */
404 if ((namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0xffffu, 6)) == 0) {
405 /*tex
406 Finally we're falling back to Mac Roman name field. Some bad Japanese TTfonts
407 using SJIS encoded string in the Mac Roman name field.
408 */
409 namelen = tt_get_name(sfont, dest, destlen, 1, 0, 0, 1);
410 }
411 return namelen;
412 }