| 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 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <time.h> |
| 14 |
#include <math.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) |
| 18 |
#define PTS_TO_GLYPH(v) ((v) / h->glyph.scale) |
| 19 |
|
| 20 |
#define TILE_SIZE 35 |
| 21 |
#define TILE_COLS 16 |
| 22 |
#define TILE_ROWS 20 |
| 23 |
#define TILE_TOP (TILE_ROWS * TILE_SIZE) |
| 24 |
#define TILE_RIGHT (TILE_COLS * TILE_SIZE) |
| 25 |
#define TILE_TEXT_FONT "Helvetica-Narrow" |
| 26 |
#define TILE_TEXT_SIZE 5.0f |
| 27 |
#define TILE_TEXT_BASE (TILE_TEXT_SIZE * 0.7f) |
| 28 |
#define TILE_GLYPH_SIZE 24.0f |
| 29 |
#define TILE_GLYPH_BASE (TILE_GLYPH_SIZE * 0.7f) |
| 30 |
|
| 31 |
#define PAGE_RIGHT TILE_RIGHT |
| 32 |
#define PAGE_TOP 747 |
| 33 |
#define PAGE_TEXT_FONT "Helvetica-Narrow" |
| 34 |
#define PAGE_TEXT_SIZE 10.0f |
| 35 |
#define PAGE_TEXT_BASE (PAGE_TEXT_SIZE * 0.7f) |
| 36 |
|
| 37 |
#define LABEL_TEXT_SIZE 4.0f |
| 38 |
#define LABEL_TEXT_FONT "Courier" |
| 39 |
#define LABEL_TEXT_BASE (LABEL_TEXT_SIZE * 0.7f) |
| 40 |
|
| 41 |
/* One-glyph-per-page origin */ |
| 42 |
#define EM_ORIG_H 72.0 |
| 43 |
#define EM_ORIG_V 252.0 |
| 44 |
|
| 45 |
/* Map value to em coordinate space */ |
| 46 |
#define TO_EM(v) ((float)(v) / h->glyph.scale) |
| 47 |
|
| 48 |
/* Express value in ems */ |
| 49 |
#define IN_EM(v) ((float)(v)*h->top->sup.UnitsPerEm) |
| 50 |
|
| 51 |
/* Flags bits */ |
| 52 |
#define NEW_PAGE (1UL << 31) |
| 53 |
|
| 54 |
typedef struct |
| 55 |
{ |
| 56 |
float x; |
| 57 |
float y; |
| 58 |
} Vector; |
| 59 |
|
| 60 |
/* Draw and label tile. */ |
| 61 |
static void drawTile(abfDrawCtx h, float x, float y, |
| 62 |
char *tag, char *hAdv, char *gname) { |
| 63 |
/* Draw tile box */ |
| 64 |
fprintf(h->fp, |
| 65 |
"newpath\n" |
| 66 |
"%g %g moveto %d 0 rlineto 0 %d rlineto %d 0 rlineto\n" |
| 67 |
"closepath 0 setlinewidth stroke\n", |
| 68 |
x, y, TILE_SIZE, -TILE_SIZE, -TILE_SIZE); |
| 69 |
|
| 70 |
/* Draw tag and gname labels */ |
| 71 |
fprintf(h->fp, |
| 72 |
"%g %g moveto (%s) show\n" |
| 73 |
"%g %g moveto (%s) show\n", |
| 74 |
x + 1, y - 1 - TILE_TEXT_BASE, tag, |
| 75 |
x + 1, y - TILE_SIZE + TILE_TEXT_SIZE - TILE_TEXT_BASE, gname); |
| 76 |
|
| 77 |
/* Draw hAdv label */ |
| 78 |
fprintf(h->fp, |
| 79 |
"%g (%s) stringwidth pop sub %g moveto (%s) show\n", |
| 80 |
x + TILE_SIZE - 1, hAdv, y - 1 - TILE_TEXT_BASE, hAdv); |
| 81 |
} |
| 82 |
|
| 83 |
/* Begin page. */ |
| 84 |
static void pageBeg(abfDrawCtx h, abfGlyphInfo *info) { |
| 85 |
char datestr[20]; |
| 86 |
char timestr[20]; |
| 87 |
time_t seconds_since_epoch; |
| 88 |
struct tm local_time; |
| 89 |
float y; |
| 90 |
char *fontname; |
| 91 |
|
| 92 |
if (h->top->sup.flags & ABF_CID_FONT) |
| 93 |
fontname = h->top->cid.CIDFontName.ptr; |
| 94 |
else |
| 95 |
fontname = h->top->FDArray.array[0].FontName.ptr; |
| 96 |
if (fontname == ABF_UNSET_PTR) |
| 97 |
fontname = "<unknown>"; |
| 98 |
|
| 99 |
/* Make formatted date and time string */ |
| 100 |
seconds_since_epoch = time(NULL); |
| 101 |
SAFE_LOCALTIME(&seconds_since_epoch, &local_time); |
| 102 |
strftime(datestr, sizeof(datestr), "Date: %m/%d/%y", &local_time); |
| 103 |
strftime(timestr, sizeof(timestr), "Time: %H:%M", &local_time); |
| 104 |
|
| 105 |
fprintf(h->fp, |
| 106 |
"%% page: %d\n" |
| 107 |
"18 18 translate\n" |
| 108 |
"/%s findfont %g scalefont setfont\n", |
| 109 |
h->pageno, |
| 110 |
PAGE_TEXT_FONT, PAGE_TEXT_SIZE); |
| 111 |
|
| 112 |
y = (int)(PAGE_TOP - PAGE_TEXT_BASE + 0.5); |
| 113 |
fprintf(h->fp, |
| 114 |
"0 %g moveto (Filename: ", |
| 115 |
y); |
| 116 |
if (h->top->sup.filename == ABF_UNSET_PTR) |
| 117 |
fprintf(h->fp, "<unknown>"); |
| 118 |
else { |
| 119 |
char *p = h->top->sup.filename; |
| 120 |
while (*p != '\0') { |
| 121 |
int c = *p++; |
| 122 |
fputc(c, h->fp); |
| 123 |
if (c == '\\') |
| 124 |
fputc(c, h->fp); |
| 125 |
} |
| 126 |
} |
| 127 |
fprintf(h->fp, |
| 128 |
") show\n" |
| 129 |
"%d (%s) stringwidth pop sub %g moveto (%s) show\n", |
| 130 |
PAGE_RIGHT, datestr, y, datestr); |
| 131 |
|
| 132 |
y -= (PAGE_TEXT_SIZE + 1); |
| 133 |
fprintf(h->fp, |
| 134 |
"0 %g moveto (FontName: %s) show\n", |
| 135 |
y, fontname); |
| 136 |
fprintf(h->fp, |
| 137 |
"%d (%s) stringwidth pop sub %g moveto (%s) show\n", |
| 138 |
PAGE_RIGHT, timestr, y, timestr); |
| 139 |
|
| 140 |
y -= (PAGE_TEXT_SIZE + 1); |
| 141 |
fprintf(h->fp, |
| 142 |
"0 %g moveto (Em: %ld units) show\n", |
| 143 |
y, h->top->sup.UnitsPerEm); |
| 144 |
fprintf(h->fp, |
| 145 |
"%d (Page: %d) stringwidth pop sub %g moveto (Page: %d) show\n", |
| 146 |
PAGE_RIGHT, h->pageno, y, h->pageno); |
| 147 |
|
| 148 |
if (h->level == 0) { |
| 149 |
fprintf(h->fp, |
| 150 |
"/%s findfont %g scalefont setfont\n", |
| 151 |
TILE_TEXT_FONT, TILE_TEXT_SIZE); |
| 152 |
if (info->flags & ABF_CID_FONT) |
| 153 |
drawTile(h, (TILE_COLS - 3) * TILE_SIZE, PAGE_TOP, |
| 154 |
"tag,fd", "hAdv", "cid"); |
| 155 |
else |
| 156 |
drawTile(h, (TILE_COLS - 3) * TILE_SIZE, PAGE_TOP, |
| 157 |
"tag,enc", "hAdv", "gname"); |
| 158 |
|
| 159 |
if (h->flags & ABF_SHOW_BY_ENC) { |
| 160 |
/* Draw encoding grid */ |
| 161 |
int i; |
| 162 |
|
| 163 |
fprintf(h->fp, "gsave\n"); |
| 164 |
|
| 165 |
/* Draw horizontal grid lines */ |
| 166 |
for (i = 0; i < 17; i++) { |
| 167 |
int v = TILE_TOP - i * TILE_SIZE; |
| 168 |
fprintf(h->fp, |
| 169 |
"%d %d moveto\n" |
| 170 |
"%d %d lineto\n", |
| 171 |
0, v, |
| 172 |
TILE_RIGHT, v); |
| 173 |
} |
| 174 |
|
| 175 |
/* Draw vertical grid lines */ |
| 176 |
for (i = 0; i < 17; i++) |
| 177 |
fprintf(h->fp, |
| 178 |
"%d %d moveto\n" |
| 179 |
"%d %d lineto\n", |
| 180 |
i * TILE_SIZE, TILE_TOP, |
| 181 |
i * TILE_SIZE, TILE_TOP - 16 * TILE_SIZE); |
| 182 |
|
| 183 |
fprintf(h->fp, |
| 184 |
"0 setlinewidth\n" |
| 185 |
".4 setgray\n" |
| 186 |
"stroke\n" |
| 187 |
"grestore\n"); |
| 188 |
} |
| 189 |
} else |
| 190 |
fprintf(h->fp, |
| 191 |
"/%s findfont %g scalefont setfont\n", |
| 192 |
LABEL_TEXT_FONT, TO_EM(LABEL_TEXT_SIZE)); |
| 193 |
|
| 194 |
h->flags &= ~NEW_PAGE; |
| 195 |
} |
| 196 |
|
| 197 |
/* End page. */ |
| 198 |
static void pageEnd(abfDrawCtx h) { |
| 199 |
fprintf(h->fp, "showpage\n"); |
| 200 |
h->pageno++; |
| 201 |
h->flags |= NEW_PAGE; |
| 202 |
} |
| 203 |
|
| 204 |
/* Begin drawing new font. */ |
| 205 |
void abfDrawBegFont(abfDrawCtx h, abfTopDict *top) { |
| 206 |
if (h->level > 1 || h->level < 0) |
| 207 |
h->level = 0; /* Set invalid level to tile mode */ |
| 208 |
|
| 209 |
h->flags |= NEW_PAGE; |
| 210 |
|
| 211 |
fprintf(h->fp, "%%!\n"); |
| 212 |
if (h->flags & ABF_DUPLEX_PRINT) |
| 213 |
fprintf(h->fp, |
| 214 |
"mark\n" |
| 215 |
"{2 dict dup /Duplex true put\n" |
| 216 |
"dup /Tumble false put setpagedevice} stopped\n" |
| 217 |
"cleartomark\n"); |
| 218 |
|
| 219 |
if (h->level == 0) { |
| 220 |
h->glyph.scale = TILE_GLYPH_SIZE / top->sup.UnitsPerEm; |
| 221 |
h->tile.h = 0; |
| 222 |
h->tile.v = (int)TILE_TOP; |
| 223 |
} else { |
| 224 |
h->glyph.scale = 500.0f / top->sup.UnitsPerEm; |
| 225 |
fprintf(h->fp, |
| 226 |
"/cntlpt{gsave newpath %g 0 360 arc fill grestore}bind def\n" |
| 227 |
"/arrow{newpath\n" |
| 228 |
"0 0 moveto %g %g rlineto 0 %g rlineto closepath fill\n" |
| 229 |
"}bind def\n" |
| 230 |
"/closept{newpath 0 0 %g 0 360 arc fill}bind def\n", |
| 231 |
TO_EM(0.5), |
| 232 |
TO_EM(-6), TO_EM(-1.5), TO_EM(3), |
| 233 |
TO_EM(1)); |
| 234 |
h->metrics.cb = abfGlyphMetricsCallbacks; |
| 235 |
h->metrics.cb.direct_ctx = &h->metrics.ctx; |
| 236 |
h->metrics.ctx.flags = 0; |
| 237 |
} |
| 238 |
|
| 239 |
h->pageno = 1; |
| 240 |
h->top = top; |
| 241 |
} |
| 242 |
|
| 243 |
/* End drawing new font. */ |
| 244 |
void abfDrawEndFont(abfDrawCtx h) { |
| 245 |
if (h->level != 0) |
| 246 |
return; |
| 247 |
|
| 248 |
if (h->flags & ABF_SHOW_BY_ENC || |
| 249 |
(h->tile.h != 0 || h->tile.v != TILE_TOP)) |
| 250 |
pageEnd(h); /* Show last page */ |
| 251 |
} |
| 252 |
|
| 253 |
/* ------------------------------- Glyph Draw ------------------------------ */ |
| 254 |
|
| 255 |
/* Draw glyph beginning. */ |
| 256 |
static int glyphBeg(abfGlyphCallbacks *cb, abfGlyphInfo *info) { |
| 257 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 258 |
|
| 259 |
cb->info = info; |
| 260 |
if (h->flags & NEW_PAGE) |
| 261 |
pageBeg(h, info); |
| 262 |
|
| 263 |
if (h->level == 0 && h->flags & ABF_SHOW_BY_ENC) { |
| 264 |
unsigned long code = info->encoding.code; |
| 265 |
if (code > 255) |
| 266 |
h->showglyph = 0; |
| 267 |
else { |
| 268 |
h->tile.h = (code & 0xf) * TILE_SIZE; |
| 269 |
h->tile.v = TILE_TOP - (code >> 4 & 0xf) * TILE_SIZE; |
| 270 |
h->showglyph = 1; |
| 271 |
} |
| 272 |
} else |
| 273 |
h->showglyph = 1; |
| 274 |
|
| 275 |
if (h->showglyph) { |
| 276 |
if (info->flags & ABF_GLYPH_CID) |
| 277 |
fprintf(h->fp, "%% glyph: \\%hu\n", info->cid); |
| 278 |
else |
| 279 |
fprintf(h->fp, "%% glyph: %s\n", info->gname.ptr); |
| 280 |
|
| 281 |
h->path.moves = 0; |
| 282 |
h->path.lines = 0; |
| 283 |
h->path.curves = 0; |
| 284 |
|
| 285 |
if (h->level != 0) |
| 286 |
h->metrics.cb.beg(&h->metrics.cb, info); |
| 287 |
} |
| 288 |
|
| 289 |
return ABF_CONT_RET; |
| 290 |
} |
| 291 |
|
| 292 |
/* Draw glyph width. */ |
| 293 |
static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) { |
| 294 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 295 |
|
| 296 |
if (!h->showglyph) |
| 297 |
return; |
| 298 |
|
| 299 |
if (h->level != 0) { |
| 300 |
/* Draw ruler */ |
| 301 |
int i; |
| 302 |
|
| 303 |
fprintf(h->fp, |
| 304 |
"gsave\n" |
| 305 |
"%g %g scale\n" |
| 306 |
"0 0 moveto\n" |
| 307 |
"100 0 lineto\n", |
| 308 |
h->glyph.scale, h->glyph.scale); |
| 309 |
|
| 310 |
for (i = 0; i <= 100; i += 10) |
| 311 |
fprintf(h->fp, |
| 312 |
"%d 0 moveto\n" |
| 313 |
"%d 10 lineto\n", |
| 314 |
i, i); |
| 315 |
for (i = 5; i < 100; i += 10) |
| 316 |
fprintf(h->fp, |
| 317 |
"%d 0 moveto\n" |
| 318 |
"%d 7 lineto\n", |
| 319 |
i, i); |
| 320 |
|
| 321 |
fprintf(h->fp, |
| 322 |
"0 setlinewidth stroke\n" |
| 323 |
"110 0 moveto (100 units) show\n" |
| 324 |
"grestore\n"); |
| 325 |
} |
| 326 |
|
| 327 |
/* Prepare em space */ |
| 328 |
fprintf(h->fp, "gsave\n"); |
| 329 |
if (h->level == 0) |
| 330 |
fprintf(h->fp, |
| 331 |
"%g %g translate\n" |
| 332 |
"%g %g scale\n", |
| 333 |
h->tile.h + |
| 334 |
(TILE_SIZE - TILE_GLYPH_SIZE * hAdv / h->top->sup.UnitsPerEm) / 2, |
| 335 |
h->tile.v - TILE_SIZE * 0.7, |
| 336 |
h->glyph.scale, h->glyph.scale); |
| 337 |
else |
| 338 |
fprintf(h->fp, |
| 339 |
"%g %g translate\n" |
| 340 |
"%g %g scale\n", |
| 341 |
EM_ORIG_H, EM_ORIG_V, |
| 342 |
h->glyph.scale, h->glyph.scale); |
| 343 |
|
| 344 |
/* Draw origin */ |
| 345 |
fprintf(h->fp, |
| 346 |
"%% origin tic\n" |
| 347 |
"%g 0 moveto\n" |
| 348 |
"%g 0 rlineto\n" |
| 349 |
"0 %g rlineto\n", |
| 350 |
IN_EM(-0.03), IN_EM(0.03), IN_EM(-0.03)); |
| 351 |
|
| 352 |
/* Draw width */ |
| 353 |
fprintf(h->fp, |
| 354 |
"%% width tic\n" |
| 355 |
"%g 0 moveto\n" |
| 356 |
"%g 0 rlineto\n" |
| 357 |
"0 %g rlineto\n", |
| 358 |
hAdv + IN_EM(0.03), IN_EM(-0.03), IN_EM(-0.03)); |
| 359 |
|
| 360 |
fprintf(h->fp, "0 setlinewidth stroke\n"); |
| 361 |
if (h->level != 0) |
| 362 |
h->metrics.cb.width(&h->metrics.cb, hAdv); |
| 363 |
|
| 364 |
h->glyph.hAdv = hAdv; |
| 365 |
} |
| 366 |
|
| 367 |
/* Make normalized vector. */ |
| 368 |
static Vector makeVec(float x, float y) { |
| 369 |
Vector v; |
| 370 |
float d = (float)sqrt(x * x + y * y); |
| 371 |
if (d == 0) { |
| 372 |
v.x = 1; |
| 373 |
v.y = 1; |
| 374 |
} else { |
| 375 |
v.x = x / d; |
| 376 |
v.y = y / d; |
| 377 |
} |
| 378 |
return v; |
| 379 |
} |
| 380 |
|
| 381 |
/* Save label point. */ |
| 382 |
static void savePt(abfDrawCtx h, float x, float y) { |
| 383 |
if (h->path.cnt == 1) { |
| 384 |
h->path.sx = x; |
| 385 |
h->path.sy = y; |
| 386 |
} |
| 387 |
h->path.cx = h->path.bx; |
| 388 |
h->path.cy = h->path.by; |
| 389 |
h->path.bx = x; |
| 390 |
h->path.by = y; |
| 391 |
h->path.cnt++; |
| 392 |
} |
| 393 |
|
| 394 |
/* Draw numeric label on last point. */ |
| 395 |
static void drawLabel(abfDrawCtx h, float ax, float ay) { |
| 396 |
if (h->level == 0 || (h->flags & ABF_NO_LABELS)) |
| 397 |
return; /* Tile mode */ |
| 398 |
|
| 399 |
if (h->path.cnt >= 2) { |
| 400 |
/* Make vector 90 degrees counter clockwise to a->c */ |
| 401 |
Vector s = makeVec(h->path.bx - ax, h->path.by - ay); |
| 402 |
Vector t = makeVec(h->path.cx - h->path.bx, h->path.cy - h->path.by); |
| 403 |
Vector u = makeVec(s.x + t.x, s.y + t.y); |
| 404 |
Vector v; |
| 405 |
v.x = -u.y * TO_EM(5.5); |
| 406 |
v.y = u.x * TO_EM(5.5); |
| 407 |
|
| 408 |
if (h->flags & ABF_FLIP_TICS) { |
| 409 |
v.x = -v.x; |
| 410 |
v.y = -v.y; |
| 411 |
} |
| 412 |
|
| 413 |
fprintf(h->fp, |
| 414 |
"%% draw label\n" |
| 415 |
"gsave\n" |
| 416 |
"%g %g moveto\n" |
| 417 |
"%g %g rlineto\n", |
| 418 |
h->path.bx, h->path.by, |
| 419 |
v.x, v.y); |
| 420 |
|
| 421 |
/* Adjust position for quadrant */ |
| 422 |
if (v.x < 0 && v.y >= 0) |
| 423 |
fprintf(h->fp, "(%.0f %.0f) stringwidth pop neg 0 rmoveto\n", |
| 424 |
h->path.bx, h->path.by); |
| 425 |
else if (v.x <= 0 && v.y < 0) |
| 426 |
fprintf(h->fp, "(%.0f %.0f) stringwidth pop neg %g rmoveto\n", |
| 427 |
h->path.bx, h->path.by, TO_EM(-LABEL_TEXT_BASE)); |
| 428 |
else if (v.x > 0 && v.y <= 0) |
| 429 |
fprintf(h->fp, "0 %g rmoveto\n", TO_EM(-LABEL_TEXT_BASE)); |
| 430 |
|
| 431 |
fprintf(h->fp, |
| 432 |
"(%.0f %.0f) show\n" |
| 433 |
"0 setlinewidth stroke\n" |
| 434 |
"grestore\n", |
| 435 |
h->path.bx, h->path.by); |
| 436 |
} |
| 437 |
savePt(h, ax, ay); |
| 438 |
} |
| 439 |
|
| 440 |
/* Draw closepath. */ |
| 441 |
static void drawClose(abfDrawCtx h, int closepoint) { |
| 442 |
float dx; |
| 443 |
float dy; |
| 444 |
float l; |
| 445 |
|
| 446 |
if (closepoint) { |
| 447 |
dx = h->path.fx - h->path.cx; |
| 448 |
dy = h->path.fy - h->path.cy; |
| 449 |
} else { |
| 450 |
dx = h->path.fx - h->path.bx; |
| 451 |
dy = h->path.fy - h->path.by; |
| 452 |
} |
| 453 |
l = (float)sqrt(dx * dx + dy * dy); |
| 454 |
|
| 455 |
fprintf(h->fp, |
| 456 |
"%% draw close\n" |
| 457 |
"gsave\n"); |
| 458 |
if (l == 0) |
| 459 |
fprintf(h->fp, |
| 460 |
"[1 0 0 1 %g %g] concat\n", |
| 461 |
h->path.fx, h->path.fy); |
| 462 |
else |
| 463 |
fprintf(h->fp, |
| 464 |
"[%g %g %g %g %g %g] concat\n", |
| 465 |
dx / l, dy / l, -dy / l, dx / l, h->path.fx, h->path.fy); |
| 466 |
fprintf(h->fp, "arrow\n"); |
| 467 |
|
| 468 |
if (closepoint) |
| 469 |
fprintf(h->fp, "closept\n"); |
| 470 |
fprintf(h->fp, "grestore\n"); |
| 471 |
} |
| 472 |
|
| 473 |
/* End current path. */ |
| 474 |
static void endPath(abfDrawCtx h) { |
| 475 |
fprintf(h->fp, "closepath\n"); |
| 476 |
|
| 477 |
if (h->level == 0 || (h->flags & ABF_NO_LABELS) || h->path.moves == 0) |
| 478 |
return; |
| 479 |
|
| 480 |
if (h->path.bx == h->path.fx && h->path.by == h->path.fy) |
| 481 |
drawClose(h, 1); |
| 482 |
else { |
| 483 |
drawClose(h, 0); |
| 484 |
drawLabel(h, h->path.fx, h->path.fy); |
| 485 |
h->path.lines++; |
| 486 |
} |
| 487 |
drawLabel(h, h->path.sx, h->path.sy); |
| 488 |
} |
| 489 |
|
| 490 |
/* Draw glyph move. */ |
| 491 |
static void glyphMove(abfGlyphCallbacks *cb, float x0, float y0) { |
| 492 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 493 |
|
| 494 |
if (!h->showglyph) |
| 495 |
return; |
| 496 |
|
| 497 |
if (h->path.moves > 0) |
| 498 |
endPath(h); |
| 499 |
else |
| 500 |
fprintf(h->fp, "%% path\n"); |
| 501 |
fprintf(h->fp, "%g %g moveto\n", x0, y0); |
| 502 |
|
| 503 |
h->path.cnt = 1; |
| 504 |
if (h->level != 0) { |
| 505 |
/* Initialize point accumulator */ |
| 506 |
h->path.bx = h->path.fx = x0; |
| 507 |
h->path.by = h->path.fy = y0; |
| 508 |
h->metrics.cb.move(&h->metrics.cb, x0, y0); |
| 509 |
} |
| 510 |
|
| 511 |
h->path.moves++; |
| 512 |
} |
| 513 |
|
| 514 |
/* Draw control point. */ |
| 515 |
static void drawCntlPt(abfDrawCtx h, float x, float y) { |
| 516 |
if (h->level == 0 || (h->flags & ABF_NO_LABELS)) |
| 517 |
return; |
| 518 |
fprintf(h->fp, "%g %g cntlpt\n", x, y); |
| 519 |
} |
| 520 |
|
| 521 |
/* Draw glyph line. */ |
| 522 |
static void glyphLine(abfGlyphCallbacks *cb, float x1, float y1) { |
| 523 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 524 |
|
| 525 |
if (!h->showglyph) |
| 526 |
return; |
| 527 |
|
| 528 |
fprintf(h->fp, "%g %g lineto\n", x1, y1); |
| 529 |
drawLabel(h, x1, y1); |
| 530 |
|
| 531 |
if (h->level != 0) |
| 532 |
h->metrics.cb.line(&h->metrics.cb, x1, y1); |
| 533 |
|
| 534 |
h->path.lines++; |
| 535 |
} |
| 536 |
|
| 537 |
/* Draw glyph curve. */ |
| 538 |
static void glyphCurve(abfGlyphCallbacks *cb, |
| 539 |
float x1, float y1, |
| 540 |
float x2, float y2, |
| 541 |
float x3, float y3) { |
| 542 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 543 |
|
| 544 |
if (!h->showglyph) |
| 545 |
return; |
| 546 |
|
| 547 |
fprintf(h->fp, "%g %g %g %g %g %g curveto\n", x1, y1, x2, y2, x3, y3); |
| 548 |
|
| 549 |
drawCntlPt(h, x1, y1); |
| 550 |
drawCntlPt(h, x2, y2); |
| 551 |
|
| 552 |
drawLabel(h, x1, y1); |
| 553 |
savePt(h, x2, y2); |
| 554 |
savePt(h, x3, y3); |
| 555 |
|
| 556 |
if (h->level != 0) |
| 557 |
h->metrics.cb.curve(&h->metrics.cb, x1, y1, x2, y2, x3, y3); |
| 558 |
|
| 559 |
h->path.curves++; |
| 560 |
} |
| 561 |
|
| 562 |
/* Draw glyph stem. */ |
| 563 |
static void glyphStem(abfGlyphCallbacks *cb, |
| 564 |
int flags, float edge0, float edge1) { |
| 565 |
/* xxx want to optionally draw hints */ |
| 566 |
} |
| 567 |
|
| 568 |
/* Draw glyph flex. */ |
| 569 |
static void glyphFlex(abfGlyphCallbacks *cb, float depth, |
| 570 |
float x1, float y1, |
| 571 |
float x2, float y2, |
| 572 |
float x3, float y3, |
| 573 |
float x4, float y4, |
| 574 |
float x5, float y5, |
| 575 |
float x6, float y6) { |
| 576 |
glyphCurve(cb, x1, y1, x2, y2, x3, y3); |
| 577 |
glyphCurve(cb, x4, y4, x5, y5, x6, y6); |
| 578 |
/* xxx want to optionally draw flex */ |
| 579 |
} |
| 580 |
|
| 581 |
/* Draw glyph general operator. */ |
| 582 |
static void glyphGenop(abfGlyphCallbacks *cb, |
| 583 |
int cnt, float *args, int op) { |
| 584 |
} |
| 585 |
|
| 586 |
/* Draw glyph seac. */ |
| 587 |
static void glyphSeac(abfGlyphCallbacks *cb, |
| 588 |
float adx, float ady, int bchar, int achar) { |
| 589 |
} |
| 590 |
|
| 591 |
/* Draw numeric key/value. */ |
| 592 |
static float drawNumber(abfDrawCtx h, float y, char *key, float value) { |
| 593 |
fprintf(h->fp, "%d %g moveto (%s) show\n", PAGE_RIGHT - (54 + 30), y, key); |
| 594 |
fprintf(h->fp, "%d %g moveto (%.0f) show\n", PAGE_RIGHT - 54, y, value); |
| 595 |
return y + PAGE_TEXT_SIZE + 1; |
| 596 |
} |
| 597 |
|
| 598 |
/* Draw string key/value. */ |
| 599 |
static float drawString(abfDrawCtx h, float y, char *key, char *value) { |
| 600 |
fprintf(h->fp, "%d %g moveto (%s) show\n", PAGE_RIGHT - (54 + 30), y, key); |
| 601 |
fprintf(h->fp, "%d %g moveto (%s) show\n", PAGE_RIGHT - 54, y, value); |
| 602 |
return y + PAGE_TEXT_SIZE + 1; |
| 603 |
} |
| 604 |
|
| 605 |
/* Draw metrics. */ |
| 606 |
static void drawMetrics(abfDrawCtx h, abfGlyphInfo *info) { |
| 607 |
char buf[50]; |
| 608 |
const size_t bufLen = sizeof(buf); |
| 609 |
float y; |
| 610 |
|
| 611 |
fprintf(h->fp, "/%s findfont %g scalefont setfont\n", |
| 612 |
PAGE_TEXT_FONT, PAGE_TEXT_SIZE); |
| 613 |
|
| 614 |
y = (int)(PAGE_TEXT_SIZE - PAGE_TEXT_BASE + 0.5); |
| 615 |
y = drawNumber(h, y, "total", |
| 616 |
(float)h->path.moves + h->path.lines + h->path.curves); |
| 617 |
y = drawNumber(h, y, "curves", (float)h->path.curves); |
| 618 |
y = drawNumber(h, y, "lines", (float)h->path.lines); |
| 619 |
y = drawNumber(h, y, "moves", (float)h->path.moves); |
| 620 |
|
| 621 |
y += PAGE_TEXT_SIZE / 2; |
| 622 |
y = drawNumber(h, y, "top", (float)h->metrics.ctx.int_mtx.top); |
| 623 |
y = drawNumber(h, y, "right", (float)h->metrics.ctx.int_mtx.right); |
| 624 |
y = drawNumber(h, y, "bottom", (float)h->metrics.ctx.int_mtx.bottom); |
| 625 |
y = drawNumber(h, y, "left", (float)h->metrics.ctx.int_mtx.left); |
| 626 |
|
| 627 |
y += PAGE_TEXT_SIZE / 2; |
| 628 |
y = drawNumber(h, y, "hAdv", h->glyph.hAdv); |
| 629 |
if (info->flags & ABF_GLYPH_CID) |
| 630 |
y = drawNumber(h, y, "fd", info->iFD); |
| 631 |
else { |
| 632 |
abfEncoding *enc = &info->encoding; |
| 633 |
if (enc->code == ABF_GLYPH_UNENC) |
| 634 |
SPRINTF_S(buf, bufLen, "-"); |
| 635 |
else { |
| 636 |
int cnt = 0; |
| 637 |
char *p = buf; |
| 638 |
size_t remainingBufferLength = bufLen; |
| 639 |
char *sep = ""; |
| 640 |
do { |
| 641 |
size_t printStringLength; |
| 642 |
if (info->flags & ABF_GLYPH_UNICODE) { |
| 643 |
if (enc->code < 0x10000) |
| 644 |
SPRINTF_S(p, remainingBufferLength, "%sU+%04lX", sep, enc->code); |
| 645 |
else |
| 646 |
SPRINTF_S(p, remainingBufferLength, "%sU+%lX", sep, enc->code); |
| 647 |
} else |
| 648 |
SPRINTF_S(p, remainingBufferLength, "%s0x%02lx", sep, enc->code); |
| 649 |
printStringLength = strnlen(p, remainingBufferLength); |
| 650 |
p += printStringLength; |
| 651 |
remainingBufferLength -= printStringLength; |
| 652 |
sep = "+"; |
| 653 |
enc = enc->next; |
| 654 |
} while (enc != NULL && ++cnt < 2); |
| 655 |
if (enc != NULL) |
| 656 |
SPRINTF_S(p, remainingBufferLength, "..."); |
| 657 |
} |
| 658 |
y = drawString(h, y, "enc", buf); |
| 659 |
} |
| 660 |
y = drawNumber(h, y, "tag", info->tag); |
| 661 |
if (info->flags & ABF_GLYPH_CID) |
| 662 |
SPRINTF_S(buf, bufLen, "\\\\%hu", info->cid); |
| 663 |
else |
| 664 |
SPRINTF_S(buf, bufLen, "%s", info->gname.ptr); |
| 665 |
drawString(h, y, "glyph", buf); |
| 666 |
} |
| 667 |
|
| 668 |
/* Draw glyph end. */ |
| 669 |
static void glyphEnd(abfGlyphCallbacks *cb) { |
| 670 |
abfDrawCtx h = (abfDrawCtx)cb->direct_ctx; |
| 671 |
char gname[50]; |
| 672 |
abfGlyphInfo *info = cb->info; |
| 673 |
|
| 674 |
if (!h->showglyph) |
| 675 |
return; |
| 676 |
|
| 677 |
endPath(h); |
| 678 |
|
| 679 |
if (h->level == 0) |
| 680 |
FPRINTF_S(h->fp, "fill\n"); |
| 681 |
else |
| 682 |
fprintf(h->fp, "0 setlinewidth stroke\n"); |
| 683 |
FPRINTF_S(h->fp, "grestore\n"); |
| 684 |
|
| 685 |
/* Set glyph name */ |
| 686 |
if (info->flags & ABF_GLYPH_CID) |
| 687 |
SPRINTF_S(gname, sizeof(gname), "\\\\%hu", info->cid); |
| 688 |
else |
| 689 |
SPRINTF_S(gname, sizeof(gname), "%s", info->gname.ptr); |
| 690 |
|
| 691 |
if (h->level == 0) { |
| 692 |
char tag[20]; |
| 693 |
const size_t tagLen = sizeof(tag); |
| 694 |
char hAdv[20]; |
| 695 |
|
| 696 |
if (info->flags & ABF_GLYPH_CID) |
| 697 |
SPRINTF_S(tag, tagLen, "%hu,%u", info->tag, info->iFD); |
| 698 |
else { |
| 699 |
abfEncoding *enc = &info->encoding; |
| 700 |
SPRINTF_S(tag, tagLen, "%hu,", info->tag); |
| 701 |
if (enc->code != ABF_GLYPH_UNENC) { |
| 702 |
int cnt = 0; |
| 703 |
char *p = tag; |
| 704 |
size_t remainingTagLength = tagLen; |
| 705 |
char *sep = ""; |
| 706 |
do { |
| 707 |
size_t printStringLength = strnlen(p, remainingTagLength); |
| 708 |
p += printStringLength; |
| 709 |
remainingTagLength -= printStringLength; |
| 710 |
if (info->flags & ABF_GLYPH_UNICODE) { |
| 711 |
if (enc->code < 0x10000) |
| 712 |
SPRINTF_S(p, remainingTagLength, "%sU+%04lX", sep, enc->code); |
| 713 |
else |
| 714 |
SPRINTF_S(p, remainingTagLength, "%sU+%lX", sep, enc->code); |
| 715 |
} else |
| 716 |
SPRINTF_S(p, remainingTagLength, "%s0x%02lX", sep, enc->code); |
| 717 |
sep = "+"; |
| 718 |
enc = enc->next; |
| 719 |
} while (enc != NULL && ++cnt < 2); |
| 720 |
if (enc != NULL) { |
| 721 |
size_t printStringLength = strnlen(p, remainingTagLength); |
| 722 |
p += printStringLength; |
| 723 |
remainingTagLength -= printStringLength; |
| 724 |
SPRINTF_S(p, remainingTagLength, "..."); |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
SPRINTF_S(hAdv, sizeof(hAdv), "%g", h->glyph.hAdv); |
| 729 |
drawTile(h, (float)h->tile.h, (float)h->tile.v, tag, hAdv, gname); |
| 730 |
|
| 731 |
if (!(h->flags & ABF_SHOW_BY_ENC)) { |
| 732 |
h->tile.h += TILE_SIZE; |
| 733 |
if (h->tile.h == TILE_RIGHT) { |
| 734 |
/* Row full */ |
| 735 |
h->tile.v -= TILE_SIZE; |
| 736 |
if (h->tile.v == 0) { |
| 737 |
/* Page full */ |
| 738 |
pageEnd(h); |
| 739 |
h->tile.v = TILE_TOP; |
| 740 |
} |
| 741 |
h->tile.h = 0; |
| 742 |
} |
| 743 |
} |
| 744 |
} else { |
| 745 |
h->metrics.cb.end(&h->metrics.cb); |
| 746 |
drawMetrics(h, cb->info); |
| 747 |
pageEnd(h); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
/* Draw glyph callbacks */ |
| 752 |
const abfGlyphCallbacks abfGlyphDrawCallbacks = |
| 753 |
{ |
| 754 |
NULL, |
| 755 |
NULL, |
| 756 |
NULL, |
| 757 |
glyphBeg, |
| 758 |
glyphWidth, |
| 759 |
glyphMove, |
| 760 |
glyphLine, |
| 761 |
glyphCurve, |
| 762 |
glyphStem, |
| 763 |
glyphFlex, |
| 764 |
glyphGenop, |
| 765 |
glyphSeac, |
| 766 |
glyphEnd, |
| 767 |
}; |