| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// FoFiTrueType.cc |
| 4 |
// |
| 5 |
// Copyright 1999-2003 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#include <aconf.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#if HAVE_STD_SORT |
| 14 |
#include <algorithm> |
| 15 |
#endif |
| 16 |
#include "gtypes.h" |
| 17 |
#include "gmem.h" |
| 18 |
#include "gmempp.h" |
| 19 |
#include "GString.h" |
| 20 |
#include "GHash.h" |
| 21 |
#include "FoFiType1C.h" |
| 22 |
#include "FoFiTrueType.h" |
| 23 |
|
| 24 |
// |
| 25 |
// Terminology |
| 26 |
// ----------- |
| 27 |
// |
| 28 |
// character code = number used as an element of a text string |
| 29 |
// |
| 30 |
// character name = glyph name = name for a particular glyph within a |
| 31 |
// font |
| 32 |
// |
| 33 |
// glyph index = GID = position (within some internal table in the font) |
| 34 |
// where the instructions to draw a particular glyph are |
| 35 |
// stored |
| 36 |
// |
| 37 |
// Type 1 fonts |
| 38 |
// ------------ |
| 39 |
// |
| 40 |
// Type 1 fonts contain: |
| 41 |
// |
| 42 |
// Encoding: array of glyph names, maps char codes to glyph names |
| 43 |
// |
| 44 |
// Encoding[charCode] = charName |
| 45 |
// |
| 46 |
// CharStrings: dictionary of instructions, keyed by character names, |
| 47 |
// maps character name to glyph data |
| 48 |
// |
| 49 |
// CharStrings[charName] = glyphData |
| 50 |
// |
| 51 |
// TrueType fonts |
| 52 |
// -------------- |
| 53 |
// |
| 54 |
// TrueType fonts contain: |
| 55 |
// |
| 56 |
// 'cmap' table: mapping from character code to glyph index; there may |
| 57 |
// be multiple cmaps in a TrueType font |
| 58 |
// |
| 59 |
// cmap[charCode] = gid |
| 60 |
// |
| 61 |
// 'post' table: mapping from glyph index to glyph name |
| 62 |
// |
| 63 |
// post[gid] = glyphName |
| 64 |
// |
| 65 |
// Type 42 fonts |
| 66 |
// ------------- |
| 67 |
// |
| 68 |
// Type 42 fonts contain: |
| 69 |
// |
| 70 |
// Encoding: array of glyph names, maps char codes to glyph names |
| 71 |
// |
| 72 |
// Encoding[charCode] = charName |
| 73 |
// |
| 74 |
// CharStrings: dictionary of glyph indexes, keyed by character names, |
| 75 |
// maps character name to glyph index |
| 76 |
// |
| 77 |
// CharStrings[charName] = gid |
| 78 |
// |
| 79 |
|
| 80 |
//------------------------------------------------------------------------ |
| 81 |
|
| 82 |
#define ttcfTag 0x74746366 |
| 83 |
|
| 84 |
//------------------------------------------------------------------------ |
| 85 |
|
| 86 |
struct TrueTypeTable { |
| 87 |
Guint tag; |
| 88 |
Guint checksum; |
| 89 |
int offset; |
| 90 |
int origOffset; |
| 91 |
int len; |
| 92 |
}; |
| 93 |
|
| 94 |
struct TrueTypeCmap { |
| 95 |
int platform; |
| 96 |
int encoding; |
| 97 |
int offset; |
| 98 |
int len; |
| 99 |
int fmt; |
| 100 |
}; |
| 101 |
|
| 102 |
struct TrueTypeLoca { |
| 103 |
int idx; |
| 104 |
int origOffset; |
| 105 |
int newOffset; |
| 106 |
int len; |
| 107 |
}; |
| 108 |
|
| 109 |
#define cmapTag 0x636d6170 |
| 110 |
#define glyfTag 0x676c7966 |
| 111 |
#define headTag 0x68656164 |
| 112 |
#define hheaTag 0x68686561 |
| 113 |
#define hmtxTag 0x686d7478 |
| 114 |
#define locaTag 0x6c6f6361 |
| 115 |
#define nameTag 0x6e616d65 |
| 116 |
#define os2Tag 0x4f532f32 |
| 117 |
#define postTag 0x706f7374 |
| 118 |
|
| 119 |
#if HAVE_STD_SORT |
| 120 |
|
| 121 |
struct cmpTrueTypeLocaOffsetFunctor { |
| 122 |
bool operator()(const TrueTypeLoca &loca1, const TrueTypeLoca &loca2) { |
| 123 |
if (loca1.origOffset == loca2.origOffset) { |
| 124 |
return loca1.idx < loca2.idx; |
| 125 |
} |
| 126 |
return loca1.origOffset < loca2.origOffset; |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
struct cmpTrueTypeLocaIdxFunctor { |
| 131 |
bool operator()(const TrueTypeLoca &loca1, const TrueTypeLoca &loca2) { |
| 132 |
return loca1.idx < loca2.idx; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
struct cmpTrueTypeTableTagFunctor { |
| 137 |
bool operator()(const TrueTypeTable &tab1, const TrueTypeTable &tab2) { |
| 138 |
return tab1.tag < tab2.tag; |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
#else // HAVE_STD_SORT |
| 143 |
|
| 144 |
static int cmpTrueTypeLocaOffset(const void *p1, const void *p2) { |
| 145 |
TrueTypeLoca *loca1 = (TrueTypeLoca *)p1; |
| 146 |
TrueTypeLoca *loca2 = (TrueTypeLoca *)p2; |
| 147 |
|
| 148 |
if (loca1->origOffset == loca2->origOffset) { |
| 149 |
return loca1->idx - loca2->idx; |
| 150 |
} |
| 151 |
return loca1->origOffset - loca2->origOffset; |
| 152 |
} |
| 153 |
|
| 154 |
static int cmpTrueTypeLocaIdx(const void *p1, const void *p2) { |
| 155 |
TrueTypeLoca *loca1 = (TrueTypeLoca *)p1; |
| 156 |
TrueTypeLoca *loca2 = (TrueTypeLoca *)p2; |
| 157 |
|
| 158 |
return loca1->idx - loca2->idx; |
| 159 |
} |
| 160 |
|
| 161 |
static int cmpTrueTypeTableTag(const void *p1, const void *p2) { |
| 162 |
TrueTypeTable *tab1 = (TrueTypeTable *)p1; |
| 163 |
TrueTypeTable *tab2 = (TrueTypeTable *)p2; |
| 164 |
|
| 165 |
return (int)tab1->tag - (int)tab2->tag; |
| 166 |
} |
| 167 |
|
| 168 |
#endif // HAVE_STD_SORT |
| 169 |
|
| 170 |
//------------------------------------------------------------------------ |
| 171 |
|
| 172 |
struct T42Table { |
| 173 |
const char *tag; // 4-byte tag |
| 174 |
GBool required; // required by the TrueType spec? |
| 175 |
}; |
| 176 |
|
| 177 |
// TrueType tables to be embedded in Type 42 fonts. |
| 178 |
// NB: the table names must be in alphabetical order here. |
| 179 |
#define nT42Tables 11 |
| 180 |
static T42Table t42Tables[nT42Tables] = { |
| 181 |
{ "cvt ", gTrue }, |
| 182 |
{ "fpgm", gTrue }, |
| 183 |
{ "glyf", gTrue }, |
| 184 |
{ "head", gTrue }, |
| 185 |
{ "hhea", gTrue }, |
| 186 |
{ "hmtx", gTrue }, |
| 187 |
{ "loca", gTrue }, |
| 188 |
{ "maxp", gTrue }, |
| 189 |
{ "prep", gTrue }, |
| 190 |
{ "vhea", gFalse }, |
| 191 |
{ "vmtx", gFalse } |
| 192 |
}; |
| 193 |
#define t42HeadTable 3 |
| 194 |
#define t42LocaTable 6 |
| 195 |
#define t42GlyfTable 2 |
| 196 |
#define t42VheaTable 9 |
| 197 |
#define t42VmtxTable 10 |
| 198 |
|
| 199 |
//------------------------------------------------------------------------ |
| 200 |
|
| 201 |
// Glyph names in some arbitrary standard order that Apple uses for |
| 202 |
// their TrueType fonts. |
| 203 |
static const char *macGlyphNames[258] = { |
| 204 |
".notdef", "null", "CR", "space", |
| 205 |
"exclam", "quotedbl", "numbersign", "dollar", |
| 206 |
"percent", "ampersand", "quotesingle", "parenleft", |
| 207 |
"parenright", "asterisk", "plus", "comma", |
| 208 |
"hyphen", "period", "slash", "zero", |
| 209 |
"one", "two", "three", "four", |
| 210 |
"five", "six", "seven", "eight", |
| 211 |
"nine", "colon", "semicolon", "less", |
| 212 |
"equal", "greater", "question", "at", |
| 213 |
"A", "B", "C", "D", |
| 214 |
"E", "F", "G", "H", |
| 215 |
"I", "J", "K", "L", |
| 216 |
"M", "N", "O", "P", |
| 217 |
"Q", "R", "S", "T", |
| 218 |
"U", "V", "W", "X", |
| 219 |
"Y", "Z", "bracketleft", "backslash", |
| 220 |
"bracketright", "asciicircum", "underscore", "grave", |
| 221 |
"a", "b", "c", "d", |
| 222 |
"e", "f", "g", "h", |
| 223 |
"i", "j", "k", "l", |
| 224 |
"m", "n", "o", "p", |
| 225 |
"q", "r", "s", "t", |
| 226 |
"u", "v", "w", "x", |
| 227 |
"y", "z", "braceleft", "bar", |
| 228 |
"braceright", "asciitilde", "Adieresis", "Aring", |
| 229 |
"Ccedilla", "Eacute", "Ntilde", "Odieresis", |
| 230 |
"Udieresis", "aacute", "agrave", "acircumflex", |
| 231 |
"adieresis", "atilde", "aring", "ccedilla", |
| 232 |
"eacute", "egrave", "ecircumflex", "edieresis", |
| 233 |
"iacute", "igrave", "icircumflex", "idieresis", |
| 234 |
"ntilde", "oacute", "ograve", "ocircumflex", |
| 235 |
"odieresis", "otilde", "uacute", "ugrave", |
| 236 |
"ucircumflex", "udieresis", "dagger", "degree", |
| 237 |
"cent", "sterling", "section", "bullet", |
| 238 |
"paragraph", "germandbls", "registered", "copyright", |
| 239 |
"trademark", "acute", "dieresis", "notequal", |
| 240 |
"AE", "Oslash", "infinity", "plusminus", |
| 241 |
"lessequal", "greaterequal", "yen", "mu1", |
| 242 |
"partialdiff", "summation", "product", "pi", |
| 243 |
"integral", "ordfeminine", "ordmasculine", "Ohm", |
| 244 |
"ae", "oslash", "questiondown", "exclamdown", |
| 245 |
"logicalnot", "radical", "florin", "approxequal", |
| 246 |
"increment", "guillemotleft", "guillemotright", "ellipsis", |
| 247 |
"nbspace", "Agrave", "Atilde", "Otilde", |
| 248 |
"OE", "oe", "endash", "emdash", |
| 249 |
"quotedblleft", "quotedblright", "quoteleft", "quoteright", |
| 250 |
"divide", "lozenge", "ydieresis", "Ydieresis", |
| 251 |
"fraction", "currency", "guilsinglleft", "guilsinglright", |
| 252 |
"fi", "fl", "daggerdbl", "periodcentered", |
| 253 |
"quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", |
| 254 |
"Ecircumflex", "Aacute", "Edieresis", "Egrave", |
| 255 |
"Iacute", "Icircumflex", "Idieresis", "Igrave", |
| 256 |
"Oacute", "Ocircumflex", "applelogo", "Ograve", |
| 257 |
"Uacute", "Ucircumflex", "Ugrave", "dotlessi", |
| 258 |
"circumflex", "tilde", "overscore", "breve", |
| 259 |
"dotaccent", "ring", "cedilla", "hungarumlaut", |
| 260 |
"ogonek", "caron", "Lslash", "lslash", |
| 261 |
"Scaron", "scaron", "Zcaron", "zcaron", |
| 262 |
"brokenbar", "Eth", "eth", "Yacute", |
| 263 |
"yacute", "Thorn", "thorn", "minus", |
| 264 |
"multiply", "onesuperior", "twosuperior", "threesuperior", |
| 265 |
"onehalf", "onequarter", "threequarters", "franc", |
| 266 |
"Gbreve", "gbreve", "Idot", "Scedilla", |
| 267 |
"scedilla", "Cacute", "cacute", "Ccaron", |
| 268 |
"ccaron", "dmacron" |
| 269 |
}; |
| 270 |
|
| 271 |
//------------------------------------------------------------------------ |
| 272 |
// FoFiTrueType |
| 273 |
//------------------------------------------------------------------------ |
| 274 |
|
| 275 |
FoFiTrueType *FoFiTrueType::make(char *fileA, int lenA, int fontNum, |
| 276 |
GBool allowHeadlessCFF) { |
| 277 |
FoFiTrueType *ff; |
| 278 |
|
| 279 |
ff = new FoFiTrueType(fileA, lenA, gFalse, fontNum, gFalse, allowHeadlessCFF); |
| 280 |
if (!ff->parsedOk) { |
| 281 |
delete ff; |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
return ff; |
| 285 |
} |
| 286 |
|
| 287 |
FoFiTrueType *FoFiTrueType::load(char *fileName, int fontNum, |
| 288 |
GBool allowHeadlessCFF) { |
| 289 |
FoFiTrueType *ff; |
| 290 |
char *fileA; |
| 291 |
int lenA, n; |
| 292 |
GBool isDfontA; |
| 293 |
|
| 294 |
if (!(fileA = FoFiBase::readFile(fileName, &lenA))) { |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
n = (int)strlen(fileName); |
| 298 |
isDfontA = n >= 6 && !strcmp(fileName + n - 6, ".dfont"); |
| 299 |
ff = new FoFiTrueType(fileA, lenA, gTrue, fontNum, isDfontA, |
| 300 |
allowHeadlessCFF); |
| 301 |
if (!ff->parsedOk) { |
| 302 |
delete ff; |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
return ff; |
| 306 |
} |
| 307 |
|
| 308 |
FoFiTrueType::FoFiTrueType(char *fileA, int lenA, GBool freeFileDataA, |
| 309 |
int fontNum, GBool isDfontA, |
| 310 |
GBool allowHeadlessCFF): |
| 311 |
FoFiBase(fileA, lenA, freeFileDataA) |
| 312 |
{ |
| 313 |
tables = NULL; |
| 314 |
nTables = 0; |
| 315 |
cmaps = NULL; |
| 316 |
nCmaps = 0; |
| 317 |
nameToGID = NULL; |
| 318 |
isDfont = isDfontA; |
| 319 |
isTTC = gFalse; |
| 320 |
parsedOk = gFalse; |
| 321 |
|
| 322 |
parse(fontNum, allowHeadlessCFF); |
| 323 |
} |
| 324 |
|
| 325 |
FoFiTrueType::~FoFiTrueType() { |
| 326 |
gfree(tables); |
| 327 |
gfree(cmaps); |
| 328 |
if (nameToGID) { |
| 329 |
delete nameToGID; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
int FoFiTrueType::getNumCmaps() { |
| 334 |
return nCmaps; |
| 335 |
} |
| 336 |
|
| 337 |
int FoFiTrueType::getCmapPlatform(int i) { |
| 338 |
return cmaps[i].platform; |
| 339 |
} |
| 340 |
|
| 341 |
int FoFiTrueType::getCmapEncoding(int i) { |
| 342 |
return cmaps[i].encoding; |
| 343 |
} |
| 344 |
|
| 345 |
int FoFiTrueType::findCmap(int platform, int encoding) { |
| 346 |
int i; |
| 347 |
|
| 348 |
for (i = 0; i < nCmaps; ++i) { |
| 349 |
if (cmaps[i].platform == platform && cmaps[i].encoding == encoding) { |
| 350 |
return i; |
| 351 |
} |
| 352 |
} |
| 353 |
return -1; |
| 354 |
} |
| 355 |
|
| 356 |
int FoFiTrueType::mapCodeToGID(int i, int c) { |
| 357 |
int gid; |
| 358 |
int segCnt, segEnd, segStart, segDelta, segOffset; |
| 359 |
int cmapFirst, cmapLen; |
| 360 |
int pos, a, b, m; |
| 361 |
GBool ok; |
| 362 |
|
| 363 |
if (i < 0 || i >= nCmaps) { |
| 364 |
return 0; |
| 365 |
} |
| 366 |
ok = gTrue; |
| 367 |
pos = cmaps[i].offset; |
| 368 |
switch (cmaps[i].fmt) { |
| 369 |
case 0: |
| 370 |
if (c < 0 || c >= cmaps[i].len - 6) { |
| 371 |
return 0; |
| 372 |
} |
| 373 |
gid = getU8(pos + 6 + c, &ok); |
| 374 |
break; |
| 375 |
case 2: |
| 376 |
// this only handles single-byte codes |
| 377 |
if (c < 0 || c > 0xff) { |
| 378 |
return 0; |
| 379 |
} |
| 380 |
// check that: subHeaderKeys[0] = 0 |
| 381 |
// subHeaders[0].firstCode = 0 |
| 382 |
// subHeaders[0].entryCount = 256 |
| 383 |
// subHeaders[0].idDelta = 0 |
| 384 |
if (getU16BE(pos + 6, &ok) != 0 || |
| 385 |
getU16BE(pos + 518 + 0, &ok) != 0 || |
| 386 |
getU16BE(pos + 518 + 2, &ok) != 256 || |
| 387 |
getU16BE(pos + 518 + 4, &ok) != 0) { |
| 388 |
return 0; |
| 389 |
} |
| 390 |
// subHeaders[0].idRangeOffset is a byte offset from itself |
| 391 |
pos = pos + 518 + 6 + getU16BE(pos + 518 + 6, &ok); |
| 392 |
gid = getU16BE(pos + 2 * c, &ok); |
| 393 |
break; |
| 394 |
case 4: |
| 395 |
segCnt = getU16BE(pos + 6, &ok) / 2; |
| 396 |
a = -1; |
| 397 |
b = segCnt - 1; |
| 398 |
segEnd = getU16BE(pos + 14 + 2*b, &ok); |
| 399 |
if (c > segEnd) { |
| 400 |
// malformed font -- the TrueType spec requires the last segEnd |
| 401 |
// to be 0xffff |
| 402 |
return 0; |
| 403 |
} |
| 404 |
// invariant: seg[a].end < c <= seg[b].end |
| 405 |
while (b - a > 1 && ok) { |
| 406 |
m = (a + b) / 2; |
| 407 |
segEnd = getU16BE(pos + 14 + 2*m, &ok); |
| 408 |
if (segEnd < c) { |
| 409 |
a = m; |
| 410 |
} else { |
| 411 |
b = m; |
| 412 |
} |
| 413 |
} |
| 414 |
segStart = getU16BE(pos + 16 + 2*segCnt + 2*b, &ok); |
| 415 |
segDelta = getU16BE(pos + 16 + 4*segCnt + 2*b, &ok); |
| 416 |
segOffset = getU16BE(pos + 16 + 6*segCnt + 2*b, &ok); |
| 417 |
if (c < segStart) { |
| 418 |
return 0; |
| 419 |
} |
| 420 |
if (segOffset == 0) { |
| 421 |
gid = (c + segDelta) & 0xffff; |
| 422 |
} else { |
| 423 |
gid = getU16BE(pos + 16 + 6*segCnt + 2*b + |
| 424 |
segOffset + 2 * (c - segStart), &ok); |
| 425 |
if (gid != 0) { |
| 426 |
gid = (gid + segDelta) & 0xffff; |
| 427 |
} |
| 428 |
} |
| 429 |
break; |
| 430 |
case 6: |
| 431 |
cmapFirst = getU16BE(pos + 6, &ok); |
| 432 |
cmapLen = getU16BE(pos + 8, &ok); |
| 433 |
if (c < cmapFirst || c >= cmapFirst + cmapLen) { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
gid = getU16BE(pos + 10 + 2 * (c - cmapFirst), &ok); |
| 437 |
break; |
| 438 |
case 12: |
| 439 |
segCnt = getU32BE(pos + 12, &ok); |
| 440 |
a = -1; |
| 441 |
b = segCnt - 1; |
| 442 |
segEnd = getU32BE(pos + 16 + 12*b + 4, &ok); |
| 443 |
if (c > segEnd) { |
| 444 |
return 0; |
| 445 |
} |
| 446 |
// invariant: seg[a].end < c <= seg[b].end |
| 447 |
while (b - a > 1 && ok) { |
| 448 |
m = (a + b) / 2; |
| 449 |
segEnd = getU32BE(pos + 16 + 12*m + 4, &ok); |
| 450 |
if (segEnd < c) { |
| 451 |
a = m; |
| 452 |
} else { |
| 453 |
b = m; |
| 454 |
} |
| 455 |
} |
| 456 |
segStart = getU32BE(pos + 16 + 12*b, &ok); |
| 457 |
if (c < segStart) { |
| 458 |
return 0; |
| 459 |
} |
| 460 |
segOffset = getU32BE(pos + 16 + 12*b + 8, &ok); |
| 461 |
gid = segOffset + (c - segStart); |
| 462 |
break; |
| 463 |
default: |
| 464 |
return 0; |
| 465 |
} |
| 466 |
if (!ok) { |
| 467 |
return 0; |
| 468 |
} |
| 469 |
return gid; |
| 470 |
} |
| 471 |
|
| 472 |
int FoFiTrueType::mapNameToGID(char *name) { |
| 473 |
if (!nameToGID) { |
| 474 |
return 0; |
| 475 |
} |
| 476 |
return nameToGID->lookupInt(name); |
| 477 |
} |
| 478 |
|
| 479 |
GBool FoFiTrueType::getCFFBlock(char **start, int *length) { |
| 480 |
int i; |
| 481 |
|
| 482 |
if (!openTypeCFF) { |
| 483 |
return gFalse; |
| 484 |
} |
| 485 |
i = seekTable("CFF "); |
| 486 |
if (!checkRegion(tables[i].offset, tables[i].len)) { |
| 487 |
return gFalse; |
| 488 |
} |
| 489 |
*start = (char *)file + tables[i].offset; |
| 490 |
*length = tables[i].len; |
| 491 |
return gTrue; |
| 492 |
} |
| 493 |
|
| 494 |
int *FoFiTrueType::getCIDToGIDMap(int *nCIDs) { |
| 495 |
char *start; |
| 496 |
int length; |
| 497 |
FoFiType1C *ff; |
| 498 |
int *map; |
| 499 |
|
| 500 |
*nCIDs = 0; |
| 501 |
if (!getCFFBlock(&start, &length)) { |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
if (!(ff = FoFiType1C::make(start, length))) { |
| 505 |
return NULL; |
| 506 |
} |
| 507 |
map = ff->getCIDToGIDMap(nCIDs); |
| 508 |
delete ff; |
| 509 |
return map; |
| 510 |
} |
| 511 |
|
| 512 |
int FoFiTrueType::getEmbeddingRights() { |
| 513 |
int i, fsType; |
| 514 |
GBool ok; |
| 515 |
|
| 516 |
if ((i = seekTable("OS/2")) < 0) { |
| 517 |
return 4; |
| 518 |
} |
| 519 |
ok = gTrue; |
| 520 |
fsType = getU16BE(tables[i].offset + 8, &ok); |
| 521 |
if (!ok) { |
| 522 |
return 4; |
| 523 |
} |
| 524 |
if (fsType & 0x0008) { |
| 525 |
return 2; |
| 526 |
} |
| 527 |
if (fsType & 0x0004) { |
| 528 |
return 1; |
| 529 |
} |
| 530 |
if (fsType & 0x0002) { |
| 531 |
return 0; |
| 532 |
} |
| 533 |
return 3; |
| 534 |
} |
| 535 |
|
| 536 |
void FoFiTrueType::getFontMatrix(double *mat) { |
| 537 |
char *start; |
| 538 |
int length; |
| 539 |
FoFiType1C *ff; |
| 540 |
|
| 541 |
if (!getCFFBlock(&start, &length)) { |
| 542 |
return; |
| 543 |
} |
| 544 |
if (!(ff = FoFiType1C::make(start, length))) { |
| 545 |
return; |
| 546 |
} |
| 547 |
ff->getFontMatrix(mat); |
| 548 |
delete ff; |
| 549 |
} |
| 550 |
|
| 551 |
struct FoFiTrueTypeTrickyFont { |
| 552 |
int cvtLen; |
| 553 |
Guint cvtChecksum; |
| 554 |
int fpgmLen; |
| 555 |
Guint fpgmChecksum; |
| 556 |
int prepLen; |
| 557 |
Guint prepChecksum; |
| 558 |
}; |
| 559 |
|
| 560 |
// This data was taken from the FreeType project. |
| 561 |
static FoFiTrueTypeTrickyFont trickyFonts[] = { |
| 562 |
{ 0x000002e4, 0x05bcf058, // MingLiU 1995 |
| 563 |
0x000087c4, 0x28233bf1, |
| 564 |
0x000001e1, 0xa344a1ea }, |
| 565 |
{ 0x000002e4, 0x05bcf058, // MingLiU 1996- |
| 566 |
0x000087c4, 0x28233bf1, |
| 567 |
0x000001e1, 0xa344a1eb }, |
| 568 |
{ 0x00000350, 0x12c3ebb2, // DFGothic-EB |
| 569 |
0x000087a7, 0xb680ee64, |
| 570 |
0x00000758, 0xce939563 }, |
| 571 |
{ 0x00000350, 0x11e5ead4, // DFGyoSho-Lt |
| 572 |
0x0000bc85, 0xce5956e9, |
| 573 |
0x00000045, 0x8272f416 }, |
| 574 |
{ 0x00000350, 0x1262eb4e, // DFHSGothic-W5 |
| 575 |
0x00007940, 0xe86a5d64, |
| 576 |
0x000005ff, 0x7850f729 }, |
| 577 |
{ 0x00000350, 0x122deb0a, // DFHSMincho-W3 |
| 578 |
0x0000859b, 0x3d16328a, |
| 579 |
0x000002cb, 0xa93fc33b }, |
| 580 |
{ 0x00000350, 0x125feb26, // DFHSMincho-W7 |
| 581 |
0x00007ee1, 0xa5acc982, |
| 582 |
0x0000041f, 0x90999196 }, |
| 583 |
{ 0x00000350, 0x11e5ead4, // DFKaiShu |
| 584 |
0x00009063, 0x5a30ca3b, |
| 585 |
0x0000007e, 0x13a42602 }, |
| 586 |
{ 0x00000350, 0x11e5ead4, // DFKaiShu2 |
| 587 |
0x00008998, 0xa6e78c01, |
| 588 |
0x0000007e, 0x13a42602 }, |
| 589 |
{ 0x00000008, 0xfffbfffc, // HuaTianKaiTi |
| 590 |
0x0000bea2, 0x9c9e48b8, |
| 591 |
0x00000008, 0x70020112 }, |
| 592 |
{ 0x00000008, 0xfffbfffc, // HuaTianSongTi |
| 593 |
0x00017c39, 0x0a5a0483, |
| 594 |
0x00000008, 0x70020112 }, |
| 595 |
{ 0x00000000, 0x00000000, // NEC fadpop7.ttf |
| 596 |
0x000000e5, 0x40c92555, |
| 597 |
0x0000117c, 0xa39b58e3 }, |
| 598 |
{ 0x00000000, 0x00000000, // NEC fadrei5.ttf |
| 599 |
0x000000e5, 0x33c41652, |
| 600 |
0x00000f6a, 0x26d6c52a }, |
| 601 |
{ 0x00000000, 0x00000000, // NEC fangot7.ttf |
| 602 |
0x0000019d, 0x6db1651d, |
| 603 |
0x00002492, 0x6c6e4b03 }, |
| 604 |
{ 0x00000000, 0x00000000, // NEC fangyo5.ttf |
| 605 |
0x000000e5, 0x40c92555, |
| 606 |
0x0000117c, 0xde51fad0 }, |
| 607 |
{ 0x00000000, 0x00000000, // NEC fankyo5.ttf |
| 608 |
0x000000e5, 0x85e47664, |
| 609 |
0x00001caa, 0xa6c62831 }, |
| 610 |
{ 0x00000000, 0x00000000, // NEC fanrgo5.ttf |
| 611 |
0x0000019d, 0x2d891cfd, |
| 612 |
0x00001de8, 0xa0604633 }, |
| 613 |
{ 0x00000000, 0x00000000, // NEC fangot5.ttc |
| 614 |
0x000001cb, 0x40aa774c, |
| 615 |
0x00001f9a, 0x9b5caa96 }, |
| 616 |
{ 0x00000000, 0x00000000, // NEC fanmin3.ttc |
| 617 |
0x00000141, 0x0d3de9cb, |
| 618 |
0x00002280, 0xd4127766 }, |
| 619 |
{ 0x00000000, 0x00000000, // NEC FA-Gothic, 1996 |
| 620 |
0x000001f0, 0x4a692698, |
| 621 |
0x00001fca, 0x340d4346 }, |
| 622 |
{ 0x00000000, 0x00000000, // NEC FA-Minchou, 1996 |
| 623 |
0x00000166, 0xcd34c604, |
| 624 |
0x000022b0, 0x6cf31046 }, |
| 625 |
{ 0x00000000, 0x00000000, // NEC FA-RoundGothicB, 1996 |
| 626 |
0x0000019d, 0x5da75315, |
| 627 |
0x000022e0, 0x40745a5f }, |
| 628 |
{ 0x00000000, 0x00000000, // NEC FA-RoundGothicM, 1996 |
| 629 |
0x000001c2, 0xf055fc48, |
| 630 |
0x00001e18, 0x3900ded3 }, |
| 631 |
{ 0x00000060, 0x00170003, // MINGLI, 1992 |
| 632 |
0x000058aa, 0xdbb4306e, |
| 633 |
0x00000035, 0xd643482a } |
| 634 |
}; |
| 635 |
|
| 636 |
#define nTrickyFonts ((int)(sizeof(trickyFonts) / sizeof(FoFiTrueTypeTrickyFont))) |
| 637 |
|
| 638 |
// This looks at the length and checksum values for the cvt, fpgm, and |
| 639 |
// prep tables. The theory is that those tables will be the same, |
| 640 |
// even if the font is renamed and/or subsetted. |
| 641 |
GBool FoFiTrueType::checkForTrickyCJK() { |
| 642 |
int cvtIdx, fpgmIdx, prepIdx; |
| 643 |
int cvtLen, fpgmLen, prepLen; |
| 644 |
Guint cvtChecksum, fpgmChecksum, prepChecksum; |
| 645 |
int i; |
| 646 |
|
| 647 |
if ((cvtIdx = seekTable("cvt ")) >= 0) { |
| 648 |
cvtLen = tables[cvtIdx].len; |
| 649 |
cvtChecksum = tables[cvtIdx].checksum; |
| 650 |
} else { |
| 651 |
cvtLen = 0; |
| 652 |
cvtChecksum = 0; |
| 653 |
} |
| 654 |
if ((fpgmIdx = seekTable("fpgm")) >= 0) { |
| 655 |
fpgmLen = tables[fpgmIdx].len; |
| 656 |
fpgmChecksum = tables[fpgmIdx].checksum; |
| 657 |
} else { |
| 658 |
fpgmLen = 0; |
| 659 |
fpgmChecksum = 0; |
| 660 |
} |
| 661 |
if ((prepIdx = seekTable("prep")) >= 0) { |
| 662 |
prepLen = tables[prepIdx].len; |
| 663 |
prepChecksum = tables[prepIdx].checksum; |
| 664 |
} else { |
| 665 |
prepLen = 0; |
| 666 |
prepChecksum = 0; |
| 667 |
} |
| 668 |
|
| 669 |
for (i = 0; i < nTrickyFonts; ++i) { |
| 670 |
if (cvtLen == trickyFonts[i].cvtLen && |
| 671 |
cvtChecksum == trickyFonts[i].cvtChecksum && |
| 672 |
fpgmLen == trickyFonts[i].fpgmLen && |
| 673 |
fpgmChecksum == trickyFonts[i].fpgmChecksum && |
| 674 |
prepLen == trickyFonts[i].prepLen && |
| 675 |
prepChecksum == trickyFonts[i].prepChecksum) { |
| 676 |
return gTrue; |
| 677 |
} |
| 678 |
} |
| 679 |
return gFalse; |
| 680 |
} |
| 681 |
|
| 682 |
void FoFiTrueType::convertToType42(char *psName, char **encoding, |
| 683 |
int *codeToGID, |
| 684 |
FoFiOutputFunc outputFunc, |
| 685 |
void *outputStream) { |
| 686 |
GString *buf; |
| 687 |
int maxUsedGlyph; |
| 688 |
GBool ok; |
| 689 |
|
| 690 |
if (openTypeCFF) { |
| 691 |
return; |
| 692 |
} |
| 693 |
|
| 694 |
// write the header |
| 695 |
ok = gTrue; |
| 696 |
buf = GString::format("%!PS-TrueTypeFont-{0:2g}\n", |
| 697 |
(double)getS32BE(0, &ok) / 65536.0); |
| 698 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 699 |
delete buf; |
| 700 |
|
| 701 |
// begin the font dictionary |
| 702 |
(*outputFunc)(outputStream, "10 dict begin\n", 14); |
| 703 |
(*outputFunc)(outputStream, "/FontName /", 11); |
| 704 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 705 |
(*outputFunc)(outputStream, " def\n", 5); |
| 706 |
(*outputFunc)(outputStream, "/FontType 42 def\n", 17); |
| 707 |
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30); |
| 708 |
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n", |
| 709 |
bbox[0], bbox[1], bbox[2], bbox[3]); |
| 710 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 711 |
delete buf; |
| 712 |
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17); |
| 713 |
|
| 714 |
// write the guts of the dictionary |
| 715 |
cvtEncoding(encoding, outputFunc, outputStream); |
| 716 |
cvtCharStrings(encoding, codeToGID, outputFunc, outputStream); |
| 717 |
cvtSfnts(outputFunc, outputStream, NULL, gFalse, &maxUsedGlyph); |
| 718 |
|
| 719 |
// end the dictionary and define the font |
| 720 |
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40); |
| 721 |
} |
| 722 |
|
| 723 |
void FoFiTrueType::convertToType1(char *psName, const char **newEncoding, |
| 724 |
GBool ascii, FoFiOutputFunc outputFunc, |
| 725 |
void *outputStream) { |
| 726 |
char *start; |
| 727 |
int length; |
| 728 |
FoFiType1C *ff; |
| 729 |
|
| 730 |
if (!getCFFBlock(&start, &length)) { |
| 731 |
return; |
| 732 |
} |
| 733 |
if (!(ff = FoFiType1C::make(start, length))) { |
| 734 |
return; |
| 735 |
} |
| 736 |
ff->convertToType1(psName, newEncoding, ascii, outputFunc, outputStream); |
| 737 |
delete ff; |
| 738 |
} |
| 739 |
|
| 740 |
void FoFiTrueType::convertToCIDType2(char *psName, |
| 741 |
int *cidMap, int nCIDs, |
| 742 |
GBool needVerticalMetrics, |
| 743 |
FoFiOutputFunc outputFunc, |
| 744 |
void *outputStream) { |
| 745 |
GString *buf; |
| 746 |
int cid, maxUsedGlyph; |
| 747 |
GBool ok; |
| 748 |
int i, j, k; |
| 749 |
|
| 750 |
if (openTypeCFF) { |
| 751 |
return; |
| 752 |
} |
| 753 |
|
| 754 |
// write the header |
| 755 |
ok = gTrue; |
| 756 |
buf = GString::format("%!PS-TrueTypeFont-{0:2g}\n", |
| 757 |
(double)getS32BE(0, &ok) / 65536.0); |
| 758 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 759 |
delete buf; |
| 760 |
|
| 761 |
// begin the font dictionary |
| 762 |
(*outputFunc)(outputStream, "20 dict begin\n", 14); |
| 763 |
(*outputFunc)(outputStream, "/CIDFontName /", 14); |
| 764 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 765 |
(*outputFunc)(outputStream, " def\n", 5); |
| 766 |
(*outputFunc)(outputStream, "/CIDFontType 2 def\n", 19); |
| 767 |
(*outputFunc)(outputStream, "/FontType 42 def\n", 17); |
| 768 |
(*outputFunc)(outputStream, "/CIDSystemInfo 3 dict dup begin\n", 32); |
| 769 |
(*outputFunc)(outputStream, " /Registry (Adobe) def\n", 24); |
| 770 |
(*outputFunc)(outputStream, " /Ordering (Identity) def\n", 27); |
| 771 |
(*outputFunc)(outputStream, " /Supplement 0 def\n", 20); |
| 772 |
(*outputFunc)(outputStream, " end def\n", 10); |
| 773 |
(*outputFunc)(outputStream, "/GDBytes 2 def\n", 15); |
| 774 |
if (cidMap) { |
| 775 |
buf = GString::format("/CIDCount {0:d} def\n", nCIDs); |
| 776 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 777 |
delete buf; |
| 778 |
if (nCIDs > 32767) { |
| 779 |
(*outputFunc)(outputStream, "/CIDMap [", 9); |
| 780 |
for (i = 0; i < nCIDs; i += 32768 - 16) { |
| 781 |
(*outputFunc)(outputStream, "<\n", 2); |
| 782 |
for (j = 0; j < 32768 - 16 && i+j < nCIDs; j += 16) { |
| 783 |
(*outputFunc)(outputStream, " ", 2); |
| 784 |
for (k = 0; k < 16 && i+j+k < nCIDs; ++k) { |
| 785 |
cid = cidMap[i+j+k]; |
| 786 |
buf = GString::format("{0:02x}{1:02x}", |
| 787 |
(cid >> 8) & 0xff, cid & 0xff); |
| 788 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 789 |
delete buf; |
| 790 |
} |
| 791 |
(*outputFunc)(outputStream, "\n", 1); |
| 792 |
} |
| 793 |
(*outputFunc)(outputStream, " >", 3); |
| 794 |
} |
| 795 |
(*outputFunc)(outputStream, "\n", 1); |
| 796 |
(*outputFunc)(outputStream, "] def\n", 6); |
| 797 |
} else { |
| 798 |
(*outputFunc)(outputStream, "/CIDMap <\n", 10); |
| 799 |
for (i = 0; i < nCIDs; i += 16) { |
| 800 |
(*outputFunc)(outputStream, " ", 2); |
| 801 |
for (j = 0; j < 16 && i+j < nCIDs; ++j) { |
| 802 |
cid = cidMap[i+j]; |
| 803 |
buf = GString::format("{0:02x}{1:02x}", |
| 804 |
(cid >> 8) & 0xff, cid & 0xff); |
| 805 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 806 |
delete buf; |
| 807 |
} |
| 808 |
(*outputFunc)(outputStream, "\n", 1); |
| 809 |
} |
| 810 |
(*outputFunc)(outputStream, "> def\n", 6); |
| 811 |
} |
| 812 |
} else { |
| 813 |
// direct mapping - just fill the string(s) with s[i]=i |
| 814 |
buf = GString::format("/CIDCount {0:d} def\n", nGlyphs); |
| 815 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 816 |
delete buf; |
| 817 |
if (nGlyphs > 32767) { |
| 818 |
(*outputFunc)(outputStream, "/CIDMap [\n", 10); |
| 819 |
for (i = 0; i < nGlyphs; i += 32767) { |
| 820 |
j = nGlyphs - i < 32767 ? nGlyphs - i : 32767; |
| 821 |
buf = GString::format(" {0:d} string 0 1 {1:d} {{\n", 2 * j, j - 1); |
| 822 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 823 |
delete buf; |
| 824 |
buf = GString::format(" 2 copy dup 2 mul exch {0:d} add -8 bitshift put\n", |
| 825 |
i); |
| 826 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 827 |
delete buf; |
| 828 |
buf = GString::format(" 1 index exch dup 2 mul 1 add exch {0:d} add" |
| 829 |
" 255 and put\n", i); |
| 830 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 831 |
delete buf; |
| 832 |
(*outputFunc)(outputStream, " } for\n", 8); |
| 833 |
} |
| 834 |
(*outputFunc)(outputStream, "] def\n", 6); |
| 835 |
} else { |
| 836 |
buf = GString::format("/CIDMap {0:d} string\n", 2 * nGlyphs); |
| 837 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 838 |
delete buf; |
| 839 |
buf = GString::format(" 0 1 {0:d} {{\n", nGlyphs - 1); |
| 840 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 841 |
delete buf; |
| 842 |
(*outputFunc)(outputStream, |
| 843 |
" 2 copy dup 2 mul exch -8 bitshift put\n", 42); |
| 844 |
(*outputFunc)(outputStream, |
| 845 |
" 1 index exch dup 2 mul 1 add exch 255 and put\n", 50); |
| 846 |
(*outputFunc)(outputStream, " } for\n", 8); |
| 847 |
(*outputFunc)(outputStream, "def\n", 4); |
| 848 |
} |
| 849 |
} |
| 850 |
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30); |
| 851 |
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n", |
| 852 |
bbox[0], bbox[1], bbox[2], bbox[3]); |
| 853 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 854 |
delete buf; |
| 855 |
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17); |
| 856 |
(*outputFunc)(outputStream, "/Encoding [] readonly def\n", 26); |
| 857 |
(*outputFunc)(outputStream, "/CharStrings 1 dict dup begin\n", 30); |
| 858 |
(*outputFunc)(outputStream, " /.notdef 0 def\n", 17); |
| 859 |
(*outputFunc)(outputStream, " end readonly def\n", 19); |
| 860 |
|
| 861 |
// write the guts of the dictionary |
| 862 |
cvtSfnts(outputFunc, outputStream, NULL, needVerticalMetrics, &maxUsedGlyph); |
| 863 |
|
| 864 |
// end the dictionary and define the font |
| 865 |
(*outputFunc)(outputStream, |
| 866 |
"CIDFontName currentdict end /CIDFont defineresource pop\n", |
| 867 |
56); |
| 868 |
} |
| 869 |
|
| 870 |
void FoFiTrueType::convertToCIDType0(char *psName, int *cidMap, int nCIDs, |
| 871 |
FoFiOutputFunc outputFunc, |
| 872 |
void *outputStream) { |
| 873 |
char *start; |
| 874 |
int length; |
| 875 |
FoFiType1C *ff; |
| 876 |
|
| 877 |
if (!getCFFBlock(&start, &length)) { |
| 878 |
return; |
| 879 |
} |
| 880 |
if (!(ff = FoFiType1C::make(start, length))) { |
| 881 |
return; |
| 882 |
} |
| 883 |
ff->convertToCIDType0(psName, cidMap, nCIDs, outputFunc, outputStream); |
| 884 |
delete ff; |
| 885 |
} |
| 886 |
|
| 887 |
void FoFiTrueType::convertToType0(char *psName, int *cidMap, int nCIDs, |
| 888 |
GBool needVerticalMetrics, |
| 889 |
FoFiOutputFunc outputFunc, |
| 890 |
void *outputStream) { |
| 891 |
GString *buf; |
| 892 |
GString *sfntsName; |
| 893 |
int maxUsedGlyph, n, i, j; |
| 894 |
|
| 895 |
if (openTypeCFF) { |
| 896 |
return; |
| 897 |
} |
| 898 |
|
| 899 |
// write the Type 42 sfnts array |
| 900 |
sfntsName = (new GString(psName))->append("_sfnts"); |
| 901 |
cvtSfnts(outputFunc, outputStream, sfntsName, needVerticalMetrics, |
| 902 |
&maxUsedGlyph); |
| 903 |
delete sfntsName; |
| 904 |
|
| 905 |
// write the descendant Type 42 fonts |
| 906 |
// (The following is a kludge: nGlyphs is the glyph count from the |
| 907 |
// maxp table; maxUsedGlyph is the max glyph number that has a |
| 908 |
// non-zero-length description, from the loca table. The problem is |
| 909 |
// that some TrueType font subsets fail to change the glyph count, |
| 910 |
// i.e., nGlyphs is much larger than maxUsedGlyph+1, which results |
| 911 |
// in an unnecessarily huge Type 0 font. But some other PDF files |
| 912 |
// have fonts with only zero or one used glyph, and a content stream |
| 913 |
// that refers to one of the unused glyphs -- this results in PS |
| 914 |
// errors if we simply use maxUsedGlyph+1 for the Type 0 font. So |
| 915 |
// we compromise by always defining at least 256 glyphs.) |
| 916 |
if (cidMap) { |
| 917 |
n = nCIDs; |
| 918 |
} else if (nGlyphs > maxUsedGlyph + 256) { |
| 919 |
if (maxUsedGlyph <= 255) { |
| 920 |
n = 256; |
| 921 |
} else { |
| 922 |
n = maxUsedGlyph + 1; |
| 923 |
} |
| 924 |
} else { |
| 925 |
n = nGlyphs; |
| 926 |
} |
| 927 |
for (i = 0; i < n; i += 256) { |
| 928 |
(*outputFunc)(outputStream, "10 dict begin\n", 14); |
| 929 |
(*outputFunc)(outputStream, "/FontName /", 11); |
| 930 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 931 |
buf = GString::format("_{0:02x} def\n", i >> 8); |
| 932 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 933 |
delete buf; |
| 934 |
(*outputFunc)(outputStream, "/FontType 42 def\n", 17); |
| 935 |
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30); |
| 936 |
buf = GString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n", |
| 937 |
bbox[0], bbox[1], bbox[2], bbox[3]); |
| 938 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 939 |
delete buf; |
| 940 |
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17); |
| 941 |
(*outputFunc)(outputStream, "/sfnts ", 7); |
| 942 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 943 |
(*outputFunc)(outputStream, "_sfnts def\n", 11); |
| 944 |
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20); |
| 945 |
for (j = 0; j < 256 && i+j < n; ++j) { |
| 946 |
buf = GString::format("dup {0:d} /c{1:02x} put\n", j, j); |
| 947 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 948 |
delete buf; |
| 949 |
} |
| 950 |
(*outputFunc)(outputStream, "readonly def\n", 13); |
| 951 |
(*outputFunc)(outputStream, "/CharStrings 257 dict dup begin\n", 32); |
| 952 |
(*outputFunc)(outputStream, "/.notdef 0 def\n", 15); |
| 953 |
for (j = 0; j < 256 && i+j < n; ++j) { |
| 954 |
buf = GString::format("/c{0:02x} {1:d} def\n", |
| 955 |
j, cidMap ? cidMap[i+j] : i+j); |
| 956 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 957 |
delete buf; |
| 958 |
} |
| 959 |
(*outputFunc)(outputStream, "end readonly def\n", 17); |
| 960 |
(*outputFunc)(outputStream, |
| 961 |
"FontName currentdict end definefont pop\n", 40); |
| 962 |
} |
| 963 |
|
| 964 |
// write the Type 0 parent font |
| 965 |
(*outputFunc)(outputStream, "16 dict begin\n", 14); |
| 966 |
(*outputFunc)(outputStream, "/FontName /", 11); |
| 967 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 968 |
(*outputFunc)(outputStream, " def\n", 5); |
| 969 |
(*outputFunc)(outputStream, "/FontType 0 def\n", 16); |
| 970 |
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30); |
| 971 |
(*outputFunc)(outputStream, "/FMapType 2 def\n", 16); |
| 972 |
(*outputFunc)(outputStream, "/Encoding [\n", 12); |
| 973 |
for (i = 0; i < n; i += 256) { |
| 974 |
buf = GString::format("{0:d}\n", i >> 8); |
| 975 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 976 |
delete buf; |
| 977 |
} |
| 978 |
(*outputFunc)(outputStream, "] def\n", 6); |
| 979 |
(*outputFunc)(outputStream, "/FDepVector [\n", 14); |
| 980 |
for (i = 0; i < n; i += 256) { |
| 981 |
(*outputFunc)(outputStream, "/", 1); |
| 982 |
(*outputFunc)(outputStream, psName, (int)strlen(psName)); |
| 983 |
buf = GString::format("_{0:02x} findfont\n", i >> 8); |
| 984 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 985 |
delete buf; |
| 986 |
} |
| 987 |
(*outputFunc)(outputStream, "] def\n", 6); |
| 988 |
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40); |
| 989 |
} |
| 990 |
|
| 991 |
void FoFiTrueType::convertToType0(char *psName, int *cidMap, int nCIDs, |
| 992 |
FoFiOutputFunc outputFunc, |
| 993 |
void *outputStream) { |
| 994 |
char *start; |
| 995 |
int length; |
| 996 |
FoFiType1C *ff; |
| 997 |
|
| 998 |
if (!getCFFBlock(&start, &length)) { |
| 999 |
return; |
| 1000 |
} |
| 1001 |
if (!(ff = FoFiType1C::make(start, length))) { |
| 1002 |
return; |
| 1003 |
} |
| 1004 |
ff->convertToType0(psName, cidMap, nCIDs, outputFunc, outputStream); |
| 1005 |
delete ff; |
| 1006 |
} |
| 1007 |
|
| 1008 |
GBool FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc, |
| 1009 |
void *outputStream, char *name, |
| 1010 |
int *codeToGID, Guchar *replacementCmapTable, |
| 1011 |
int replacementCmapTableLen) { |
| 1012 |
// this substitute cmap table maps char code ffff to glyph 0, |
| 1013 |
// with tables for MacRoman and MS Unicode |
| 1014 |
static char cmapTab[44] = { |
| 1015 |
0, 0, // table version number |
| 1016 |
0, 2, // number of encoding tables |
| 1017 |
0, 1, // platform ID |
| 1018 |
0, 0, // encoding ID |
| 1019 |
0, 0, 0, 20, // offset of subtable |
| 1020 |
0, 3, // platform ID |
| 1021 |
0, 1, // encoding ID |
| 1022 |
0, 0, 0, 20, // offset of subtable |
| 1023 |
0, 4, // subtable format |
| 1024 |
0, 24, // subtable length |
| 1025 |
0, 0, // subtable version |
| 1026 |
0, 2, // segment count * 2 |
| 1027 |
0, 2, // 2 * 2 ^ floor(log2(segCount)) |
| 1028 |
0, 0, // floor(log2(segCount)) |
| 1029 |
0, 0, // 2*segCount - 2*2^floor(log2(segCount)) |
| 1030 |
(char)0xff, (char)0xff, // endCount[0] |
| 1031 |
0, 0, // reserved |
| 1032 |
(char)0xff, (char)0xff, // startCount[0] |
| 1033 |
0, 1, // idDelta[0] |
| 1034 |
0, 0 // idRangeOffset[0] |
| 1035 |
}; |
| 1036 |
static char nameTab[8] = { |
| 1037 |
0, 0, // format |
| 1038 |
0, 0, // number of name records |
| 1039 |
0, 6, // offset to start of string storage |
| 1040 |
0, 0 // pad to multiple of four bytes |
| 1041 |
}; |
| 1042 |
static char postTab[32] = { |
| 1043 |
0, 1, 0, 0, // format |
| 1044 |
0, 0, 0, 0, // italic angle |
| 1045 |
0, 0, // underline position |
| 1046 |
0, 0, // underline thickness |
| 1047 |
0, 0, 0, 0, // fixed pitch |
| 1048 |
0, 0, 0, 0, // min Type 42 memory |
| 1049 |
0, 0, 0, 0, // max Type 42 memory |
| 1050 |
0, 0, 0, 0, // min Type 1 memory |
| 1051 |
0, 0, 0, 0 // max Type 1 memory |
| 1052 |
}; |
| 1053 |
static char os2Tab[86] = { |
| 1054 |
0, 1, // version |
| 1055 |
0, 1, // xAvgCharWidth |
| 1056 |
0x01, (char)0x90, // usWeightClass |
| 1057 |
0, 5, // usWidthClass |
| 1058 |
0, 0, // fsType |
| 1059 |
0, 0, // ySubscriptXSize |
| 1060 |
0, 0, // ySubscriptYSize |
| 1061 |
0, 0, // ySubscriptXOffset |
| 1062 |
0, 0, // ySubscriptYOffset |
| 1063 |
0, 0, // ySuperscriptXSize |
| 1064 |
0, 0, // ySuperscriptYSize |
| 1065 |
0, 0, // ySuperscriptXOffset |
| 1066 |
0, 0, // ySuperscriptYOffset |
| 1067 |
0, 0, // yStrikeoutSize |
| 1068 |
0, 0, // yStrikeoutPosition |
| 1069 |
0, 0, // sFamilyClass |
| 1070 |
0, 0, 0, 0, 0, // panose |
| 1071 |
0, 0, 0, 0, 0, |
| 1072 |
0, 0, 0, 0, // ulUnicodeRange1 |
| 1073 |
0, 0, 0, 0, // ulUnicodeRange2 |
| 1074 |
0, 0, 0, 0, // ulUnicodeRange3 |
| 1075 |
0, 0, 0, 0, // ulUnicodeRange4 |
| 1076 |
0, 0, 0, 0, // achVendID |
| 1077 |
0, 0, // fsSelection |
| 1078 |
0, 0, // usFirstCharIndex |
| 1079 |
0, 0, // usLastCharIndex |
| 1080 |
0, 0, // sTypoAscender |
| 1081 |
0, 0, // sTypoDescender |
| 1082 |
0, 0, // sTypoLineGap |
| 1083 |
0x20, 0x00, // usWinAscent |
| 1084 |
0x20, 0x00, // usWinDescent |
| 1085 |
0, 0, 0, 1, // ulCodePageRange1 |
| 1086 |
0, 0, 0, 0 // ulCodePageRange2 |
| 1087 |
}; |
| 1088 |
GBool missingCmap, missingName, missingPost, missingOS2; |
| 1089 |
GBool unsortedLoca, bogusLastLoca, emptyCmap, badCmapLen, abbrevHMTX; |
| 1090 |
int nZeroLengthTables, nBogusTables; |
| 1091 |
int nHMetrics, advWidth, lsb; |
| 1092 |
TrueTypeLoca *locaTable; |
| 1093 |
TrueTypeTable *newTables; |
| 1094 |
char *newNameTab, *newCmapTab, *newHHEATab, *newHMTXTab; |
| 1095 |
int nNewTables, cmapIdx, cmapLen, glyfLen, newNameLen, newCmapLen, next; |
| 1096 |
int newHHEALen, newHMTXLen; |
| 1097 |
Guint locaChecksum, glyfChecksum, fileChecksum; |
| 1098 |
char *tableDir; |
| 1099 |
char locaBuf[4], checksumBuf[4]; |
| 1100 |
GBool ok; |
| 1101 |
Guint t; |
| 1102 |
int pos, i, j, k, n; |
| 1103 |
|
| 1104 |
if (openTypeCFF) { |
| 1105 |
return gFalse; |
| 1106 |
} |
| 1107 |
|
| 1108 |
// check for missing tables |
| 1109 |
// (Note: if the OS/2 table is missing, the Microsoft PCL5 driver |
| 1110 |
// will embed a PCL TrueType font with the pitch field set to zero, |
| 1111 |
// which apparently causes divide-by-zero errors. As far as I can |
| 1112 |
// tell, the only important field in the OS/2 table is |
| 1113 |
// xAvgCharWidth.) |
| 1114 |
missingCmap = (cmapIdx = seekTable("cmap")) < 0; |
| 1115 |
missingName = seekTable("name") < 0; |
| 1116 |
missingPost = seekTable("post") < 0; |
| 1117 |
missingOS2 = seekTable("OS/2") < 0; |
| 1118 |
|
| 1119 |
// read the loca table, check to see if it's sorted |
| 1120 |
locaTable = (TrueTypeLoca *)gmallocn(nGlyphs + 1, sizeof(TrueTypeLoca)); |
| 1121 |
unsortedLoca = gFalse; |
| 1122 |
bogusLastLoca = gFalse; |
| 1123 |
i = seekTable("loca"); |
| 1124 |
pos = tables[i].offset; |
| 1125 |
glyfLen = tables[seekTable("glyf")].len; |
| 1126 |
ok = gTrue; |
| 1127 |
for (i = 0; i <= nGlyphs; ++i) { |
| 1128 |
if (locaFmt) { |
| 1129 |
locaTable[i].origOffset = (int)getU32BE(pos + i*4, &ok); |
| 1130 |
} else { |
| 1131 |
locaTable[i].origOffset = 2 * getU16BE(pos + i*2, &ok); |
| 1132 |
} |
| 1133 |
if (locaTable[i].origOffset < 0 || |
| 1134 |
locaTable[i].origOffset > glyfLen) { |
| 1135 |
locaTable[i].origOffset = glyfLen; |
| 1136 |
unsortedLoca = gTrue; |
| 1137 |
} |
| 1138 |
if (i > 0 && locaTable[i].origOffset < locaTable[i-1].origOffset) { |
| 1139 |
if (i < nGlyphs) { |
| 1140 |
unsortedLoca = gTrue; |
| 1141 |
} else { |
| 1142 |
bogusLastLoca = gTrue; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
// glyph descriptions must be at least 12 bytes long (nContours, |
| 1146 |
// xMin, yMin, xMax, yMax, instructionLength - two bytes each); |
| 1147 |
// invalid glyph descriptions (even if they're never used) make |
| 1148 |
// Windows choke, so we work around that problem here (ideally, |
| 1149 |
// this would parse the glyph descriptions in the glyf table and |
| 1150 |
// remove any that were invalid, but this quick test is a decent |
| 1151 |
// start) |
| 1152 |
if (i > 0 && |
| 1153 |
locaTable[i].origOffset - locaTable[i-1].origOffset > 0 && |
| 1154 |
locaTable[i].origOffset - locaTable[i-1].origOffset < 12) { |
| 1155 |
locaTable[i-1].origOffset = locaTable[i].origOffset; |
| 1156 |
unsortedLoca = gTrue; |
| 1157 |
} |
| 1158 |
locaTable[i].idx = i; |
| 1159 |
} |
| 1160 |
if (unsortedLoca) { |
| 1161 |
bogusLastLoca = gFalse; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// check for zero-length tables and bogus tags |
| 1165 |
nZeroLengthTables = nBogusTables = 0; |
| 1166 |
for (i = 0; i < nTables; ++i) { |
| 1167 |
if (tables[i].len == 0) { |
| 1168 |
++nZeroLengthTables; |
| 1169 |
if (tables[i].tag == cmapTag) { |
| 1170 |
missingCmap = gTrue; |
| 1171 |
} else if (tables[i].tag == nameTag) { |
| 1172 |
missingName = gTrue; |
| 1173 |
} else if (tables[i].tag == postTag) { |
| 1174 |
missingPost = gTrue; |
| 1175 |
} else if (tables[i].tag == os2Tag) { |
| 1176 |
missingOS2 = gTrue; |
| 1177 |
} |
| 1178 |
} else if (!(tables[i].tag & 0xe0000000) || |
| 1179 |
!(tables[i].tag & 0x00e00000) || |
| 1180 |
!(tables[i].tag & 0x0000e000) || |
| 1181 |
!(tables[i].tag & 0x000000e0)) { |
| 1182 |
// tags where any of the bytes are < 0x20 are probably bogus |
| 1183 |
// (the TrueType spec uses ASCII sequences for tags) -- this |
| 1184 |
// catches problems where the number of tables given in the |
| 1185 |
// header is too large, and so gibberish data is read at the end |
| 1186 |
// of the table directory |
| 1187 |
++nBogusTables; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
// check for an empty cmap table or an incorrect cmap table length |
| 1192 |
emptyCmap = badCmapLen = gFalse; |
| 1193 |
cmapLen = 0; // make gcc happy |
| 1194 |
if (!missingCmap) { |
| 1195 |
if (nCmaps == 0) { |
| 1196 |
emptyCmap = gTrue; |
| 1197 |
} else { |
| 1198 |
cmapLen = cmaps[0].offset + cmaps[0].len; |
| 1199 |
for (i = 1; i < nCmaps; ++i) { |
| 1200 |
if (cmaps[i].offset + cmaps[i].len > cmapLen) { |
| 1201 |
cmapLen = cmaps[i].offset + cmaps[i].len; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
cmapLen -= tables[cmapIdx].offset; |
| 1205 |
if (cmapLen > tables[cmapIdx].len) { |
| 1206 |
badCmapLen = gTrue; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
// check for an abbreviated hmtx table (this is completely legal, |
| 1212 |
// but confuses the Microsoft PCL5 printer driver, which generates |
| 1213 |
// embedded fonts with the pitch field set to zero) |
| 1214 |
i = seekTable("hhea"); |
| 1215 |
nHMetrics = getU16BE(tables[i].offset + 34, &ok); |
| 1216 |
abbrevHMTX = nHMetrics < nGlyphs; |
| 1217 |
|
| 1218 |
// if nothing is broken, just write the TTF file as is |
| 1219 |
if (!missingCmap && !missingName && !missingPost && !missingOS2 && |
| 1220 |
!unsortedLoca && !bogusLastLoca && |
| 1221 |
!emptyCmap && !badCmapLen && !abbrevHMTX && |
| 1222 |
nZeroLengthTables == 0 && nBogusTables == 0 && |
| 1223 |
!name && !codeToGID && !replacementCmapTable && !isDfont && !isTTC) { |
| 1224 |
(*outputFunc)(outputStream, (char *)file, len); |
| 1225 |
gfree(locaTable); |
| 1226 |
return gFalse; |
| 1227 |
} |
| 1228 |
|
| 1229 |
// sort the 'loca' table: some (non-compliant) fonts have |
| 1230 |
// out-of-order loca tables; in order to correctly handle the case |
| 1231 |
// where (compliant) fonts have empty entries in the middle of the |
| 1232 |
// table, cmpTrueTypeLocaOffset uses offset as its primary sort key, |
| 1233 |
// and idx as its secondary key (ensuring that adjacent entries with |
| 1234 |
// the same pos value remain in the same order) |
| 1235 |
glyfLen = 0; // make gcc happy |
| 1236 |
if (unsortedLoca) { |
| 1237 |
#if HAVE_STD_SORT |
| 1238 |
std::sort(locaTable, locaTable + nGlyphs + 1, |
| 1239 |
cmpTrueTypeLocaOffsetFunctor()); |
| 1240 |
#else |
| 1241 |
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), |
| 1242 |
&cmpTrueTypeLocaOffset); |
| 1243 |
#endif |
| 1244 |
for (i = 0; i < nGlyphs; ++i) { |
| 1245 |
locaTable[i].len = locaTable[i+1].origOffset - locaTable[i].origOffset; |
| 1246 |
} |
| 1247 |
locaTable[nGlyphs].len = 0; |
| 1248 |
#if HAVE_STD_SORT |
| 1249 |
std::sort(locaTable, locaTable + nGlyphs + 1, cmpTrueTypeLocaIdxFunctor()); |
| 1250 |
#else |
| 1251 |
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), |
| 1252 |
&cmpTrueTypeLocaIdx); |
| 1253 |
#endif |
| 1254 |
// if the last entry in the loca is not the max offset (size of |
| 1255 |
// the glyf table), something is wrong -- work around the problem |
| 1256 |
// by forcing the last sorted entry to have a zero length |
| 1257 |
locaTable[nGlyphs].len = 0; |
| 1258 |
pos = 0; |
| 1259 |
for (i = 0; i <= nGlyphs; ++i) { |
| 1260 |
locaTable[i].newOffset = pos; |
| 1261 |
pos += locaTable[i].len; |
| 1262 |
if (pos & 3) { |
| 1263 |
pos += 4 - (pos & 3); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
glyfLen = pos; |
| 1267 |
} |
| 1268 |
|
| 1269 |
// if only the last loca entry is bogus (less than the one before |
| 1270 |
// it), just replace it with the glyf table size -- the last entry |
| 1271 |
// is just the end of the previous glyph, i.e., not the start of a |
| 1272 |
// valid glyph |
| 1273 |
if (bogusLastLoca) { |
| 1274 |
for (i = 0; i < nGlyphs; ++i) { |
| 1275 |
locaTable[i].newOffset = locaTable[i].origOffset; |
| 1276 |
} |
| 1277 |
i = seekTable("glyf"); |
| 1278 |
locaTable[nGlyphs].newOffset = tables[i].len; |
| 1279 |
} |
| 1280 |
|
| 1281 |
// compute checksums for the loca and glyf tables |
| 1282 |
locaChecksum = glyfChecksum = 0; |
| 1283 |
if (unsortedLoca || bogusLastLoca) { |
| 1284 |
if (locaFmt) { |
| 1285 |
for (j = 0; j <= nGlyphs; ++j) { |
| 1286 |
locaChecksum += locaTable[j].newOffset; |
| 1287 |
} |
| 1288 |
} else { |
| 1289 |
for (j = 0; j <= nGlyphs; j += 2) { |
| 1290 |
locaChecksum += locaTable[j].newOffset << 16; |
| 1291 |
if (j + 1 <= nGlyphs) { |
| 1292 |
locaChecksum += locaTable[j+1].newOffset; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |
| 1297 |
if (unsortedLoca) { |
| 1298 |
pos = tables[seekTable("glyf")].offset; |
| 1299 |
for (j = 0; j < nGlyphs; ++j) { |
| 1300 |
n = locaTable[j].len; |
| 1301 |
if (n > 0) { |
| 1302 |
k = locaTable[j].origOffset; |
| 1303 |
if (checkRegion(pos + k, n)) { |
| 1304 |
glyfChecksum += computeTableChecksum(file + pos + k, n); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
// construct the new name table |
| 1311 |
if (name) { |
| 1312 |
n = (int)strlen(name); |
| 1313 |
newNameLen = (6 + 4*12 + 2 * (3*n + 7) + 3) & ~3; |
| 1314 |
newNameTab = (char *)gmalloc(newNameLen); |
| 1315 |
memset(newNameTab, 0, newNameLen); |
| 1316 |
newNameTab[0] = 0; // format selector |
| 1317 |
newNameTab[1] = 0; |
| 1318 |
newNameTab[2] = 0; // number of name records |
| 1319 |
newNameTab[3] = 4; |
| 1320 |
newNameTab[4] = 0; // offset to start of string storage |
| 1321 |
newNameTab[5] = 6 + 4*12; |
| 1322 |
next = 0; |
| 1323 |
for (i = 0; i < 4; ++i) { |
| 1324 |
newNameTab[6 + i*12 + 0] = 0; // platform ID = Microsoft |
| 1325 |
newNameTab[6 + i*12 + 1] = 3; |
| 1326 |
newNameTab[6 + i*12 + 2] = 0; // encoding ID = Unicode |
| 1327 |
newNameTab[6 + i*12 + 3] = 1; |
| 1328 |
newNameTab[6 + i*12 + 4] = 0x04; // language ID = American English |
| 1329 |
newNameTab[6 + i*12 + 5] = 0x09; |
| 1330 |
newNameTab[6 + i*12 + 6] = 0; // name ID |
| 1331 |
newNameTab[6 + i*12 + 7] = (char)(i + 1); |
| 1332 |
// string length |
| 1333 |
newNameTab[6 + i*12 + 8] = (char)(i+1 == 2 ? 0 : ((2*n) >> 8)); |
| 1334 |
newNameTab[6 + i*12 + 9] = (char)(i+1 == 2 ? 14 : ((2*n) & 0xff)); |
| 1335 |
// string offset |
| 1336 |
newNameTab[6 + i*12 + 10] = (char)(next >> 8); |
| 1337 |
newNameTab[6 + i*12 + 11] = (char)(next & 0xff); |
| 1338 |
if (i+1 == 2) { |
| 1339 |
memcpy(newNameTab + 6 + 4*12 + next, "\0R\0e\0g\0u\0l\0a\0r", 14); |
| 1340 |
next += 14; |
| 1341 |
} else { |
| 1342 |
for (j = 0; j < n; ++j) { |
| 1343 |
newNameTab[6 + 4*12 + next + 2*j] = 0; |
| 1344 |
newNameTab[6 + 4*12 + next + 2*j + 1] = name[j]; |
| 1345 |
} |
| 1346 |
next += 2*n; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
} else { |
| 1350 |
newNameLen = 0; |
| 1351 |
newNameTab = NULL; |
| 1352 |
} |
| 1353 |
|
| 1354 |
// construct the new cmap table |
| 1355 |
if (codeToGID && !replacementCmapTable) { |
| 1356 |
newCmapLen = 44 + 256 * 2; |
| 1357 |
newCmapTab = (char *)gmalloc(newCmapLen); |
| 1358 |
newCmapTab[0] = 0; // table version number = 0 |
| 1359 |
newCmapTab[1] = 0; |
| 1360 |
newCmapTab[2] = 0; // number of encoding tables = 1 |
| 1361 |
newCmapTab[3] = 1; |
| 1362 |
newCmapTab[4] = 0; // platform ID = Microsoft |
| 1363 |
newCmapTab[5] = 3; |
| 1364 |
newCmapTab[6] = 0; // encoding ID = Unicode |
| 1365 |
newCmapTab[7] = 1; |
| 1366 |
newCmapTab[8] = 0; // offset of subtable |
| 1367 |
newCmapTab[9] = 0; |
| 1368 |
newCmapTab[10] = 0; |
| 1369 |
newCmapTab[11] = 12; |
| 1370 |
newCmapTab[12] = 0; // subtable format = 4 |
| 1371 |
newCmapTab[13] = 4; |
| 1372 |
newCmapTab[14] = 0x02; // subtable length |
| 1373 |
newCmapTab[15] = 0x20; |
| 1374 |
newCmapTab[16] = 0; // subtable version = 0 |
| 1375 |
newCmapTab[17] = 0; |
| 1376 |
newCmapTab[18] = 0; // segment count * 2 |
| 1377 |
newCmapTab[19] = 4; |
| 1378 |
newCmapTab[20] = 0; // 2 * 2 ^ floor(log2(segCount)) |
| 1379 |
newCmapTab[21] = 4; |
| 1380 |
newCmapTab[22] = 0; // floor(log2(segCount)) |
| 1381 |
newCmapTab[23] = 1; |
| 1382 |
newCmapTab[24] = 0; // 2*segCount - 2*2^floor(log2(segCount)) |
| 1383 |
newCmapTab[25] = 0; |
| 1384 |
newCmapTab[26] = 0x00; // endCount[0] |
| 1385 |
newCmapTab[27] = (char)0xff; |
| 1386 |
newCmapTab[28] = (char)0xff; // endCount[1] |
| 1387 |
newCmapTab[29] = (char)0xff; |
| 1388 |
newCmapTab[30] = 0; // reserved |
| 1389 |
newCmapTab[31] = 0; |
| 1390 |
newCmapTab[32] = 0x00; // startCount[0] |
| 1391 |
newCmapTab[33] = 0x00; |
| 1392 |
newCmapTab[34] = (char)0xff; // startCount[1] |
| 1393 |
newCmapTab[35] = (char)0xff; |
| 1394 |
newCmapTab[36] = 0; // idDelta[0] |
| 1395 |
newCmapTab[37] = 0; |
| 1396 |
newCmapTab[38] = 0; // idDelta[1] |
| 1397 |
newCmapTab[39] = 1; |
| 1398 |
newCmapTab[40] = 0; // idRangeOffset[0] |
| 1399 |
newCmapTab[41] = 4; |
| 1400 |
newCmapTab[42] = 0; // idRangeOffset[1] |
| 1401 |
newCmapTab[43] = 0; |
| 1402 |
for (i = 0; i < 256; ++i) { |
| 1403 |
if (codeToGID[i] < 0) { |
| 1404 |
//~ this may not be correct - we want this character to never be |
| 1405 |
//~ displayed, but mapping it to the notdef glyph may result in |
| 1406 |
//~ little boxes being displayed |
| 1407 |
newCmapTab[44 + 2*i] = 0; |
| 1408 |
newCmapTab[44 + 2*i + 1] = 0; |
| 1409 |
} else { |
| 1410 |
newCmapTab[44 + 2*i] = (char)(codeToGID[i] >> 8); |
| 1411 |
newCmapTab[44 + 2*i + 1] = (char)(codeToGID[i] & 0xff); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
} else { |
| 1415 |
newCmapLen = 0; |
| 1416 |
newCmapTab = NULL; |
| 1417 |
} |
| 1418 |
|
| 1419 |
// generate the new hmtx table and the updated hhea table |
| 1420 |
if (abbrevHMTX) { |
| 1421 |
i = seekTable("hhea"); |
| 1422 |
pos = tables[i].offset; |
| 1423 |
newHHEALen = 36; |
| 1424 |
newHHEATab = (char *)gmalloc(newHHEALen); |
| 1425 |
for (i = 0; i < newHHEALen; ++i) { |
| 1426 |
newHHEATab[i] = (char)getU8(pos++, &ok); |
| 1427 |
} |
| 1428 |
newHHEATab[34] = (char)(nGlyphs >> 8); |
| 1429 |
newHHEATab[35] = (char)(nGlyphs & 0xff); |
| 1430 |
i = seekTable("hmtx"); |
| 1431 |
pos = tables[i].offset; |
| 1432 |
newHMTXLen = 4 * nGlyphs; |
| 1433 |
newHMTXTab = (char *)gmalloc(newHMTXLen); |
| 1434 |
advWidth = 0; |
| 1435 |
for (i = 0; i < nHMetrics; ++i) { |
| 1436 |
advWidth = getU16BE(pos, &ok); |
| 1437 |
lsb = getU16BE(pos + 2, &ok); |
| 1438 |
pos += 4; |
| 1439 |
newHMTXTab[4*i ] = (char)(advWidth >> 8); |
| 1440 |
newHMTXTab[4*i + 1] = (char)(advWidth & 0xff); |
| 1441 |
newHMTXTab[4*i + 2] = (char)(lsb >> 8); |
| 1442 |
newHMTXTab[4*i + 3] = (char)(lsb & 0xff); |
| 1443 |
} |
| 1444 |
for (; i < nGlyphs; ++i) { |
| 1445 |
lsb = getU16BE(pos, &ok); |
| 1446 |
pos += 2; |
| 1447 |
newHMTXTab[4*i ] = (char)(advWidth >> 8); |
| 1448 |
newHMTXTab[4*i + 1] = (char)(advWidth & 0xff); |
| 1449 |
newHMTXTab[4*i + 2] = (char)(lsb >> 8); |
| 1450 |
newHMTXTab[4*i + 3] = (char)(lsb & 0xff); |
| 1451 |
} |
| 1452 |
} else { |
| 1453 |
newHHEATab = newHMTXTab = NULL; |
| 1454 |
newHHEALen = newHMTXLen = 0; // make gcc happy |
| 1455 |
} |
| 1456 |
|
| 1457 |
// construct the new table directory: |
| 1458 |
// - keep all original tables with non-zero length |
| 1459 |
// - fix the cmap table's length, if necessary |
| 1460 |
// - add missing tables |
| 1461 |
// - sort the table by tag |
| 1462 |
// - compute new table positions, including 4-byte alignment |
| 1463 |
// - (re)compute table checksums |
| 1464 |
nNewTables = nTables - nZeroLengthTables - nBogusTables + |
| 1465 |
(missingCmap ? 1 : 0) + (missingName ? 1 : 0) + |
| 1466 |
(missingPost ? 1 : 0) + (missingOS2 ? 1 : 0); |
| 1467 |
newTables = (TrueTypeTable *)gmallocn(nNewTables, sizeof(TrueTypeTable)); |
| 1468 |
j = 0; |
| 1469 |
for (i = 0; i < nTables; ++i) { |
| 1470 |
if (tables[i].len > 0 && |
| 1471 |
(tables[i].tag & 0xe0000000) && |
| 1472 |
(tables[i].tag & 0x00e00000) && |
| 1473 |
(tables[i].tag & 0x0000e000) && |
| 1474 |
(tables[i].tag & 0x000000e0)) { |
| 1475 |
newTables[j] = tables[i]; |
| 1476 |
newTables[j].origOffset = tables[i].offset; |
| 1477 |
if (checkRegion(tables[i].offset, tables[i].len)) { |
| 1478 |
newTables[j].checksum = |
| 1479 |
computeTableChecksum(file + tables[i].offset, tables[i].len); |
| 1480 |
if (tables[i].tag == headTag) { |
| 1481 |
// don't include the file checksum |
| 1482 |
newTables[j].checksum -= getU32BE(tables[i].offset + 8, &ok); |
| 1483 |
} |
| 1484 |
} else { |
| 1485 |
// we'll write four zero bytes for this table |
| 1486 |
newTables[j].len = 4; |
| 1487 |
} |
| 1488 |
if (newTables[j].tag == cmapTag && replacementCmapTable) { |
| 1489 |
newTables[j].len = replacementCmapTableLen; |
| 1490 |
newTables[j].checksum = computeTableChecksum(replacementCmapTable, |
| 1491 |
replacementCmapTableLen); |
| 1492 |
} else if (newTables[j].tag == cmapTag && codeToGID) { |
| 1493 |
newTables[j].len = newCmapLen; |
| 1494 |
newTables[j].checksum = computeTableChecksum((Guchar *)newCmapTab, |
| 1495 |
newCmapLen); |
| 1496 |
} else if (newTables[j].tag == cmapTag && emptyCmap) { |
| 1497 |
newTables[j].checksum = computeTableChecksum((Guchar *)cmapTab, |
| 1498 |
sizeof(cmapTab)); |
| 1499 |
newTables[j].len = sizeof(cmapTab); |
| 1500 |
} else if (newTables[j].tag == cmapTag && badCmapLen) { |
| 1501 |
newTables[j].len = cmapLen; |
| 1502 |
} else if (newTables[j].tag == locaTag && |
| 1503 |
(unsortedLoca || bogusLastLoca)) { |
| 1504 |
newTables[j].len = (nGlyphs + 1) * (locaFmt ? 4 : 2); |
| 1505 |
newTables[j].checksum = locaChecksum; |
| 1506 |
} else if (newTables[j].tag == glyfTag && unsortedLoca) { |
| 1507 |
newTables[j].len = glyfLen; |
| 1508 |
newTables[j].checksum = glyfChecksum; |
| 1509 |
} else if (newTables[j].tag == nameTag && name) { |
| 1510 |
newTables[j].len = newNameLen; |
| 1511 |
newTables[j].checksum = computeTableChecksum((Guchar *)newNameTab, |
| 1512 |
newNameLen); |
| 1513 |
} else if (newTables[j].tag == hheaTag && abbrevHMTX) { |
| 1514 |
newTables[j].len = newHHEALen; |
| 1515 |
newTables[j].checksum = computeTableChecksum((Guchar *)newHHEATab, |
| 1516 |
newHHEALen); |
| 1517 |
} else if (newTables[j].tag == hmtxTag && abbrevHMTX) { |
| 1518 |
newTables[j].len = newHMTXLen; |
| 1519 |
newTables[j].checksum = computeTableChecksum((Guchar *)newHMTXTab, |
| 1520 |
newHMTXLen); |
| 1521 |
} |
| 1522 |
++j; |
| 1523 |
} |
| 1524 |
} |
| 1525 |
if (missingCmap) { |
| 1526 |
newTables[j].tag = cmapTag; |
| 1527 |
if (replacementCmapTable) { |
| 1528 |
newTables[j].checksum = computeTableChecksum(replacementCmapTable, |
| 1529 |
replacementCmapTableLen); |
| 1530 |
newTables[j].len = replacementCmapTableLen; |
| 1531 |
} else if (codeToGID) { |
| 1532 |
newTables[j].checksum = computeTableChecksum((Guchar *)newCmapTab, |
| 1533 |
newCmapLen); |
| 1534 |
newTables[j].len = newCmapLen; |
| 1535 |
} else { |
| 1536 |
newTables[j].checksum = computeTableChecksum((Guchar *)cmapTab, |
| 1537 |
sizeof(cmapTab)); |
| 1538 |
newTables[j].len = sizeof(cmapTab); |
| 1539 |
} |
| 1540 |
++j; |
| 1541 |
} |
| 1542 |
if (missingName) { |
| 1543 |
newTables[j].tag = nameTag; |
| 1544 |
if (name) { |
| 1545 |
newTables[j].checksum = computeTableChecksum((Guchar *)newNameTab, |
| 1546 |
newNameLen); |
| 1547 |
newTables[j].len = newNameLen; |
| 1548 |
} else { |
| 1549 |
newTables[j].checksum = computeTableChecksum((Guchar *)nameTab, |
| 1550 |
sizeof(nameTab)); |
| 1551 |
newTables[j].len = sizeof(nameTab); |
| 1552 |
} |
| 1553 |
++j; |
| 1554 |
} |
| 1555 |
if (missingPost) { |
| 1556 |
newTables[j].tag = postTag; |
| 1557 |
newTables[j].checksum = computeTableChecksum((Guchar *)postTab, |
| 1558 |
sizeof(postTab)); |
| 1559 |
newTables[j].len = sizeof(postTab); |
| 1560 |
++j; |
| 1561 |
} |
| 1562 |
if (missingOS2) { |
| 1563 |
newTables[j].tag = os2Tag; |
| 1564 |
newTables[j].checksum = computeTableChecksum((Guchar *)os2Tab, |
| 1565 |
sizeof(os2Tab)); |
| 1566 |
newTables[j].len = sizeof(os2Tab); |
| 1567 |
++j; |
| 1568 |
} |
| 1569 |
#if HAVE_STD_SORT |
| 1570 |
std::sort(newTables, newTables + nNewTables, cmpTrueTypeTableTagFunctor()); |
| 1571 |
#else |
| 1572 |
qsort(newTables, nNewTables, sizeof(TrueTypeTable), |
| 1573 |
&cmpTrueTypeTableTag); |
| 1574 |
#endif |
| 1575 |
pos = 12 + nNewTables * 16; |
| 1576 |
for (i = 0; i < nNewTables; ++i) { |
| 1577 |
newTables[i].offset = pos; |
| 1578 |
pos += newTables[i].len; |
| 1579 |
if (pos & 3) { |
| 1580 |
pos += 4 - (pos & 3); |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
// write the table directory |
| 1585 |
tableDir = (char *)gmalloc(12 + nNewTables * 16); |
| 1586 |
tableDir[0] = 0x00; // sfnt version |
| 1587 |
tableDir[1] = 0x01; |
| 1588 |
tableDir[2] = 0x00; |
| 1589 |
tableDir[3] = 0x00; |
| 1590 |
tableDir[4] = (char)((nNewTables >> 8) & 0xff); // numTables |
| 1591 |
tableDir[5] = (char)(nNewTables & 0xff); |
| 1592 |
for (i = -1, t = (Guint)nNewTables; t; ++i, t >>= 1) ; |
| 1593 |
t = 1 << (4 + i); |
| 1594 |
tableDir[6] = (char)((t >> 8) & 0xff); // searchRange |
| 1595 |
tableDir[7] = (char)(t & 0xff); |
| 1596 |
tableDir[8] = (char)((i >> 8) & 0xff); // entrySelector |
| 1597 |
tableDir[9] = (char)(i & 0xff); |
| 1598 |
t = nNewTables * 16 - t; |
| 1599 |
tableDir[10] = (char)((t >> 8) & 0xff); // rangeShift |
| 1600 |
tableDir[11] = (char)(t & 0xff); |
| 1601 |
pos = 12; |
| 1602 |
for (i = 0; i < nNewTables; ++i) { |
| 1603 |
tableDir[pos ] = (char)(newTables[i].tag >> 24); |
| 1604 |
tableDir[pos+ 1] = (char)(newTables[i].tag >> 16); |
| 1605 |
tableDir[pos+ 2] = (char)(newTables[i].tag >> 8); |
| 1606 |
tableDir[pos+ 3] = (char) newTables[i].tag; |
| 1607 |
tableDir[pos+ 4] = (char)(newTables[i].checksum >> 24); |
| 1608 |
tableDir[pos+ 5] = (char)(newTables[i].checksum >> 16); |
| 1609 |
tableDir[pos+ 6] = (char)(newTables[i].checksum >> 8); |
| 1610 |
tableDir[pos+ 7] = (char) newTables[i].checksum; |
| 1611 |
tableDir[pos+ 8] = (char)(newTables[i].offset >> 24); |
| 1612 |
tableDir[pos+ 9] = (char)(newTables[i].offset >> 16); |
| 1613 |
tableDir[pos+10] = (char)(newTables[i].offset >> 8); |
| 1614 |
tableDir[pos+11] = (char) newTables[i].offset; |
| 1615 |
tableDir[pos+12] = (char)(newTables[i].len >> 24); |
| 1616 |
tableDir[pos+13] = (char)(newTables[i].len >> 16); |
| 1617 |
tableDir[pos+14] = (char)(newTables[i].len >> 8); |
| 1618 |
tableDir[pos+15] = (char) newTables[i].len; |
| 1619 |
pos += 16; |
| 1620 |
} |
| 1621 |
(*outputFunc)(outputStream, tableDir, 12 + nNewTables * 16); |
| 1622 |
|
| 1623 |
// compute the file checksum |
| 1624 |
fileChecksum = computeTableChecksum((Guchar *)tableDir, |
| 1625 |
12 + nNewTables * 16); |
| 1626 |
for (i = 0; i < nNewTables; ++i) { |
| 1627 |
fileChecksum += newTables[i].checksum; |
| 1628 |
} |
| 1629 |
fileChecksum = 0xb1b0afba - fileChecksum; |
| 1630 |
|
| 1631 |
// write the tables |
| 1632 |
for (i = 0; i < nNewTables; ++i) { |
| 1633 |
if (newTables[i].tag == headTag) { |
| 1634 |
if (checkRegion(newTables[i].origOffset, newTables[i].len)) { |
| 1635 |
(*outputFunc)(outputStream, (char *)file + newTables[i].origOffset, 8); |
| 1636 |
checksumBuf[0] = (char)(fileChecksum >> 24); |
| 1637 |
checksumBuf[1] = (char)(fileChecksum >> 16); |
| 1638 |
checksumBuf[2] = (char)(fileChecksum >> 8); |
| 1639 |
checksumBuf[3] = (char)fileChecksum; |
| 1640 |
(*outputFunc)(outputStream, checksumBuf, 4); |
| 1641 |
(*outputFunc)(outputStream, |
| 1642 |
(char *)file + newTables[i].origOffset + 12, |
| 1643 |
newTables[i].len - 12); |
| 1644 |
} else { |
| 1645 |
for (j = 0; j < newTables[i].len; ++j) { |
| 1646 |
(*outputFunc)(outputStream, "\0", 1); |
| 1647 |
} |
| 1648 |
} |
| 1649 |
} else if (newTables[i].tag == cmapTag && replacementCmapTable) { |
| 1650 |
(*outputFunc)(outputStream, (char *)replacementCmapTable, |
| 1651 |
newTables[i].len); |
| 1652 |
} else if (newTables[i].tag == cmapTag && codeToGID) { |
| 1653 |
(*outputFunc)(outputStream, newCmapTab, newTables[i].len); |
| 1654 |
} else if (newTables[i].tag == cmapTag && missingCmap) { |
| 1655 |
(*outputFunc)(outputStream, cmapTab, newTables[i].len); |
| 1656 |
} else if (newTables[i].tag == nameTag && name) { |
| 1657 |
(*outputFunc)(outputStream, newNameTab, newTables[i].len); |
| 1658 |
} else if (newTables[i].tag == nameTag && missingName) { |
| 1659 |
(*outputFunc)(outputStream, nameTab, newTables[i].len); |
| 1660 |
} else if (newTables[i].tag == postTag && missingPost) { |
| 1661 |
(*outputFunc)(outputStream, postTab, newTables[i].len); |
| 1662 |
} else if (newTables[i].tag == os2Tag && missingOS2) { |
| 1663 |
(*outputFunc)(outputStream, os2Tab, newTables[i].len); |
| 1664 |
} else if (newTables[i].tag == hheaTag && abbrevHMTX) { |
| 1665 |
(*outputFunc)(outputStream, newHHEATab, newTables[i].len); |
| 1666 |
} else if (newTables[i].tag == hmtxTag && abbrevHMTX) { |
| 1667 |
(*outputFunc)(outputStream, newHMTXTab, newTables[i].len); |
| 1668 |
} else if (newTables[i].tag == locaTag && |
| 1669 |
(unsortedLoca || bogusLastLoca)) { |
| 1670 |
for (j = 0; j <= nGlyphs; ++j) { |
| 1671 |
if (locaFmt) { |
| 1672 |
locaBuf[0] = (char)(locaTable[j].newOffset >> 24); |
| 1673 |
locaBuf[1] = (char)(locaTable[j].newOffset >> 16); |
| 1674 |
locaBuf[2] = (char)(locaTable[j].newOffset >> 8); |
| 1675 |
locaBuf[3] = (char) locaTable[j].newOffset; |
| 1676 |
(*outputFunc)(outputStream, locaBuf, 4); |
| 1677 |
} else { |
| 1678 |
locaBuf[0] = (char)(locaTable[j].newOffset >> 9); |
| 1679 |
locaBuf[1] = (char)(locaTable[j].newOffset >> 1); |
| 1680 |
(*outputFunc)(outputStream, locaBuf, 2); |
| 1681 |
} |
| 1682 |
} |
| 1683 |
} else if (newTables[i].tag == glyfTag && unsortedLoca) { |
| 1684 |
pos = tables[seekTable("glyf")].offset; |
| 1685 |
for (j = 0; j < nGlyphs; ++j) { |
| 1686 |
n = locaTable[j].len; |
| 1687 |
if (n > 0) { |
| 1688 |
k = locaTable[j].origOffset; |
| 1689 |
if (checkRegion(pos + k, n)) { |
| 1690 |
(*outputFunc)(outputStream, (char *)file + pos + k, n); |
| 1691 |
} else { |
| 1692 |
for (k = 0; k < n; ++k) { |
| 1693 |
(*outputFunc)(outputStream, "\0", 1); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
if ((k = locaTable[j].len & 3)) { |
| 1697 |
(*outputFunc)(outputStream, "\0\0\0\0", 4 - k); |
| 1698 |
} |
| 1699 |
} |
| 1700 |
} |
| 1701 |
} else { |
| 1702 |
if (checkRegion(newTables[i].origOffset, newTables[i].len)) { |
| 1703 |
(*outputFunc)(outputStream, (char *)file + newTables[i].origOffset, |
| 1704 |
newTables[i].len); |
| 1705 |
} else { |
| 1706 |
for (j = 0; j < newTables[i].len; ++j) { |
| 1707 |
(*outputFunc)(outputStream, "\0", 1); |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
if (newTables[i].len & 3) { |
| 1712 |
(*outputFunc)(outputStream, "\0\0\0", 4 - (newTables[i].len & 3)); |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
gfree(newHMTXTab); |
| 1717 |
gfree(newHHEATab); |
| 1718 |
gfree(newCmapTab); |
| 1719 |
gfree(newNameTab); |
| 1720 |
gfree(tableDir); |
| 1721 |
gfree(newTables); |
| 1722 |
gfree(locaTable); |
| 1723 |
return gTrue; |
| 1724 |
} |
| 1725 |
|
| 1726 |
void FoFiTrueType::cvtEncoding(char **encoding, |
| 1727 |
FoFiOutputFunc outputFunc, |
| 1728 |
void *outputStream) { |
| 1729 |
const char *name; |
| 1730 |
GString *buf; |
| 1731 |
int i; |
| 1732 |
|
| 1733 |
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20); |
| 1734 |
if (encoding) { |
| 1735 |
for (i = 0; i < 256; ++i) { |
| 1736 |
if (!(name = encoding[i])) { |
| 1737 |
name = ".notdef"; |
| 1738 |
} |
| 1739 |
buf = GString::format("dup {0:d} /", i); |
| 1740 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 1741 |
delete buf; |
| 1742 |
(*outputFunc)(outputStream, name, (int)strlen(name)); |
| 1743 |
(*outputFunc)(outputStream, " put\n", 5); |
| 1744 |
} |
| 1745 |
} else { |
| 1746 |
for (i = 0; i < 256; ++i) { |
| 1747 |
buf = GString::format("dup {0:d} /c{1:02x} put\n", i, i); |
| 1748 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 1749 |
delete buf; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
(*outputFunc)(outputStream, "readonly def\n", 13); |
| 1753 |
} |
| 1754 |
|
| 1755 |
void FoFiTrueType::cvtCharStrings(char **encoding, |
| 1756 |
int *codeToGID, |
| 1757 |
FoFiOutputFunc outputFunc, |
| 1758 |
void *outputStream) { |
| 1759 |
char *name; |
| 1760 |
GString *buf; |
| 1761 |
char buf2[16]; |
| 1762 |
int i, k; |
| 1763 |
|
| 1764 |
// always define '.notdef' |
| 1765 |
(*outputFunc)(outputStream, "/CharStrings 256 dict dup begin\n", 32); |
| 1766 |
(*outputFunc)(outputStream, "/.notdef 0 def\n", 15); |
| 1767 |
|
| 1768 |
// map char name to glyph index: |
| 1769 |
// 1. use encoding to map name to char code |
| 1770 |
// 2. use codeToGID to map char code to glyph index |
| 1771 |
// N.B. We do this in reverse order because font subsets can have |
| 1772 |
// weird encodings that use the same character name twice, and |
| 1773 |
// the first definition is probably the one we want. |
| 1774 |
k = 0; // make gcc happy |
| 1775 |
for (i = 255; i >= 0; --i) { |
| 1776 |
if (encoding) { |
| 1777 |
name = encoding[i]; |
| 1778 |
} else { |
| 1779 |
snprintf(buf2, sizeof(buf2), "c%02x", i); |
| 1780 |
name = buf2; |
| 1781 |
} |
| 1782 |
if (name && strcmp(name, ".notdef")) { |
| 1783 |
k = codeToGID[i]; |
| 1784 |
// note: Distiller (maybe Adobe's PS interpreter in general) |
| 1785 |
// doesn't like TrueType fonts that have CharStrings entries |
| 1786 |
// which point to nonexistent glyphs, hence the (k < nGlyphs) |
| 1787 |
// test |
| 1788 |
if (k > 0 && k < nGlyphs) { |
| 1789 |
(*outputFunc)(outputStream, "/", 1); |
| 1790 |
(*outputFunc)(outputStream, name, (int)strlen(name)); |
| 1791 |
buf = GString::format(" {0:d} def\n", k); |
| 1792 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 1793 |
delete buf; |
| 1794 |
} |
| 1795 |
} |
| 1796 |
} |
| 1797 |
|
| 1798 |
(*outputFunc)(outputStream, "end readonly def\n", 17); |
| 1799 |
} |
| 1800 |
|
| 1801 |
void FoFiTrueType::cvtSfnts(FoFiOutputFunc outputFunc, |
| 1802 |
void *outputStream, GString *name, |
| 1803 |
GBool needVerticalMetrics, |
| 1804 |
int *maxUsedGlyph) { |
| 1805 |
Guchar headData[54]; |
| 1806 |
TrueTypeLoca *locaTable; |
| 1807 |
Guchar *locaData; |
| 1808 |
TrueTypeTable newTables[nT42Tables]; |
| 1809 |
Guchar tableDir[12 + nT42Tables*16]; |
| 1810 |
GBool ok; |
| 1811 |
Guint checksum; |
| 1812 |
int nNewTables; |
| 1813 |
int glyfTableLen, length, pos, glyfPos, i, j, k; |
| 1814 |
Guchar vheaTab[36] = { |
| 1815 |
0, 1, 0, 0, // table version number |
| 1816 |
0, 0, // ascent |
| 1817 |
0, 0, // descent |
| 1818 |
0, 0, // reserved |
| 1819 |
0, 0, // max advance height |
| 1820 |
0, 0, // min top side bearing |
| 1821 |
0, 0, // min bottom side bearing |
| 1822 |
0, 0, // y max extent |
| 1823 |
0, 0, // caret slope rise |
| 1824 |
0, 1, // caret slope run |
| 1825 |
0, 0, // caret offset |
| 1826 |
0, 0, // reserved |
| 1827 |
0, 0, // reserved |
| 1828 |
0, 0, // reserved |
| 1829 |
0, 0, // reserved |
| 1830 |
0, 0, // metric data format |
| 1831 |
0, 1 // number of advance heights in vmtx table |
| 1832 |
}; |
| 1833 |
Guchar *vmtxTab; |
| 1834 |
GBool needVhea, needVmtx; |
| 1835 |
int advance; |
| 1836 |
|
| 1837 |
// construct the 'head' table, zero out the font checksum |
| 1838 |
i = seekTable("head"); |
| 1839 |
pos = tables[i].offset; |
| 1840 |
if (!checkRegion(pos, 54)) { |
| 1841 |
return; |
| 1842 |
} |
| 1843 |
memcpy(headData, file + pos, 54); |
| 1844 |
headData[8] = headData[9] = headData[10] = headData[11] = (Guchar)0; |
| 1845 |
|
| 1846 |
// check for a bogus loca format field in the 'head' table |
| 1847 |
// (I've encountered fonts with loca format set to 0x0100 instead of 0x0001) |
| 1848 |
if (locaFmt != 0 && locaFmt != 1) { |
| 1849 |
headData[50] = 0; |
| 1850 |
headData[51] = 1; |
| 1851 |
} |
| 1852 |
|
| 1853 |
// read the original 'loca' table, pad entries out to 4 bytes, and |
| 1854 |
// sort it into proper order -- some (non-compliant) fonts have |
| 1855 |
// out-of-order loca tables; in order to correctly handle the case |
| 1856 |
// where (compliant) fonts have empty entries in the middle of the |
| 1857 |
// table, cmpTrueTypeLocaOffset uses offset as its primary sort key, |
| 1858 |
// and idx as its secondary key (ensuring that adjacent entries with |
| 1859 |
// the same pos value remain in the same order) |
| 1860 |
// |
| 1861 |
// NB: a glyph description containing 12 zero bytes should be a |
| 1862 |
// valid empty glyph (from my reading of the TrueType spec), but |
| 1863 |
// Acrobat chokes on this (which is an issue when an Xpdf-generated |
| 1864 |
// PS file is converted back to PDF - with Ghostscript or |
| 1865 |
// Distiller), so we drop any glyph descriptions of 12 or fewer |
| 1866 |
// bytes -- an empty glyph description generates an empty glyph with |
| 1867 |
// no errors |
| 1868 |
locaTable = (TrueTypeLoca *)gmallocn(nGlyphs + 1, sizeof(TrueTypeLoca)); |
| 1869 |
i = seekTable("loca"); |
| 1870 |
pos = tables[i].offset; |
| 1871 |
i = seekTable("glyf"); |
| 1872 |
glyfTableLen = tables[i].len; |
| 1873 |
ok = gTrue; |
| 1874 |
for (i = 0; i <= nGlyphs; ++i) { |
| 1875 |
locaTable[i].idx = i; |
| 1876 |
if (locaFmt) { |
| 1877 |
locaTable[i].origOffset = (int)getU32BE(pos + i*4, &ok); |
| 1878 |
} else { |
| 1879 |
locaTable[i].origOffset = 2 * getU16BE(pos + i*2, &ok); |
| 1880 |
} |
| 1881 |
if (locaTable[i].origOffset < 0 || |
| 1882 |
locaTable[i].origOffset > glyfTableLen) { |
| 1883 |
locaTable[i].origOffset = glyfTableLen; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
#if HAVE_STD_SORT |
| 1887 |
std::sort(locaTable, locaTable + nGlyphs + 1, |
| 1888 |
cmpTrueTypeLocaOffsetFunctor()); |
| 1889 |
#else |
| 1890 |
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), |
| 1891 |
&cmpTrueTypeLocaOffset); |
| 1892 |
#endif |
| 1893 |
for (i = 0; i < nGlyphs; ++i) { |
| 1894 |
locaTable[i].len = locaTable[i+1].origOffset - locaTable[i].origOffset; |
| 1895 |
} |
| 1896 |
locaTable[nGlyphs].len = 0; |
| 1897 |
#if HAVE_STD_SORT |
| 1898 |
std::sort(locaTable, locaTable + nGlyphs + 1, cmpTrueTypeLocaIdxFunctor()); |
| 1899 |
#else |
| 1900 |
qsort(locaTable, nGlyphs + 1, sizeof(TrueTypeLoca), |
| 1901 |
&cmpTrueTypeLocaIdx); |
| 1902 |
#endif |
| 1903 |
pos = 0; |
| 1904 |
*maxUsedGlyph = -1; |
| 1905 |
for (i = 0; i <= nGlyphs; ++i) { |
| 1906 |
locaTable[i].newOffset = pos; |
| 1907 |
if (locaTable[i].len > 12) { |
| 1908 |
pos += locaTable[i].len; |
| 1909 |
if (pos & 3) { |
| 1910 |
pos += 4 - (pos & 3); |
| 1911 |
} |
| 1912 |
*maxUsedGlyph = i; |
| 1913 |
} |
| 1914 |
} |
| 1915 |
|
| 1916 |
// construct the new 'loca' table |
| 1917 |
locaData = (Guchar *)gmallocn(nGlyphs + 1, (locaFmt ? 4 : 2)); |
| 1918 |
for (i = 0; i <= nGlyphs; ++i) { |
| 1919 |
pos = locaTable[i].newOffset; |
| 1920 |
if (locaFmt) { |
| 1921 |
locaData[4*i ] = (Guchar)(pos >> 24); |
| 1922 |
locaData[4*i+1] = (Guchar)(pos >> 16); |
| 1923 |
locaData[4*i+2] = (Guchar)(pos >> 8); |
| 1924 |
locaData[4*i+3] = (Guchar) pos; |
| 1925 |
} else { |
| 1926 |
locaData[2*i ] = (Guchar)(pos >> 9); |
| 1927 |
locaData[2*i+1] = (Guchar)(pos >> 1); |
| 1928 |
} |
| 1929 |
} |
| 1930 |
|
| 1931 |
// count the number of tables |
| 1932 |
nNewTables = 0; |
| 1933 |
for (i = 0; i < nT42Tables; ++i) { |
| 1934 |
if (t42Tables[i].required || |
| 1935 |
seekTable(t42Tables[i].tag) >= 0) { |
| 1936 |
++nNewTables; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
vmtxTab = NULL; // make gcc happy |
| 1940 |
advance = 0; // make gcc happy |
| 1941 |
if (needVerticalMetrics) { |
| 1942 |
needVhea = seekTable("vhea") < 0; |
| 1943 |
needVmtx = seekTable("vmtx") < 0; |
| 1944 |
if (needVhea || needVmtx) { |
| 1945 |
i = seekTable("head"); |
| 1946 |
advance = getU16BE(tables[i].offset + 18, &ok); // units per em |
| 1947 |
if (needVhea) { |
| 1948 |
++nNewTables; |
| 1949 |
} |
| 1950 |
if (needVmtx) { |
| 1951 |
++nNewTables; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
} |
| 1955 |
|
| 1956 |
// construct the new table headers, including table checksums |
| 1957 |
// (pad each table out to a multiple of 4 bytes) |
| 1958 |
pos = 12 + nNewTables*16; |
| 1959 |
k = 0; |
| 1960 |
for (i = 0; i < nT42Tables; ++i) { |
| 1961 |
length = -1; |
| 1962 |
checksum = 0; // make gcc happy |
| 1963 |
if (i == t42HeadTable) { |
| 1964 |
length = 54; |
| 1965 |
checksum = computeTableChecksum(headData, 54); |
| 1966 |
} else if (i == t42LocaTable) { |
| 1967 |
length = (nGlyphs + 1) * (locaFmt ? 4 : 2); |
| 1968 |
checksum = computeTableChecksum(locaData, length); |
| 1969 |
} else if (i == t42GlyfTable) { |
| 1970 |
length = 0; |
| 1971 |
checksum = 0; |
| 1972 |
glyfPos = tables[seekTable("glyf")].offset; |
| 1973 |
for (j = 0; j < nGlyphs; ++j) { |
| 1974 |
if (locaTable[j].len > 12) { |
| 1975 |
length += locaTable[j].len; |
| 1976 |
if (length & 3) { |
| 1977 |
length += 4 - (length & 3); |
| 1978 |
} |
| 1979 |
if (checkRegion(glyfPos + locaTable[j].origOffset, |
| 1980 |
locaTable[j].len)) { |
| 1981 |
checksum += |
| 1982 |
computeTableChecksum(file + glyfPos + locaTable[j].origOffset, |
| 1983 |
locaTable[j].len); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} |
| 1987 |
} else { |
| 1988 |
if ((j = seekTable(t42Tables[i].tag)) >= 0) { |
| 1989 |
length = tables[j].len; |
| 1990 |
if (checkRegion(tables[j].offset, length)) { |
| 1991 |
checksum = computeTableChecksum(file + tables[j].offset, length); |
| 1992 |
} |
| 1993 |
} else if (needVerticalMetrics && i == t42VheaTable) { |
| 1994 |
vheaTab[10] = (Guchar)(advance / 256); // max advance height |
| 1995 |
vheaTab[11] = (Guchar)(advance % 256); |
| 1996 |
length = sizeof(vheaTab); |
| 1997 |
checksum = computeTableChecksum(vheaTab, length); |
| 1998 |
} else if (needVerticalMetrics && i == t42VmtxTable) { |
| 1999 |
length = 4 + (nGlyphs - 1) * 2; |
| 2000 |
vmtxTab = (Guchar *)gmalloc(length); |
| 2001 |
vmtxTab[0] = (Guchar)(advance / 256); |
| 2002 |
vmtxTab[1] = (Guchar)(advance % 256); |
| 2003 |
for (j = 2; j < length; j += 2) { |
| 2004 |
vmtxTab[j] = 0; |
| 2005 |
vmtxTab[j+1] = 0; |
| 2006 |
} |
| 2007 |
checksum = computeTableChecksum(vmtxTab, length); |
| 2008 |
} else if (t42Tables[i].required) { |
| 2009 |
//~ error(-1, "Embedded TrueType font is missing a required table ('%s')", |
| 2010 |
//~ t42Tables[i].tag); |
| 2011 |
length = 0; |
| 2012 |
checksum = 0; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
if (length >= 0) { |
| 2016 |
newTables[k].tag = ((t42Tables[i].tag[0] & 0xff) << 24) | |
| 2017 |
((t42Tables[i].tag[1] & 0xff) << 16) | |
| 2018 |
((t42Tables[i].tag[2] & 0xff) << 8) | |
| 2019 |
(t42Tables[i].tag[3] & 0xff); |
| 2020 |
newTables[k].checksum = checksum; |
| 2021 |
newTables[k].offset = pos; |
| 2022 |
newTables[k].len = length; |
| 2023 |
pos += length; |
| 2024 |
if (pos & 3) { |
| 2025 |
pos += 4 - (length & 3); |
| 2026 |
} |
| 2027 |
++k; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
// construct the table directory |
| 2032 |
tableDir[0] = 0x00; // sfnt version |
| 2033 |
tableDir[1] = 0x01; |
| 2034 |
tableDir[2] = 0x00; |
| 2035 |
tableDir[3] = 0x00; |
| 2036 |
tableDir[4] = 0; // numTables |
| 2037 |
tableDir[5] = (Guchar)nNewTables; |
| 2038 |
tableDir[6] = 0; // searchRange |
| 2039 |
tableDir[7] = (Guchar)128; |
| 2040 |
tableDir[8] = 0; // entrySelector |
| 2041 |
tableDir[9] = 3; |
| 2042 |
tableDir[10] = 0; // rangeShift |
| 2043 |
tableDir[11] = (Guchar)(16 * nNewTables - 128); |
| 2044 |
pos = 12; |
| 2045 |
for (i = 0; i < nNewTables; ++i) { |
| 2046 |
tableDir[pos ] = (Guchar)(newTables[i].tag >> 24); |
| 2047 |
tableDir[pos+ 1] = (Guchar)(newTables[i].tag >> 16); |
| 2048 |
tableDir[pos+ 2] = (Guchar)(newTables[i].tag >> 8); |
| 2049 |
tableDir[pos+ 3] = (Guchar) newTables[i].tag; |
| 2050 |
tableDir[pos+ 4] = (Guchar)(newTables[i].checksum >> 24); |
| 2051 |
tableDir[pos+ 5] = (Guchar)(newTables[i].checksum >> 16); |
| 2052 |
tableDir[pos+ 6] = (Guchar)(newTables[i].checksum >> 8); |
| 2053 |
tableDir[pos+ 7] = (Guchar) newTables[i].checksum; |
| 2054 |
tableDir[pos+ 8] = (Guchar)(newTables[i].offset >> 24); |
| 2055 |
tableDir[pos+ 9] = (Guchar)(newTables[i].offset >> 16); |
| 2056 |
tableDir[pos+10] = (Guchar)(newTables[i].offset >> 8); |
| 2057 |
tableDir[pos+11] = (Guchar) newTables[i].offset; |
| 2058 |
tableDir[pos+12] = (Guchar)(newTables[i].len >> 24); |
| 2059 |
tableDir[pos+13] = (Guchar)(newTables[i].len >> 16); |
| 2060 |
tableDir[pos+14] = (Guchar)(newTables[i].len >> 8); |
| 2061 |
tableDir[pos+15] = (Guchar) newTables[i].len; |
| 2062 |
pos += 16; |
| 2063 |
} |
| 2064 |
|
| 2065 |
// compute the font checksum and store it in the head table |
| 2066 |
checksum = computeTableChecksum(tableDir, 12 + nNewTables*16); |
| 2067 |
for (i = 0; i < nNewTables; ++i) { |
| 2068 |
checksum += newTables[i].checksum; |
| 2069 |
} |
| 2070 |
checksum = 0xb1b0afba - checksum; // because the TrueType spec says so |
| 2071 |
headData[ 8] = (Guchar)(checksum >> 24); |
| 2072 |
headData[ 9] = (Guchar)(checksum >> 16); |
| 2073 |
headData[10] = (Guchar)(checksum >> 8); |
| 2074 |
headData[11] = (Guchar) checksum; |
| 2075 |
|
| 2076 |
// start the sfnts array |
| 2077 |
if (name) { |
| 2078 |
(*outputFunc)(outputStream, "/", 1); |
| 2079 |
(*outputFunc)(outputStream, name->getCString(), name->getLength()); |
| 2080 |
(*outputFunc)(outputStream, " [\n", 3); |
| 2081 |
} else { |
| 2082 |
(*outputFunc)(outputStream, "/sfnts [\n", 9); |
| 2083 |
} |
| 2084 |
|
| 2085 |
// write the table directory |
| 2086 |
dumpString(tableDir, 12 + nNewTables*16, outputFunc, outputStream); |
| 2087 |
|
| 2088 |
// write the tables |
| 2089 |
for (i = 0; i < nNewTables; ++i) { |
| 2090 |
if (i == t42HeadTable) { |
| 2091 |
dumpString(headData, 54, outputFunc, outputStream); |
| 2092 |
} else if (i == t42LocaTable) { |
| 2093 |
length = (nGlyphs + 1) * (locaFmt ? 4 : 2); |
| 2094 |
dumpString(locaData, length, outputFunc, outputStream); |
| 2095 |
} else if (i == t42GlyfTable) { |
| 2096 |
glyfPos = tables[seekTable("glyf")].offset; |
| 2097 |
for (j = 0; j < nGlyphs; ++j) { |
| 2098 |
if (locaTable[j].len > 12 && |
| 2099 |
checkRegion(glyfPos + locaTable[j].origOffset, locaTable[j].len)) { |
| 2100 |
dumpString(file + glyfPos + locaTable[j].origOffset, |
| 2101 |
locaTable[j].len, outputFunc, outputStream); |
| 2102 |
} |
| 2103 |
} |
| 2104 |
} else { |
| 2105 |
// length == 0 means the table is missing and the error was |
| 2106 |
// already reported during the construction of the table |
| 2107 |
// headers |
| 2108 |
if ((length = newTables[i].len) > 0) { |
| 2109 |
if ((j = seekTable(t42Tables[i].tag)) >= 0 && |
| 2110 |
checkRegion(tables[j].offset, tables[j].len)) { |
| 2111 |
dumpString(file + tables[j].offset, tables[j].len, |
| 2112 |
outputFunc, outputStream); |
| 2113 |
} else if (needVerticalMetrics && i == t42VheaTable) { |
| 2114 |
dumpString(vheaTab, length, outputFunc, outputStream); |
| 2115 |
} else if (needVerticalMetrics && i == t42VmtxTable) { |
| 2116 |
dumpString(vmtxTab, length, outputFunc, outputStream); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
} |
| 2121 |
|
| 2122 |
// end the sfnts array |
| 2123 |
(*outputFunc)(outputStream, "] def\n", 6); |
| 2124 |
|
| 2125 |
gfree(locaData); |
| 2126 |
gfree(locaTable); |
| 2127 |
if (vmtxTab) { |
| 2128 |
gfree(vmtxTab); |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
void FoFiTrueType::dumpString(Guchar *s, int length, |
| 2133 |
FoFiOutputFunc outputFunc, |
| 2134 |
void *outputStream) { |
| 2135 |
GString *buf; |
| 2136 |
int pad, i, j; |
| 2137 |
|
| 2138 |
(*outputFunc)(outputStream, "<", 1); |
| 2139 |
for (i = 0; i < length; i += 32) { |
| 2140 |
for (j = 0; j < 32 && i+j < length; ++j) { |
| 2141 |
buf = GString::format("{0:02x}", s[i+j] & 0xff); |
| 2142 |
(*outputFunc)(outputStream, buf->getCString(), buf->getLength()); |
| 2143 |
delete buf; |
| 2144 |
} |
| 2145 |
if (i % (65536 - 32) == 65536 - 64) { |
| 2146 |
(*outputFunc)(outputStream, ">\n<", 3); |
| 2147 |
} else if (i+32 < length) { |
| 2148 |
(*outputFunc)(outputStream, "\n", 1); |
| 2149 |
} |
| 2150 |
} |
| 2151 |
if (length & 3) { |
| 2152 |
pad = 4 - (length & 3); |
| 2153 |
for (i = 0; i < pad; ++i) { |
| 2154 |
(*outputFunc)(outputStream, "00", 2); |
| 2155 |
} |
| 2156 |
} |
| 2157 |
// add an extra zero byte because the Adobe Type 42 spec says so |
| 2158 |
(*outputFunc)(outputStream, "00>\n", 4); |
| 2159 |
} |
| 2160 |
|
| 2161 |
Guint FoFiTrueType::computeTableChecksum(Guchar *data, int length) { |
| 2162 |
Guint checksum, word; |
| 2163 |
int i; |
| 2164 |
|
| 2165 |
checksum = 0; |
| 2166 |
for (i = 0; i+3 < length; i += 4) { |
| 2167 |
word = ((data[i ] & 0xff) << 24) + |
| 2168 |
((data[i+1] & 0xff) << 16) + |
| 2169 |
((data[i+2] & 0xff) << 8) + |
| 2170 |
(data[i+3] & 0xff); |
| 2171 |
checksum += word; |
| 2172 |
} |
| 2173 |
if (length & 3) { |
| 2174 |
word = 0; |
| 2175 |
i = length & ~3; |
| 2176 |
switch (length & 3) { |
| 2177 |
case 3: |
| 2178 |
word |= (data[i+2] & 0xff) << 8; |
| 2179 |
case 2: |
| 2180 |
word |= (data[i+1] & 0xff) << 16; |
| 2181 |
case 1: |
| 2182 |
word |= (data[i ] & 0xff) << 24; |
| 2183 |
break; |
| 2184 |
} |
| 2185 |
checksum += word; |
| 2186 |
} |
| 2187 |
return checksum; |
| 2188 |
} |
| 2189 |
|
| 2190 |
void FoFiTrueType::parse(int fontNum, GBool allowHeadlessCFF) { |
| 2191 |
Guint topTag; |
| 2192 |
int offset, pos, ver, i, j, k; |
| 2193 |
|
| 2194 |
parsedOk = gTrue; |
| 2195 |
|
| 2196 |
// check for a dfont or TrueType collection (TTC) |
| 2197 |
// offset = start of actual TrueType font file (table positions are |
| 2198 |
// relative to this |
| 2199 |
// pos = position of table directory (relative to offset) |
| 2200 |
if (isDfont) { |
| 2201 |
parseDfont(fontNum, &offset, &pos); |
| 2202 |
} else { |
| 2203 |
offset = 0; |
| 2204 |
topTag = getU32BE(0, &parsedOk); |
| 2205 |
if (!parsedOk) { |
| 2206 |
return; |
| 2207 |
} |
| 2208 |
if (topTag == ttcfTag) { |
| 2209 |
parseTTC(fontNum, &pos); |
| 2210 |
} else { |
| 2211 |
pos = 0; |
| 2212 |
} |
| 2213 |
} |
| 2214 |
if (!parsedOk) { |
| 2215 |
return; |
| 2216 |
} |
| 2217 |
|
| 2218 |
// check the sfnt version |
| 2219 |
ver = getU32BE(offset + pos, &parsedOk); |
| 2220 |
if (!parsedOk) { |
| 2221 |
return; |
| 2222 |
} |
| 2223 |
openTypeCFF = ver == 0x4f54544f; // 'OTTO' |
| 2224 |
|
| 2225 |
// read the table directory |
| 2226 |
nTables = getU16BE(offset + pos + 4, &parsedOk); |
| 2227 |
if (!parsedOk) { |
| 2228 |
return; |
| 2229 |
} |
| 2230 |
tables = (TrueTypeTable *)gmallocn(nTables, sizeof(TrueTypeTable)); |
| 2231 |
pos += 12; |
| 2232 |
j = 0; |
| 2233 |
for (i = 0; i < nTables; ++i) { |
| 2234 |
tables[j].tag = getU32BE(offset + pos, &parsedOk); |
| 2235 |
tables[j].checksum = getU32BE(offset + pos + 4, &parsedOk); |
| 2236 |
tables[j].offset = offset + (int)getU32BE(offset + pos + 8, &parsedOk); |
| 2237 |
tables[j].len = (int)getU32BE(offset + pos + 12, &parsedOk); |
| 2238 |
if (tables[j].offset >= 0 && |
| 2239 |
tables[j].len >= 0 && |
| 2240 |
tables[j].offset + tables[j].len >= tables[j].offset && |
| 2241 |
tables[j].offset + tables[j].len <= len) { |
| 2242 |
// ignore any bogus entries in the table directory |
| 2243 |
++j; |
| 2244 |
} |
| 2245 |
pos += 16; |
| 2246 |
} |
| 2247 |
nTables = j; |
| 2248 |
if (!parsedOk) { |
| 2249 |
return; |
| 2250 |
} |
| 2251 |
|
| 2252 |
// check for the head table; allow for a head-less OpenType CFF font |
| 2253 |
headlessCFF = gFalse; |
| 2254 |
if (seekTable("head") < 0) { |
| 2255 |
if (openTypeCFF && allowHeadlessCFF && seekTable("CFF ") >= 0) { |
| 2256 |
headlessCFF = gTrue; |
| 2257 |
nGlyphs = 0; |
| 2258 |
bbox[0] = bbox[1] = bbox[2] = bbox[3] = 0; |
| 2259 |
locaFmt = 0; |
| 2260 |
return; |
| 2261 |
} |
| 2262 |
parsedOk = gFalse; |
| 2263 |
return; |
| 2264 |
} |
| 2265 |
|
| 2266 |
// check for other tables that are required by both the TrueType |
| 2267 |
// spec and the Type 42 spec |
| 2268 |
if (seekTable("hhea") < 0 || |
| 2269 |
seekTable("maxp") < 0 || |
| 2270 |
seekTable("hmtx") < 0 || |
| 2271 |
(!openTypeCFF && seekTable("loca") < 0) || |
| 2272 |
(!openTypeCFF && seekTable("glyf") < 0) || |
| 2273 |
(openTypeCFF && seekTable("CFF ") < 0)) { |
| 2274 |
parsedOk = gFalse; |
| 2275 |
return; |
| 2276 |
} |
| 2277 |
|
| 2278 |
// read the cmaps |
| 2279 |
if ((i = seekTable("cmap")) >= 0 && tables[i].len >= 4) { |
| 2280 |
pos = tables[i].offset + 2; |
| 2281 |
nCmaps = getU16BE(pos, &parsedOk); |
| 2282 |
pos += 2; |
| 2283 |
if (!parsedOk) { |
| 2284 |
return; |
| 2285 |
} |
| 2286 |
cmaps = (TrueTypeCmap *)gmallocn(nCmaps, sizeof(TrueTypeCmap)); |
| 2287 |
k = 0; |
| 2288 |
for (j = 0; j < nCmaps; ++j) { |
| 2289 |
cmaps[k].platform = getU16BE(pos, &parsedOk); |
| 2290 |
cmaps[k].encoding = getU16BE(pos + 2, &parsedOk); |
| 2291 |
cmaps[k].offset = getU32BE(pos + 4, &parsedOk); |
| 2292 |
pos += 8; |
| 2293 |
if (cmaps[k].offset >= tables[i].len) { |
| 2294 |
// skip any invalid subtables |
| 2295 |
continue; |
| 2296 |
} |
| 2297 |
cmaps[k].offset += tables[i].offset; |
| 2298 |
cmaps[k].fmt = getU16BE(cmaps[k].offset, &parsedOk); |
| 2299 |
cmaps[k].len = getU16BE(cmaps[k].offset + 2, &parsedOk); |
| 2300 |
++k; |
| 2301 |
} |
| 2302 |
nCmaps = k; |
| 2303 |
if (!parsedOk) { |
| 2304 |
return; |
| 2305 |
} |
| 2306 |
} |
| 2307 |
|
| 2308 |
// get the number of glyphs from the maxp table |
| 2309 |
i = seekTable("maxp"); |
| 2310 |
nGlyphs = getU16BE(tables[i].offset + 4, &parsedOk); |
| 2311 |
if (!parsedOk) { |
| 2312 |
return; |
| 2313 |
} |
| 2314 |
|
| 2315 |
// get the bbox and loca table format from the head table |
| 2316 |
i = seekTable("head"); |
| 2317 |
bbox[0] = getS16BE(tables[i].offset + 36, &parsedOk); |
| 2318 |
bbox[1] = getS16BE(tables[i].offset + 38, &parsedOk); |
| 2319 |
bbox[2] = getS16BE(tables[i].offset + 40, &parsedOk); |
| 2320 |
bbox[3] = getS16BE(tables[i].offset + 42, &parsedOk); |
| 2321 |
locaFmt = getS16BE(tables[i].offset + 50, &parsedOk); |
| 2322 |
if (!parsedOk) { |
| 2323 |
return; |
| 2324 |
} |
| 2325 |
|
| 2326 |
// make sure the loca table is sane (correct length) |
| 2327 |
// NB: out-of-bounds entries are handled in writeTTF() |
| 2328 |
if (!openTypeCFF) { |
| 2329 |
i = seekTable("loca"); |
| 2330 |
if (tables[i].len < (locaFmt ? 4 : 2)) { |
| 2331 |
parsedOk = gFalse; |
| 2332 |
return; |
| 2333 |
} |
| 2334 |
if (tables[i].len < (nGlyphs + 1) * (locaFmt ? 4 : 2)) { |
| 2335 |
nGlyphs = tables[i].len / (locaFmt ? 4 : 2) - 1; |
| 2336 |
} |
| 2337 |
if (!parsedOk) { |
| 2338 |
return; |
| 2339 |
} |
| 2340 |
} |
| 2341 |
|
| 2342 |
// read the post table |
| 2343 |
readPostTable(); |
| 2344 |
} |
| 2345 |
|
| 2346 |
// Get the table directory position |
| 2347 |
void FoFiTrueType::parseTTC(int fontNum, int *pos) { |
| 2348 |
int nFonts; |
| 2349 |
|
| 2350 |
isTTC = gTrue; |
| 2351 |
nFonts = getU32BE(8, &parsedOk); |
| 2352 |
if (!parsedOk) { |
| 2353 |
return; |
| 2354 |
} |
| 2355 |
if (fontNum < 0 || fontNum >= nFonts) { |
| 2356 |
parsedOk = gFalse; |
| 2357 |
return; |
| 2358 |
} |
| 2359 |
*pos = getU32BE(12 + 4 * fontNum, &parsedOk); |
| 2360 |
} |
| 2361 |
|
| 2362 |
void FoFiTrueType::parseDfont(int fontNum, int *offset, int *startPos) { |
| 2363 |
int resMapOffset, resDataOffset; |
| 2364 |
int resTypeListOffset, nTypes, typeTag; |
| 2365 |
int nFonts, refListOffset, dataOffset; |
| 2366 |
int pos, i; |
| 2367 |
|
| 2368 |
resDataOffset = getU32BE(0, &parsedOk); |
| 2369 |
resMapOffset = getU32BE(4, &parsedOk); |
| 2370 |
if (!parsedOk) { |
| 2371 |
return; |
| 2372 |
} |
| 2373 |
|
| 2374 |
resTypeListOffset = getU16BE(resMapOffset + 24, &parsedOk); |
| 2375 |
// resNameListOffset = getU16BE(resMapOffset + 26, &parsedOk); |
| 2376 |
nTypes = getU16BE(resMapOffset + 28, &parsedOk) + 1; |
| 2377 |
if (!parsedOk) { |
| 2378 |
return; |
| 2379 |
} |
| 2380 |
|
| 2381 |
pos = 0; // make gcc happy |
| 2382 |
for (i = 0; i < nTypes; ++i) { |
| 2383 |
pos = resMapOffset + resTypeListOffset + 2 + 8*i; |
| 2384 |
typeTag = getU32BE(pos, &parsedOk); |
| 2385 |
if (!parsedOk) { |
| 2386 |
return; |
| 2387 |
} |
| 2388 |
if (typeTag == 0x73666e74) { // 'sfnt' |
| 2389 |
break; |
| 2390 |
} |
| 2391 |
} |
| 2392 |
if (i >= nTypes) { |
| 2393 |
parsedOk = gFalse; |
| 2394 |
return; |
| 2395 |
} |
| 2396 |
nFonts = getU16BE(pos + 4, &parsedOk) + 1; |
| 2397 |
refListOffset = getU16BE(pos + 6, &parsedOk); |
| 2398 |
if (!parsedOk) { |
| 2399 |
return; |
| 2400 |
} |
| 2401 |
if (fontNum < 0 || fontNum >= nFonts) { |
| 2402 |
parsedOk = gFalse; |
| 2403 |
return; |
| 2404 |
} |
| 2405 |
pos = resMapOffset + resTypeListOffset + refListOffset + 12 * fontNum; |
| 2406 |
dataOffset = getU32BE(pos + 4, &parsedOk) & 0x00ffffff; |
| 2407 |
if (!parsedOk) { |
| 2408 |
return; |
| 2409 |
} |
| 2410 |
// the data offset points to a 4-byte length field, which we skip over |
| 2411 |
*offset = resDataOffset + dataOffset + 4; |
| 2412 |
*startPos = 0; |
| 2413 |
} |
| 2414 |
|
| 2415 |
void FoFiTrueType::readPostTable() { |
| 2416 |
GString *name; |
| 2417 |
int tablePos, postFmt, stringIdx, stringPos; |
| 2418 |
GBool ok; |
| 2419 |
int i, j, n, m; |
| 2420 |
|
| 2421 |
ok = gTrue; |
| 2422 |
if ((i = seekTable("post")) < 0) { |
| 2423 |
return; |
| 2424 |
} |
| 2425 |
tablePos = tables[i].offset; |
| 2426 |
postFmt = getU32BE(tablePos, &ok); |
| 2427 |
if (!ok) { |
| 2428 |
goto err; |
| 2429 |
} |
| 2430 |
if (postFmt == 0x00010000) { |
| 2431 |
nameToGID = new GHash(gTrue); |
| 2432 |
for (i = 0; i < 258; ++i) { |
| 2433 |
nameToGID->add(new GString(macGlyphNames[i]), i); |
| 2434 |
} |
| 2435 |
} else if (postFmt == 0x00020000) { |
| 2436 |
nameToGID = new GHash(gTrue); |
| 2437 |
n = getU16BE(tablePos + 32, &ok); |
| 2438 |
if (!ok) { |
| 2439 |
goto err; |
| 2440 |
} |
| 2441 |
stringIdx = 0; |
| 2442 |
stringPos = tablePos + 34 + 2*n; |
| 2443 |
for (i = 0; i < n && i < nGlyphs; ++i) { |
| 2444 |
ok = gTrue; |
| 2445 |
j = getU16BE(tablePos + 34 + 2*i, &ok); |
| 2446 |
if (j < 258) { |
| 2447 |
nameToGID->removeInt(macGlyphNames[j]); |
| 2448 |
nameToGID->add(new GString(macGlyphNames[j]), i); |
| 2449 |
} else { |
| 2450 |
j -= 258; |
| 2451 |
if (j != stringIdx) { |
| 2452 |
for (stringIdx = 0, stringPos = tablePos + 34 + 2*n; |
| 2453 |
stringIdx < j; |
| 2454 |
++stringIdx, stringPos += 1 + getU8(stringPos, &ok)) ; |
| 2455 |
if (!ok) { |
| 2456 |
continue; |
| 2457 |
} |
| 2458 |
} |
| 2459 |
m = getU8(stringPos, &ok); |
| 2460 |
if (!ok || !checkRegion(stringPos + 1, m)) { |
| 2461 |
continue; |
| 2462 |
} |
| 2463 |
name = new GString((char *)&file[stringPos + 1], m); |
| 2464 |
nameToGID->removeInt(name); |
| 2465 |
nameToGID->add(name, i); |
| 2466 |
++stringIdx; |
| 2467 |
stringPos += 1 + m; |
| 2468 |
} |
| 2469 |
} |
| 2470 |
} else if (postFmt == 0x00028000) { |
| 2471 |
nameToGID = new GHash(gTrue); |
| 2472 |
for (i = 0; i < nGlyphs; ++i) { |
| 2473 |
j = getU8(tablePos + 32 + i, &ok); |
| 2474 |
if (!ok) { |
| 2475 |
continue; |
| 2476 |
} |
| 2477 |
if (j < 258) { |
| 2478 |
nameToGID->removeInt(macGlyphNames[j]); |
| 2479 |
nameToGID->add(new GString(macGlyphNames[j]), i); |
| 2480 |
} |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
return; |
| 2485 |
|
| 2486 |
err: |
| 2487 |
if (nameToGID) { |
| 2488 |
delete nameToGID; |
| 2489 |
nameToGID = NULL; |
| 2490 |
} |
| 2491 |
} |
| 2492 |
|
| 2493 |
int FoFiTrueType::seekTable(const char *tag) { |
| 2494 |
Guint tagI; |
| 2495 |
int i; |
| 2496 |
|
| 2497 |
tagI = ((tag[0] & 0xff) << 24) | |
| 2498 |
((tag[1] & 0xff) << 16) | |
| 2499 |
((tag[2] & 0xff) << 8) | |
| 2500 |
(tag[3] & 0xff); |
| 2501 |
for (i = 0; i < nTables; ++i) { |
| 2502 |
if (tables[i].tag == tagI) { |
| 2503 |
return i; |
| 2504 |
} |
| 2505 |
} |
| 2506 |
return -1; |
| 2507 |
} |