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