File: tt_glyf.w

package info (click to toggle)
texlive-bin 2018.20181218.49446-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 186,920 kB
  • sloc: ansic: 873,264; cpp: 311,278; perl: 82,918; sh: 23,243; makefile: 8,590; lex: 4,939; python: 4,462; pascal: 3,813; java: 3,569; yacc: 2,901; tcl: 2,379; exp: 2,031; xml: 844; ruby: 678; lisp: 398; sed: 331; asm: 140; csh: 46; awk: 30
file content (695 lines) | stat: -rw-r--r-- 21,472 bytes parent folder | download | duplicates (2)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
% tt_glyf.w
%
% Copyright 2002 by Jin-Hwan Cho and Shunsaku Hirata,
% the dvipdfmx project team <dvipdfmx@@project.ktug.or.kr>
% Copyright 2006-2012 Taco Hoekwater <taco@@luatex.org>
%
% This file is part of LuaTeX.
%
% LuaTeX is free software; you can redistribute it and/or modify it under
% the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% LuaTeX is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License along
% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

@* Subsetting glyf, updating loca, hmtx, etc.

@ @c


#include "ptexlib.h"

#include "font/sfnt.h"
#include "font/tt_table.h"
#include "font/tt_glyf.h"
#include "font/writettf.h"

@ @c
#define NUM_GLYPH_LIMIT        65534
#define TABLE_DATA_ALLOC_SIZE  40960
#define GLYPH_ARRAY_ALLOC_SIZE 256

static USHORT find_empty_slot(struct tt_glyphs *g)
{
    USHORT gid;

    ASSERT(g);

    for (gid = 0; gid < NUM_GLYPH_LIMIT; gid++) {
        if (!(g->used_slot[gid / 8] & (1 << (7 - (gid % 8)))))
            break;
    }
    if (gid == NUM_GLYPH_LIMIT)
        normal_error("ttf","no empty glyph slot available.");

    return gid;
}

USHORT tt_find_glyph(struct tt_glyphs * g, USHORT gid)
{
    USHORT idx, new_gid = 0;

    ASSERT(g);

    for (idx = 0; idx < g->num_glyphs; idx++) {
        if (gid == g->gd[idx].ogid) {
            new_gid = g->gd[idx].gid;
            break;
        }
    }

    return new_gid;
}

USHORT tt_get_index(struct tt_glyphs * g, USHORT gid)
{
    USHORT idx;

    ASSERT(g);

    for (idx = 0; idx < g->num_glyphs; idx++) {
        if (gid == g->gd[idx].gid)
            break;
    }
    if (idx == g->num_glyphs)
        idx = 0;

    return idx;
}

USHORT tt_add_glyph(struct tt_glyphs * g, USHORT gid, USHORT new_gid)
{
    ASSERT(g);

    if (g->used_slot[new_gid / 8] & (1 << (7 - (new_gid % 8)))) {
        formatted_warning("ttf","slot %u already used", new_gid);
    } else {
        if (g->num_glyphs + 1 >= NUM_GLYPH_LIMIT)
            normal_error("ttf","too many glyphs");

        if (g->num_glyphs >= g->max_glyphs) {
            g->max_glyphs = (USHORT) (g->max_glyphs + GLYPH_ARRAY_ALLOC_SIZE);
            g->gd = RENEW(g->gd, g->max_glyphs, struct tt_glyph_desc);
        }
        g->gd[g->num_glyphs].gid = new_gid;
        g->gd[g->num_glyphs].ogid = gid;
        g->gd[g->num_glyphs].length = 0;
        g->gd[g->num_glyphs].data = NULL;
        g->used_slot[new_gid / 8] =
            (unsigned char) (g->used_slot[new_gid /
                                          8] | (1 << (7 - (new_gid % 8))));
        g->num_glyphs++;
    }

    if (new_gid > g->last_gid) {
        g->last_gid = new_gid;
    }

    return new_gid;
}


@ Initialization
@c
struct tt_glyphs *tt_build_init(void)
{
    struct tt_glyphs *g;

    g = NEW(1, struct tt_glyphs);

    g->num_glyphs = 0;
    g->max_glyphs = 0;
    g->last_gid = 0;
    g->emsize = 1;
    g->default_advh = 0;
    g->default_tsb = 0;
    g->gd = NULL;
    g->used_slot = NEW(8192, unsigned char);
    memset(g->used_slot, 0, 8192);
    tt_add_glyph(g, 0, 0);

    return g;
}

void tt_build_finish(struct tt_glyphs *g)
{
    if (g) {
        if (g->gd) {
            USHORT idx;
            for (idx = 0; idx < g->num_glyphs; idx++) {
                if (g->gd[idx].data)
                    RELEASE(g->gd[idx].data);
            }
            RELEASE(g->gd);
        }
        if (g->used_slot)
            RELEASE(g->used_slot);
        RELEASE(g);
    }
}

static int glyf_cmp(const void *v1, const void *v2)
{
    int cmp = 0;
    const struct tt_glyph_desc *sv1, *sv2;

    sv1 = (const struct tt_glyph_desc *) v1;
    sv2 = (const struct tt_glyph_desc *) v2;

    if (sv1->gid == sv2->gid)
        cmp = 0;
    else if (sv1->gid < sv2->gid)
        cmp = -1;
    else
        cmp = 1;

    return cmp;
}

@ @c
int tt_build_tables(sfnt * sfont, struct tt_glyphs *g, fd_entry * fd)
{
    char *hmtx_table_data = NULL, *loca_table_data = NULL;
    char *glyf_table_data = NULL;
    ULONG hmtx_table_size, loca_table_size, glyf_table_size, glyf_table_used;
    /* some information available from other TrueType table */
    struct tt_head_table *head = NULL;
    struct tt_hhea_table *hhea = NULL;
    struct tt_maxp_table *maxp = NULL;
    struct tt_longMetrics *hmtx, *vmtx = NULL;
    struct tt_os2__table *os2;
    /* temp */
    ULONG *location, offset;
    long i;
    USHORT *w_stat;             /* Estimate most frequently appeared width */

    int tex_font = fd->tex_font;
    int streamprovider = 0;
    int callback_id = 0 ;
    if ((tex_font > 0) && (font_streamprovider(tex_font) == 2)) {
        streamprovider = font_streamprovider(tex_font);
        callback_id = callback_defined(glyph_stream_provider_callback);
    }

    ASSERT(g);

    if (sfont->type != SFNT_TYPE_TRUETYPE && sfont->type != SFNT_TYPE_TTC)
        normal_error("ttf","invalid font type");

    if (g->num_glyphs > NUM_GLYPH_LIMIT)
        normal_error("ttf","too many glyphs");

    /*
     Read head, hhea, maxp, loca:

     unitsPerEm       --> head

     numHMetrics      --> hhea

     indexToLocFormat --> head

     numGlyphs        --> maxp
     */
    head = tt_read_head_table(sfont);
    hhea = tt_read_hhea_table(sfont);
    maxp = tt_read_maxp_table(sfont);

    if (hhea->metricDataFormat != 0)
        normal_error("ttf","unknown metricDataFormat");

    g->emsize = head->unitsPerEm;

    sfnt_locate_table(sfont, "hmtx");
    hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numberOfHMetrics);

    os2 = tt_read_os2__table(sfont);
    if (os2) {
        g->default_advh = (USHORT) (os2->sTypoAscender - os2->sTypoDescender);
        g->default_tsb = (SHORT) (g->default_advh - os2->sTypoAscender);

        /* dvipdfmx does this elsewhere! */
        fd_cur->font_dim[STEMV_CODE].val =
            (os2->usWeightClass / 65) * (os2->usWeightClass / 65) + 50;
    }

    if (sfnt_find_table_pos(sfont, "vmtx") > 0) {
        struct tt_vhea_table *vhea;
        vhea = tt_read_vhea_table(sfont);
        sfnt_locate_table(sfont, "vmtx");
        vmtx =
            tt_read_longMetrics(sfont, maxp->numGlyphs,
                                vhea->numOfLongVerMetrics);
        RELEASE(vhea);
    } else {
        vmtx = NULL;
    }

    sfnt_locate_table(sfont, "loca");
    location = NEW(maxp->numGlyphs + 1, ULONG);
    if (head->indexToLocFormat == 0) {
        for (i = 0; i <= maxp->numGlyphs; i++)
            location[i] = 2 * ((ULONG) sfnt_get_ushort(sfont));
    } else if (head->indexToLocFormat == 1) {
        for (i = 0; i <= maxp->numGlyphs; i++)
            location[i] = sfnt_get_ulong(sfont);
    } else {
        normal_error("ttf","unknown IndexToLocFormat");
    }

    w_stat = NEW(g->emsize + 2, USHORT);
    memset(w_stat, 0,
           (size_t) (sizeof(USHORT) * ((long unsigned) g->emsize + 2)));
    /*
     * Read glyf table.
     */
    offset = sfnt_locate_table(sfont, "glyf");
    /*
     The |num_glyphs| may grow when composite glyph is found.
     A component of glyph refered by a composite glyph is appended
     to |used_glyphs| if it is not already registered in |used_glyphs|.
     Glyph programs of composite glyphs are modified so that it
     correctly refer to new gid of their components.
     */
    for (i = 0; i < NUM_GLYPH_LIMIT; i++) {
        USHORT gid;             /* old gid */
        ULONG loc, len;
        BYTE *p, *endptr;
        SHORT number_of_contours;

        if (i >= g->num_glyphs) /* finished */
            break;

        gid = g->gd[i].ogid;
        if (gid >= maxp->numGlyphs)
            formatted_error("ttf","invalid glyph index (gid %u)", gid);

        loc = location[gid];
        len = location[gid + 1] - loc;
        g->gd[i].advw = hmtx[gid].advance;
        g->gd[i].lsb = hmtx[gid].sideBearing;
        if (vmtx) {
            g->gd[i].advh = vmtx[gid].advance;
            g->gd[i].tsb = vmtx[gid].sideBearing;
        } else {
            g->gd[i].advh = g->default_advh;
            g->gd[i].tsb = g->default_tsb;
        }
        g->gd[i].length = len;
        g->gd[i].data = NULL;
        if (g->gd[i].advw <= g->emsize) {
            w_stat[g->gd[i].advw]++;
        } else {
            w_stat[g->emsize + 1]++;    /* larger than em */
        }

        if (len == 0) {         /* Does not contains any data. */
            continue;
        } else if (len < 10) {
            formatted_error("ttf","invalid glyph data (gid %u)", gid);
        }

/* todo: no need for this */
        g->gd[i].data = p = NEW(len, BYTE);
        endptr = p + len;

        sfnt_seek_set(sfont, (long) (offset + loc));
        number_of_contours = sfnt_get_short(sfont);
        p += sfnt_put_short(p, number_of_contours);

        /* BoundingBox: FWord x 4 */
        g->gd[i].llx = sfnt_get_short(sfont);
        g->gd[i].lly = sfnt_get_short(sfont);
        g->gd[i].urx = sfnt_get_short(sfont);
        g->gd[i].ury = sfnt_get_short(sfont);
        /* |_FIXME_| */
#if  1
        if (!vmtx)              /* |vertOriginY == sTypeAscender| */
            g->gd[i].tsb =
                (SHORT) (g->default_advh - g->default_tsb - g->gd[i].ury);
#endif
        p += sfnt_put_short(p, g->gd[i].llx);
        p += sfnt_put_short(p, g->gd[i].lly);
        p += sfnt_put_short(p, g->gd[i].urx);
        p += sfnt_put_short(p, g->gd[i].ury);

        /* Read evrything else. */
        sfnt_read(p, (int) len - 10, sfont);
        /*
         Fix GIDs of composite glyphs.
         */
        if (number_of_contours < 0) {
            USHORT flags, cgid, new_gid;        /* flag, gid of a component */
            do {
                if (p >= endptr)
                    formatted_error("ttf","invalid glyph data (gid %u): %u bytes", gid, (unsigned int) len);
                /*
                 * Flags and gid of component glyph are both USHORT.
                 */
                flags = (USHORT) (((*p) << 8) | *(p + 1));
                p += 2;
                cgid = (USHORT) (((*p) << 8) | *(p + 1));
                if (cgid >= maxp->numGlyphs) {
                    formatted_error("ttf","invalid gid (%u > %u) in composite glyph %u", cgid, maxp->numGlyphs, gid);
                }
                new_gid = tt_find_glyph(g, cgid);
                if (new_gid == 0) {
                    new_gid = tt_add_glyph(g, cgid, find_empty_slot(g));
                }
                p += sfnt_put_ushort(p, new_gid);
                /*
                 * Just skip remaining part.
                 */
                p += (flags & ARG_1_AND_2_ARE_WORDS) ? 4 : 2;
                if (flags & WE_HAVE_A_SCALE)    /* F2Dot14 */
                    p += 2;
                else if (flags & WE_HAVE_AN_X_AND_Y_SCALE)      /* F2Dot14 x 2 */
                    p += 4;
                else if (flags & WE_HAVE_A_TWO_BY_TWO)  /* F2Dot14 x 4 */
                    p += 8;
            } while (flags & MORE_COMPONENTS);
            /*
             TrueType instructions comes here:

             |length_of_instruction| (|ushort|)

             instruction (|byte * length_of_instruction|)
             */
        }
    }
    RELEASE(location);
    RELEASE(hmtx);
    if (vmtx)
        RELEASE(vmtx);

    {
        int max_count = -1;

        g->dw = g->gd[0].advw;
        for (i = 0; i < g->emsize + 1; i++) {
            if (w_stat[i] > max_count) {
                max_count = w_stat[i];
                g->dw = (USHORT) i;
            }
        }
    }
    RELEASE(w_stat);

    qsort(g->gd, g->num_glyphs, sizeof(struct tt_glyph_desc), glyf_cmp);
    {
        USHORT prev, last_advw;
        char *p, *q;
        int padlen, num_hm_known;

        glyf_table_size = 0UL;
        num_hm_known = 0;
        last_advw = g->gd[g->num_glyphs - 1].advw;
        for (i = g->num_glyphs - 1; i >= 0; i--) {
            padlen =
                (int) ((g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0);
            glyf_table_size += (ULONG) (g->gd[i].length + (ULONG) padlen);
            if (!num_hm_known && last_advw != g->gd[i].advw) {
                hhea->numberOfHMetrics = (USHORT) (g->gd[i].gid + 2);
                num_hm_known = 1;
            }
        }
        /* All advance widths are same. */
        if (!num_hm_known) {
            hhea->numberOfHMetrics = 1;
        }
        hmtx_table_size =
            (ULONG) (hhea->numberOfHMetrics * 2 + (g->last_gid + 1) * 2);

        /*
         Choosing short format does not always give good result
         when compressed. Sometimes increases size.
         */
        if (glyf_table_size < 0x20000UL) {
            head->indexToLocFormat = 0;
            loca_table_size = (ULONG) ((g->last_gid + 2) * 2);
        } else {
            head->indexToLocFormat = 1;
            loca_table_size = (ULONG) ((g->last_gid + 2) * 4);
        }

        hmtx_table_data = p = NEW(hmtx_table_size, char);
        loca_table_data = q = NEW(loca_table_size, char);
        glyf_table_data = NEW(glyf_table_size, char);
        glyf_table_used = 0;

        offset = 0UL;
        prev = 0;
        for (i = 0; i < g->num_glyphs; i++) {
            long gap, j;
            gap = (long) g->gd[i].gid - prev - 1;
            for (j = 1; j <= gap; j++) {
                if (prev + j == hhea->numberOfHMetrics - 1) {
                    p += sfnt_put_ushort(p, last_advw);
                } else if (prev + j < hhea->numberOfHMetrics) {
                    p += sfnt_put_ushort(p, 0);
                }
                p += sfnt_put_short(p, 0);
                if (head->indexToLocFormat == 0) {
                    q += sfnt_put_ushort(q, (USHORT) (offset / 2));
                } else {
                    q += sfnt_put_ulong(q, (LONG) offset);
                }
            }
            if (g->gd[i].gid < hhea->numberOfHMetrics) {
                p += sfnt_put_ushort(p, g->gd[i].advw);
            }
            p += sfnt_put_short(p, g->gd[i].lsb);
            if (head->indexToLocFormat == 0) {
                q += sfnt_put_ushort(q, (USHORT) (offset / 2));
            } else {
                q += sfnt_put_ulong(q, (LONG) offset);
            }

            if (callback_id > 0) {

                lstring * result;
                long size = 0;
                run_callback(callback_id, "ddd->L", tex_font, g->gd[i].gid, streamprovider, &result); /* this call can be sped up */
                padlen = (int) ((result->l % 4) ? (4 - (result->l % 4)) : 0);
                size = (size_t) result->l + (ULONG) padlen;
                if (glyf_table_used + size >= glyf_table_size) {
                    glyf_table_size = glyf_table_size + 20 * size; /* just a guess */
                    glyf_table_data = xrealloc(glyf_table_data, (unsigned)((unsigned)glyf_table_size*sizeof(char)));
                }
                glyf_table_used += size;
                memset(glyf_table_data + offset, 0, (size_t) size);
                memcpy(glyf_table_data + offset, (const char *) result->s, (size_t) result->l);
                offset += size;
                xfree(result);

            } else {

                padlen = (int) ((g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0);
                memset(glyf_table_data + offset, 0, (size_t) (g->gd[i].length + (ULONG) padlen));
                memcpy(glyf_table_data + offset, g->gd[i].data, g->gd[i].length);
                offset += (g->gd[i].length + (ULONG) padlen);

            }
            prev = g->gd[i].gid;
            RELEASE(g->gd[i].data);
            /* free data here since it consume much memory */
            g->gd[i].length = 0;
            g->gd[i].data = NULL;
        }
        if (head->indexToLocFormat == 0) {
            q += sfnt_put_ushort(q, (USHORT) (offset / 2));
        } else {
            q += sfnt_put_ulong(q, (LONG) offset);
        }

        sfnt_set_table(sfont, "hmtx", (char *) hmtx_table_data, hmtx_table_size);
        sfnt_set_table(sfont, "loca", (char *) loca_table_data, loca_table_size);
        if (callback_id > 0) {
            glyf_table_size = glyf_table_used;
        }
        sfnt_set_table(sfont, "glyf", (char *) glyf_table_data, glyf_table_size);
    }

    head->checkSumAdjustment = 0;
    maxp->numGlyphs = (USHORT) (g->last_gid + 1);

    /* TODO */
    sfnt_set_table(sfont, "maxp", tt_pack_maxp_table(maxp), TT_MAXP_TABLE_SIZE);
    sfnt_set_table(sfont, "hhea", tt_pack_hhea_table(hhea), TT_HHEA_TABLE_SIZE);
    sfnt_set_table(sfont, "head", tt_pack_head_table(head), TT_HEAD_TABLE_SIZE);
    RELEASE(maxp);
    RELEASE(hhea);
    RELEASE(head);
    if (os2)
        RELEASE(os2);

    return 0;
}

int tt_get_metrics(sfnt * sfont, struct tt_glyphs *g)
{
    struct tt_head_table *head = NULL;
    struct tt_hhea_table *hhea = NULL;
    struct tt_maxp_table *maxp = NULL;
    struct tt_longMetrics *hmtx, *vmtx = NULL;
    struct tt_os2__table *os2;
    /* temp */
    ULONG *location, offset;
    long i;
    USHORT *w_stat;

    ASSERT(g);

    if (sfont == NULL ||
#ifdef XETEX
        sfont->ft_face == NULL
#elif defined(pdfTeX)
        sfont->buffer == NULL
#else
        sfont->stream == NULL
#endif
        )
        normal_error("ttf","file not opened");

    if (sfont->type != SFNT_TYPE_TRUETYPE && sfont->type != SFNT_TYPE_TTC)
        normal_error("ttf","invalid font type");

    /*
     Read head, hhea, maxp, loca:

     unitsPerEm       --> head

     numHMetrics      --> hhea

     indexToLocFormat --> head

     numGlyphs        --> maxp
     */
    head = tt_read_head_table(sfont);
    hhea = tt_read_hhea_table(sfont);
    maxp = tt_read_maxp_table(sfont);

    if (hhea->metricDataFormat != 0)
        normal_error("ttf","unknown metricDataFormat");

    g->emsize = head->unitsPerEm;

    sfnt_locate_table(sfont, "hmtx");
    hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numberOfHMetrics);

    os2 = tt_read_os2__table(sfont);
    g->default_advh = (USHORT) (os2->sTypoAscender - os2->sTypoDescender);
    g->default_tsb = (SHORT) (g->default_advh - os2->sTypoAscender);

    if (sfnt_find_table_pos(sfont, "vmtx") > 0) {
        struct tt_vhea_table *vhea;
        vhea = tt_read_vhea_table(sfont);
        sfnt_locate_table(sfont, "vmtx");
        vmtx =
            tt_read_longMetrics(sfont, maxp->numGlyphs,
                                vhea->numOfLongVerMetrics);
        RELEASE(vhea);
    } else {
        vmtx = NULL;
    }

    sfnt_locate_table(sfont, "loca");
    location = NEW(maxp->numGlyphs + 1, ULONG);
    if (head->indexToLocFormat == 0) {
        for (i = 0; i <= maxp->numGlyphs; i++)
            location[i] = 2 * ((ULONG) sfnt_get_ushort(sfont));
    } else if (head->indexToLocFormat == 1) {
        for (i = 0; i <= maxp->numGlyphs; i++)
            location[i] = sfnt_get_ulong(sfont);
    } else {
        normal_error("ttf","inknown IndexToLocFormat");
    }

    w_stat = NEW(g->emsize + 2, USHORT);
    memset(w_stat, 0, (size_t) ((int) sizeof(USHORT) * (g->emsize + 2)));
    /*
     Read glyf table.
     */
    offset = sfnt_locate_table(sfont, "glyf");
    for (i = 0; i < g->num_glyphs; i++) {
        USHORT gid;             /* old gid */
        ULONG loc, len;
        /*SHORT number_of_contours;*/

        gid = g->gd[i].ogid;
        if (gid >= maxp->numGlyphs)
            formatted_error("ttf","invalid glyph index (gid %u)", gid);

        loc = location[gid];
        len = location[gid + 1] - loc;
        g->gd[i].advw = hmtx[gid].advance;
        g->gd[i].lsb = hmtx[gid].sideBearing;
        if (vmtx) {
            g->gd[i].advh = vmtx[gid].advance;
            g->gd[i].tsb = vmtx[gid].sideBearing;
        } else {
            g->gd[i].advh = g->default_advh;
            g->gd[i].tsb = g->default_tsb;
        }
        g->gd[i].length = len;
        g->gd[i].data = NULL;

        if (g->gd[i].advw <= g->emsize) {
            w_stat[g->gd[i].advw]++;
        } else {
            w_stat[g->emsize + 1]++;    /* larger than em */
        }

        if (len == 0) {         /* Does not contains any data. */
            continue;
        } else if (len < 10) {
            formatted_error("ttf","invalid glyph data (gid %u)", gid);
        }

        sfnt_seek_set(sfont, (long) (offset + loc));
        /*number_of_contours = */(void)sfnt_get_short(sfont);

        /* BoundingBox: FWord x 4 */
        g->gd[i].llx = sfnt_get_short(sfont);
        g->gd[i].lly = sfnt_get_short(sfont);
        g->gd[i].urx = sfnt_get_short(sfont);
        g->gd[i].ury = sfnt_get_short(sfont);
        /* |_FIXME_| */
#if  1
        if (!vmtx)              /* |vertOriginY == sTypeAscender| */
            g->gd[i].tsb =
                (SHORT) (g->default_advh - g->default_tsb - g->gd[i].ury);
#endif
    }
    RELEASE(location);
    RELEASE(hmtx);
    RELEASE(maxp);
    RELEASE(hhea);
    RELEASE(head);
    RELEASE(os2);

    if (vmtx)
        RELEASE(vmtx);

    {
        int max_count = -1;

        g->dw = g->gd[0].advw;
        for (i = 0; i < g->emsize + 1; i++) {
            if (w_stat[i] > max_count) {
                max_count = w_stat[i];
                g->dw = (USHORT) i;
            }
        }
    }
    RELEASE(w_stat);


    return 0;
}