| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2002 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team <[email protected]> |
| 5 |
Copyright 2006-2012 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 |
| 10 |
the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 2 of the License, or (at your |
| 12 |
option) any later version. |
| 13 |
|
| 14 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 15 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 |
License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
#include "ptexlib.h" |
| 25 |
|
| 26 |
#include "font/sfnt.h" |
| 27 |
#include "font/tt_table.h" |
| 28 |
#include "font/tt_glyf.h" |
| 29 |
#include "font/writettf.h" |
| 30 |
|
| 31 |
#define NUM_GLYPH_LIMIT 65534 |
| 32 |
#define TABLE_DATA_ALLOC_SIZE 40960 |
| 33 |
#define GLYPH_ARRAY_ALLOC_SIZE 256 |
| 34 |
|
| 35 |
static USHORT find_empty_slot(struct tt_glyphs *g) |
| 36 |
{ |
| 37 |
USHORT gid; |
| 38 |
for (gid = 0; gid < NUM_GLYPH_LIMIT; gid++) { |
| 39 |
if (!(g->used_slot[gid / 8] & (1 << (7 - (gid % 8))))) |
| 40 |
break; |
| 41 |
} |
| 42 |
if (gid == NUM_GLYPH_LIMIT) |
| 43 |
normal_error("ttf","no empty glyph slot available."); |
| 44 |
return gid; |
| 45 |
} |
| 46 |
|
| 47 |
USHORT tt_find_glyph(struct tt_glyphs * g, USHORT gid) |
| 48 |
{ |
| 49 |
USHORT idx, new_gid = 0; |
| 50 |
for (idx = 0; idx < g->num_glyphs; idx++) { |
| 51 |
if (gid == g->gd[idx].ogid) { |
| 52 |
new_gid = g->gd[idx].gid; |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
return new_gid; |
| 57 |
} |
| 58 |
|
| 59 |
USHORT tt_get_index(struct tt_glyphs * g, USHORT gid) |
| 60 |
{ |
| 61 |
USHORT idx; |
| 62 |
for (idx = 0; idx < g->num_glyphs; idx++) { |
| 63 |
if (gid == g->gd[idx].gid) |
| 64 |
break; |
| 65 |
} |
| 66 |
if (idx == g->num_glyphs) |
| 67 |
idx = 0; |
| 68 |
return idx; |
| 69 |
} |
| 70 |
|
| 71 |
USHORT tt_add_glyph(struct tt_glyphs * g, USHORT gid, USHORT new_gid) |
| 72 |
{ |
| 73 |
if (g->used_slot[new_gid / 8] & (1 << (7 - (new_gid % 8)))) { |
| 74 |
formatted_warning("ttf","slot %u already used", new_gid); |
| 75 |
} else { |
| 76 |
if (g->num_glyphs + 1 >= NUM_GLYPH_LIMIT) |
| 77 |
normal_error("ttf","too many glyphs"); |
| 78 |
|
| 79 |
if (g->num_glyphs >= g->max_glyphs) { |
| 80 |
g->max_glyphs = (USHORT) (g->max_glyphs + GLYPH_ARRAY_ALLOC_SIZE); |
| 81 |
g->gd = RENEW(g->gd, g->max_glyphs, struct tt_glyph_desc); |
| 82 |
} |
| 83 |
g->gd[g->num_glyphs].gid = new_gid; |
| 84 |
g->gd[g->num_glyphs].ogid = gid; |
| 85 |
g->gd[g->num_glyphs].length = 0; |
| 86 |
g->gd[g->num_glyphs].data = NULL; |
| 87 |
g->used_slot[new_gid / 8] = (unsigned char) (g->used_slot[new_gid / 8] | (1 << (7 - (new_gid % 8)))); |
| 88 |
g->num_glyphs++; |
| 89 |
} |
| 90 |
if (new_gid > g->last_gid) { |
| 91 |
g->last_gid = new_gid; |
| 92 |
} |
| 93 |
return new_gid; |
| 94 |
} |
| 95 |
|
| 96 |
/*tex Initialization */ |
| 97 |
|
| 98 |
struct tt_glyphs *tt_build_init(void) |
| 99 |
{ |
| 100 |
struct tt_glyphs *g; |
| 101 |
g = NEW(1, struct tt_glyphs); |
| 102 |
g->num_glyphs = 0; |
| 103 |
g->max_glyphs = 0; |
| 104 |
g->last_gid = 0; |
| 105 |
g->emsize = 1; |
| 106 |
g->default_advh = 0; |
| 107 |
g->default_tsb = 0; |
| 108 |
g->gd = NULL; |
| 109 |
g->used_slot = NEW(8192, unsigned char); |
| 110 |
memset(g->used_slot, 0, 8192); |
| 111 |
tt_add_glyph(g, 0, 0); |
| 112 |
return g; |
| 113 |
} |
| 114 |
|
| 115 |
void tt_build_finish(struct tt_glyphs *g) |
| 116 |
{ |
| 117 |
if (g) { |
| 118 |
if (g->gd) { |
| 119 |
USHORT idx; |
| 120 |
for (idx = 0; idx < g->num_glyphs; idx++) { |
| 121 |
if (g->gd[idx].data) |
| 122 |
RELEASE(g->gd[idx].data); |
| 123 |
} |
| 124 |
RELEASE(g->gd); |
| 125 |
} |
| 126 |
if (g->used_slot) |
| 127 |
RELEASE(g->used_slot); |
| 128 |
RELEASE(g); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static int glyf_cmp(const void *v1, const void *v2) |
| 133 |
{ |
| 134 |
int cmp = 0; |
| 135 |
const struct tt_glyph_desc *sv1, *sv2; |
| 136 |
sv1 = (const struct tt_glyph_desc *) v1; |
| 137 |
sv2 = (const struct tt_glyph_desc *) v2; |
| 138 |
if (sv1->gid == sv2->gid) |
| 139 |
cmp = 0; |
| 140 |
else if (sv1->gid < sv2->gid) |
| 141 |
cmp = -1; |
| 142 |
else |
| 143 |
cmp = 1; |
| 144 |
return cmp; |
| 145 |
} |
| 146 |
|
| 147 |
int tt_build_tables(sfnt * sfont, struct tt_glyphs *g, fd_entry * fd) |
| 148 |
{ |
| 149 |
char *hmtx_table_data = NULL, *loca_table_data = NULL; |
| 150 |
char *glyf_table_data = NULL; |
| 151 |
ULONG hmtx_table_size, loca_table_size, glyf_table_size, glyf_table_used; |
| 152 |
/*tex Some information available from other \TRUETYPE\ table. */ |
| 153 |
struct tt_head_table *head = NULL; |
| 154 |
struct tt_hhea_table *hhea = NULL; |
| 155 |
struct tt_maxp_table *maxp = NULL; |
| 156 |
struct tt_longMetrics *hmtx, *vmtx = NULL; |
| 157 |
struct tt_os2__table *os2; |
| 158 |
/*tex Something temporary: */ |
| 159 |
ULONG *location, offset; |
| 160 |
long i; |
| 161 |
/*tex Estimate the most frequently appeared width. */ |
| 162 |
USHORT *w_stat; |
| 163 |
int tex_font = fd->tex_font; |
| 164 |
int streamprovider = 0; |
| 165 |
int callback_id = 0 ; |
| 166 |
if ((tex_font > 0) && (font_streamprovider(tex_font) == 2 || font_streamprovider(tex_font) == 3)) { |
| 167 |
streamprovider = font_streamprovider(tex_font); |
| 168 |
callback_id = callback_defined(glyph_stream_provider_callback); |
| 169 |
} |
| 170 |
if (sfont->type != SFNT_TYPE_TRUETYPE && sfont->type != SFNT_TYPE_TTC) |
| 171 |
normal_error("ttf","invalid font type"); |
| 172 |
if (g->num_glyphs > NUM_GLYPH_LIMIT) |
| 173 |
normal_error("ttf","too many glyphs"); |
| 174 |
head = tt_read_head_table(sfont); |
| 175 |
hhea = tt_read_hhea_table(sfont); |
| 176 |
maxp = tt_read_maxp_table(sfont); |
| 177 |
if (hhea->metricDataFormat != 0) |
| 178 |
normal_error("ttf","unknown metricDataFormat"); |
| 179 |
g->emsize = head->unitsPerEm; |
| 180 |
sfnt_locate_table(sfont, "hmtx"); |
| 181 |
hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numberOfHMetrics); |
| 182 |
os2 = tt_read_os2__table(sfont); |
| 183 |
if (os2) { |
| 184 |
g->default_advh = (USHORT) (os2->sTypoAscender - os2->sTypoDescender); |
| 185 |
g->default_tsb = (SHORT) (g->default_advh - os2->sTypoAscender); |
| 186 |
/*tex |dvipdfmx| does this elsewhere! */ |
| 187 |
fd_cur->font_dim[STEMV_CODE].val = |
| 188 |
(os2->usWeightClass / 65) * (os2->usWeightClass / 65) + 50; |
| 189 |
} |
| 190 |
if (sfnt_find_table_pos(sfont, "vmtx") > 0) { |
| 191 |
struct tt_vhea_table *vhea; |
| 192 |
vhea = tt_read_vhea_table(sfont); |
| 193 |
sfnt_locate_table(sfont, "vmtx"); |
| 194 |
vmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics); |
| 195 |
RELEASE(vhea); |
| 196 |
} else { |
| 197 |
vmtx = NULL; |
| 198 |
} |
| 199 |
sfnt_locate_table(sfont, "loca"); |
| 200 |
location = NEW(maxp->numGlyphs + 1, ULONG); |
| 201 |
if (head->indexToLocFormat == 0) { |
| 202 |
for (i = 0; i <= maxp->numGlyphs; i++) |
| 203 |
location[i] = 2 * ((ULONG) sfnt_get_ushort(sfont)); |
| 204 |
} else if (head->indexToLocFormat == 1) { |
| 205 |
for (i = 0; i <= maxp->numGlyphs; i++) |
| 206 |
location[i] = sfnt_get_ulong(sfont); |
| 207 |
} else { |
| 208 |
normal_error("ttf","unknown IndexToLocFormat"); |
| 209 |
} |
| 210 |
w_stat = NEW(g->emsize + 2, USHORT); |
| 211 |
memset(w_stat, 0, (size_t) (sizeof(USHORT) * ((long unsigned) g->emsize + 2))); |
| 212 |
offset = sfnt_locate_table(sfont, "glyf"); |
| 213 |
/*tex |
| 214 |
|
| 215 |
The |num_glyphs| may grow when composite glyph is found. A component of |
| 216 |
glyph refered by a composite glyph is appended to |used_glyphs| if it is |
| 217 |
not already registered in |used_glyphs|. Glyph programs of composite |
| 218 |
glyphs are modified so that it correctly refer to new gid of their |
| 219 |
components. |
| 220 |
*/ |
| 221 |
for (i = 0; i < NUM_GLYPH_LIMIT; i++) { |
| 222 |
USHORT gid; |
| 223 |
ULONG loc, len; |
| 224 |
BYTE *p, *endptr; |
| 225 |
SHORT number_of_contours; |
| 226 |
if (i >= g->num_glyphs) |
| 227 |
break; |
| 228 |
gid = g->gd[i].ogid; |
| 229 |
if (gid >= maxp->numGlyphs) |
| 230 |
formatted_error("ttf","invalid glyph index (gid %u)", gid); |
| 231 |
loc = location[gid]; |
| 232 |
len = location[gid + 1] - loc; |
| 233 |
if (callback_id > 0 && streamprovider == 3) { |
| 234 |
int advw, lsb, advh, tsb; |
| 235 |
run_callback(callback_id, "dddd->dddd", tex_font, g->gd[i].gid, |
| 236 |
streamprovider, gid, &advw, &lsb, &advh, &tsb); |
| 237 |
g->gd[i].advw = advw; |
| 238 |
g->gd[i].lsb = lsb; |
| 239 |
g->gd[i].advh = advh; |
| 240 |
g->gd[i].tsb = tsb; |
| 241 |
} else { |
| 242 |
g->gd[i].advw = hmtx[gid].advance; |
| 243 |
g->gd[i].lsb = hmtx[gid].sideBearing; |
| 244 |
if (vmtx) { |
| 245 |
g->gd[i].advh = vmtx[gid].advance; |
| 246 |
g->gd[i].tsb = vmtx[gid].sideBearing; |
| 247 |
} else { |
| 248 |
g->gd[i].advh = g->default_advh; |
| 249 |
g->gd[i].tsb = g->default_tsb; |
| 250 |
} |
| 251 |
} |
| 252 |
g->gd[i].length = len; |
| 253 |
g->gd[i].data = NULL; |
| 254 |
if (g->gd[i].advw <= g->emsize) { |
| 255 |
w_stat[g->gd[i].advw]++; |
| 256 |
} else { |
| 257 |
/*tex It's larger than em. */ |
| 258 |
w_stat[g->emsize + 1]++; |
| 259 |
} |
| 260 |
|
| 261 |
if (len == 0) { |
| 262 |
/*tex Does not contain any data. */ |
| 263 |
continue; |
| 264 |
} else if (len < 10) { |
| 265 |
formatted_error("ttf","invalid glyph data (gid %u)", gid); |
| 266 |
} |
| 267 |
/*tex There is no real need for this. */ |
| 268 |
g->gd[i].data = p = NEW(len, BYTE); |
| 269 |
endptr = p + len; |
| 270 |
sfnt_seek_set(sfont, (long) (offset + loc)); |
| 271 |
number_of_contours = sfnt_get_short(sfont); |
| 272 |
p += sfnt_put_short(p, number_of_contours); |
| 273 |
/* BoundingBox: FWord x 4 */ |
| 274 |
g->gd[i].llx = sfnt_get_short(sfont); |
| 275 |
g->gd[i].lly = sfnt_get_short(sfont); |
| 276 |
g->gd[i].urx = sfnt_get_short(sfont); |
| 277 |
g->gd[i].ury = sfnt_get_short(sfont); |
| 278 |
if (!vmtx) { |
| 279 |
/*tex A fix: |vertOriginY == sTypeAscender| */ |
| 280 |
g->gd[i].tsb = (SHORT) (g->default_advh - g->default_tsb - g->gd[i].ury); |
| 281 |
} |
| 282 |
p += sfnt_put_short(p, g->gd[i].llx); |
| 283 |
p += sfnt_put_short(p, g->gd[i].lly); |
| 284 |
p += sfnt_put_short(p, g->gd[i].urx); |
| 285 |
p += sfnt_put_short(p, g->gd[i].ury); |
| 286 |
/*tex Read evrything else. */ |
| 287 |
sfnt_read(p, (int) len - 10, sfont); |
| 288 |
/*tex Fix GIDs of composite glyphs. */ |
| 289 |
if (number_of_contours < 0) { |
| 290 |
USHORT flags, cgid, new_gid; |
| 291 |
do { |
| 292 |
if (p >= endptr) |
| 293 |
formatted_error("ttf","invalid glyph data (gid %u): %u bytes", gid, (unsigned int) len); |
| 294 |
flags = (USHORT) (((*p) << 8) | *(p + 1)); |
| 295 |
p += 2; |
| 296 |
cgid = (USHORT) (((*p) << 8) | *(p + 1)); |
| 297 |
if (cgid >= maxp->numGlyphs) { |
| 298 |
formatted_error("ttf","invalid gid (%u > %u) in composite glyph %u", cgid, maxp->numGlyphs, gid); |
| 299 |
} |
| 300 |
new_gid = tt_find_glyph(g, cgid); |
| 301 |
if (new_gid == 0) { |
| 302 |
new_gid = tt_add_glyph(g, cgid, find_empty_slot(g)); |
| 303 |
} |
| 304 |
p += sfnt_put_ushort(p, new_gid); |
| 305 |
/*tex Just skip remaining part. */ |
| 306 |
p += (flags & ARG_1_AND_2_ARE_WORDS) ? 4 : 2; |
| 307 |
if (flags & WE_HAVE_A_SCALE) { |
| 308 |
/*tex |F2Dot14| */ |
| 309 |
p += 2; |
| 310 |
} else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) { |
| 311 |
/*tex two times |F2Dot14| */ |
| 312 |
p += 4; |
| 313 |
} else if (flags & WE_HAVE_A_TWO_BY_TWO) { |
| 314 |
/*tex four times |F2Dot14| */ |
| 315 |
p += 8; |
| 316 |
} |
| 317 |
} while (flags & MORE_COMPONENTS); |
| 318 |
/*tex |
| 319 |
|
| 320 |
TrueType instructions comes here. The call pattern is: |
| 321 |
|
| 322 |
\starttyping |
| 323 |
|length_of_instruction| (|ushort|) |
| 324 |
instruction (|byte * length_of_instruction|) |
| 325 |
\stoptyping |
| 326 |
|
| 327 |
*/ |
| 328 |
} |
| 329 |
} |
| 330 |
RELEASE(location); |
| 331 |
RELEASE(hmtx); |
| 332 |
if (vmtx) { |
| 333 |
RELEASE(vmtx); |
| 334 |
} |
| 335 |
{ |
| 336 |
int max_count = -1; |
| 337 |
g->dw = g->gd[0].advw; |
| 338 |
for (i = 0; i < g->emsize + 1; i++) { |
| 339 |
if (w_stat[i] > max_count) { |
| 340 |
max_count = w_stat[i]; |
| 341 |
g->dw = (USHORT) i; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
RELEASE(w_stat); |
| 346 |
qsort(g->gd, g->num_glyphs, sizeof(struct tt_glyph_desc), glyf_cmp); |
| 347 |
{ |
| 348 |
USHORT prev, last_advw; |
| 349 |
char *p, *q; |
| 350 |
int padlen, num_hm_known; |
| 351 |
glyf_table_size = 0UL; |
| 352 |
num_hm_known = 0; |
| 353 |
last_advw = g->gd[g->num_glyphs - 1].advw; |
| 354 |
for (i = g->num_glyphs - 1; i >= 0; i--) { |
| 355 |
padlen = |
| 356 |
(int) ((g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0); |
| 357 |
glyf_table_size += (ULONG) (g->gd[i].length + (ULONG) padlen); |
| 358 |
if (!num_hm_known && last_advw != g->gd[i].advw) { |
| 359 |
hhea->numberOfHMetrics = (USHORT) (g->gd[i].gid + 2); |
| 360 |
num_hm_known = 1; |
| 361 |
} |
| 362 |
} |
| 363 |
/*tex All advance widths are the same. */ |
| 364 |
if (!num_hm_known) { |
| 365 |
hhea->numberOfHMetrics = 1; |
| 366 |
} |
| 367 |
hmtx_table_size = (ULONG) (hhea->numberOfHMetrics * 2 + (g->last_gid + 1) * 2); |
| 368 |
/*tex |
| 369 |
|
| 370 |
Choosing short format does not always give good result when |
| 371 |
compressed. Sometimes increases size. |
| 372 |
|
| 373 |
*/ |
| 374 |
if (glyf_table_size < 0x20000UL) { |
| 375 |
head->indexToLocFormat = 0; |
| 376 |
loca_table_size = (ULONG) ((g->last_gid + 2) * 2); |
| 377 |
} else { |
| 378 |
head->indexToLocFormat = 1; |
| 379 |
loca_table_size = (ULONG) ((g->last_gid + 2) * 4); |
| 380 |
} |
| 381 |
hmtx_table_data = p = NEW(hmtx_table_size, char); |
| 382 |
loca_table_data = q = NEW(loca_table_size, char); |
| 383 |
glyf_table_data = NEW(glyf_table_size, char); |
| 384 |
glyf_table_used = 0; |
| 385 |
offset = 0UL; |
| 386 |
prev = 0; |
| 387 |
for (i = 0; i < g->num_glyphs; i++) { |
| 388 |
long gap, j; |
| 389 |
gap = (long) g->gd[i].gid - prev - 1; |
| 390 |
for (j = 1; j <= gap; j++) { |
| 391 |
if (prev + j == hhea->numberOfHMetrics - 1) { |
| 392 |
p += sfnt_put_ushort(p, last_advw); |
| 393 |
} else if (prev + j < hhea->numberOfHMetrics) { |
| 394 |
p += sfnt_put_ushort(p, 0); |
| 395 |
} |
| 396 |
p += sfnt_put_short(p, 0); |
| 397 |
if (head->indexToLocFormat == 0) { |
| 398 |
q += sfnt_put_ushort(q, (USHORT) (offset / 2)); |
| 399 |
} else { |
| 400 |
q += sfnt_put_ulong(q, (LONG) offset); |
| 401 |
} |
| 402 |
} |
| 403 |
if (g->gd[i].gid < hhea->numberOfHMetrics) { |
| 404 |
p += sfnt_put_ushort(p, g->gd[i].advw); |
| 405 |
} |
| 406 |
p += sfnt_put_short(p, g->gd[i].lsb); |
| 407 |
if (head->indexToLocFormat == 0) { |
| 408 |
q += sfnt_put_ushort(q, (USHORT) (offset / 2)); |
| 409 |
} else { |
| 410 |
q += sfnt_put_ulong(q, (LONG) offset); |
| 411 |
} |
| 412 |
if (callback_id > 0) { |
| 413 |
lstring * result; |
| 414 |
long size = 0; |
| 415 |
/*tex |
| 416 |
|
| 417 |
The streamprovider here is always 2, even when we actually have streamprovider == 3, |
| 418 |
to differentiate this call which behaves exactly like the call in the |
| 419 |
streamprovider == 2 case from the streamprovider == 3 specific call earlier. |
| 420 |
|
| 421 |
*/ |
| 422 |
run_callback(callback_id, "ddd->L", tex_font, g->gd[i].gid, 2, &result); |
| 423 |
padlen = (int) ((result->l % 4) ? (4 - (result->l % 4)) : 0); |
| 424 |
size = (size_t) result->l + (ULONG) padlen; |
| 425 |
if (glyf_table_used + size >= glyf_table_size) { |
| 426 |
glyf_table_size = glyf_table_size + 20 * size; /* just a guess */ |
| 427 |
glyf_table_data = xrealloc(glyf_table_data, (unsigned)((unsigned)glyf_table_size*sizeof(char))); |
| 428 |
} |
| 429 |
glyf_table_used += size; |
| 430 |
memset(glyf_table_data + offset, 0, (size_t) size); |
| 431 |
memcpy(glyf_table_data + offset, (const char *) result->s, (size_t) result->l); |
| 432 |
offset += size; |
| 433 |
xfree(result); |
| 434 |
} else { |
| 435 |
padlen = (int) ((g->gd[i].length % 4) ? (4 - (g->gd[i].length % 4)) : 0); |
| 436 |
memset(glyf_table_data + offset, 0, (size_t) (g->gd[i].length + (ULONG) padlen)); |
| 437 |
memcpy(glyf_table_data + offset, g->gd[i].data, g->gd[i].length); |
| 438 |
offset += (g->gd[i].length + (ULONG) padlen); |
| 439 |
} |
| 440 |
prev = g->gd[i].gid; |
| 441 |
RELEASE(g->gd[i].data); |
| 442 |
/*tex We free data here since it consume much memory. */ |
| 443 |
g->gd[i].length = 0; |
| 444 |
g->gd[i].data = NULL; |
| 445 |
} |
| 446 |
if (head->indexToLocFormat == 0) { |
| 447 |
q += sfnt_put_ushort(q, (USHORT) (offset / 2)); |
| 448 |
} else { |
| 449 |
q += sfnt_put_ulong(q, (LONG) offset); |
| 450 |
} |
| 451 |
sfnt_set_table(sfont, "hmtx", (char *) hmtx_table_data, hmtx_table_size); |
| 452 |
sfnt_set_table(sfont, "loca", (char *) loca_table_data, loca_table_size); |
| 453 |
if (callback_id > 0) { |
| 454 |
glyf_table_size = glyf_table_used; |
| 455 |
} |
| 456 |
sfnt_set_table(sfont, "glyf", (char *) glyf_table_data, glyf_table_size); |
| 457 |
} |
| 458 |
head->checkSumAdjustment = 0; |
| 459 |
maxp->numGlyphs = (USHORT) (g->last_gid + 1); |
| 460 |
sfnt_set_table(sfont, "maxp", tt_pack_maxp_table(maxp), TT_MAXP_TABLE_SIZE); |
| 461 |
sfnt_set_table(sfont, "hhea", tt_pack_hhea_table(hhea), TT_HHEA_TABLE_SIZE); |
| 462 |
sfnt_set_table(sfont, "head", tt_pack_head_table(head), TT_HEAD_TABLE_SIZE); |
| 463 |
RELEASE(maxp); |
| 464 |
RELEASE(hhea); |
| 465 |
RELEASE(head); |
| 466 |
if (os2) { |
| 467 |
RELEASE(os2); |
| 468 |
} |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
int tt_get_metrics(sfnt * sfont, struct tt_glyphs *g) |
| 473 |
{ |
| 474 |
struct tt_head_table *head = NULL; |
| 475 |
struct tt_hhea_table *hhea = NULL; |
| 476 |
struct tt_maxp_table *maxp = NULL; |
| 477 |
struct tt_longMetrics *hmtx, *vmtx = NULL; |
| 478 |
struct tt_os2__table *os2; |
| 479 |
ULONG *location, offset; |
| 480 |
long i; |
| 481 |
USHORT *w_stat; |
| 482 |
if (sfont == NULL || sfont->buffer == NULL) |
| 483 |
normal_error("ttf","file not opened"); |
| 484 |
if (sfont->type != SFNT_TYPE_TRUETYPE && sfont->type != SFNT_TYPE_TTC) |
| 485 |
normal_error("ttf","invalid font type"); |
| 486 |
head = tt_read_head_table(sfont); |
| 487 |
hhea = tt_read_hhea_table(sfont); |
| 488 |
maxp = tt_read_maxp_table(sfont); |
| 489 |
if (hhea->metricDataFormat != 0) |
| 490 |
normal_error("ttf","unknown metricDataFormat"); |
| 491 |
g->emsize = head->unitsPerEm; |
| 492 |
sfnt_locate_table(sfont, "hmtx"); |
| 493 |
hmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numberOfHMetrics); |
| 494 |
os2 = tt_read_os2__table(sfont); |
| 495 |
g->default_advh = (USHORT) (os2->sTypoAscender - os2->sTypoDescender); |
| 496 |
g->default_tsb = (SHORT) (g->default_advh - os2->sTypoAscender); |
| 497 |
if (sfnt_find_table_pos(sfont, "vmtx") > 0) { |
| 498 |
struct tt_vhea_table *vhea; |
| 499 |
vhea = tt_read_vhea_table(sfont); |
| 500 |
sfnt_locate_table(sfont, "vmtx"); |
| 501 |
vmtx = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics); |
| 502 |
RELEASE(vhea); |
| 503 |
} else { |
| 504 |
vmtx = NULL; |
| 505 |
} |
| 506 |
sfnt_locate_table(sfont, "loca"); |
| 507 |
location = NEW(maxp->numGlyphs + 1, ULONG); |
| 508 |
if (head->indexToLocFormat == 0) { |
| 509 |
for (i = 0; i <= maxp->numGlyphs; i++) |
| 510 |
location[i] = 2 * ((ULONG) sfnt_get_ushort(sfont)); |
| 511 |
} else if (head->indexToLocFormat == 1) { |
| 512 |
for (i = 0; i <= maxp->numGlyphs; i++) |
| 513 |
location[i] = sfnt_get_ulong(sfont); |
| 514 |
} else { |
| 515 |
normal_error("ttf","inknown IndexToLocFormat"); |
| 516 |
} |
| 517 |
w_stat = NEW(g->emsize + 2, USHORT); |
| 518 |
memset(w_stat, 0, (size_t) ((int) sizeof(USHORT) * (g->emsize + 2))); |
| 519 |
/*tex Read glyf table. */ |
| 520 |
offset = sfnt_locate_table(sfont, "glyf"); |
| 521 |
for (i = 0; i < g->num_glyphs; i++) { |
| 522 |
USHORT gid; |
| 523 |
ULONG loc, len; |
| 524 |
gid = g->gd[i].ogid; |
| 525 |
if (gid >= maxp->numGlyphs) |
| 526 |
formatted_error("ttf","invalid glyph index (gid %u)", gid); |
| 527 |
loc = location[gid]; |
| 528 |
len = location[gid + 1] - loc; |
| 529 |
g->gd[i].advw = hmtx[gid].advance; |
| 530 |
g->gd[i].lsb = hmtx[gid].sideBearing; |
| 531 |
if (vmtx) { |
| 532 |
g->gd[i].advh = vmtx[gid].advance; |
| 533 |
g->gd[i].tsb = vmtx[gid].sideBearing; |
| 534 |
} else { |
| 535 |
g->gd[i].advh = g->default_advh; |
| 536 |
g->gd[i].tsb = g->default_tsb; |
| 537 |
} |
| 538 |
g->gd[i].length = len; |
| 539 |
g->gd[i].data = NULL; |
| 540 |
if (g->gd[i].advw <= g->emsize) { |
| 541 |
w_stat[g->gd[i].advw]++; |
| 542 |
} else { |
| 543 |
/*tex It's larger than em: */ |
| 544 |
w_stat[g->emsize + 1]++; |
| 545 |
} |
| 546 |
if (len == 0) { |
| 547 |
/*tex No data. */ |
| 548 |
continue; |
| 549 |
} else if (len < 10) { |
| 550 |
formatted_error("ttf","invalid glyph data (gid %u)", gid); |
| 551 |
} |
| 552 |
sfnt_seek_set(sfont, (long) (offset + loc)); |
| 553 |
/*tex Skip the number of contours */ |
| 554 |
(void)sfnt_get_short(sfont); |
| 555 |
/*tex Fetch the BoundingBox. */ |
| 556 |
g->gd[i].llx = sfnt_get_short(sfont); |
| 557 |
g->gd[i].lly = sfnt_get_short(sfont); |
| 558 |
g->gd[i].urx = sfnt_get_short(sfont); |
| 559 |
g->gd[i].ury = sfnt_get_short(sfont); |
| 560 |
if (!vmtx) { |
| 561 |
/*tex We fix |vertOriginY == sTypeAscender|. */ |
| 562 |
g->gd[i].tsb = (SHORT) (g->default_advh - g->default_tsb - g->gd[i].ury); |
| 563 |
} |
| 564 |
} |
| 565 |
RELEASE(location); |
| 566 |
RELEASE(hmtx); |
| 567 |
RELEASE(maxp); |
| 568 |
RELEASE(hhea); |
| 569 |
RELEASE(head); |
| 570 |
RELEASE(os2); |
| 571 |
if (vmtx) { |
| 572 |
RELEASE(vmtx); |
| 573 |
} |
| 574 |
{ |
| 575 |
int max_count = -1; |
| 576 |
g->dw = g->gd[0].advw; |
| 577 |
for (i = 0; i < g->emsize + 1; i++) { |
| 578 |
if (w_stat[i] > max_count) { |
| 579 |
max_count = w_stat[i]; |
| 580 |
g->dw = (USHORT) i; |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
RELEASE(w_stat); |
| 585 |
return 0; |
| 586 |
} |