| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
/* |
| 6 |
* Abstract font dump. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "absfont.h" |
| 10 |
#include "dictops.h" |
| 11 |
#include "txops.h" |
| 12 |
#include "ctutil.h" |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdarg.h> |
| 17 |
#include <string.h> |
| 18 |
#include <ctype.h> |
| 19 |
#include <math.h> |
| 20 |
#include <limits.h> |
| 21 |
#include <float.h> |
| 22 |
|
| 23 |
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) |
| 24 |
#define DUMP_LANG_GROUP (1 << 0) |
| 25 |
#define RND(v) ((float)floor((v) + 0.5)) |
| 26 |
|
| 27 |
/* Dump string. */ |
| 28 |
static void dumpString(abfDumpCtx h, char *name, char *ptr) { |
| 29 |
if (ptr == ABF_UNSET_PTR) |
| 30 |
return; |
| 31 |
|
| 32 |
FPRINTF_S(h->fp, "%-20s", name); |
| 33 |
|
| 34 |
/* Dump string with carriage return and control character handling */ |
| 35 |
fputc('"', h->fp); |
| 36 |
for (;;) { |
| 37 |
int c = *ptr++; |
| 38 |
switch (c) { |
| 39 |
case '\0': |
| 40 |
FPRINTF_S(h->fp, "\"\n"); |
| 41 |
return; |
| 42 |
case '\r': |
| 43 |
fputc('\n', h->fp); |
| 44 |
if (*ptr == '\n') |
| 45 |
ptr++; |
| 46 |
break; |
| 47 |
case '\n': |
| 48 |
fputc('\n', h->fp); |
| 49 |
break; |
| 50 |
default: |
| 51 |
if (iscntrl(c)) |
| 52 |
FPRINTF_S(h->fp, "\\x%02X", c & 0xff); |
| 53 |
else |
| 54 |
fputc(c, h->fp); |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/* Dump integer array. */ |
| 61 |
static void dumpIntArray(abfDumpCtx h, char *name, long cnt, long *array) { |
| 62 |
int i; |
| 63 |
char *sep; |
| 64 |
|
| 65 |
if (cnt == ABF_EMPTY_ARRAY) |
| 66 |
return; |
| 67 |
|
| 68 |
FPRINTF_S(h->fp, "%-20s", name); |
| 69 |
sep = "{"; |
| 70 |
for (i = 0; i < cnt; i++) { |
| 71 |
FPRINTF_S(h->fp, "%s%ld", sep, array[i]); |
| 72 |
sep = ","; |
| 73 |
} |
| 74 |
FPRINTF_S(h->fp, "}\n"); |
| 75 |
} |
| 76 |
|
| 77 |
/* Dump real array. */ |
| 78 |
static void dumpRealArray(abfDumpCtx h, char *name, long cnt, float *array) { |
| 79 |
int i; |
| 80 |
char *sep; |
| 81 |
|
| 82 |
if (cnt == ABF_EMPTY_ARRAY) |
| 83 |
return; |
| 84 |
|
| 85 |
FPRINTF_S(h->fp, "%-20s", name); |
| 86 |
sep = "{"; |
| 87 |
for (i = 0; i < cnt; i++) { |
| 88 |
FPRINTF_S(h->fp, "%s%g", sep, array[i]); |
| 89 |
sep = ","; |
| 90 |
} |
| 91 |
FPRINTF_S(h->fp, "}\n"); |
| 92 |
} |
| 93 |
|
| 94 |
/* Dump FontMatrix. */ |
| 95 |
static void dumpFontMatrix(abfDumpCtx h, char *name, abfFontMatrix *FontMatrix) { |
| 96 |
if (!abfIsDefaultFontMatrix(FontMatrix)) { |
| 97 |
/* Print values with higher precision than dumpRealArray() provides */ |
| 98 |
int i; |
| 99 |
char *sep; |
| 100 |
FPRINTF_S(h->fp, "%-20s", name); |
| 101 |
sep = "{"; |
| 102 |
for (i = 0; i < FontMatrix->cnt; i++) { |
| 103 |
FPRINTF_S(h->fp, "%s%.8g", sep, FontMatrix->array[i]); |
| 104 |
sep = ","; |
| 105 |
} |
| 106 |
FPRINTF_S(h->fp, "}\n"); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* Dump top dictionary. */ |
| 111 |
static void dumpTopDict(abfDumpCtx h, abfTopDict *top) { |
| 112 |
char *OrigFontType; |
| 113 |
char *srcFontType; |
| 114 |
|
| 115 |
dumpString(h, "version", top->version.ptr); |
| 116 |
dumpString(h, "Notice", top->Notice.ptr); |
| 117 |
dumpString(h, "Copyright", top->Copyright.ptr); |
| 118 |
dumpString(h, "FullName", top->FullName.ptr); |
| 119 |
dumpString(h, "FamilyName", top->FamilyName.ptr); |
| 120 |
dumpString(h, "Weight", top->Weight.ptr); |
| 121 |
if (top->isFixedPitch != cff_DFLT_isFixedPitch) |
| 122 |
FPRINTF_S(h->fp, "isFixedPitch true\n"); |
| 123 |
if (top->ItalicAngle != cff_DFLT_ItalicAngle) |
| 124 |
FPRINTF_S(h->fp, "ItalicAngle %g\n", top->ItalicAngle); |
| 125 |
if (top->UnderlinePosition != cff_DFLT_UnderlinePosition) |
| 126 |
FPRINTF_S(h->fp, "UnderlinePosition %g\n", top->UnderlinePosition); |
| 127 |
if (top->UnderlineThickness != cff_DFLT_UnderlineThickness) |
| 128 |
FPRINTF_S(h->fp, "UnderlineThickness %g\n", top->UnderlineThickness); |
| 129 |
if (top->UniqueID != ABF_UNSET_INT) |
| 130 |
FPRINTF_S(h->fp, "UniqueID %ld\n", top->UniqueID); |
| 131 |
if (top->FontBBox[0] != 0.0 || top->FontBBox[1] != 0.0 || |
| 132 |
top->FontBBox[2] != 0.0 || top->FontBBox[3] != 0.0) |
| 133 |
FPRINTF_S(h->fp, "FontBBox {%g,%g,%g,%g}\n", |
| 134 |
top->FontBBox[0], top->FontBBox[1], |
| 135 |
top->FontBBox[2], top->FontBBox[3]); |
| 136 |
if (top->StrokeWidth != cff_DFLT_StrokeWidth) |
| 137 |
FPRINTF_S(h->fp, "StrokeWidth %g\n", top->StrokeWidth); |
| 138 |
dumpIntArray(h, "XUID", top->XUID.cnt, top->XUID.array); |
| 139 |
|
| 140 |
/* CFF and Acrobat extensions */ |
| 141 |
dumpString(h, "PostScript", top->PostScript.ptr); |
| 142 |
dumpString(h, "BaseFontName", top->BaseFontName.ptr); |
| 143 |
dumpIntArray(h, "BaseFontBlend", |
| 144 |
top->BaseFontBlend.cnt, top->BaseFontBlend.array); |
| 145 |
if (top->FSType != ABF_UNSET_INT) |
| 146 |
FPRINTF_S(h->fp, "FSType %ld\n", top->FSType); |
| 147 |
switch (top->OrigFontType) { |
| 148 |
case abfOrigFontTypeType1: |
| 149 |
OrigFontType = "Type1"; |
| 150 |
break; |
| 151 |
case abfOrigFontTypeCID: |
| 152 |
OrigFontType = "CID"; |
| 153 |
break; |
| 154 |
case abfOrigFontTypeTrueType: |
| 155 |
OrigFontType = "TrueType"; |
| 156 |
break; |
| 157 |
case abfOrigFontTypeOCF: |
| 158 |
OrigFontType = "OCF"; |
| 159 |
break; |
| 160 |
default: |
| 161 |
OrigFontType = NULL; |
| 162 |
break; |
| 163 |
} |
| 164 |
if (OrigFontType != NULL) |
| 165 |
FPRINTF_S(h->fp, "OrigFontType %s\n", OrigFontType); |
| 166 |
if (top->WasEmbedded != 0) |
| 167 |
FPRINTF_S(h->fp, "WasEmbedded true\n"); |
| 168 |
|
| 169 |
/* Synthetic font */ |
| 170 |
dumpString(h, "SynBaseFontName", top->SynBaseFontName.ptr); |
| 171 |
|
| 172 |
/* CID-keyed font extensions */ |
| 173 |
dumpFontMatrix(h, "cid.FontMatrix", &top->cid.FontMatrix); |
| 174 |
dumpString(h, "cid.CIDFontName", top->cid.CIDFontName.ptr); |
| 175 |
dumpString(h, "cid.Registry", top->cid.Registry.ptr); |
| 176 |
dumpString(h, "cid.Ordering", top->cid.Ordering.ptr); |
| 177 |
if (top->cid.Supplement != ABF_UNSET_INT) |
| 178 |
FPRINTF_S(h->fp, "cid.Supplement %ld\n", top->cid.Supplement); |
| 179 |
if (top->cid.CIDFontVersion != cff_DFLT_CIDFontVersion) |
| 180 |
FPRINTF_S(h->fp, "cid.CIDFontVersion %.03f\n", top->cid.CIDFontVersion); |
| 181 |
if (top->cid.CIDFontRevision != cff_DFLT_CIDFontRevision) |
| 182 |
FPRINTF_S(h->fp, "cid.CIDFontRevision %ld\n", top->cid.CIDFontRevision); |
| 183 |
if (top->cid.CIDCount != cff_DFLT_CIDCount) |
| 184 |
FPRINTF_S(h->fp, "cid.CIDCount %ld\n", top->cid.CIDCount); |
| 185 |
if (top->cid.UIDBase != ABF_UNSET_INT) |
| 186 |
FPRINTF_S(h->fp, "cid.UIDBase %ld\n", top->cid.UIDBase); |
| 187 |
|
| 188 |
/* Supplementary data */ |
| 189 |
if (top->sup.flags != 0) { |
| 190 |
char *sep = " ("; |
| 191 |
FPRINTF_S(h->fp, "sup.flags 0x%08lx", top->sup.flags); |
| 192 |
if (top->sup.flags & ABF_SYN_FONT) { |
| 193 |
FPRINTF_S(h->fp, "%sABF_SYN_FONT", sep); |
| 194 |
sep = ","; |
| 195 |
} |
| 196 |
if (top->sup.flags & ABF_CID_FONT) { |
| 197 |
FPRINTF_S(h->fp, "%sABF_CID_FONT", sep); |
| 198 |
sep = ","; |
| 199 |
} |
| 200 |
if (sep[0] == ',') |
| 201 |
FPRINTF_S(h->fp, ")"); |
| 202 |
FPRINTF_S(h->fp, "\n"); |
| 203 |
} |
| 204 |
switch (top->sup.srcFontType) { |
| 205 |
case abfSrcFontTypeType1Name: |
| 206 |
srcFontType = "Type 1 (name-keyed)"; |
| 207 |
break; |
| 208 |
case abfSrcFontTypeType1CID: |
| 209 |
srcFontType = "Type 1 (cid-keyed)"; |
| 210 |
break; |
| 211 |
case abfSrcFontTypeCFFName: |
| 212 |
srcFontType = "CFF (name-keyed)"; |
| 213 |
break; |
| 214 |
case abfSrcFontTypeCFFCID: |
| 215 |
srcFontType = "CFF (cid-keyed)"; |
| 216 |
break; |
| 217 |
case abfSrcFontTypeSVGName: |
| 218 |
srcFontType = "SVG (name-keyed)"; |
| 219 |
break; |
| 220 |
case abfSrcFontTypeUFOName: |
| 221 |
srcFontType = "UFO (name-keyed)"; |
| 222 |
break; |
| 223 |
case abfSrcFontTypeUFOCID: |
| 224 |
srcFontType = "UFO (cid-keyed)"; |
| 225 |
break; |
| 226 |
case abfSrcFontTypeTrueType: |
| 227 |
srcFontType = "TrueType"; |
| 228 |
break; |
| 229 |
default: |
| 230 |
srcFontType = NULL; |
| 231 |
break; |
| 232 |
} |
| 233 |
if (srcFontType != NULL) |
| 234 |
FPRINTF_S(h->fp, "sup.srcFontType %s\n", srcFontType); |
| 235 |
if (top->sup.UnitsPerEm != 1000) |
| 236 |
FPRINTF_S(h->fp, "sup.UnitsPerEm %ld\n", top->sup.UnitsPerEm); |
| 237 |
if (top->sup.nGlyphs != ABF_UNSET_INT) |
| 238 |
FPRINTF_S(h->fp, "sup.nGlyphs %ld\n", top->sup.nGlyphs); |
| 239 |
} |
| 240 |
|
| 241 |
/* Dump Private dictionary. */ |
| 242 |
static void dumpPrivateDict(abfDumpCtx h, abfPrivateDict *priv) { |
| 243 |
dumpRealArray(h, "BlueValues", |
| 244 |
priv->BlueValues.cnt, priv->BlueValues.array); |
| 245 |
dumpRealArray(h, "OtherBlues", |
| 246 |
priv->OtherBlues.cnt, priv->OtherBlues.array); |
| 247 |
dumpRealArray(h, "FamilyBlues", |
| 248 |
priv->FamilyBlues.cnt, priv->FamilyBlues.array); |
| 249 |
dumpRealArray(h, "FamilyOtherBlues", |
| 250 |
priv->FamilyOtherBlues.cnt, |
| 251 |
priv->FamilyOtherBlues.array); |
| 252 |
if (priv->BlueScale != (float)cff_DFLT_BlueScale) |
| 253 |
FPRINTF_S(h->fp, "BlueScale %g\n", priv->BlueScale); |
| 254 |
if (priv->BlueShift != cff_DFLT_BlueShift) |
| 255 |
FPRINTF_S(h->fp, "BlueShift %g\n", priv->BlueShift); |
| 256 |
if (priv->BlueFuzz != cff_DFLT_BlueFuzz) |
| 257 |
FPRINTF_S(h->fp, "BlueFuzz %g\n", priv->BlueFuzz); |
| 258 |
if (priv->StdHW != ABF_UNSET_REAL) |
| 259 |
FPRINTF_S(h->fp, "StdHW %g\n", priv->StdHW); |
| 260 |
if (priv->StdVW != ABF_UNSET_REAL) |
| 261 |
FPRINTF_S(h->fp, "StdVW %g\n", priv->StdVW); |
| 262 |
dumpRealArray(h, "StemSnapH", |
| 263 |
priv->StemSnapH.cnt, priv->StemSnapH.array); |
| 264 |
dumpRealArray(h, "StemSnapV", |
| 265 |
priv->StemSnapV.cnt, priv->StemSnapV.array); |
| 266 |
if (priv->ForceBold != cff_DFLT_ForceBold) |
| 267 |
FPRINTF_S(h->fp, "ForceBold true\n"); |
| 268 |
if (priv->LanguageGroup != cff_DFLT_LanguageGroup) |
| 269 |
FPRINTF_S(h->fp, "LanguageGroup %ld\n", priv->LanguageGroup); |
| 270 |
if (priv->ExpansionFactor != (float)cff_DFLT_ExpansionFactor) |
| 271 |
FPRINTF_S(h->fp, "ExpansionFactor %g\n", priv->ExpansionFactor); |
| 272 |
if (priv->initialRandomSeed != cff_DFLT_initialRandomSeed) |
| 273 |
FPRINTF_S(h->fp, "initialRandomSeed %g\n", priv->initialRandomSeed); |
| 274 |
} |
| 275 |
|
| 276 |
/* Dump font dictionary. */ |
| 277 |
static void dumpFontDict(abfDumpCtx h, abfFontDict *font) { |
| 278 |
dumpString(h, "FontName", font->FontName.ptr); |
| 279 |
if (font->PaintType != cff_DFLT_PaintType) |
| 280 |
FPRINTF_S(h->fp, "PaintType %ld\n", font->PaintType); |
| 281 |
dumpFontMatrix(h, "FontMatrix", &font->FontMatrix); |
| 282 |
FPRINTF_S(h->fp, "## Private\n"); |
| 283 |
dumpPrivateDict(h, &font->Private); |
| 284 |
} |
| 285 |
|
| 286 |
/* Begin font dump by dumping dicts. */ |
| 287 |
void abfDumpBegFont(abfDumpCtx h, abfTopDict *top) { |
| 288 |
if (h->level < 0 || h->level > 6) |
| 289 |
h->level = 1; /* Set default for invalid levels */ |
| 290 |
|
| 291 |
if (h->level <= 3) { |
| 292 |
long i, j; |
| 293 |
|
| 294 |
FPRINTF_S(h->fp, "## Filename %s\n", |
| 295 |
(top->sup.filename == ABF_UNSET_PTR) ? "<unknown>" : top->sup.filename); |
| 296 |
|
| 297 |
FPRINTF_S(h->fp, "## Top Dict\n"); |
| 298 |
dumpTopDict(h, top); |
| 299 |
for (i = 0; i < top->FDArray.cnt; i++) { |
| 300 |
if (h->fdCnt == 0) { |
| 301 |
FPRINTF_S(h->fp, "## FontDict[%ld]\n", i); |
| 302 |
dumpFontDict(h, &top->FDArray.array[i]); |
| 303 |
} else { |
| 304 |
if (h->excludeSubset) { |
| 305 |
unsigned int match = 0; |
| 306 |
for (j = 0; j < h->fdCnt; j++) { |
| 307 |
if (i == h->fdArray[j]) { |
| 308 |
match = 1; |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
if (!match) { |
| 313 |
FPRINTF_S(h->fp, "## FontDict[%ld]\n", i); |
| 314 |
dumpFontDict(h, &top->FDArray.array[i]); |
| 315 |
} |
| 316 |
|
| 317 |
} else { |
| 318 |
for (j = 0; j < h->fdCnt; j++) { |
| 319 |
if (i == h->fdArray[j]) { |
| 320 |
FPRINTF_S(h->fp, "## FontDict[%ld]\n", i); |
| 321 |
dumpFontDict(h, &top->FDArray.array[i]); |
| 322 |
break; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (h->level == 0) |
| 331 |
return; |
| 332 |
|
| 333 |
{ |
| 334 |
long i; |
| 335 |
h->flags = 0; |
| 336 |
for (i = 0; i < top->FDArray.cnt; i++) |
| 337 |
if (top->FDArray.array[i].Private.LanguageGroup == 1) { |
| 338 |
h->flags |= DUMP_LANG_GROUP; |
| 339 |
break; |
| 340 |
} |
| 341 |
h->left = 0; |
| 342 |
} |
| 343 |
|
| 344 |
/* Print glyph comment */ |
| 345 |
if (top->sup.flags & ABF_CID_FONT) |
| 346 |
/* UFO can store names even when CID-keyed */ |
| 347 |
if (top->sup.srcFontType == abfSrcFontTypeUFOCID) { |
| 348 |
FPRINTF_S(h->fp, "## glyph[tag] {name,cid,iFD"); |
| 349 |
} else { |
| 350 |
FPRINTF_S(h->fp, "## glyph[tag] {cid,iFD"); |
| 351 |
} else |
| 352 |
FPRINTF_S(h->fp, "## glyph[tag] {name,encoding"); |
| 353 |
if (h->flags & DUMP_LANG_GROUP) |
| 354 |
FPRINTF_S(h->fp, ",LanguageGroup"); |
| 355 |
if (h->level == 1 || h->level == 4) |
| 356 |
FPRINTF_S(h->fp, "}\n"); |
| 357 |
else |
| 358 |
FPRINTF_S(h->fp, ",path}\n"); |
| 359 |
} |
| 360 |
|
| 361 |
/* ------------------------------- Glyph Dump ------------------------------ */ |
| 362 |
|
| 363 |
/* Dump glyph beginning. */ |
| 364 |
static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) { |
| 365 |
abfDumpCtx h = (abfDumpCtx)cb->direct_ctx; |
| 366 |
|
| 367 |
cb->info = info; |
| 368 |
if (h->level == 0) |
| 369 |
return ABF_SKIP_RET; |
| 370 |
|
| 371 |
FPRINTF_S(h->fp, "glyph[%hu] {", info->tag); |
| 372 |
if (info->flags & ABF_GLYPH_CID) |
| 373 |
/* Dump CID-keyed glyph. Check for names first since UFO can store name and CID */ |
| 374 |
if (info->gname.ptr != NULL) { |
| 375 |
FPRINTF_S(h->fp, "%s,%hu,%u", info->gname.ptr, info->cid, info->iFD); |
| 376 |
} else { |
| 377 |
FPRINTF_S(h->fp, "%hu,%u", info->cid, info->iFD); |
| 378 |
} else { |
| 379 |
/* Dump name-keyed glyph */ |
| 380 |
abfEncoding *enc = &info->encoding; |
| 381 |
if (info->gname.ptr == NULL) |
| 382 |
FPRINTF_S(h->fp, "(missing)"); |
| 383 |
else |
| 384 |
FPRINTF_S(h->fp, "%s", info->gname.ptr); |
| 385 |
if (enc->code == ABF_GLYPH_UNENC) |
| 386 |
FPRINTF_S(h->fp, ",-"); |
| 387 |
else { |
| 388 |
/* Dump encoding */ |
| 389 |
char *sep = ","; |
| 390 |
do { |
| 391 |
if (info->flags & ABF_GLYPH_UNICODE) { |
| 392 |
if (enc->code < 0x10000) |
| 393 |
FPRINTF_S(h->fp, "%sU+%04lX", sep, enc->code); |
| 394 |
else |
| 395 |
FPRINTF_S(h->fp, "%sU+%lX", sep, enc->code); |
| 396 |
} else |
| 397 |
FPRINTF_S(h->fp, "%s0x%02lX", sep, enc->code); |
| 398 |
sep = "+"; |
| 399 |
enc = enc->next; |
| 400 |
} while (enc != NULL); |
| 401 |
} |
| 402 |
} |
| 403 |
if (h->flags & DUMP_LANG_GROUP) |
| 404 |
FPRINTF_S(h->fp, ",%d", (info->flags & ABF_GLYPH_LANG_1) != 0); |
| 405 |
switch (h->level) { |
| 406 |
case 1: |
| 407 |
case 4: |
| 408 |
FPRINTF_S(h->fp, "}\n"); |
| 409 |
return ABF_SKIP_RET; |
| 410 |
case 2: |
| 411 |
case 5: |
| 412 |
FPRINTF_S(h->fp, ","); |
| 413 |
break; |
| 414 |
case 3: |
| 415 |
case 6: |
| 416 |
FPRINTF_S(h->fp, ",\n"); |
| 417 |
break; |
| 418 |
} |
| 419 |
h->left = 0; |
| 420 |
|
| 421 |
return ABF_CONT_RET; |
| 422 |
} |
| 423 |
|
| 424 |
/* Dump instruction according to dump level. */ |
| 425 |
static void CTL_CDECL dumpInstr(abfGlyphCallbacks *cb, char *fmt, ...) { |
| 426 |
abfDumpCtx h = (abfDumpCtx)cb->direct_ctx; |
| 427 |
va_list ap; |
| 428 |
va_start(ap, fmt); |
| 429 |
switch (h->level) { |
| 430 |
case 2: |
| 431 |
case 5: { |
| 432 |
int length; |
| 433 |
char buf[128]; |
| 434 |
const size_t bufLen = sizeof(buf); |
| 435 |
VSPRINTF_S(buf, bufLen, fmt, ap); |
| 436 |
length = (int)strnlen(buf, bufLen); |
| 437 |
if (length > h->left) { |
| 438 |
char *p; |
| 439 |
/* Find break point */ |
| 440 |
for (p = &buf[h->left]; *p != ' '; p--) |
| 441 |
; |
| 442 |
FPRINTF_S(h->fp, "%.*s\n", (int)(p - buf), buf); |
| 443 |
FPRINTF_S(h->fp, "%s", p); |
| 444 |
h->left = (int)(79 - (strlen(p) + 1)); |
| 445 |
} else { |
| 446 |
FPRINTF_S(h->fp, "%s", buf); |
| 447 |
h->left -= length; |
| 448 |
} |
| 449 |
} break; |
| 450 |
case 3: |
| 451 |
case 6: |
| 452 |
FPRINTF_S(h->fp, " "); |
| 453 |
VFPRINTF_S(h->fp, fmt, ap); |
| 454 |
FPRINTF_S(h->fp, "\n"); |
| 455 |
break; |
| 456 |
default: |
| 457 |
break; |
| 458 |
} |
| 459 |
va_end(ap); |
| 460 |
} |
| 461 |
|
| 462 |
/* Dump glyph width. */ |
| 463 |
static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) { |
| 464 |
dumpInstr(cb, " %g width", roundf(hAdv)); |
| 465 |
} |
| 466 |
|
| 467 |
/* Write real number in ASCII to dst stream. */ |
| 468 |
#define TX_EPSILON 0.0003 |
| 469 |
/*In Xcode, FLT_EPSILON is 1.192..x10-7, but the diff between value-roundf(value) can be 3.05x10-5, when the input value is an integer. */ |
| 470 |
static void writeReal(char *buf, const size_t bufLen, float value) { |
| 471 |
char tmp[50]; |
| 472 |
|
| 473 |
/* if no decimal component, perform a faster to string conversion */ |
| 474 |
if ((fabs(value - roundf(value)) < TX_EPSILON) && (value > LONG_MIN) && (value < LONG_MAX)) |
| 475 |
SPRINTF_S(tmp, sizeof(tmp), " %ld", (long int)roundf(value)); |
| 476 |
else { |
| 477 |
int l; |
| 478 |
float value2 = (float)RND_ON_WRITE(value); // to avoid getting -0 from 0.0004. |
| 479 |
if ((value2 == 0) && (value < 0)) |
| 480 |
value2 = 0; |
| 481 |
SPRINTF_S(tmp, sizeof(tmp), " %.2f", value2); |
| 482 |
l = (int)strlen(tmp) - 1; |
| 483 |
if ((tmp[l] == '0') && (tmp[l - 1] == '0')) { |
| 484 |
tmp[l - 2] = 0; |
| 485 |
} |
| 486 |
} |
| 487 |
STRCAT_S(buf, bufLen, tmp); |
| 488 |
} |
| 489 |
|
| 490 |
/* Dump glyph move. */ |
| 491 |
static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0) { |
| 492 |
char buf[128]; |
| 493 |
const size_t bufLen = sizeof(buf); |
| 494 |
buf[0] = 0; |
| 495 |
writeReal(buf, bufLen, x0); |
| 496 |
writeReal(buf, bufLen, y0); |
| 497 |
STRCAT_S(buf, bufLen, " move"); |
| 498 |
dumpInstr(cb, "%s", buf); |
| 499 |
} |
| 500 |
|
| 501 |
/* Dump glyph line. */ |
| 502 |
static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1) { |
| 503 |
char buf[128]; |
| 504 |
const size_t bufLen = sizeof(buf); |
| 505 |
buf[0] = 0; |
| 506 |
writeReal(buf, bufLen, x1); |
| 507 |
writeReal(buf, bufLen, y1); |
| 508 |
STRCAT_S(buf, bufLen, " line"); |
| 509 |
dumpInstr(cb, "%s", buf); |
| 510 |
} |
| 511 |
|
| 512 |
/* Dump glyph curve. */ |
| 513 |
static void glyphCurve(abfGlyphCallbacks *cb, |
| 514 |
float x1, float y1, |
| 515 |
float x2, float y2, |
| 516 |
float x3, float y3) { |
| 517 |
char buf[128]; |
| 518 |
const size_t bufLen = sizeof(buf); |
| 519 |
buf[0] = 0; |
| 520 |
writeReal(buf, bufLen, x1); |
| 521 |
writeReal(buf, bufLen, y1); |
| 522 |
writeReal(buf, bufLen, x2); |
| 523 |
writeReal(buf, bufLen, y2); |
| 524 |
writeReal(buf, bufLen, x3); |
| 525 |
writeReal(buf, bufLen, y3); |
| 526 |
STRCAT_S(buf, bufLen, " curve"); |
| 527 |
dumpInstr(cb, "%s", buf); |
| 528 |
} |
| 529 |
|
| 530 |
/* Dump glyph stem. */ |
| 531 |
static void glyphStem(abfGlyphCallbacks *cb, |
| 532 |
int flags, float edge0, float edge1) { |
| 533 |
char buf[128]; |
| 534 |
const size_t bufLen = sizeof(buf); |
| 535 |
buf[0] = 0; |
| 536 |
|
| 537 |
if (flags & ABF_NEW_HINTS) |
| 538 |
dumpInstr(cb, " newhints"); |
| 539 |
if (flags & ABF_NEW_GROUP) |
| 540 |
dumpInstr(cb, " newgroup"); |
| 541 |
if (flags & ABF_CNTR_STEM) { |
| 542 |
writeReal(buf, bufLen, edge0); |
| 543 |
writeReal(buf, bufLen, edge1); |
| 544 |
STRCAT_S(buf, bufLen, (flags & ABF_VERT_STEM) ? " vcntr" : " hcntr"); |
| 545 |
// dumpInstr(cb, " %.3f %.3f %s", edge0, edge1, (flags & ABF_VERT_STEM)? "vcntr": "hcntr"); |
| 546 |
} else if (flags & ABF_STEM3_STEM) { |
| 547 |
writeReal(buf, bufLen, edge0); |
| 548 |
writeReal(buf, bufLen, edge1); |
| 549 |
STRCAT_S(buf, bufLen, (flags & ABF_VERT_STEM) ? " vstem3" : " hstem3"); |
| 550 |
// dumpInstr(cb, " %.3f %.3f %s", edge0, edge1, (flags & ABF_VERT_STEM)? "vstem3": "hstem3"); |
| 551 |
} else { |
| 552 |
float width = RND(edge1 - edge0); |
| 553 |
if (width == ABF_EDGE_HINT_LO) { |
| 554 |
writeReal(buf, bufLen, edge1); |
| 555 |
STRCAT_S(buf, bufLen, (flags & ABF_VERT_STEM) ? " leftedge" : " bottomedge"); |
| 556 |
// dumpInstr(cb, " %.3f %s", edge1, (flags & ABF_VERT_STEM)? "leftedge": "bottomedge"); |
| 557 |
} else if (width == ABF_EDGE_HINT_HI) { |
| 558 |
writeReal(buf, bufLen, edge0); |
| 559 |
STRCAT_S(buf, bufLen, (flags & ABF_VERT_STEM) ? " rightedge" : " topedge"); |
| 560 |
// dumpInstr(cb, " %.3f %s", edge0, (flags & ABF_VERT_STEM)? "rightedge": "topedge"); |
| 561 |
} else { |
| 562 |
writeReal(buf, bufLen, edge0); |
| 563 |
writeReal(buf, bufLen, edge1); |
| 564 |
STRCAT_S(buf, bufLen, (flags & ABF_VERT_STEM) ? " vstem" : " hstem"); |
| 565 |
// dumpInstr(cb, " %.3f %.3f %s", edge0, edge1, (flags & ABF_VERT_STEM)? "vstem": "hstem"); |
| 566 |
} |
| 567 |
} |
| 568 |
dumpInstr(cb, "%s", buf); |
| 569 |
} |
| 570 |
|
| 571 |
/* Dump glyph flex. */ |
| 572 |
static void glyphFlex(abfGlyphCallbacks *cb, float depth, |
| 573 |
float x1, float y1, |
| 574 |
float x2, float y2, |
| 575 |
float x3, float y3, |
| 576 |
float x4, float y4, |
| 577 |
float x5, float y5, |
| 578 |
float x6, float y6) { |
| 579 |
char buf[256]; |
| 580 |
const size_t bufLen = sizeof(buf); |
| 581 |
buf[0] = 0; |
| 582 |
|
| 583 |
writeReal(buf, bufLen, x1); |
| 584 |
writeReal(buf, bufLen, y1); |
| 585 |
writeReal(buf, bufLen, x2); |
| 586 |
writeReal(buf, bufLen, y2); |
| 587 |
writeReal(buf, bufLen, x3); |
| 588 |
writeReal(buf, bufLen, y3); |
| 589 |
writeReal(buf, bufLen, x4); |
| 590 |
writeReal(buf, bufLen, y4); |
| 591 |
writeReal(buf, bufLen, x5); |
| 592 |
writeReal(buf, bufLen, y5); |
| 593 |
writeReal(buf, bufLen, x6); |
| 594 |
writeReal(buf, bufLen, y6); |
| 595 |
writeReal(buf, bufLen, depth); |
| 596 |
STRCAT_S(buf, bufLen, " flex"); |
| 597 |
dumpInstr(cb, "%s", buf); |
| 598 |
} |
| 599 |
|
| 600 |
/* Dump glyph general operator. */ |
| 601 |
static void glyphGenop(abfGlyphCallbacks *cb, |
| 602 |
int cnt, float *args, int op) { |
| 603 |
static char *opnames[] = |
| 604 |
{ |
| 605 |
"reserved0", |
| 606 |
"hstem", |
| 607 |
"reserved2", |
| 608 |
"vstem", |
| 609 |
"vmoveto", |
| 610 |
"rlineto", |
| 611 |
"hlineto", |
| 612 |
"vlineto", |
| 613 |
"rrcurveto", |
| 614 |
"reserved9", |
| 615 |
"callsubr", |
| 616 |
"return", |
| 617 |
"escape", |
| 618 |
"reserved13", |
| 619 |
"endchar", |
| 620 |
"vsindex", |
| 621 |
"blend", |
| 622 |
"reserved17", |
| 623 |
"hstemhm", |
| 624 |
"hintmask", |
| 625 |
"cntrmask", |
| 626 |
"rmoveto", |
| 627 |
"hmoveto", |
| 628 |
"vstemhm", |
| 629 |
"rcurveline", |
| 630 |
"rlinecurve", |
| 631 |
"vvcurveto", |
| 632 |
"hhcurveto", |
| 633 |
"shortint", |
| 634 |
"callgsubr", |
| 635 |
"vhcurveto", |
| 636 |
"hvcurveto", |
| 637 |
}; |
| 638 |
static char *escopnames[] = |
| 639 |
{ |
| 640 |
"dotsection", |
| 641 |
"reservedESC1", |
| 642 |
"reservedESC2", |
| 643 |
"and", |
| 644 |
"or", |
| 645 |
"not", |
| 646 |
"reservedESC6", |
| 647 |
"reservedESC7", |
| 648 |
"reservedESC8", |
| 649 |
"abs", |
| 650 |
"add", |
| 651 |
"sub", |
| 652 |
"div", |
| 653 |
"reservedESC13", |
| 654 |
"neg", |
| 655 |
"eq", |
| 656 |
"reservedESC16", |
| 657 |
"reservedESC17", |
| 658 |
"drop", |
| 659 |
"reservedESC19", |
| 660 |
"put", |
| 661 |
"get", |
| 662 |
"ifelse", |
| 663 |
"random", |
| 664 |
"mul", |
| 665 |
"reservedESC25", |
| 666 |
"sqrt", |
| 667 |
"dup", |
| 668 |
"exch", |
| 669 |
"index", |
| 670 |
"roll", |
| 671 |
"reservedESC31", |
| 672 |
"reservedESC32", |
| 673 |
"cntroff", /* Reserved but used as cntroff by implementation */ |
| 674 |
"hflex", |
| 675 |
"flex", |
| 676 |
"hflex1", |
| 677 |
"flex1", |
| 678 |
"cntron", |
| 679 |
}; |
| 680 |
int hi = op >> 8 & 0xff; |
| 681 |
int lo = op & 0xff; |
| 682 |
char buf[80]; |
| 683 |
const size_t bufLen = sizeof(buf); |
| 684 |
char *p = buf; |
| 685 |
|
| 686 |
/* Format args */ |
| 687 |
size_t remainingBufLength = bufLen; |
| 688 |
while (cnt--) { |
| 689 |
size_t usedPrintLength; |
| 690 |
SPRINTF_S(p, remainingBufLength, " %g", *args++); |
| 691 |
usedPrintLength = strnlen(p, remainingBufLength); |
| 692 |
p += usedPrintLength; |
| 693 |
remainingBufLength -= usedPrintLength; |
| 694 |
} |
| 695 |
|
| 696 |
/* Format operator */ |
| 697 |
if (hi) { |
| 698 |
if (hi != tx_escape) |
| 699 |
SPRINTF_S(p, remainingBufLength, " invalid"); |
| 700 |
else if (lo >= (int)ARRAY_LEN(escopnames)) |
| 701 |
SPRINTF_S(p, remainingBufLength, " reservedESC%d", lo); |
| 702 |
else |
| 703 |
SPRINTF_S(p, remainingBufLength, " %s", escopnames[lo]); |
| 704 |
} else if (lo >= (int)ARRAY_LEN(opnames)) |
| 705 |
SPRINTF_S(p, remainingBufLength, " reserved%d", lo); |
| 706 |
else |
| 707 |
SPRINTF_S(p, remainingBufLength, " %s", opnames[lo]); |
| 708 |
|
| 709 |
dumpInstr(cb, buf); |
| 710 |
} |
| 711 |
|
| 712 |
/* Dump glyph seac. */ |
| 713 |
static void glyphSeac(abfGlyphCallbacks *cb, |
| 714 |
float adx, float ady, int bchar, int achar) { |
| 715 |
dumpInstr(cb, " %g %g %d %d seac", adx, ady, bchar, achar); |
| 716 |
} |
| 717 |
|
| 718 |
/* Dump glyph end. */ |
| 719 |
static void glyphEnd(abfGlyphCallbacks *cb) { |
| 720 |
abfDumpCtx h = (abfDumpCtx)cb->direct_ctx; |
| 721 |
|
| 722 |
dumpInstr(cb, " endchar}"); |
| 723 |
switch (h->level) { |
| 724 |
case 2: |
| 725 |
case 5: |
| 726 |
FPRINTF_S(h->fp, "\n"); |
| 727 |
break; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
/* Dump glyph callbacks */ |
| 732 |
const abfGlyphCallbacks abfGlyphDumpCallbacks = |
| 733 |
{ |
| 734 |
NULL, |
| 735 |
NULL, |
| 736 |
NULL, |
| 737 |
glyphBeg, |
| 738 |
glyphWidth, |
| 739 |
glyphMove, |
| 740 |
glyphLine, |
| 741 |
glyphCurve, |
| 742 |
glyphStem, |
| 743 |
glyphFlex, |
| 744 |
glyphGenop, |
| 745 |
glyphSeac, |
| 746 |
glyphEnd, |
| 747 |
}; |