summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipdfm-x/tt_glyf.c
blob: 134af589e342e039d92cdf17bbdf44e4f4b3187c (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
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
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.

    Copyright (C) 2002-2016 by Jin-Hwan Cho and Shunsaku Hirata,
    the dvipdfmx project team.
    
    This program 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.
    
    This program 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 General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

/*
 * Subsetting glyf, updating loca, hmtx, ...
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "system.h"
#include "mem.h"
#include "error.h"
#include "dpxutil.h"

#include "sfnt.h"
#include "tt_table.h"
#include "tt_glyf.h"

#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)
    ERROR("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)))) {
    WARN("Slot %u already used.", new_gid);
  } else {
    if (g->num_glyphs+1 >= NUM_GLYPH_LIMIT)
      ERROR("Too many glyphs.");

    if (g->num_glyphs >= g->max_glyphs) {
      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] |= (1 << (7 - (new_gid % 8)));
    g->num_glyphs += 1;
  }

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

  return new_gid;
}

/*
 * Initialization
 */
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 inline 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;
}

/*
 * TrueType outline data.
 */
#define ARG_1_AND_2_ARE_WORDS     (1 << 0)
#define ARGS_ARE_XY_VALUES        (1 << 1)
#define ROUND_XY_TO_GRID          (1 << 2)
#define WE_HAVE_A_SCALE           (1 << 3)
#define RESERVED                  (1 << 4)
#define MORE_COMPONENT            (1 << 5)
#define WE_HAVE_AN_X_AND_Y_SCALE  (1 << 6)
#define WE_HAVE_A_TWO_BY_TWO      (1 << 7)
#define WE_HAVE_INSTRUCTIONS      (1 << 8)
#define USE_MY_METRICS            (1 << 9)

int
tt_build_tables (sfnt *sfont, struct tt_glyphs *g)
{
  char  *hmtx_table_data = NULL, *loca_table_data = NULL;
  char  *glyf_table_data = NULL;
  ULONG  hmtx_table_size, loca_table_size, glyf_table_size;
  /* 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;
  int     i;
  USHORT *w_stat; /* Estimate most frequently appeared width */

  ASSERT(g);

  if (sfont == NULL || sfont->stream == NULL)
    ERROR("File not opened.");

  if (sfont->type != SFNT_TYPE_TRUETYPE &&
      sfont->type != SFNT_TYPE_TTC &&
      sfont->type != SFNT_TYPE_DFONT)
    ERROR("Invalid font type");

  if (g->num_glyphs > NUM_GLYPH_LIMIT)
    ERROR("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)
    ERROR("Unknown metricDataFormat.");

  g->emsize = head->unitsPerEm;

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

  os2 = tt_read_os2__table(sfont);
  if (os2) {
    g->default_advh = os2->sTypoAscender - os2->sTypoDescender;
    g->default_tsb  = 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, vhea->numOfExSideBearings);
    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 {
    ERROR("Unknown IndexToLocFormat.");
  }

  w_stat = NEW(g->emsize+2, USHORT);
  memset(w_stat, 0, sizeof(USHORT)*(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)
      ERROR("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] += 1;
    } else {
      w_stat[g->emsize+1]   += 1; /* larger than em */
    }

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

    g->gd[i].data = p = NEW(len, BYTE);
    endptr = p + len;

    sfnt_seek_set(sfont, 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 = 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, 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)
	  ERROR("Invalid TrueType glyph data (gid %u): %u bytes", gid, len);
	/*
	 * Flags and gid of component glyph are both USHORT.
	 */
	flags = ((*p) << 8)| *(p+1);
	p += 2;
	cgid  = ((*p) << 8)| *(p+1);
	if (cgid >= maxp->numGlyphs) {
	  ERROR("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_COMPONENT);
      /*
       * 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 = 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 = (g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0;
      glyf_table_size += g->gd[i].length + padlen;
      if (!num_hm_known && last_advw != g->gd[i].advw) {
	hhea->numOfLongHorMetrics = g->gd[i].gid + 2;
	num_hm_known = 1;
      }
    }
    /* All advance widths are same. */
    if (!num_hm_known) {
      hhea->numOfLongHorMetrics = 1;
    }
    hmtx_table_size = hhea->numOfLongHorMetrics * 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 = (g->last_gid + 2)*2;
    } else {
      head->indexToLocFormat = 1;
      loca_table_size = (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);

    offset = 0UL; prev = 0;
    for (i = 0; i < g->num_glyphs; i++) {
      int gap, j;
      gap = g->gd[i].gid - prev - 1;
      for (j = 1; j <= gap; j++) {
	if (prev + j == hhea->numOfLongHorMetrics - 1) {
	  p += sfnt_put_ushort(p, last_advw);
	} else if (prev + j < hhea->numOfLongHorMetrics) {
	  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, offset);
	}
      }
      padlen = (g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0;
      if (g->gd[i].gid < hhea->numOfLongHorMetrics) {
	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, offset);
      }
      memset(glyf_table_data + offset, 0, g->gd[i].length + padlen);
      memcpy(glyf_table_data + offset, g->gd[i].data, g->gd[i].length);
      offset += g->gd[i].length + padlen;
      prev    = g->gd[i].gid;
      /* free data here since it consume much memory */
      RELEASE(g->gd[i].data);
      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, 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);
    sfnt_set_table(sfont, "glyf", (char *) glyf_table_data, glyf_table_size);
  }

  head->checkSumAdjustment = 0;
  maxp->numGlyphs          = 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;
  int     i;
  USHORT *w_stat;

  ASSERT(g);

  if (sfont == NULL || sfont->stream == NULL)
    ERROR("File not opened.");

  if (sfont->type != SFNT_TYPE_TRUETYPE &&
      sfont->type != SFNT_TYPE_TTC &&
      sfont->type != SFNT_TYPE_DFONT)
    ERROR("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)
    ERROR("Unknown metricDataFormat.");

  g->emsize = head->unitsPerEm;

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

  os2 = tt_read_os2__table(sfont);
  g->default_advh = os2->sTypoAscender - os2->sTypoDescender;
  g->default_tsb  = 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, vhea->numOfExSideBearings);
    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 {
    ERROR("Unknown IndexToLocFormat.");
  }

  w_stat = NEW(g->emsize+2, USHORT);
  memset(w_stat, 0, 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;

    gid = g->gd[i].ogid;
    if (gid >= maxp->numGlyphs)
      ERROR("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] += 1;
    } else {
      w_stat[g->emsize+1]   += 1; /* larger than em */
    }

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

    sfnt_seek_set(sfont, offset+loc);
    (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 = 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 = i;
      }
    }
  }
  RELEASE(w_stat);


  return 0;
}