Debian Codegraph @

texk/dvipdfm-x/tt_glyf.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 /*
22 * Subsetting glyf, updating loca, hmtx, ...
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include "system.h"
31 #include "mem.h"
32 #include "error.h"
33 #include "dpxutil.h"
34
35 #include "sfnt.h"
36 #include "tt_table.h"
37 #include "tt_glyf.h"
38
39 #define NUM_GLYPH_LIMIT 65534
40 #define TABLE_DATA_ALLOC_SIZE 40960
41 #define GLYPH_ARRAY_ALLOC_SIZE 256
42
43 static USHORT
44 find_empty_slot (struct tt_glyphs *g)
45 {
46 USHORT gid;
47
48 ASSERT(g);
49
50 for (gid = 0; gid < NUM_GLYPH_LIMIT; gid++) {
51 if (!(g->used_slot[gid/8] & (1 << (7 - (gid % 8)))))
52 break;
53 }
54 if (gid == NUM_GLYPH_LIMIT)
55 ERROR("No empty glyph slot available.");
56
57 return gid;
58 }
59
60 USHORT
61 tt_find_glyph (struct tt_glyphs *g, USHORT gid)
62 {
63 USHORT idx, new_gid = 0;
64
65 ASSERT(g);
66
67 for (idx = 0; idx < g->num_glyphs; idx++) {
68 if (gid == g->gd[idx].ogid) {
69 new_gid = g->gd[idx].gid;
70 break;
71 }
72 }
73
74 return new_gid;
75 }
76
77 USHORT
78 tt_get_index (struct tt_glyphs *g, USHORT gid)
79 {
80 USHORT idx;
81
82 ASSERT(g);
83
84 for (idx = 0; idx < g->num_glyphs; idx++) {
85 if (gid == g->gd[idx].gid)
86 break;
87 }
88 if (idx == g->num_glyphs)
89 idx = 0;
90
91 return idx;
92 }
93
94 USHORT
95 tt_add_glyph (struct tt_glyphs *g, USHORT gid, USHORT new_gid)
96 {
97 ASSERT(g);
98
99 if (g->used_slot[new_gid/8] & (1 << (7 - (new_gid % 8)))) {
100 WARN("Slot %u already used.", new_gid);
101 } else {
102 if (g->num_glyphs+1 >= NUM_GLYPH_LIMIT)
103 ERROR("Too many glyphs.");
104
105 if (g->num_glyphs >= g->max_glyphs) {
106 g->max_glyphs += GLYPH_ARRAY_ALLOC_SIZE;
107 g->gd = RENEW(g->gd, g->max_glyphs, struct tt_glyph_desc);
108 }
109 g->gd[g->num_glyphs].gid = new_gid;
110 g->gd[g->num_glyphs].ogid = gid;
111 g->gd[g->num_glyphs].length = 0;
112 g->gd[g->num_glyphs].data = NULL;
113 g->used_slot[new_gid/8] |= (1 << (7 - (new_gid % 8)));
114 g->num_glyphs += 1;
115 }
116
117 if (new_gid > g->last_gid) {
118 g->last_gid = new_gid;
119 }
120
121 return new_gid;
122 }
123
124 /*
125 * Initialization
126 */
127 struct tt_glyphs *
128 tt_build_init (void)
129 {
130 struct tt_glyphs *g;
131
132 g = NEW(1, struct tt_glyphs);
133
134 g->num_glyphs = 0;
135 g->max_glyphs = 0;
136 g->last_gid = 0;
137 g->emsize = 1;
138 g->default_advh = 0;
139 g->default_tsb = 0;
140 g->gd = NULL;
141 g->used_slot = NEW(8192, unsigned char);
142 memset(g->used_slot, 0, 8192);
143 tt_add_glyph(g, 0, 0);
144
145 return g;
146 }
147
148 void
149 tt_build_finish (struct tt_glyphs *g)
150 {
151 if (g) {
152 if (g->gd) {
153 USHORT idx;
154 for (idx = 0; idx < g->num_glyphs; idx++) {
155 if (g->gd[idx].data)
156 RELEASE(g->gd[idx].data);
157 }
158 RELEASE(g->gd);
159 }
160 if (g->used_slot)
161 RELEASE(g->used_slot);
162 RELEASE(g);
163 }
164 }
165
166 static inline int
167 glyf_cmp (const void *v1, const void *v2)
168 {
169 int cmp = 0;
170 const struct tt_glyph_desc *sv1, *sv2;
171
172 sv1 = (const struct tt_glyph_desc *) v1;
173 sv2 = (const struct tt_glyph_desc *) v2;
174
175 if (sv1->gid == sv2->gid)
176 cmp = 0;
177 else if (sv1->gid < sv2->gid)
178 cmp = -1;
179 else
180 cmp = 1;
181
182 return cmp;
183 }
184
185 /*
186 * TrueType outline data.
187 */
188 #define ARG_1_AND_2_ARE_WORDS (1 << 0)
189 #define ARGS_ARE_XY_VALUES (1 << 1)
190 #define ROUND_XY_TO_GRID (1 << 2)
191 #define WE_HAVE_A_SCALE (1 << 3)
192 #define RESERVED (1 << 4)
193 #define MORE_COMPONENT (1 << 5)
194 #define WE_HAVE_AN_X_AND_Y_SCALE (1 << 6)
195 #define WE_HAVE_A_TWO_BY_TWO (1 << 7)
196 #define WE_HAVE_INSTRUCTIONS (1 << 8)
197 #define USE_MY_METRICS (1 << 9)
198
199 int
200 tt_build_tables (sfnt *sfont, struct tt_glyphs *g)
201 {
202 char *hmtx_table_data = NULL, *loca_table_data = NULL;
203 char *glyf_table_data = NULL;
204 ULONG hmtx_table_size, loca_table_size, glyf_table_size;
205 /* some information available from other TrueType table */
206 struct tt_head_table *head = NULL;
207 struct tt_hhea_table *hhea = NULL;
208 struct tt_maxp_table *maxp = NULL;
209 struct tt_longMetrics *hmtx, *vmtx = NULL;
210 struct tt_os2__table *os2;
211 /* temp */
212 ULONG *location, offset;
213 int i;
214 USHORT *w_stat; /* Estimate most frequently appeared width */
215
216 ASSERT(g);
217
218 if (sfont == NULL || sfont->stream == NULL)
219 ERROR("File not opened.");
220
221 if (sfont->type != SFNT_TYPE_TRUETYPE &&
222 sfont->type != SFNT_TYPE_TTC &&
223 sfont->type != SFNT_TYPE_DFONT)
224 ERROR("Invalid font type");
225
226 if (g->num_glyphs > NUM_GLYPH_LIMIT)
227 ERROR("Too many glyphs.");
228
229 /*
230 * Read head, hhea, maxp, loca:
231 *
232 * unitsPerEm --> head
233 * numHMetrics --> hhea
234 * indexToLocFormat --> head
235 * numGlyphs --> maxp
236 */
237 head = tt_read_head_table(sfont);
238 hhea = tt_read_hhea_table(sfont);
239 maxp = tt_read_maxp_table(sfont);
240
241 if (hhea->metricDataFormat != 0)
242 ERROR("Unknown metricDataFormat.");
243
244 g->emsize = head->unitsPerEm;
245
246 sfnt_locate_table(sfont, "hmtx");
247 hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numOfLongHorMetrics, hhea->numOfExSideBearings);
248
249 os2 = tt_read_os2__table(sfont);
250 if (os2) {
251 g->default_advh = os2->sTypoAscender - os2->sTypoDescender;
252 g->default_tsb = g->default_advh - os2->sTypoAscender;
253 }
254
255 if (sfnt_find_table_pos(sfont, "vmtx") > 0) {
256 struct tt_vhea_table *vhea;
257 vhea = tt_read_vhea_table(sfont);
258 sfnt_locate_table(sfont, "vmtx");
259 vmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics, vhea->numOfExSideBearings);
260 RELEASE(vhea);
261 } else {
262 vmtx = NULL;
263 }
264
265 sfnt_locate_table(sfont, "loca");
266 location = NEW(maxp->numGlyphs + 1, ULONG);
267 if (head->indexToLocFormat == 0) {
268 for (i = 0; i <= maxp->numGlyphs; i++)
269 location[i] = 2*((ULONG) sfnt_get_ushort(sfont));
270 } else if (head->indexToLocFormat == 1) {
271 for (i = 0; i <= maxp->numGlyphs; i++)
272 location[i] = sfnt_get_ulong(sfont);
273 } else {
274 ERROR("Unknown IndexToLocFormat.");
275 }
276
277 w_stat = NEW(g->emsize+2, USHORT);
278 memset(w_stat, 0, sizeof(USHORT)*(g->emsize+2));
279 /*
280 * Read glyf table.
281 */
282 offset = sfnt_locate_table(sfont, "glyf");
283 /*
284 * The num_glyphs may grow when composite glyph is found.
285 * A component of glyph refered by a composite glyph is appended
286 * to used_glyphs if it is not already registered in used_glyphs.
287 * Glyph programs of composite glyphs are modified so that it
288 * correctly refer to new gid of their components.
289 */
290 for (i = 0; i < NUM_GLYPH_LIMIT; i++) {
291 USHORT gid; /* old gid */
292 ULONG loc, len;
293 BYTE *p, *endptr;
294 SHORT number_of_contours;
295
296 if (i >= g->num_glyphs) /* finished */
297 break;
298
299 gid = g->gd[i].ogid;
300 if (gid >= maxp->numGlyphs)
301 ERROR("Invalid glyph index (gid %u)", gid);
302
303 loc = location[gid];
304 len = location[gid+1] - loc;
305 g->gd[i].advw = hmtx[gid].advance;
306 g->gd[i].lsb = hmtx[gid].sideBearing;
307 if (vmtx) {
308 g->gd[i].advh = vmtx[gid].advance;
309 g->gd[i].tsb = vmtx[gid].sideBearing;
310 } else {
311 g->gd[i].advh = g->default_advh;
312 g->gd[i].tsb = g->default_tsb;
313 }
314 g->gd[i].length = len;
315 g->gd[i].data = NULL;
316 if (g->gd[i].advw <= g->emsize) {
317 w_stat[g->gd[i].advw] += 1;
318 } else {
319 w_stat[g->emsize+1] += 1; /* larger than em */
320 }
321
322 if (len == 0) { /* Does not contains any data. */
323 continue;
324 } else if (len < 10) {
325 ERROR("Invalid TrueType glyph data (gid %u).", gid);
326 }
327
328 g->gd[i].data = p = NEW(len, BYTE);
329 endptr = p + len;
330
331 sfnt_seek_set(sfont, offset+loc);
332 number_of_contours = sfnt_get_short(sfont);
333 p += sfnt_put_short(p, number_of_contours);
334
335 /* BoundingBox: FWord x 4 */
336 g->gd[i].llx = sfnt_get_short(sfont);
337 g->gd[i].lly = sfnt_get_short(sfont);
338 g->gd[i].urx = sfnt_get_short(sfont);
339 g->gd[i].ury = sfnt_get_short(sfont);
340 /* _FIXME_ */
341 #if 1
342 if (!vmtx) /* vertOriginY == sTypeAscender */
343 g->gd[i].tsb = g->default_advh - g->default_tsb - g->gd[i].ury;
344 #endif
345 p += sfnt_put_short(p, g->gd[i].llx);
346 p += sfnt_put_short(p, g->gd[i].lly);
347 p += sfnt_put_short(p, g->gd[i].urx);
348 p += sfnt_put_short(p, g->gd[i].ury);
349
350 /* Read evrything else. */
351 sfnt_read(p, len - 10, sfont);
352 /*
353 * Fix GIDs of composite glyphs.
354 */
355 if (number_of_contours < 0) {
356 USHORT flags, cgid, new_gid; /* flag, gid of a component */
357 do {
358 if (p >= endptr)
359 ERROR("Invalid TrueType glyph data (gid %u): %u bytes", gid, len);
360 /*
361 * Flags and gid of component glyph are both USHORT.
362 */
363 flags = ((*p) << 8)| *(p+1);
364 p += 2;
365 cgid = ((*p) << 8)| *(p+1);
366 if (cgid >= maxp->numGlyphs) {
367 ERROR("Invalid gid (%u > %u) in composite glyph %u.", cgid, maxp->numGlyphs, gid);
368 }
369 new_gid = tt_find_glyph(g, cgid);
370 if (new_gid == 0) {
371 new_gid = tt_add_glyph(g, cgid, find_empty_slot(g));
372 }
373 p += sfnt_put_ushort(p, new_gid);
374 /*
375 * Just skip remaining part.
376 */
377 p += (flags & ARG_1_AND_2_ARE_WORDS) ? 4 : 2;
378 if (flags & WE_HAVE_A_SCALE) /* F2Dot14 */
379 p += 2;
380 else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) /* F2Dot14 x 2 */
381 p += 4;
382 else if (flags & WE_HAVE_A_TWO_BY_TWO) /* F2Dot14 x 4 */
383 p += 8;
384 } while (flags & MORE_COMPONENT);
385 /*
386 * TrueType instructions comes here:
387 * length_of_instruction (ushort)
388 * instruction (byte * length_of_instruction)
389 */
390 }
391 }
392 RELEASE(location);
393 RELEASE(hmtx);
394 if (vmtx)
395 RELEASE(vmtx);
396
397 {
398 int max_count = -1;
399
400 g->dw = g->gd[0].advw;
401 for (i = 0; i < g->emsize + 1; i++) {
402 if (w_stat[i] > max_count) {
403 max_count = w_stat[i];
404 g->dw = i;
405 }
406 }
407 }
408 RELEASE(w_stat);
409
410 qsort(g->gd, g->num_glyphs, sizeof(struct tt_glyph_desc), glyf_cmp);
411 {
412 USHORT prev, last_advw;
413 char *p, *q;
414 int padlen, num_hm_known;
415
416 glyf_table_size = 0UL;
417 num_hm_known = 0;
418 last_advw = g->gd[g->num_glyphs - 1].advw;
419 for (i = g->num_glyphs - 1; i >= 0; i--) {
420 padlen = (g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0;
421 glyf_table_size += g->gd[i].length + padlen;
422 if (!num_hm_known && last_advw != g->gd[i].advw) {
423 hhea->numOfLongHorMetrics = g->gd[i].gid + 2;
424 num_hm_known = 1;
425 }
426 }
427 /* All advance widths are same. */
428 if (!num_hm_known) {
429 hhea->numOfLongHorMetrics = 1;
430 }
431 hmtx_table_size = hhea->numOfLongHorMetrics * 2 + (g->last_gid + 1) * 2;
432
433 /*
434 * Choosing short format does not always give good result
435 * when compressed. Sometimes increases size.
436 */
437 if (glyf_table_size < 0x20000UL) {
438 head->indexToLocFormat = 0;
439 loca_table_size = (g->last_gid + 2)*2;
440 } else {
441 head->indexToLocFormat = 1;
442 loca_table_size = (g->last_gid + 2)*4;
443 }
444
445 hmtx_table_data = p = NEW(hmtx_table_size, char);
446 loca_table_data = q = NEW(loca_table_size, char);
447 glyf_table_data = NEW(glyf_table_size, char);
448
449 offset = 0UL; prev = 0;
450 for (i = 0; i < g->num_glyphs; i++) {
451 int gap, j;
452 gap = g->gd[i].gid - prev - 1;
453 for (j = 1; j <= gap; j++) {
454 if (prev + j == hhea->numOfLongHorMetrics - 1) {
455 p += sfnt_put_ushort(p, last_advw);
456 } else if (prev + j < hhea->numOfLongHorMetrics) {
457 p += sfnt_put_ushort(p, 0);
458 }
459 p += sfnt_put_short (p, 0);
460 if (head->indexToLocFormat == 0) {
461 q += sfnt_put_ushort(q, (USHORT) (offset/2));
462 } else {
463 q += sfnt_put_ulong(q, offset);
464 }
465 }
466 padlen = (g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0;
467 if (g->gd[i].gid < hhea->numOfLongHorMetrics) {
468 p += sfnt_put_ushort(p, g->gd[i].advw);
469 }
470 p += sfnt_put_short (p, g->gd[i].lsb);
471 if (head->indexToLocFormat == 0) {
472 q += sfnt_put_ushort(q, (USHORT) (offset/2));
473 } else {
474 q += sfnt_put_ulong(q, offset);
475 }
476 memset(glyf_table_data + offset, 0, g->gd[i].length + padlen);
477 memcpy(glyf_table_data + offset, g->gd[i].data, g->gd[i].length);
478 offset += g->gd[i].length + padlen;
479 prev = g->gd[i].gid;
480 /* free data here since it consume much memory */
481 RELEASE(g->gd[i].data);
482 g->gd[i].length = 0; g->gd[i].data = NULL;
483 }
484 if (head->indexToLocFormat == 0) {
485 q += sfnt_put_ushort(q, (USHORT) (offset/2));
486 } else {
487 q += sfnt_put_ulong(q, offset);
488 }
489
490 sfnt_set_table(sfont, "hmtx", (char *) hmtx_table_data, hmtx_table_size);
491 sfnt_set_table(sfont, "loca", (char *) loca_table_data, loca_table_size);
492 sfnt_set_table(sfont, "glyf", (char *) glyf_table_data, glyf_table_size);
493 }
494
495 head->checkSumAdjustment = 0;
496 maxp->numGlyphs = g->last_gid + 1;
497
498 /* TODO */
499 sfnt_set_table(sfont, "maxp", tt_pack_maxp_table(maxp), TT_MAXP_TABLE_SIZE);
500 sfnt_set_table(sfont, "hhea", tt_pack_hhea_table(hhea), TT_HHEA_TABLE_SIZE);
501 sfnt_set_table(sfont, "head", tt_pack_head_table(head), TT_HEAD_TABLE_SIZE);
502 RELEASE(maxp);
503 RELEASE(hhea);
504 RELEASE(head);
505 if (os2)
506 RELEASE(os2);
507
508 return 0;
509 }
510
511 int
512 tt_get_metrics (sfnt *sfont, struct tt_glyphs *g)
513 {
514 struct tt_head_table *head = NULL;
515 struct tt_hhea_table *hhea = NULL;
516 struct tt_maxp_table *maxp = NULL;
517 struct tt_longMetrics *hmtx, *vmtx = NULL;
518 struct tt_os2__table *os2;
519 /* temp */
520 ULONG *location, offset;
521 int i;
522 USHORT *w_stat;
523
524 ASSERT(g);
525
526 if (sfont == NULL || sfont->stream == NULL)
527 ERROR("File not opened.");
528
529 if (sfont->type != SFNT_TYPE_TRUETYPE &&
530 sfont->type != SFNT_TYPE_TTC &&
531 sfont->type != SFNT_TYPE_DFONT)
532 ERROR("Invalid font type");
533
534 /*
535 * Read head, hhea, maxp, loca:
536 *
537 * unitsPerEm --> head
538 * numHMetrics --> hhea
539 * indexToLocFormat --> head
540 * numGlyphs --> maxp
541 */
542 head = tt_read_head_table(sfont);
543 hhea = tt_read_hhea_table(sfont);
544 maxp = tt_read_maxp_table(sfont);
545
546 if (hhea->metricDataFormat != 0)
547 ERROR("Unknown metricDataFormat.");
548
549 g->emsize = head->unitsPerEm;
550
551 sfnt_locate_table(sfont, "hmtx");
552 hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numOfLongHorMetrics, hhea->numOfExSideBearings);
553
554 os2 = tt_read_os2__table(sfont);
555 g->default_advh = os2->sTypoAscender - os2->sTypoDescender;
556 g->default_tsb = g->default_advh - os2->sTypoAscender;
557
558 if (sfnt_find_table_pos(sfont, "vmtx") > 0) {
559 struct tt_vhea_table *vhea;
560 vhea = tt_read_vhea_table(sfont);
561 sfnt_locate_table(sfont, "vmtx");
562 vmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics, vhea->numOfExSideBearings);
563 RELEASE(vhea);
564 } else {
565 vmtx = NULL;
566 }
567
568 sfnt_locate_table(sfont, "loca");
569 location = NEW(maxp->numGlyphs + 1, ULONG);
570 if (head->indexToLocFormat == 0) {
571 for (i = 0; i <= maxp->numGlyphs; i++)
572 location[i] = 2*((ULONG) sfnt_get_ushort(sfont));
573 } else if (head->indexToLocFormat == 1) {
574 for (i = 0; i <= maxp->numGlyphs; i++)
575 location[i] = sfnt_get_ulong(sfont);
576 } else {
577 ERROR("Unknown IndexToLocFormat.");
578 }
579
580 w_stat = NEW(g->emsize+2, USHORT);
581 memset(w_stat, 0, sizeof(USHORT)*(g->emsize+2));
582 /*
583 * Read glyf table.
584 */
585 offset = sfnt_locate_table(sfont, "glyf");
586 for (i = 0; i < g->num_glyphs; i++) {
587 USHORT gid; /* old gid */
588 ULONG loc, len;
589
590 gid = g->gd[i].ogid;
591 if (gid >= maxp->numGlyphs)
592 ERROR("Invalid glyph index (gid %u)", gid);
593
594 loc = location[gid];
595 len = location[gid+1] - loc;
596 g->gd[i].advw = hmtx[gid].advance;
597 g->gd[i].lsb = hmtx[gid].sideBearing;
598 if (vmtx) {
599 g->gd[i].advh = vmtx[gid].advance;
600 g->gd[i].tsb = vmtx[gid].sideBearing;
601 } else {
602 g->gd[i].advh = g->default_advh;
603 g->gd[i].tsb = g->default_tsb;
604 }
605 g->gd[i].length = len;
606 g->gd[i].data = NULL;
607
608 if (g->gd[i].advw <= g->emsize) {
609 w_stat[g->gd[i].advw] += 1;
610 } else {
611 w_stat[g->emsize+1] += 1; /* larger than em */
612 }
613
614 if (len == 0) { /* Does not contains any data. */
615 continue;
616 } else if (len < 10) {
617 ERROR("Invalid TrueType glyph data (gid %u).", gid);
618 }
619
620 sfnt_seek_set(sfont, offset+loc);
621 (void) sfnt_get_short(sfont);
622
623 /* BoundingBox: FWord x 4 */
624 g->gd[i].llx = sfnt_get_short(sfont);
625 g->gd[i].lly = sfnt_get_short(sfont);
626 g->gd[i].urx = sfnt_get_short(sfont);
627 g->gd[i].ury = sfnt_get_short(sfont);
628 /* _FIXME_ */
629 #if 1
630 if (!vmtx) /* vertOriginY == sTypeAscender */
631 g->gd[i].tsb = g->default_advh - g->default_tsb - g->gd[i].ury;
632 #endif
633 }
634 RELEASE(location);
635 RELEASE(hmtx);
636 RELEASE(maxp);
637 RELEASE(hhea);
638 RELEASE(head);
639 RELEASE(os2);
640
641 if (vmtx)
642 RELEASE(vmtx);
643
644 {
645 int max_count = -1;
646
647 g->dw = g->gd[0].advw;
648 for (i = 0; i < g->emsize + 1; i++) {
649 if (w_stat[i] > max_count) {
650 max_count = w_stat[i];
651 g->dw = i;
652 }
653 }
654 }
655 RELEASE(w_stat);
656
657
658 return 0;
659 }