| 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 |
* Type 2 charstring support. |
| 7 |
*/ |
| 8 |
|
| 9 |
#if defined(_WIN32) && !defined(_USE_MATH_DEFINES) |
| 10 |
#define _USE_MATH_DEFINES /* Needed to define M_PI under Windows */ |
| 11 |
#endif |
| 12 |
|
| 13 |
#include "t2cstr.h" |
| 14 |
#include "txops.h" |
| 15 |
#include "ctutil.h" |
| 16 |
#include "varread.h" |
| 17 |
#include "supportexcept.h" |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
#include <stdarg.h> |
| 21 |
#include <math.h> |
| 22 |
#include <limits.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
/* Make operator for internal use */ |
| 27 |
#define t2_cntroff t2_reservedESC33 |
| 28 |
|
| 29 |
#define MAX_RECURSION_DEPTH 1000 |
| 30 |
|
| 31 |
enum /* seac operator conversion phase */ |
| 32 |
{ |
| 33 |
seacNone, /* No seac conversion */ |
| 34 |
seacBase, /* In base char */ |
| 35 |
seacAccentPreMove, /* In accent char before first moveto */ |
| 36 |
seacAccentPostMove /* In accent char after first moveto */ |
| 37 |
}; |
| 38 |
|
| 39 |
typedef struct /* Stem data */ |
| 40 |
{ |
| 41 |
union { |
| 42 |
float edge0; |
| 43 |
abfBlendArg edge0v; |
| 44 |
}; |
| 45 |
union { |
| 46 |
float edge1; |
| 47 |
abfBlendArg edge1v; |
| 48 |
}; |
| 49 |
short flags; /* Uses stem flags defined in absfont.h */ |
| 50 |
} Stem; |
| 51 |
|
| 52 |
/* Module context */ |
| 53 |
typedef struct _t2cCtx *t2cCtx; |
| 54 |
struct _t2cCtx { |
| 55 |
long flags; /* Control flags */ |
| 56 |
#define PEND_WIDTH (1<<0) /* Flags width pending */ |
| 57 |
#define PEND_MASK (1<<1) /* Flags hintmask pending */ |
| 58 |
#define SEEN_ENDCHAR (1<<2) /* Flags endchar operator seen */ |
| 59 |
#define SEEN_BLEND (1<<8) /* Seen blend operator: need to round position after each op. */ |
| 60 |
#define USE_MATRIX (1<<9) /* Apply transform*/ |
| 61 |
#define USE_GLOBAL_MATRIX (1<<10) /* Transform is applied to entire charstring */ |
| 62 |
#define FLATTEN_BLEND (1<<13) /* Flag that we are flattening a CFF2 charstring. */ |
| 63 |
#define SEEN_CFF2_BLEND (1<<14) /* Seen CFF2 blend operator. */ |
| 64 |
#define IS_CFF2 (1<<15) /* CFF2 charstring */ |
| 65 |
struct /* Operand stack */ |
| 66 |
{ |
| 67 |
long cnt; |
| 68 |
float array[CFF2_MAX_OP_STACK]; |
| 69 |
unsigned short numRegions; |
| 70 |
long blendCnt; |
| 71 |
abfOpEntry blendArray[CFF2_MAX_OP_STACK]; |
| 72 |
abfBlendArg blendArgs[T2_MAX_STEMS]; |
| 73 |
} stack; |
| 74 |
long maxOpStack; |
| 75 |
float BCA[TX_BCA_LENGTH]; /* BuildCharArray */ |
| 76 |
float x; /* Path x-coord */ |
| 77 |
float y; /* Path y-coord */ |
| 78 |
int subrDepth; |
| 79 |
float transformMatrix[6]; |
| 80 |
struct /* Stem hints */ |
| 81 |
{ |
| 82 |
long cnt; |
| 83 |
Stem array[T2_MAX_STEMS]; |
| 84 |
} stems; |
| 85 |
struct /* hint/cntrmask */ |
| 86 |
{ |
| 87 |
short state; /* cntrmask state */ |
| 88 |
short length; /* Number of bytes in mask op */ |
| 89 |
short unused; /* Mask unused bits in last byte of mask */ |
| 90 |
unsigned char bytes[T2_MAX_STEMS / 8]; /* Current mask */ |
| 91 |
} mask; |
| 92 |
struct /* seac conversion data */ |
| 93 |
{ |
| 94 |
float adx; |
| 95 |
float ady; |
| 96 |
int phase; |
| 97 |
} seac; |
| 98 |
struct /* Source data */ |
| 99 |
{ |
| 100 |
char *buf; /* Buffer */ |
| 101 |
long length; /* Buffer length */ |
| 102 |
long offset; /* offset in file */ |
| 103 |
long endOffset; /* offset in file for end of charstring */ |
| 104 |
long left; /* Bytes remaining in charstring */ |
| 105 |
} src; |
| 106 |
short LanguageGroup; |
| 107 |
t2cAuxData *aux; /* Auxiliary parse data */ |
| 108 |
unsigned short gid; /* glyph ID */ |
| 109 |
unsigned short regionIndices[CFF2_MAX_MASTERS]; /* variable font region indices */ |
| 110 |
cff2GlyphCallbacks *cff2; /* CFF2 font callbacks */ |
| 111 |
abfGlyphCallbacks *glyph; /* Glyph callbacks */ |
| 112 |
ctlMemoryCallbacks *mem; /* Glyph callbacks */ |
| 113 |
struct /* Error handling */ |
| 114 |
{ |
| 115 |
_Exc_Buf env; |
| 116 |
} err; |
| 117 |
}; |
| 118 |
|
| 119 |
/* Check stack contains at least n elements. */ |
| 120 |
#define CHKUFLOW(h, n) \ |
| 121 |
do { \ |
| 122 |
if (h->stack.cnt < (n)) return t2cErrStackUnderflow; \ |
| 123 |
} while (0) |
| 124 |
|
| 125 |
/* Check stack has room for n elements. */ |
| 126 |
#define CHKOFLOW(h, n) \ |
| 127 |
do { \ |
| 128 |
if (((h->flags & IS_CFF2) && ((h->stack.blendCnt) + (n) > CFF2_MAX_OP_STACK)) || (h->stack.cnt) + (n) > h->maxOpStack) return t2cErrStackOverflow; \ |
| 129 |
} while (0) |
| 130 |
|
| 131 |
/* Stack access without check. */ |
| 132 |
#define INDEX(i) (h->stack.array[i]) |
| 133 |
#define INDEX_BLEND(i) (h->stack.blendArray[i]) |
| 134 |
#define POP() (h->stack.array[--h->stack.cnt]) |
| 135 |
#define PUSH(v) \ |
| 136 |
{ \ |
| 137 |
if (h->flags & IS_CFF2) h->stack.blendArray[h->stack.blendCnt++].value = (float)(v); \ |
| 138 |
h->stack.array[h->stack.cnt++] = (float)(v); \ |
| 139 |
} |
| 140 |
|
| 141 |
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) |
| 142 |
|
| 143 |
/* Transform coordinates by matrix. */ |
| 144 |
#define RND(v) ((float)floor((v) + 0.5)) |
| 145 |
#define TX(x, y) \ |
| 146 |
RND(h->transformMatrix[0] * (x) + h->transformMatrix[2] * (y) + h->transformMatrix[4]) |
| 147 |
#define TY(x, y) \ |
| 148 |
RND(h->transformMatrix[1] * (x) + h->transformMatrix[3] * (y) + h->transformMatrix[5]) |
| 149 |
|
| 150 |
/* Scale coordinates by matrix. */ |
| 151 |
#define SX(x) RND(h->transformMatrix[0] * (x)) |
| 152 |
#define SY(y) RND(h->transformMatrix[3] * (y)) |
| 153 |
|
| 154 |
static int t2Decode(t2cCtx h, long offset, int depth); |
| 155 |
static void convertToAbsolute(t2cCtx h, float x1, float y1, abfBlendArg *blendArgs, int num); |
| 156 |
static void copyBlendArgs(t2cCtx h, abfBlendArg *blendArg, abfOpEntry *opEntry); |
| 157 |
|
| 158 |
/* ------------------------------- Error handling -------------------------- */ |
| 159 |
|
| 160 |
/* Write message to debug stream from va_list. */ |
| 161 |
static void vmessage(t2cCtx h, char *fmt, va_list ap) { |
| 162 |
char text[500]; |
| 163 |
|
| 164 |
if (h->aux->dbg == NULL) |
| 165 |
return; /* Debug stream not available */ |
| 166 |
|
| 167 |
VSPRINTF_S(text, sizeof(text), fmt, ap); |
| 168 |
(void)h->aux->stm->write(h->aux->stm, h->aux->dbg, strlen(text), text); |
| 169 |
} |
| 170 |
|
| 171 |
/* Write message to debug stream from varargs. */ |
| 172 |
static void CTL_CDECL message(t2cCtx h, char *fmt, ...) { |
| 173 |
va_list ap; |
| 174 |
va_start(ap, fmt); |
| 175 |
vmessage(h, fmt, ap); |
| 176 |
va_end(ap); |
| 177 |
} |
| 178 |
|
| 179 |
/* Handle fatal error. */ |
| 180 |
static void fatal(t2cCtx h, int err_code) { |
| 181 |
message(h, "%s", t2cErrStr(err_code)); |
| 182 |
RAISE(&h->err.env, err_code, NULL); |
| 183 |
} |
| 184 |
|
| 185 |
/* --------------------------- Memory Management --------------------------- */ |
| 186 |
|
| 187 |
/* Allocate memory. */ |
| 188 |
static void *memNew(t2cCtx h, size_t size) { |
| 189 |
void *ptr = h->mem->manage(h->mem, NULL, size); |
| 190 |
if (ptr == NULL) { |
| 191 |
fatal(h, t2cErrMemory); |
| 192 |
} else { |
| 193 |
/* Safety initialization */ |
| 194 |
memset(ptr, 0, size); |
| 195 |
} |
| 196 |
return ptr; |
| 197 |
} |
| 198 |
|
| 199 |
/* Free memory. */ |
| 200 |
static void memFree(t2cCtx h, void *ptr) { |
| 201 |
h->mem->manage(h->mem, ptr, 0); |
| 202 |
} |
| 203 |
|
| 204 |
/* ------------------------------- Data Input ------------------------------ */ |
| 205 |
|
| 206 |
/* Refill input buffer. Return NULL on stream error else pointer to buffer. */ |
| 207 |
static unsigned char *refill(t2cCtx h, unsigned char **end) { |
| 208 |
long int offset; |
| 209 |
/* Read buffer */ |
| 210 |
/* 64-bit warning fixed by cast here HO */ |
| 211 |
h->src.length = (long)h->aux->stm->read(h->aux->stm, h->aux->src, &h->src.buf); |
| 212 |
if (h->src.length == 0 && !(h->flags & IS_CFF2)) { |
| 213 |
char *errMsg; |
| 214 |
errMsg = strerror(errno); |
| 215 |
message(h, "%s", errMsg); |
| 216 |
return NULL; /* Stream error */ |
| 217 |
} |
| 218 |
|
| 219 |
/* Update offset of next read */ |
| 220 |
offset = h->src.offset + h->src.length; |
| 221 |
if (offset >= h->src.endOffset) { |
| 222 |
h->src.length = h->src.endOffset - h->src.offset; |
| 223 |
h->src.offset = h->src.offset + h->src.length; |
| 224 |
} else |
| 225 |
h->src.offset = offset; |
| 226 |
|
| 227 |
/* Set buffer limit */ |
| 228 |
*end = (unsigned char *)h->src.buf + h->src.length; |
| 229 |
|
| 230 |
return (unsigned char *)h->src.buf; |
| 231 |
} |
| 232 |
|
| 233 |
/* Seek to offset on source stream. */ |
| 234 |
static int srcSeek(t2cCtx h, long offset) { |
| 235 |
if (h->aux->stm->seek(h->aux->stm, h->aux->src, offset)) |
| 236 |
return 1; |
| 237 |
h->src.offset = offset; |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
/* Check for the first of possibly multiple bytes for an operator or operand. |
| 242 |
If we're currently empty, but more bytes are available, refill from buffer. |
| 243 |
If this is a CFF2 font and we're completely out of bytes, |
| 244 |
treat this condition as equivalent to an endchar in CFF. */ |
| 245 |
|
| 246 |
#define CHECK_FIRST_BYTE(h) \ |
| 247 |
do \ |
| 248 |
if (next == end) { \ |
| 249 |
if (h->src.offset >= h->src.endOffset) { \ |
| 250 |
if (h->flags & IS_CFF2) \ |
| 251 |
goto do_endchar; \ |
| 252 |
return t2cErrSrcStream; \ |
| 253 |
} \ |
| 254 |
next = refill(h, &end); \ |
| 255 |
if (next == NULL) \ |
| 256 |
return t2cErrSrcStream; \ |
| 257 |
} \ |
| 258 |
while (0) |
| 259 |
|
| 260 |
/* Check for a byte that follows the first byte of an operator or operand. |
| 261 |
If we're currently empty, but more bytes are available, refill from buffer. |
| 262 |
If we're completely out of bytes, throw an error. */ |
| 263 |
|
| 264 |
#define CHECK_SUBSEQUENT_BYTE(h) \ |
| 265 |
do \ |
| 266 |
if (next == end) { \ |
| 267 |
if (h->src.offset >= h->src.endOffset) { \ |
| 268 |
return t2cErrSrcStream; \ |
| 269 |
} \ |
| 270 |
next = refill(h, &end); \ |
| 271 |
if (next == NULL) \ |
| 272 |
return t2cErrSrcStream; \ |
| 273 |
} \ |
| 274 |
while (0) |
| 275 |
|
| 276 |
/* ------------------------------- Callbacks ------------------------------- */ |
| 277 |
|
| 278 |
/* Compute and callback width. Return non-0 to end parse else 0. */ |
| 279 |
static int callbackWidth(t2cCtx h, int odd_args) { |
| 280 |
if (h->flags & PEND_WIDTH) { |
| 281 |
float width; |
| 282 |
|
| 283 |
if (h->flags & IS_CFF2) { |
| 284 |
width = h->cff2->getWidth(h->cff2, h->gid); |
| 285 |
} else { |
| 286 |
width = (odd_args == (h->stack.cnt & 1)) ? INDEX(0) + h->aux->nominalWidthX : h->aux->defaultWidthX; |
| 287 |
if (h->flags & USE_MATRIX) |
| 288 |
width = SX(width); |
| 289 |
else if (h->flags & SEEN_BLEND) |
| 290 |
width = RND(width); |
| 291 |
} |
| 292 |
|
| 293 |
h->glyph->width(h->glyph, width); |
| 294 |
h->flags &= ~PEND_WIDTH; |
| 295 |
if (h->aux->flags & T2C_WIDTH_ONLY) { |
| 296 |
/* Only width required; pretend we've seen endchar operator */ |
| 297 |
h->flags |= SEEN_ENDCHAR; |
| 298 |
return 1; |
| 299 |
} |
| 300 |
} |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
/* Test if cntrmask contains valid stem3 counters. */ |
| 305 |
static int validStem3(t2cCtx h) { |
| 306 |
int i; |
| 307 |
int hcntrs = 0; |
| 308 |
int vcntrs = 0; |
| 309 |
|
| 310 |
/* Count counters */ |
| 311 |
for (i = 0; i < h->mask.length; i++) { |
| 312 |
int j; |
| 313 |
unsigned char byte = h->mask.bytes[i]; |
| 314 |
for (j = i * 8; byte; j++) { |
| 315 |
if (byte & 0x80) { |
| 316 |
if (h->stems.array[j].flags & ABF_VERT_STEM) |
| 317 |
vcntrs++; |
| 318 |
else |
| 319 |
hcntrs++; |
| 320 |
} |
| 321 |
byte <<= 1; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/* Do we have valid stem3s? */ |
| 326 |
return ((hcntrs == 0 && vcntrs == 3) || |
| 327 |
(hcntrs == 3 && vcntrs == 0) || |
| 328 |
(hcntrs == 3 && vcntrs == 3)); |
| 329 |
} |
| 330 |
|
| 331 |
/* Callback operator and args. */ |
| 332 |
static void callbackOp(t2cCtx h, int op) { |
| 333 |
h->glyph->genop(h->glyph, h->stack.cnt, h->stack.array, op); |
| 334 |
} |
| 335 |
|
| 336 |
static void callBackStem(t2cCtx h, Stem *stem, int cntr, int flags) { |
| 337 |
if ((h->glyph->stemVF != NULL) && (h->flags & IS_CFF2) && |
| 338 |
!(h->flags & FLATTEN_BLEND)) { |
| 339 |
if (cntr) |
| 340 |
flags |= ABF_CNTR_STEM; |
| 341 |
h->glyph->stemVF(h->glyph, flags, &stem->edge0v, &stem->edge1v); |
| 342 |
} else { |
| 343 |
float edge0 = stem->edge0; |
| 344 |
float edge1 = stem->edge1; |
| 345 |
if (h->flags & USE_MATRIX) { |
| 346 |
if (stem->flags & ABF_VERT_STEM) { |
| 347 |
edge0 = SX(edge0); |
| 348 |
edge1 = SX(edge1); |
| 349 |
} else { |
| 350 |
edge0 = SY(edge0); |
| 351 |
edge1 = SY(edge1); |
| 352 |
} |
| 353 |
} else if (h->flags & SEEN_BLEND) { |
| 354 |
edge0 = RND(edge0); |
| 355 |
edge1 = RND(edge1); |
| 356 |
} |
| 357 |
|
| 358 |
if (cntr) |
| 359 |
flags |= ABF_CNTR_STEM; |
| 360 |
h->glyph->stem(h->glyph, flags, edge0, edge1); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/* Callback stems in mask. */ |
| 365 |
static void callbackStems(t2cCtx h, int cntr) { |
| 366 |
int i; |
| 367 |
int flags; |
| 368 |
|
| 369 |
if (cntr) { |
| 370 |
for (i = 0; i < h->mask.length; i++) |
| 371 |
if (h->mask.bytes[i] != 0) |
| 372 |
goto set_flags; |
| 373 |
|
| 374 |
/* Null cntrmask; turn off global coloring */ |
| 375 |
callbackOp(h, t2_cntroff); |
| 376 |
return; |
| 377 |
|
| 378 |
set_flags: |
| 379 |
flags = ABF_NEW_GROUP; |
| 380 |
} else |
| 381 |
flags = (h->flags & PEND_MASK) ? 0 : ABF_NEW_HINTS; |
| 382 |
|
| 383 |
for (i = 0; i < h->mask.length; i++) { |
| 384 |
int j; |
| 385 |
unsigned char byte = h->mask.bytes[i]; |
| 386 |
for (j = i * 8; byte; j++) { |
| 387 |
if (byte & 0x80) { |
| 388 |
/* Callback stem */ |
| 389 |
Stem *stem = &h->stems.array[j]; |
| 390 |
flags |= stem->flags; |
| 391 |
callBackStem(h, stem, cntr, flags); |
| 392 |
flags = 0; |
| 393 |
} |
| 394 |
byte <<= 1; |
| 395 |
} |
| 396 |
} |
| 397 |
if (!cntr) |
| 398 |
h->flags &= ~PEND_MASK; |
| 399 |
} |
| 400 |
|
| 401 |
/* Determine how to handle first cntrmask and save it. */ |
| 402 |
static void savePendCntr(t2cCtx h, int cntr) { |
| 403 |
if (cntr || h->LanguageGroup == 1 || !validStem3(h)) |
| 404 |
/* We have: |
| 405 |
- 2 or more cntrmasks |
| 406 |
- font sets LanguageGroup 1 |
| 407 |
- saw a cntron operator |
| 408 |
- don't have valid stem3 cntrs; |
| 409 |
do global coloring */ |
| 410 |
callbackStems(h, 1); |
| 411 |
else { |
| 412 |
/* Do stem3; mark stem3 stems */ |
| 413 |
int i; |
| 414 |
for (i = 0; i < h->mask.length; i++) { |
| 415 |
int j; |
| 416 |
unsigned char byte = h->mask.bytes[i]; |
| 417 |
for (j = i * 8; byte; j++) { |
| 418 |
if (byte & 0x80) |
| 419 |
h->stems.array[j].flags |= ABF_STEM3_STEM; |
| 420 |
byte <<= 1; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
h->mask.state = 2; /* Don't do this again */ |
| 426 |
} |
| 427 |
|
| 428 |
/* Callback path move. */ |
| 429 |
static void callbackMove(t2cCtx h, float dx, float dy) { |
| 430 |
int flags; |
| 431 |
float x, y; |
| 432 |
|
| 433 |
x = h->x + dx; |
| 434 |
y = h->y + dy; |
| 435 |
if (h->flags & USE_MATRIX) { |
| 436 |
/* if START_PATH_MATRIX is not set, then this either global, or left over from a previous path. */ |
| 437 |
if (h->flags & USE_GLOBAL_MATRIX) { |
| 438 |
int i; |
| 439 |
/* restore the global matrix values */ |
| 440 |
for (i = 0; i < 6; i++) { |
| 441 |
h->transformMatrix[i] = h->aux->matrix[i]; |
| 442 |
} |
| 443 |
} else { |
| 444 |
/* If USE_GLOBAL_MATRIX is not set, then the USE_MATRIX is left over from the previous path. Clear it. */ |
| 445 |
h->flags &= ~USE_MATRIX; |
| 446 |
h->transformMatrix[0] = h->transformMatrix[3] = 1.0; |
| 447 |
h->transformMatrix[1] = h->transformMatrix[2] = h->transformMatrix[4] = h->transformMatrix[5] = 1.0; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
if (h->mask.state == 1) |
| 452 |
savePendCntr(h, 0); |
| 453 |
|
| 454 |
if (h->seac.phase == seacAccentPreMove) { |
| 455 |
/* Accent component moveto; if no mask force hintsubs */ |
| 456 |
flags = ABF_NEW_HINTS; |
| 457 |
h->seac.phase = seacAccentPostMove; |
| 458 |
} else |
| 459 |
flags = 0; |
| 460 |
|
| 461 |
if (h->glyph->stem != NULL && (h->flags & PEND_MASK)) { |
| 462 |
/* No mask before first move; callback initial hints */ |
| 463 |
int i; |
| 464 |
for (i = 0; i < h->stems.cnt; i++) { |
| 465 |
/* Callback stem */ |
| 466 |
Stem *stem = &h->stems.array[i]; |
| 467 |
flags |= stem->flags; |
| 468 |
callBackStem(h, stem, 0, flags); |
| 469 |
flags = 0; |
| 470 |
} |
| 471 |
h->flags &= ~PEND_MASK; |
| 472 |
} |
| 473 |
|
| 474 |
x = roundf(x * 100) / 100; |
| 475 |
y = roundf(y * 100) / 100; |
| 476 |
h->x = x; |
| 477 |
h->y = y; |
| 478 |
|
| 479 |
if ((h->flags & IS_CFF2) && (h->glyph->moveVF != NULL) && |
| 480 |
!(h->flags & FLATTEN_BLEND)) { |
| 481 |
abfBlendArg *blendArgs = &(h->stack.blendArgs[0]); |
| 482 |
convertToAbsolute(h, x, y, blendArgs, 2); |
| 483 |
h->glyph->moveVF(h->glyph, blendArgs, blendArgs + 1); |
| 484 |
} else { |
| 485 |
if (h->flags & USE_MATRIX) |
| 486 |
h->glyph->move(h->glyph, TX(h->x, h->y), TY(h->x, h->y)); |
| 487 |
else if (h->flags & SEEN_BLEND) |
| 488 |
h->glyph->move(h->glyph, RND(h->x), RND(h->y)); |
| 489 |
else |
| 490 |
h->glyph->move(h->glyph, h->x, h->y); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
/* Callback path line. */ |
| 495 |
static void callbackLine(t2cCtx h, float dx, float dy) { |
| 496 |
h->x += dx; |
| 497 |
h->y += dy; |
| 498 |
h->x = roundf(h->x * 100) / 100; |
| 499 |
h->y = roundf(h->y * 100) / 100; |
| 500 |
|
| 501 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 502 |
!(h->flags & FLATTEN_BLEND)) { |
| 503 |
abfBlendArg *blendArgs = &(h->stack.blendArgs[0]); |
| 504 |
convertToAbsolute(h, h->x, h->y, blendArgs, 2); |
| 505 |
h->glyph->lineVF(h->glyph, blendArgs, blendArgs + 1); |
| 506 |
} else { |
| 507 |
if (h->flags & USE_MATRIX) |
| 508 |
h->glyph->line(h->glyph, TX(h->x, h->y), TY(h->x, h->y)); |
| 509 |
else if (h->flags & SEEN_BLEND) |
| 510 |
h->glyph->line(h->glyph, RND(h->x), RND(h->y)); |
| 511 |
else |
| 512 |
h->glyph->line(h->glyph, h->x, h->y); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
/* Callback path curve. */ |
| 517 |
static void callbackCurve(t2cCtx h, |
| 518 |
float dx1, float dy1, |
| 519 |
float dx2, float dy2, |
| 520 |
float dx3, float dy3) { |
| 521 |
float x1, y1, x2, y2, x3, y3; |
| 522 |
|
| 523 |
x1 = h->x + dx1; |
| 524 |
y1 = h->y + dy1; |
| 525 |
x2 = x1 + dx2; |
| 526 |
y2 = y1 + dy2; |
| 527 |
x3 = x2 + dx3; |
| 528 |
y3 = y2 + dy3; |
| 529 |
|
| 530 |
x3 = roundf(x3 * 100) / 100; |
| 531 |
y3 = roundf(y3 * 100) / 100; |
| 532 |
h->x = x3; |
| 533 |
h->y = y3; |
| 534 |
|
| 535 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 536 |
!(h->flags & FLATTEN_BLEND)) { |
| 537 |
abfBlendArg *blendArgs = &(h->stack.blendArgs[0]); |
| 538 |
convertToAbsolute(h, x1, y1, blendArgs, 6); |
| 539 |
h->glyph->curveVF(h->glyph, blendArgs, blendArgs + 1, |
| 540 |
blendArgs + 2, blendArgs + 3, |
| 541 |
blendArgs + 4, blendArgs + 5); |
| 542 |
} else { |
| 543 |
if (h->flags & USE_MATRIX) |
| 544 |
h->glyph->curve(h->glyph, |
| 545 |
TX(x1, y1), TY(x1, y1), |
| 546 |
TX(x2, y2), TY(x2, y2), |
| 547 |
TX(x3, y3), TY(x3, y3)); |
| 548 |
else if (h->flags & SEEN_BLEND) |
| 549 |
h->glyph->curve(h->glyph, |
| 550 |
RND(x1), RND(y1), |
| 551 |
RND(x2), RND(y2), |
| 552 |
RND(x3), RND(y3)); |
| 553 |
else |
| 554 |
h->glyph->curve(h->glyph, x1, y1, x2, y2, x3, y3); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/* Callback path flex. */ |
| 559 |
static void callbackFlex(t2cCtx h, |
| 560 |
float dx1, float dy1, |
| 561 |
float dx2, float dy2, |
| 562 |
float dx3, float dy3, |
| 563 |
float dx4, float dy4, |
| 564 |
float dx5, float dy5, |
| 565 |
float dx6, float dy6, |
| 566 |
float depth) { |
| 567 |
if (h->glyph->flex == NULL) { |
| 568 |
/* Callback as 2 curves */ |
| 569 |
callbackCurve(h, |
| 570 |
dx1, dy1, |
| 571 |
dx2, dy2, |
| 572 |
dx3, dy3); |
| 573 |
callbackCurve(h, |
| 574 |
dx4, dy4, |
| 575 |
dx5, dy5, |
| 576 |
dx6, dy6); |
| 577 |
} else { |
| 578 |
/* Callback as flex hint */ |
| 579 |
float x1 = h->x + dx1; |
| 580 |
float y1 = h->y + dy1; |
| 581 |
float x2 = x1 + dx2; |
| 582 |
float y2 = y1 + dy2; |
| 583 |
float x3 = x2 + dx3; |
| 584 |
float y3 = y2 + dy3; |
| 585 |
float x4 = x3 + dx4; |
| 586 |
float y4 = y3 + dy4; |
| 587 |
float x5 = x4 + dx5; |
| 588 |
float y5 = y4 + dy5; |
| 589 |
float x6 = x5 + dx6; |
| 590 |
float y6 = y5 + dy6; |
| 591 |
x6 = roundf(x6 * 100) / 100; |
| 592 |
y6 = roundf(y6 * 100) / 100; |
| 593 |
h->x = x6; |
| 594 |
h->y = y6; |
| 595 |
|
| 596 |
if (h->flags & USE_MATRIX) |
| 597 |
h->glyph->flex(h->glyph, depth, |
| 598 |
TX(x1, y1), TY(x1, y1), |
| 599 |
TX(x2, y2), TY(x2, y2), |
| 600 |
TX(x3, y3), TY(x3, y3), |
| 601 |
TX(x4, y4), TY(x4, y4), |
| 602 |
TX(x5, y5), TY(x5, y5), |
| 603 |
TX(x6, y6), TY(x6, y6)); |
| 604 |
if (h->flags & SEEN_BLEND) |
| 605 |
h->glyph->flex(h->glyph, depth, |
| 606 |
RND(x1), RND(y1), |
| 607 |
RND(x2), RND(y2), |
| 608 |
RND(x3), RND(y3), |
| 609 |
RND(x4), RND(y4), |
| 610 |
RND(x5), RND(y5), |
| 611 |
RND(x6), RND(y6)); |
| 612 |
else |
| 613 |
h->glyph->flex(h->glyph, depth, |
| 614 |
x1, y1, |
| 615 |
x2, y2, |
| 616 |
x3, y3, |
| 617 |
x4, y4, |
| 618 |
x5, y5, |
| 619 |
x6, y6); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
static void convertStemToAbsolute(t2cCtx h, float lastEdge, abfBlendArg *blendArgs) { |
| 624 |
/* Convert only the default value. At some point, need to actually convert the region |
| 625 |
deltas to absolute values, but this requires referencing the current point and the region weight map. |
| 626 |
*/ |
| 627 |
|
| 628 |
blendArgs->value += lastEdge; |
| 629 |
} |
| 630 |
|
| 631 |
/* Add stems to stem list. Return 1 on stem overflow else 0; |
| 632 |
This is called only on hstem(hm), vstem(hm), or tx_mask, so all args on the stack are stems. |
| 633 |
*/ |
| 634 |
static int addStems(t2cCtx h, int vert) { |
| 635 |
int i; |
| 636 |
Stem *stem; |
| 637 |
float lastedge = 0; |
| 638 |
|
| 639 |
if (h->stems.cnt + h->stack.cnt / 2 > T2_MAX_STEMS) |
| 640 |
return 1; |
| 641 |
|
| 642 |
/* The "i = h->stack.cnt & 1" clause lets it skip the width value, if there is one. */ |
| 643 |
if ((h->flags & IS_CFF2) && (h->glyph->stemVF != NULL) && |
| 644 |
!(h->flags & FLATTEN_BLEND)) { |
| 645 |
for (i = h->stack.cnt & 1; i < h->stack.cnt - 1; i += 2) { |
| 646 |
abfBlendArg *blendArgs = &h->stack.blendArgs[i]; |
| 647 |
abfOpEntry *opEntry = &h->stack.blendArray[i]; |
| 648 |
stem = &h->stems.array[h->stems.cnt++]; |
| 649 |
copyBlendArgs(h, blendArgs, opEntry); |
| 650 |
stem->edge0v = h->stack.blendArgs[i]; |
| 651 |
|
| 652 |
blendArgs = &h->stack.blendArgs[i + 1]; |
| 653 |
opEntry = &h->stack.blendArray[i + 1]; |
| 654 |
copyBlendArgs(h, blendArgs, opEntry); |
| 655 |
stem->edge1v = h->stack.blendArgs[i + 1]; |
| 656 |
|
| 657 |
convertStemToAbsolute(h, lastedge, &stem->edge0v); |
| 658 |
convertStemToAbsolute(h, stem->edge0v.value, &stem->edge1v); |
| 659 |
lastedge = stem->edge1v.value; |
| 660 |
stem->flags = vert ? ABF_VERT_STEM : 0; |
| 661 |
} |
| 662 |
} else { |
| 663 |
for (i = h->stack.cnt & 1; i < h->stack.cnt - 1; i += 2) { |
| 664 |
stem = &h->stems.array[h->stems.cnt++]; |
| 665 |
stem->edge0 = lastedge + INDEX(i + 0); |
| 666 |
if (h->seac.phase > seacBase) |
| 667 |
stem->edge0 += vert ? h->seac.adx : h->seac.ady; |
| 668 |
stem->edge1 = stem->edge0 + INDEX(i + 1); |
| 669 |
lastedge = stem->edge1; |
| 670 |
stem->flags = vert ? ABF_VERT_STEM : 0; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/* Compute mask length and unused bit mask */ |
| 675 |
h->mask.length = (short)((h->stems.cnt + 7) / 8); |
| 676 |
h->mask.unused = ~(~0 << (h->mask.length * 8 - h->stems.cnt)); |
| 677 |
|
| 678 |
return 0; |
| 679 |
} |
| 680 |
|
| 681 |
/* Callback hint/cntrmask. Return 0 on success else error code. */ |
| 682 |
static int callbackMask(t2cCtx h, int cntr, |
| 683 |
unsigned char **next, unsigned char **end) { |
| 684 |
int i; |
| 685 |
|
| 686 |
if (h->mask.state == 1) |
| 687 |
savePendCntr(h, cntr); |
| 688 |
|
| 689 |
if (h->stack.cnt > 1) { |
| 690 |
/* Stem args on stack; must be omitted vstem(hm) op */ |
| 691 |
if (addStems(h, 1)) |
| 692 |
return t2cErrStemOverflow; |
| 693 |
} |
| 694 |
|
| 695 |
/* Check for invalid mask */ |
| 696 |
if (h->mask.length <= 0 || h->mask.length > T2_MAX_STEMS / 8) |
| 697 |
return t2cErrHintmask; |
| 698 |
|
| 699 |
/* Read mask */ |
| 700 |
for (i = 0; i < h->mask.length; i++) { |
| 701 |
if (*next == *end) { |
| 702 |
*next = refill(h, end); |
| 703 |
if (*next == NULL) |
| 704 |
return t2cErrSrcStream; |
| 705 |
} |
| 706 |
h->mask.bytes[i] = *(*next)++; |
| 707 |
} |
| 708 |
|
| 709 |
if (h->mask.bytes[h->mask.length - 1] & h->mask.unused) { |
| 710 |
message(h, "invalid hint/cntr mask. Correcting..."); |
| 711 |
h->mask.bytes[h->mask.length - 1] &= ~h->mask.unused; /* clear the unused bits */ |
| 712 |
} |
| 713 |
|
| 714 |
if (h->glyph->stem == NULL) |
| 715 |
return 0; /* No stem callback */ |
| 716 |
|
| 717 |
if (cntr && h->mask.state == 0) { |
| 718 |
/* Save first cntrmask */ |
| 719 |
h->mask.state = 1; |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
callbackStems(h, cntr); |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
/* Callback seac operator. */ |
| 728 |
static void callback_seac(t2cCtx h, float adx, float ady, int bchar, int achar) { |
| 729 |
if (h->flags & USE_MATRIX) { |
| 730 |
adx = TX(adx, ady); |
| 731 |
ady = TY(adx, ady); |
| 732 |
} else if (h->flags & SEEN_BLEND) { |
| 733 |
adx = RND(adx); |
| 734 |
ady = RND(ady); |
| 735 |
} |
| 736 |
|
| 737 |
h->glyph->seac(h->glyph, adx, ady, bchar, achar); |
| 738 |
} |
| 739 |
|
| 740 |
/* ---------------------------- Charstring Parse --------------------------- */ |
| 741 |
|
| 742 |
/* Parse seac component glyph. */ |
| 743 |
static int parseSeacComponent(t2cCtx h, int stdcode, int depth) { |
| 744 |
long offset; |
| 745 |
|
| 746 |
if (depth > MAX_RECURSION_DEPTH) { |
| 747 |
fatal(h, t2cErrMaxRecursion); |
| 748 |
} |
| 749 |
|
| 750 |
if (stdcode < 0 || stdcode > 255) |
| 751 |
return t2cErrBadSeacComp; |
| 752 |
offset = h->aux->getStdEncGlyphOffset(h->aux->ctx, stdcode); |
| 753 |
if (offset == -1) |
| 754 |
return t2cErrBadSeacComp; |
| 755 |
|
| 756 |
h->stack.cnt = 0; |
| 757 |
return t2Decode(h, offset, depth + 1); |
| 758 |
} |
| 759 |
|
| 760 |
/* Unbias subroutine number. Return -1 on error else subroutine number. */ |
| 761 |
static long unbias(long arg, long nSubrs) { |
| 762 |
if (nSubrs < 1240) |
| 763 |
arg += 107; |
| 764 |
else if (nSubrs < 33900) |
| 765 |
arg += 1131; |
| 766 |
else |
| 767 |
arg += 32768; |
| 768 |
return (arg < 0 || arg >= nSubrs) ? -1 : arg; |
| 769 |
} |
| 770 |
|
| 771 |
/* Reverse stack elements between index i and j (i < j). */ |
| 772 |
static void reverse(t2cCtx h, int i, int j) { |
| 773 |
while (i < j) { |
| 774 |
float tmp = h->stack.array[i]; |
| 775 |
h->stack.array[i++] = h->stack.array[j]; |
| 776 |
h->stack.array[j--] = tmp; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/* Support undocumented blend operator. Retained for multiple master font |
| 781 |
substitution. Assumes 4 masters. Return 0 on success else error code. */ |
| 782 |
/* Note: there is no command line support to set a WV for Type 2, but you can |
| 783 |
compile one for testing by editing t2cParse(), below */ |
| 784 |
|
| 785 |
static int handleBlend(t2cCtx h) { |
| 786 |
/* When the 'blend operator is encountered, the last items on the stack are |
| 787 |
the blend operands. These are, in order: |
| 788 |
numMaster operands from the default font |
| 789 |
(numMaster-1)* numBlends blend delta operands |
| 790 |
numBlends value |
| 791 |
|
| 792 |
We find the first blend operand item, which is the first value from the |
| 793 |
default font. For each of the numBlends default font values, we allocate opeEntry.blendValues |
| 794 |
to hold the region font values for that operand index. |
| 795 |
We then copy the region delta values to opEntry.blendValues. */ |
| 796 |
int result = 0; |
| 797 |
abfOpEntry *opEntry; |
| 798 |
int i = 0; |
| 799 |
int numBlends; |
| 800 |
int numRegions = h->stack.numRegions; |
| 801 |
int numDeltaBlends; |
| 802 |
int numTotalBlends; |
| 803 |
int firstItemIndex; |
| 804 |
|
| 805 |
CHKUFLOW(h, 1); |
| 806 |
numBlends = (int)INDEX(h->stack.cnt - 1); |
| 807 |
numDeltaBlends = numBlends * numRegions; |
| 808 |
numTotalBlends = numBlends + numDeltaBlends; |
| 809 |
if (numBlends < 0 || numDeltaBlends < 0 || numTotalBlends < 0 /*check signed int overflow*/) |
| 810 |
return t2cErrStackUnderflow; |
| 811 |
|
| 812 |
// pop off the numBlends value |
| 813 |
h->stack.cnt--; |
| 814 |
h->stack.blendCnt--; |
| 815 |
|
| 816 |
CHKUFLOW(h, numTotalBlends); |
| 817 |
firstItemIndex = (h->stack.blendCnt - numTotalBlends); |
| 818 |
if (firstItemIndex < 0) |
| 819 |
return t2cErrStackUnderflow; |
| 820 |
|
| 821 |
opEntry = &(h->stack.blendArray[firstItemIndex]); |
| 822 |
|
| 823 |
if (h->stack.blendCnt < numDeltaBlends || h->stack.cnt < numDeltaBlends) |
| 824 |
return t2cErrStackUnderflow; |
| 825 |
|
| 826 |
if (h->flags & FLATTEN_BLEND) { |
| 827 |
/* Blend values on the blend stack and replace the default values on the regular stack with the results. |
| 828 |
*/ |
| 829 |
for (i = 0; i < numBlends; i++) { |
| 830 |
float val = opEntry[i].value; |
| 831 |
int r; |
| 832 |
|
| 833 |
for (r = 0; r < h->stack.numRegions; r++) { |
| 834 |
int index = (i * h->stack.numRegions) + r + (h->stack.blendCnt - numDeltaBlends); |
| 835 |
val += INDEX_BLEND(index).value * h->aux->scalars[h->regionIndices[r]]; |
| 836 |
} |
| 837 |
|
| 838 |
h->stack.array[i + h->stack.cnt - (numBlends + numDeltaBlends)] = val; |
| 839 |
} |
| 840 |
|
| 841 |
h->stack.cnt -= numDeltaBlends; |
| 842 |
h->stack.blendCnt -= numDeltaBlends; |
| 843 |
} else { |
| 844 |
int stackIndex = h->stack.blendCnt - numDeltaBlends; |
| 845 |
|
| 846 |
for (i = 0; i < numBlends; i++) { |
| 847 |
int r; |
| 848 |
opEntry->blendValues = (float *)memNew(h, sizeof(float) * numRegions); |
| 849 |
opEntry->numBlends = 1; |
| 850 |
for (r = 0; r < numRegions; r++) { |
| 851 |
float val = INDEX_BLEND(stackIndex++).value; |
| 852 |
opEntry->blendValues[r] = val; |
| 853 |
} |
| 854 |
opEntry++; |
| 855 |
} |
| 856 |
|
| 857 |
h->stack.cnt -= numDeltaBlends; |
| 858 |
|
| 859 |
opEntry = &(h->stack.blendArray[h->stack.blendCnt - numDeltaBlends]); |
| 860 |
for (i = 0; i < numDeltaBlends; i++) { |
| 861 |
opEntry->numBlends = 0; |
| 862 |
opEntry++; |
| 863 |
} |
| 864 |
h->stack.blendCnt -= numDeltaBlends; |
| 865 |
} |
| 866 |
return result; |
| 867 |
} |
| 868 |
|
| 869 |
static void clearBlendStack(t2cCtx h) { |
| 870 |
if (h->flags & SEEN_CFF2_BLEND) { |
| 871 |
int j; |
| 872 |
for (j = 0; j < h->stack.blendCnt; j++) { |
| 873 |
abfOpEntry *blend = &h->stack.blendArray[j]; |
| 874 |
if (blend->blendValues != NULL) { |
| 875 |
memFree(h, blend->blendValues); |
| 876 |
blend->blendValues = NULL; |
| 877 |
} |
| 878 |
blend->numBlends = 0; |
| 879 |
} |
| 880 |
h->flags &= ~SEEN_CFF2_BLEND; |
| 881 |
} |
| 882 |
/* The following needs to be always called, so that h->stack.blendCnt is |
| 883 |
reset to zero even if no blends have been seen. */ |
| 884 |
h->stack.blendCnt = 0; |
| 885 |
} |
| 886 |
|
| 887 |
static void copyBlendArgs(t2cCtx h, abfBlendArg *blendArg, abfOpEntry *opEntry) { |
| 888 |
/* If the source opEntry is NULL, fill the blendArg with all zeros, |
| 889 |
else copy the values. |
| 890 |
*/ |
| 891 |
if (opEntry != NULL) { |
| 892 |
blendArg->value = opEntry->value; |
| 893 |
if (opEntry->numBlends) { |
| 894 |
int j; |
| 895 |
blendArg->hasBlend = 1; |
| 896 |
for (j = 0; j < h->stack.numRegions; j++) { |
| 897 |
blendArg->blendValues[j] = opEntry->blendValues[j]; |
| 898 |
} |
| 899 |
} else { |
| 900 |
blendArg->hasBlend = 0; |
| 901 |
} |
| 902 |
} else { |
| 903 |
blendArg->hasBlend = 0; |
| 904 |
blendArg->value = 0; |
| 905 |
} |
| 906 |
} |
| 907 |
static void popBlendArgs2(t2cCtx h, abfOpEntry *dx, abfOpEntry *dy) { |
| 908 |
abfBlendArg *blendArgs = h->stack.blendArgs; |
| 909 |
copyBlendArgs(h, &blendArgs[0], dx); |
| 910 |
copyBlendArgs(h, &blendArgs[1], dy); |
| 911 |
} |
| 912 |
|
| 913 |
static void popBlendArgs6(t2cCtx h, abfOpEntry *dx1, abfOpEntry *dy1, |
| 914 |
abfOpEntry *dx2, abfOpEntry *dy2, |
| 915 |
abfOpEntry *dx3, abfOpEntry *dy3) { |
| 916 |
abfBlendArg *blendArgs = h->stack.blendArgs; |
| 917 |
copyBlendArgs(h, &blendArgs[0], dx1); |
| 918 |
copyBlendArgs(h, &blendArgs[1], dy1); |
| 919 |
copyBlendArgs(h, &blendArgs[2], dx2); |
| 920 |
copyBlendArgs(h, &blendArgs[3], dy2); |
| 921 |
copyBlendArgs(h, &blendArgs[4], dx3); |
| 922 |
copyBlendArgs(h, &blendArgs[5], dy3); |
| 923 |
} |
| 924 |
|
| 925 |
static void convertToAbsolute(t2cCtx h, float x1, float y1, abfBlendArg *blendArgs, int num) { |
| 926 |
/* Convert the delta values in place. */ |
| 927 |
int isX = 1; |
| 928 |
float defaultX = x1; |
| 929 |
float defaultY = y1; |
| 930 |
int i; |
| 931 |
|
| 932 |
for (i = 0; i < num; i++) { |
| 933 |
if (isX) { |
| 934 |
if (i > 1) |
| 935 |
defaultX = blendArgs->value += defaultX; |
| 936 |
else |
| 937 |
blendArgs->value = defaultX; |
| 938 |
} else { |
| 939 |
if (i > 1) |
| 940 |
defaultY = blendArgs->value += defaultY; |
| 941 |
else |
| 942 |
blendArgs->value = defaultY; |
| 943 |
} |
| 944 |
isX = !isX; |
| 945 |
blendArgs++; |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
static void setNumMasters(t2cCtx h) { |
| 950 |
unsigned short vsindex = h->glyph->info->blendInfo.vsindex; |
| 951 |
h->stack.numRegions = var_getIVDRegionCountForIndex(h->aux->varStore, vsindex); |
| 952 |
h->glyph->info->blendInfo.numRegions = h->stack.numRegions; |
| 953 |
if (h->aux->varStore && !var_getIVSRegionIndices(h->aux->varStore, vsindex, h->regionIndices, h->aux->varStore->regionList.regionCount)) { |
| 954 |
message(h, "inconsistent region indices detected in item variation store subtable %d", vsindex); |
| 955 |
h->stack.numRegions = 0; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
/* Decode Type 2 charstring. Return 0 to continue else error code. */ |
| 960 |
static int t2Decode(t2cCtx h, long offset, int depth) { |
| 961 |
unsigned char *end; |
| 962 |
unsigned char *next; |
| 963 |
|
| 964 |
if (depth > MAX_RECURSION_DEPTH) { |
| 965 |
fatal(h, t2cErrMaxRecursion); |
| 966 |
} |
| 967 |
|
| 968 |
/* Fetch charstring */ |
| 969 |
if (srcSeek(h, offset)) |
| 970 |
return t2cErrSrcStream; |
| 971 |
next = refill(h, &end); |
| 972 |
if (next == NULL) |
| 973 |
return t2cErrSrcStream; |
| 974 |
|
| 975 |
for (;;) { |
| 976 |
int i; |
| 977 |
int result; |
| 978 |
int byte0; |
| 979 |
|
| 980 |
CHECK_FIRST_BYTE(h); |
| 981 |
byte0 = *next++; |
| 982 |
switch (byte0) { |
| 983 |
case tx_reserved0: |
| 984 |
case t2_reserved9: |
| 985 |
case t2_reserved13: |
| 986 |
case tx_reserved17: |
| 987 |
return t2cErrInvalidOp; |
| 988 |
case t2_vsindex: |
| 989 |
CHKUFLOW(h, 1); |
| 990 |
h->glyph->info->blendInfo.vsindex = (unsigned short)POP(); |
| 991 |
setNumMasters(h); |
| 992 |
break; |
| 993 |
case tx_hstem: |
| 994 |
/* if not in a library element, falls through to t2_hstemhm */ |
| 995 |
case t2_hstemhm: |
| 996 |
if (callbackWidth(h, 1)) |
| 997 |
return t2cSuccess; |
| 998 |
if (addStems(h, 0)) |
| 999 |
return t2cErrStemOverflow; |
| 1000 |
break; |
| 1001 |
case tx_vstem: |
| 1002 |
/* if not in a library element, falls through to t2_vstemhm */ |
| 1003 |
case t2_vstemhm: |
| 1004 |
if (callbackWidth(h, 1)) |
| 1005 |
return t2cSuccess; |
| 1006 |
if (addStems(h, 1)) |
| 1007 |
return t2cErrStemOverflow; |
| 1008 |
break; |
| 1009 |
case tx_rmoveto: |
| 1010 |
if (callbackWidth(h, 1)) |
| 1011 |
return t2cSuccess; |
| 1012 |
CHKUFLOW(h, 2); |
| 1013 |
{ |
| 1014 |
float y = POP(); |
| 1015 |
float x = POP(); |
| 1016 |
if ((h->flags & IS_CFF2) && (h->glyph->moveVF != NULL) && |
| 1017 |
!(h->flags & FLATTEN_BLEND)) |
| 1018 |
popBlendArgs2(h, &INDEX_BLEND(0), &INDEX_BLEND(1)); |
| 1019 |
callbackMove(h, x, y); |
| 1020 |
} |
| 1021 |
break; |
| 1022 |
case tx_hmoveto: |
| 1023 |
if (callbackWidth(h, 0)) |
| 1024 |
return t2cSuccess; |
| 1025 |
if ((h->flags & IS_CFF2) && (h->glyph->moveVF != NULL) && |
| 1026 |
!(h->flags & FLATTEN_BLEND)) |
| 1027 |
popBlendArgs2(h, &INDEX_BLEND(0), NULL); |
| 1028 |
CHKUFLOW(h, 1); |
| 1029 |
callbackMove(h, POP(), 0); |
| 1030 |
break; |
| 1031 |
case tx_vmoveto: |
| 1032 |
if (callbackWidth(h, 0)) |
| 1033 |
return t2cSuccess; |
| 1034 |
if ((h->flags & IS_CFF2) && (h->glyph->moveVF != NULL) && |
| 1035 |
!(h->flags & FLATTEN_BLEND)) |
| 1036 |
popBlendArgs2(h, NULL, &INDEX_BLEND(0)); |
| 1037 |
CHKUFLOW(h, 1); |
| 1038 |
callbackMove(h, 0, POP()); |
| 1039 |
break; |
| 1040 |
case tx_rlineto: |
| 1041 |
CHKUFLOW(h, 2); |
| 1042 |
for (i = 0; i < h->stack.cnt - 1; i += 2) { |
| 1043 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 1044 |
!(h->flags & FLATTEN_BLEND)) |
| 1045 |
popBlendArgs2(h, &INDEX_BLEND(i), &INDEX_BLEND(i + 1)); |
| 1046 |
callbackLine(h, INDEX(i + 0), INDEX(i + 1)); |
| 1047 |
} |
| 1048 |
break; |
| 1049 |
case tx_hlineto: |
| 1050 |
case tx_vlineto: |
| 1051 |
CHKUFLOW(h, 1); |
| 1052 |
{ |
| 1053 |
int horz = byte0 == tx_hlineto; |
| 1054 |
for (i = 0; i < h->stack.cnt; i++) |
| 1055 |
if (horz++ & 1) { |
| 1056 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 1057 |
!(h->flags & FLATTEN_BLEND)) |
| 1058 |
popBlendArgs2(h, &INDEX_BLEND(i), NULL); |
| 1059 |
callbackLine(h, INDEX(i), 0); |
| 1060 |
} else { |
| 1061 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 1062 |
!(h->flags & FLATTEN_BLEND)) |
| 1063 |
popBlendArgs2(h, NULL, &INDEX_BLEND(i)); |
| 1064 |
callbackLine(h, 0, INDEX(i)); |
| 1065 |
} |
| 1066 |
} |
| 1067 |
break; |
| 1068 |
case tx_rrcurveto: |
| 1069 |
CHKUFLOW(h, 6); |
| 1070 |
for (i = 0; i < h->stack.cnt - 5; i += 6) { |
| 1071 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1072 |
!(h->flags & FLATTEN_BLEND)) |
| 1073 |
popBlendArgs6(h, |
| 1074 |
&INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1), |
| 1075 |
&INDEX_BLEND(i + 2), &INDEX_BLEND(i + 3), |
| 1076 |
&INDEX_BLEND(i + 4), &INDEX_BLEND(i + 5)); |
| 1077 |
callbackCurve(h, |
| 1078 |
INDEX(i + 0), INDEX(i + 1), |
| 1079 |
INDEX(i + 2), INDEX(i + 3), |
| 1080 |
INDEX(i + 4), INDEX(i + 5)); |
| 1081 |
} |
| 1082 |
break; |
| 1083 |
case tx_callsubr: |
| 1084 |
CHKUFLOW(h, 1); |
| 1085 |
{ |
| 1086 |
long saveoff = (long)(h->src.offset - (end - next)); |
| 1087 |
long saveEndOff = h->src.endOffset; |
| 1088 |
long num = unbias((long)POP(), h->aux->subrs.cnt); |
| 1089 |
h->stack.blendCnt--; // we do not blend subr indices. |
| 1090 |
if (num == -1) |
| 1091 |
return t2cErrCallsubr; |
| 1092 |
if ((num + 1) < h->aux->subrs.cnt) |
| 1093 |
h->src.endOffset = h->aux->subrs.offset[num + 1]; |
| 1094 |
else |
| 1095 |
h->src.endOffset = h->aux->subrsEnd; |
| 1096 |
h->subrDepth++; |
| 1097 |
if (h->subrDepth > TX_MAX_SUBR_DEPTH) { |
| 1098 |
message(h, "subr depth %d exceeds recursion limit %d (ignored)", h->subrDepth, TX_MAX_SUBR_DEPTH); |
| 1099 |
} |
| 1100 |
|
| 1101 |
result = t2Decode(h, h->aux->subrs.offset[num], depth + 1); |
| 1102 |
h->src.endOffset = saveEndOff; |
| 1103 |
h->subrDepth--; |
| 1104 |
|
| 1105 |
if (result || h->flags & SEEN_ENDCHAR) |
| 1106 |
return result; |
| 1107 |
else if (srcSeek(h, saveoff)) |
| 1108 |
return t2cErrSrcStream; |
| 1109 |
next = refill(h, &end); |
| 1110 |
if (next == NULL) |
| 1111 |
return t2cErrSrcStream; |
| 1112 |
} |
| 1113 |
continue; |
| 1114 |
case tx_return: |
| 1115 |
return 0; |
| 1116 |
case tx_escape: |
| 1117 |
/* Process escaped operator */ |
| 1118 |
{ |
| 1119 |
int escop; |
| 1120 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1121 |
escop = tx_ESC(*next++); |
| 1122 |
switch (escop) { |
| 1123 |
case tx_dotsection: |
| 1124 |
if (!(h->aux->flags & T2C_UPDATE_OPS || |
| 1125 |
h->glyph->stem == NULL)) |
| 1126 |
callbackOp(h, tx_dotsection); |
| 1127 |
break; |
| 1128 |
case tx_and: |
| 1129 |
if (h->flags & IS_CFF2) |
| 1130 |
return t2cErrInvalidOp; |
| 1131 |
CHKUFLOW(h, 2); |
| 1132 |
{ |
| 1133 |
int b = POP() != 0.0f; |
| 1134 |
int a = POP() != 0.0f; |
| 1135 |
PUSH(a && b); |
| 1136 |
} |
| 1137 |
continue; |
| 1138 |
case tx_or: |
| 1139 |
if (h->flags & IS_CFF2) |
| 1140 |
return t2cErrInvalidOp; |
| 1141 |
CHKUFLOW(h, 2); |
| 1142 |
{ |
| 1143 |
int b = POP() != 0.0f; |
| 1144 |
int a = POP() != 0.0f; |
| 1145 |
PUSH(a || b); |
| 1146 |
} |
| 1147 |
continue; |
| 1148 |
case tx_not: |
| 1149 |
if (h->flags & IS_CFF2) |
| 1150 |
return t2cErrInvalidOp; |
| 1151 |
CHKUFLOW(h, 1); |
| 1152 |
{ |
| 1153 |
int a = POP() == 0.0f; |
| 1154 |
PUSH(a); |
| 1155 |
} |
| 1156 |
continue; |
| 1157 |
case tx_abs: |
| 1158 |
if (h->flags & IS_CFF2) |
| 1159 |
return t2cErrInvalidOp; |
| 1160 |
CHKUFLOW(h, 1); |
| 1161 |
{ |
| 1162 |
float a = POP(); |
| 1163 |
PUSH((a < 0.0f) ? -a : a); |
| 1164 |
} |
| 1165 |
continue; |
| 1166 |
case tx_add: |
| 1167 |
if (h->flags & IS_CFF2) |
| 1168 |
return t2cErrInvalidOp; |
| 1169 |
CHKUFLOW(h, 2); |
| 1170 |
{ |
| 1171 |
float b = POP(); |
| 1172 |
float a = POP(); |
| 1173 |
PUSH(a + b); |
| 1174 |
} |
| 1175 |
continue; |
| 1176 |
case tx_sub: |
| 1177 |
if (h->flags & IS_CFF2) |
| 1178 |
return t2cErrInvalidOp; |
| 1179 |
CHKUFLOW(h, 2); |
| 1180 |
{ |
| 1181 |
float b = POP(); |
| 1182 |
float a = POP(); |
| 1183 |
PUSH(a - b); |
| 1184 |
} |
| 1185 |
continue; |
| 1186 |
case tx_div: |
| 1187 |
if (h->flags & IS_CFF2) |
| 1188 |
return t2cErrInvalidOp; |
| 1189 |
CHKUFLOW(h, 2); |
| 1190 |
{ |
| 1191 |
float b = POP(); |
| 1192 |
float a = POP(); |
| 1193 |
PUSH(a / b); |
| 1194 |
} |
| 1195 |
continue; |
| 1196 |
case tx_neg: |
| 1197 |
if (h->flags & IS_CFF2) |
| 1198 |
return t2cErrInvalidOp; |
| 1199 |
CHKUFLOW(h, 1); |
| 1200 |
{ |
| 1201 |
float a = POP(); |
| 1202 |
PUSH(-a); |
| 1203 |
} |
| 1204 |
continue; |
| 1205 |
case tx_eq: |
| 1206 |
if (h->flags & IS_CFF2) |
| 1207 |
return t2cErrInvalidOp; |
| 1208 |
CHKUFLOW(h, 2); |
| 1209 |
{ |
| 1210 |
float b = POP(); |
| 1211 |
float a = POP(); |
| 1212 |
PUSH(a == b); |
| 1213 |
} |
| 1214 |
continue; |
| 1215 |
case tx_drop: |
| 1216 |
if (h->flags & IS_CFF2) |
| 1217 |
return t2cErrInvalidOp; |
| 1218 |
CHKUFLOW(h, 1); |
| 1219 |
h->stack.cnt--; |
| 1220 |
continue; |
| 1221 |
case tx_put: |
| 1222 |
CHKUFLOW(h, 2); |
| 1223 |
{ |
| 1224 |
i = (int)POP(); |
| 1225 |
if (i < 0 || i >= TX_BCA_LENGTH) |
| 1226 |
return t2cErrPutBounds; |
| 1227 |
h->BCA[i] = POP(); |
| 1228 |
} |
| 1229 |
continue; |
| 1230 |
case tx_get: |
| 1231 |
if (h->flags & IS_CFF2) |
| 1232 |
return t2cErrInvalidOp; |
| 1233 |
CHKUFLOW(h, 1); |
| 1234 |
{ |
| 1235 |
i = (int)POP(); |
| 1236 |
if (i < 0 || i >= TX_BCA_LENGTH) |
| 1237 |
return t2cErrGetBounds; |
| 1238 |
PUSH(h->BCA[i]); |
| 1239 |
} |
| 1240 |
continue; |
| 1241 |
case tx_ifelse: |
| 1242 |
if (h->flags & IS_CFF2) |
| 1243 |
return t2cErrInvalidOp; |
| 1244 |
CHKUFLOW(h, 4); |
| 1245 |
{ |
| 1246 |
float v2 = POP(); |
| 1247 |
float v1 = POP(); |
| 1248 |
float s2 = POP(); |
| 1249 |
float s1 = POP(); |
| 1250 |
PUSH((v1 <= v2) ? s1 : s2); |
| 1251 |
} |
| 1252 |
continue; |
| 1253 |
case tx_random: |
| 1254 |
if (h->flags & IS_CFF2) |
| 1255 |
return t2cErrInvalidOp; |
| 1256 |
CHKOFLOW(h, 1); |
| 1257 |
PUSH(((float)rand() + 1) / ((float)RAND_MAX + 1)); |
| 1258 |
continue; |
| 1259 |
case tx_mul: |
| 1260 |
CHKUFLOW(h, 2); |
| 1261 |
{ |
| 1262 |
float b = POP(); |
| 1263 |
float a = POP(); |
| 1264 |
PUSH(a * b); |
| 1265 |
} |
| 1266 |
continue; |
| 1267 |
case tx_sqrt: |
| 1268 |
if (h->flags & IS_CFF2) |
| 1269 |
return t2cErrInvalidOp; |
| 1270 |
CHKUFLOW(h, 1); |
| 1271 |
{ |
| 1272 |
float a = POP(); |
| 1273 |
if (a < 0.0f) |
| 1274 |
return t2cErrSqrtDomain; |
| 1275 |
PUSH(sqrt(a)); |
| 1276 |
} |
| 1277 |
continue; |
| 1278 |
case tx_dup: |
| 1279 |
if (h->flags & IS_CFF2) |
| 1280 |
return t2cErrInvalidOp; |
| 1281 |
CHKUFLOW(h, 1); |
| 1282 |
CHKOFLOW(h, 1); |
| 1283 |
{ |
| 1284 |
float a = POP(); |
| 1285 |
PUSH(a); |
| 1286 |
PUSH(a); |
| 1287 |
} |
| 1288 |
continue; |
| 1289 |
case tx_exch: |
| 1290 |
if (h->flags & IS_CFF2) |
| 1291 |
return t2cErrInvalidOp; |
| 1292 |
CHKUFLOW(h, 2); |
| 1293 |
{ |
| 1294 |
float b = POP(); |
| 1295 |
float a = POP(); |
| 1296 |
PUSH(b); |
| 1297 |
PUSH(a); |
| 1298 |
} |
| 1299 |
continue; |
| 1300 |
case tx_index: |
| 1301 |
if (h->flags & IS_CFF2) |
| 1302 |
return t2cErrInvalidOp; |
| 1303 |
CHKUFLOW(h, 1); |
| 1304 |
{ |
| 1305 |
i = (int)POP(); |
| 1306 |
if (i < 0) |
| 1307 |
i = 0; /* Duplicate top element */ |
| 1308 |
if (i >= h->stack.cnt) |
| 1309 |
return t2cErrIndexBounds; |
| 1310 |
{ |
| 1311 |
float t = h->stack.array[h->stack.cnt - 1 - i]; |
| 1312 |
PUSH(t); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
continue; |
| 1316 |
case tx_roll: |
| 1317 |
if (h->flags & IS_CFF2) |
| 1318 |
return t2cErrInvalidOp; |
| 1319 |
CHKUFLOW(h, 2); |
| 1320 |
{ |
| 1321 |
int j = (int)POP(); |
| 1322 |
int n = (int)POP(); |
| 1323 |
int iTop = h->stack.cnt - 1; |
| 1324 |
int iBottom = h->stack.cnt - n; |
| 1325 |
|
| 1326 |
if (n <= 0 || iBottom < 0) |
| 1327 |
return t2cErrRollBounds; |
| 1328 |
|
| 1329 |
/* Constrain j to [0,n) */ |
| 1330 |
if (j < 0) |
| 1331 |
j = n - (-j % n); |
| 1332 |
j %= n; |
| 1333 |
|
| 1334 |
reverse(h, iTop - j + 1, iTop); |
| 1335 |
reverse(h, iBottom, iTop - j); |
| 1336 |
reverse(h, iBottom, iTop); |
| 1337 |
} |
| 1338 |
continue; |
| 1339 |
case t2_hflex: |
| 1340 |
CHKUFLOW(h, 7); |
| 1341 |
callbackFlex(h, |
| 1342 |
INDEX(0), 0, |
| 1343 |
INDEX(1), INDEX(2), |
| 1344 |
INDEX(3), 0, |
| 1345 |
INDEX(4), 0, |
| 1346 |
INDEX(5), -INDEX(2), |
| 1347 |
INDEX(6), 0, |
| 1348 |
TX_STD_FLEX_DEPTH); |
| 1349 |
break; |
| 1350 |
case t2_flex: |
| 1351 |
CHKUFLOW(h, 13); |
| 1352 |
callbackFlex(h, |
| 1353 |
INDEX(0), INDEX(1), |
| 1354 |
INDEX(2), INDEX(3), |
| 1355 |
INDEX(4), INDEX(5), |
| 1356 |
INDEX(6), INDEX(7), |
| 1357 |
INDEX(8), INDEX(9), |
| 1358 |
INDEX(10), INDEX(11), |
| 1359 |
INDEX(12)); |
| 1360 |
break; |
| 1361 |
case t2_hflex1: |
| 1362 |
CHKUFLOW(h, 9); |
| 1363 |
{ |
| 1364 |
float dy1 = INDEX(1); |
| 1365 |
float dy2 = INDEX(3); |
| 1366 |
float dy5 = INDEX(7); |
| 1367 |
callbackFlex(h, |
| 1368 |
INDEX(0), dy1, |
| 1369 |
INDEX(2), dy2, |
| 1370 |
INDEX(4), 0, |
| 1371 |
INDEX(5), 0, |
| 1372 |
INDEX(6), dy5, |
| 1373 |
INDEX(8), -(dy1 + dy2 + dy5), |
| 1374 |
TX_STD_FLEX_DEPTH); |
| 1375 |
} |
| 1376 |
break; |
| 1377 |
case t2_flex1: |
| 1378 |
CHKUFLOW(h, 11); |
| 1379 |
{ |
| 1380 |
float dx1 = INDEX(0); |
| 1381 |
float dy1 = INDEX(1); |
| 1382 |
float dx2 = INDEX(2); |
| 1383 |
float dy2 = INDEX(3); |
| 1384 |
float dx3 = INDEX(4); |
| 1385 |
float dy3 = INDEX(5); |
| 1386 |
float dx4 = INDEX(6); |
| 1387 |
float dy4 = INDEX(7); |
| 1388 |
float dx5 = INDEX(8); |
| 1389 |
float dy5 = INDEX(9); |
| 1390 |
float dx = dx1 + dx2 + dx3 + dx4 + dx5; |
| 1391 |
float dy = dy1 + dy2 + dy3 + dy4 + dy5; |
| 1392 |
if (ABS(dx) > ABS(dy)) { |
| 1393 |
dx = INDEX(10); |
| 1394 |
dy = -dy; |
| 1395 |
} else { |
| 1396 |
dx = -dx; |
| 1397 |
dy = INDEX(10); |
| 1398 |
} |
| 1399 |
callbackFlex(h, |
| 1400 |
dx1, dy1, |
| 1401 |
dx2, dy2, |
| 1402 |
dx3, dy3, |
| 1403 |
dx4, dy4, |
| 1404 |
dx5, dy5, |
| 1405 |
dx, dy, |
| 1406 |
TX_STD_FLEX_DEPTH); |
| 1407 |
} |
| 1408 |
break; |
| 1409 |
case t2_cntron: |
| 1410 |
if (h->flags & IS_CFF2) |
| 1411 |
return t2cErrInvalidOp; |
| 1412 |
h->LanguageGroup = 1; /* Turn on global coloring */ |
| 1413 |
callbackOp(h, t2_cntron); |
| 1414 |
continue; |
| 1415 |
case t2_reservedESC1: |
| 1416 |
case t2_reservedESC2: |
| 1417 |
case t2_reservedESC6: |
| 1418 |
case t2_reservedESC7: |
| 1419 |
case t2_reservedESC8: |
| 1420 |
case t2_reservedESC13: |
| 1421 |
case t2_reservedESC16: |
| 1422 |
case t2_reservedESC17: |
| 1423 |
case tx_reservedESC19: |
| 1424 |
case tx_reservedESC25: |
| 1425 |
case tx_reservedESC31: |
| 1426 |
case tx_reservedESC32: |
| 1427 |
case t2_reservedESC33: |
| 1428 |
break; |
| 1429 |
default: |
| 1430 |
return t2cErrInvalidOp; |
| 1431 |
} /* End: switch (escop) */ |
| 1432 |
} /* End: case tx_escape: */ |
| 1433 |
break; |
| 1434 |
|
| 1435 |
case tx_endchar: |
| 1436 |
do_endchar: |
| 1437 |
if (callbackWidth(h, 1)) |
| 1438 |
return t2cSuccess; |
| 1439 |
if ((h->subrDepth > 0) && (h->flags & IS_CFF2)) |
| 1440 |
return t2cSuccess; /* CFF2 subrs don't have a return char. */ |
| 1441 |
|
| 1442 |
if (h->stack.cnt > 1) { |
| 1443 |
CHKUFLOW(h, 4); |
| 1444 |
|
| 1445 |
/* Save arguments */ |
| 1446 |
h->aux->achar = (unsigned char)POP(); |
| 1447 |
h->aux->bchar = (unsigned char)POP(); |
| 1448 |
h->seac.ady = POP(); |
| 1449 |
h->seac.adx = POP(); |
| 1450 |
|
| 1451 |
if (h->aux->flags & T2C_UPDATE_OPS) { |
| 1452 |
/* Parse base component */ |
| 1453 |
h->seac.phase = seacBase; |
| 1454 |
result = parseSeacComponent(h, h->aux->bchar, depth + 1); |
| 1455 |
if (result) |
| 1456 |
return result; |
| 1457 |
|
| 1458 |
h->flags &= ~SEEN_ENDCHAR; |
| 1459 |
h->stems.cnt = 0; |
| 1460 |
h->mask.length = 0; |
| 1461 |
h->mask.state = 0; |
| 1462 |
h->x = h->seac.adx; |
| 1463 |
h->y = h->seac.ady; |
| 1464 |
h->x = roundf(h->x * 100) / 100; |
| 1465 |
h->y = roundf(h->y * 100) / 100; |
| 1466 |
|
| 1467 |
/* Parse accent component */ |
| 1468 |
h->seac.phase = seacAccentPreMove; |
| 1469 |
result = parseSeacComponent(h, h->aux->achar, depth + 1); |
| 1470 |
if (result) |
| 1471 |
return result; |
| 1472 |
} else |
| 1473 |
/* Callback seac data */ |
| 1474 |
callback_seac(h, h->seac.adx, h->seac.ady, |
| 1475 |
h->aux->bchar, h->aux->achar); |
| 1476 |
} |
| 1477 |
h->flags |= SEEN_ENDCHAR; |
| 1478 |
return 0; |
| 1479 |
case t2_blend: { |
| 1480 |
h->flags |= SEEN_CFF2_BLEND; |
| 1481 |
if (h->stack.numRegions == 0) |
| 1482 |
setNumMasters(h); |
| 1483 |
result = handleBlend(h); |
| 1484 |
if (result) |
| 1485 |
return result; |
| 1486 |
} |
| 1487 |
continue; |
| 1488 |
case t2_hintmask: |
| 1489 |
if (callbackWidth(h, 1)) |
| 1490 |
return t2cSuccess; |
| 1491 |
result = callbackMask(h, 0, &next, &end); |
| 1492 |
if (result) |
| 1493 |
return result; |
| 1494 |
break; |
| 1495 |
case t2_cntrmask: |
| 1496 |
if (callbackWidth(h, 1)) |
| 1497 |
return t2cSuccess; |
| 1498 |
result = callbackMask(h, 1, &next, &end); |
| 1499 |
if (result) |
| 1500 |
return result; |
| 1501 |
break; |
| 1502 |
case t2_rcurveline: |
| 1503 |
CHKUFLOW(h, 8); |
| 1504 |
for (i = 0; i < h->stack.cnt - 5; i += 6) { |
| 1505 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1506 |
!(h->flags & FLATTEN_BLEND)) |
| 1507 |
popBlendArgs6(h, |
| 1508 |
&INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1), |
| 1509 |
&INDEX_BLEND(i + 2), &INDEX_BLEND(i + 3), |
| 1510 |
&INDEX_BLEND(i + 4), &INDEX_BLEND(i + 5)); |
| 1511 |
callbackCurve(h, |
| 1512 |
INDEX(i + 0), INDEX(i + 1), |
| 1513 |
INDEX(i + 2), INDEX(i + 3), |
| 1514 |
INDEX(i + 4), INDEX(i + 5)); |
| 1515 |
} |
| 1516 |
if (i < h->stack.cnt - 1) { |
| 1517 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 1518 |
!(h->flags & FLATTEN_BLEND)) |
| 1519 |
popBlendArgs2(h, &INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1)); |
| 1520 |
callbackLine(h, INDEX(i + 0), INDEX(i + 1)); |
| 1521 |
} |
| 1522 |
break; |
| 1523 |
case t2_rlinecurve: |
| 1524 |
CHKUFLOW(h, 8); |
| 1525 |
for (i = 0; i < h->stack.cnt - 6; i += 2) { |
| 1526 |
if ((h->flags & IS_CFF2) && (h->glyph->lineVF != NULL) && |
| 1527 |
!(h->flags & FLATTEN_BLEND)) |
| 1528 |
popBlendArgs2(h, &INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1)); |
| 1529 |
callbackLine(h, INDEX(i + 0), INDEX(i + 1)); |
| 1530 |
} |
| 1531 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1532 |
!(h->flags & FLATTEN_BLEND)) |
| 1533 |
popBlendArgs6(h, |
| 1534 |
&INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1), |
| 1535 |
&INDEX_BLEND(i + 2), &INDEX_BLEND(i + 3), |
| 1536 |
&INDEX_BLEND(i + 4), &INDEX_BLEND(i + 5)); |
| 1537 |
callbackCurve(h, |
| 1538 |
INDEX(i + 0), INDEX(i + 1), |
| 1539 |
INDEX(i + 2), INDEX(i + 3), |
| 1540 |
INDEX(i + 4), INDEX(i + 5)); |
| 1541 |
|
| 1542 |
break; |
| 1543 |
case t2_vvcurveto: |
| 1544 |
if ((h->stack.cnt) & 1) { |
| 1545 |
CHKUFLOW(h, 5); |
| 1546 |
i = 0; |
| 1547 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1548 |
!(h->flags & FLATTEN_BLEND)) |
| 1549 |
popBlendArgs6(h, |
| 1550 |
&INDEX_BLEND(i + 0), &INDEX_BLEND(i + 1), |
| 1551 |
&INDEX_BLEND(i + 2), &INDEX_BLEND(i + 3), |
| 1552 |
NULL, &INDEX_BLEND(i + 4)); |
| 1553 |
callbackCurve(h, |
| 1554 |
INDEX(0), INDEX(1), |
| 1555 |
INDEX(2), INDEX(3), |
| 1556 |
0, INDEX(4)); |
| 1557 |
i = 5; |
| 1558 |
} else { |
| 1559 |
CHKUFLOW(h, 4); |
| 1560 |
i = 0; |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* Add remaining curve(s) */ |
| 1564 |
for (; i < h->stack.cnt - 3; i += 4) { |
| 1565 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1566 |
!(h->flags & FLATTEN_BLEND)) |
| 1567 |
popBlendArgs6(h, |
| 1568 |
NULL, &INDEX_BLEND(i + 0), |
| 1569 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1570 |
NULL, &INDEX_BLEND(i + 3)); |
| 1571 |
callbackCurve(h, |
| 1572 |
0, INDEX(i + 0), |
| 1573 |
INDEX(i + 1), INDEX(i + 2), |
| 1574 |
0, INDEX(i + 3)); |
| 1575 |
} |
| 1576 |
break; |
| 1577 |
case t2_hhcurveto: |
| 1578 |
if ((h->stack.cnt) & 1) { |
| 1579 |
/* Add initial curve */ |
| 1580 |
CHKUFLOW(h, 5); |
| 1581 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1582 |
!(h->flags & FLATTEN_BLEND)) |
| 1583 |
popBlendArgs6(h, |
| 1584 |
&INDEX_BLEND(1), &INDEX_BLEND(0), |
| 1585 |
&INDEX_BLEND(2), &INDEX_BLEND(3), |
| 1586 |
&INDEX_BLEND(4), NULL); |
| 1587 |
callbackCurve(h, |
| 1588 |
INDEX(1), INDEX(0), |
| 1589 |
INDEX(2), INDEX(3), |
| 1590 |
INDEX(4), 0); |
| 1591 |
i = 5; |
| 1592 |
} else { |
| 1593 |
CHKUFLOW(h, 4); |
| 1594 |
i = 0; |
| 1595 |
} |
| 1596 |
|
| 1597 |
/* Add remaining curve(s) */ |
| 1598 |
for (; i < h->stack.cnt - 3; i += 4) { |
| 1599 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1600 |
!(h->flags & FLATTEN_BLEND)) |
| 1601 |
popBlendArgs6(h, |
| 1602 |
&INDEX_BLEND(i + 0), NULL, |
| 1603 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1604 |
&INDEX_BLEND(i + 3), NULL); |
| 1605 |
callbackCurve(h, |
| 1606 |
INDEX(i + 0), 0, |
| 1607 |
INDEX(i + 1), INDEX(i + 2), |
| 1608 |
INDEX(i + 3), 0); |
| 1609 |
} |
| 1610 |
break; |
| 1611 |
case t2_callgsubr: |
| 1612 |
CHKUFLOW(h, 1); |
| 1613 |
{ |
| 1614 |
long saveoff = (long)(h->src.offset - (end - next)); |
| 1615 |
long saveEndOff = h->src.endOffset; |
| 1616 |
long num = unbias((long)POP(), h->aux->gsubrs.cnt); |
| 1617 |
h->stack.blendCnt--; // we do not blend subr indices. |
| 1618 |
if (num == -1) |
| 1619 |
return t2cErrCallgsubr; |
| 1620 |
if ((num + 1) < h->aux->gsubrs.cnt) |
| 1621 |
h->src.endOffset = h->aux->gsubrs.offset[num + 1]; |
| 1622 |
else |
| 1623 |
h->src.endOffset = h->aux->gsubrsEnd; |
| 1624 |
|
| 1625 |
h->subrDepth++; |
| 1626 |
if (h->subrDepth > TX_MAX_SUBR_DEPTH) { |
| 1627 |
message(h, "subr depth %d exceeds recursion limit %d (ignored)", h->subrDepth, TX_MAX_SUBR_DEPTH); |
| 1628 |
} |
| 1629 |
|
| 1630 |
result = t2Decode(h, h->aux->gsubrs.offset[num], depth + 1); |
| 1631 |
h->src.endOffset = saveEndOff; |
| 1632 |
h->subrDepth--; |
| 1633 |
|
| 1634 |
if (result || h->flags & SEEN_ENDCHAR) |
| 1635 |
return result; |
| 1636 |
else if (srcSeek(h, saveoff)) |
| 1637 |
return t2cErrSrcStream; |
| 1638 |
next = refill(h, &end); |
| 1639 |
if (next == NULL) |
| 1640 |
return t2cErrSrcStream; |
| 1641 |
} |
| 1642 |
continue; |
| 1643 |
case tx_vhcurveto: |
| 1644 |
case tx_hvcurveto: |
| 1645 |
CHKUFLOW(h, 4); |
| 1646 |
{ |
| 1647 |
int adjust = ((h->stack.cnt) & 1) ? 5 : 0; |
| 1648 |
int horz = byte0 == tx_hvcurveto; |
| 1649 |
|
| 1650 |
/* Add initial curve(s) */ |
| 1651 |
for (i = 0; i < h->stack.cnt - adjust - 3; i += 4) |
| 1652 |
if (horz++ & 1) { |
| 1653 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1654 |
!(h->flags & FLATTEN_BLEND)) |
| 1655 |
popBlendArgs6(h, |
| 1656 |
&INDEX_BLEND(i + 0), NULL, |
| 1657 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1658 |
NULL, &INDEX_BLEND(i + 3)); |
| 1659 |
callbackCurve(h, |
| 1660 |
INDEX(i + 0), 0, |
| 1661 |
INDEX(i + 1), INDEX(i + 2), |
| 1662 |
0, INDEX(i + 3)); |
| 1663 |
} else { |
| 1664 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1665 |
!(h->flags & FLATTEN_BLEND)) |
| 1666 |
popBlendArgs6(h, |
| 1667 |
NULL, &INDEX_BLEND(i + 0), |
| 1668 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1669 |
&INDEX_BLEND(i + 3), NULL); |
| 1670 |
callbackCurve(h, |
| 1671 |
0, INDEX(i + 0), |
| 1672 |
INDEX(i + 1), INDEX(i + 2), |
| 1673 |
INDEX(i + 3), 0); |
| 1674 |
} |
| 1675 |
|
| 1676 |
if (adjust) { |
| 1677 |
/* Add last curve */ |
| 1678 |
if (horz & 1) { |
| 1679 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1680 |
!(h->flags & FLATTEN_BLEND)) |
| 1681 |
popBlendArgs6(h, |
| 1682 |
&INDEX_BLEND(i + 0), NULL, |
| 1683 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1684 |
&INDEX_BLEND(i + 4), &INDEX_BLEND(i + 3)); |
| 1685 |
callbackCurve(h, |
| 1686 |
INDEX(i + 0), 0, |
| 1687 |
INDEX(i + 1), INDEX(i + 2), |
| 1688 |
INDEX(i + 4), INDEX(i + 3)); |
| 1689 |
} else { |
| 1690 |
if ((h->flags & IS_CFF2) && (h->glyph->curveVF != NULL) && |
| 1691 |
!(h->flags & FLATTEN_BLEND)) |
| 1692 |
popBlendArgs6(h, |
| 1693 |
NULL, &INDEX_BLEND(i + 0), |
| 1694 |
&INDEX_BLEND(i + 1), &INDEX_BLEND(i + 2), |
| 1695 |
&INDEX_BLEND(i + 3), &INDEX_BLEND(i + 4)); |
| 1696 |
callbackCurve(h, |
| 1697 |
0, INDEX(i + 0), |
| 1698 |
INDEX(i + 1), INDEX(i + 2), |
| 1699 |
INDEX(i + 3), INDEX(i + 4)); |
| 1700 |
} |
| 1701 |
} |
| 1702 |
} |
| 1703 |
break; |
| 1704 |
case t2_shortint: |
| 1705 |
/* 3 byte number */ |
| 1706 |
CHKOFLOW(h, 1); |
| 1707 |
{ |
| 1708 |
short value; |
| 1709 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1710 |
value = *next++; |
| 1711 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1712 |
value = value << 8 | *next++; |
| 1713 |
#if SHRT_MAX > 32767 |
| 1714 |
/* short greater that 2 bytes; handle negative range */ |
| 1715 |
if (value > 32767) |
| 1716 |
value -= 65536; |
| 1717 |
#endif |
| 1718 |
PUSH(value); |
| 1719 |
} |
| 1720 |
continue; |
| 1721 |
default: |
| 1722 |
/* 1 byte number */ |
| 1723 |
CHKOFLOW(h, 1); |
| 1724 |
PUSH(byte0 - 139); |
| 1725 |
continue; |
| 1726 |
case 247: |
| 1727 |
case 248: |
| 1728 |
case 249: |
| 1729 |
case 250: |
| 1730 |
/* Positive 2 byte number */ |
| 1731 |
CHKOFLOW(h, 1); |
| 1732 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1733 |
PUSH(108 + 256 * (byte0 - 247) + *next); |
| 1734 |
next++; |
| 1735 |
continue; |
| 1736 |
case 251: |
| 1737 |
case 252: |
| 1738 |
case 253: |
| 1739 |
case 254: |
| 1740 |
/* Negative 2 byte number */ |
| 1741 |
CHKOFLOW(h, 1); |
| 1742 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1743 |
PUSH(-108 - 256 * (byte0 - 251) - *next); |
| 1744 |
next++; |
| 1745 |
continue; |
| 1746 |
case 255: |
| 1747 |
/* 5 byte number (16.16 fixed) */ |
| 1748 |
CHKOFLOW(h, 1); |
| 1749 |
{ |
| 1750 |
int32_t value; |
| 1751 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1752 |
value = *next++; |
| 1753 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1754 |
value = value << 8 | *next++; |
| 1755 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1756 |
value = value << 8 | *next++; |
| 1757 |
CHECK_SUBSEQUENT_BYTE(h); |
| 1758 |
value = value << 8 | *next++; |
| 1759 |
PUSH(value / 65536.0); |
| 1760 |
} |
| 1761 |
continue; |
| 1762 |
} /* End: switch (byte0) */ |
| 1763 |
clearBlendStack(h); |
| 1764 |
h->stack.cnt = 0; /* Clear stack */ |
| 1765 |
} /* End: while (cstr < end) */ |
| 1766 |
|
| 1767 |
#if 0 |
| 1768 |
/* for CFF2 Charstrings, we hit the end of the charstring without having seen endchar or return yet. Add it now. */ |
| 1769 |
if ((h->flags & IS_CFF2) && !(h->flags & SEEN_ENDCHAR)) |
| 1770 |
|
| 1771 |
{ |
| 1772 |
/* if this was a subr, do return. */ |
| 1773 |
if (h->subrDepth > 0) |
| 1774 |
return 0; |
| 1775 |
else |
| 1776 |
goto do_endchar; |
| 1777 |
} |
| 1778 |
#endif |
| 1779 |
} |
| 1780 |
|
| 1781 |
/* Parse Type 2 charstring. */ |
| 1782 |
int t2cParse(long offset, long endOffset, t2cAuxData *aux, unsigned short gid, cff2GlyphCallbacks *cff2, abfGlyphCallbacks *glyph, ctlMemoryCallbacks *mem) { |
| 1783 |
struct _t2cCtx *h; |
| 1784 |
int retVal; |
| 1785 |
|
| 1786 |
h = malloc(sizeof(struct _t2cCtx)); |
| 1787 |
if (h == NULL) { |
| 1788 |
retVal = t2cErrMemory; |
| 1789 |
return retVal; |
| 1790 |
} |
| 1791 |
memset(h, 0, sizeof(struct _t2cCtx)); |
| 1792 |
|
| 1793 |
/* Initialize */ |
| 1794 |
h->flags = PEND_WIDTH | PEND_MASK; |
| 1795 |
if (aux->flags & T2C_IS_CFF2) |
| 1796 |
h->flags |= IS_CFF2; |
| 1797 |
h->stack.cnt = 0; |
| 1798 |
h->stack.blendCnt = 0; |
| 1799 |
memset(h->stack.blendArray, 0, sizeof(h->stack.blendArray)); |
| 1800 |
h->stack.numRegions = 0; |
| 1801 |
h->x = 0; |
| 1802 |
h->y = 0; |
| 1803 |
h->subrDepth = 0; |
| 1804 |
h->stems.cnt = 0; |
| 1805 |
h->mask.state = 0; |
| 1806 |
h->mask.length = 0; |
| 1807 |
h->seac.phase = seacNone; |
| 1808 |
h->aux = aux; |
| 1809 |
h->aux->WV[0] = 0.25f; /* use center of design space by default */ |
| 1810 |
h->aux->WV[1] = 0.25f; |
| 1811 |
h->aux->WV[2] = 0.25f; |
| 1812 |
h->aux->WV[3] = 0.25f; |
| 1813 |
h->glyph = glyph; |
| 1814 |
h->mem = mem; |
| 1815 |
h->LanguageGroup = (glyph->info->flags & ABF_GLYPH_LANG_1) != 0; |
| 1816 |
h->gid = gid; |
| 1817 |
h->cff2 = cff2; |
| 1818 |
memset(&h->BCA, 0, sizeof(h->BCA)); |
| 1819 |
aux->bchar = 0; |
| 1820 |
aux->achar = 0; |
| 1821 |
if (h->flags & IS_CFF2) |
| 1822 |
h->maxOpStack = glyph->info->blendInfo.maxstack; |
| 1823 |
else |
| 1824 |
h->maxOpStack = T2_MAX_OP_STACK; |
| 1825 |
|
| 1826 |
if (aux->flags & T2C_USE_MATRIX) { |
| 1827 |
if ((fabs(1 - aux->matrix[0]) > 0.0001) || |
| 1828 |
(fabs(1 - aux->matrix[3]) > 0.0001) || |
| 1829 |
(aux->matrix[1] != 0) || |
| 1830 |
(aux->matrix[2] != 0) || |
| 1831 |
(aux->matrix[4] != 0) || |
| 1832 |
(aux->matrix[5] != 0)) { |
| 1833 |
int i; |
| 1834 |
h->flags |= USE_MATRIX; |
| 1835 |
h->flags |= USE_GLOBAL_MATRIX; |
| 1836 |
for (i = 0; i < 6; i++) { |
| 1837 |
h->transformMatrix[i] = aux->matrix[i]; |
| 1838 |
} |
| 1839 |
} |
| 1840 |
} |
| 1841 |
if (aux->flags & T2C_FLATTEN_BLEND) { |
| 1842 |
h->flags |= FLATTEN_BLEND; |
| 1843 |
h->flags |= SEEN_BLEND; |
| 1844 |
} |
| 1845 |
h->src.endOffset = endOffset; |
| 1846 |
|
| 1847 |
DURING_EX(h->err.env) |
| 1848 |
|
| 1849 |
retVal = t2Decode(h, offset, 0); |
| 1850 |
|
| 1851 |
HANDLER |
| 1852 |
retVal = Exception.Code; |
| 1853 |
END_HANDLER |
| 1854 |
|
| 1855 |
free(h); |
| 1856 |
|
| 1857 |
return retVal; |
| 1858 |
} |
| 1859 |
|
| 1860 |
/* Get version numbers of libraries. */ |
| 1861 |
void t2cGetVersion(ctlVersionCallbacks *cb) { |
| 1862 |
if (cb->called & 1 << T2C_LIB_ID) |
| 1863 |
return; /* Already enumerated */ |
| 1864 |
|
| 1865 |
/* This library */ |
| 1866 |
cb->getversion(cb, T2C_VERSION, "t2cstr"); |
| 1867 |
|
| 1868 |
/* Record this call */ |
| 1869 |
cb->called |= 1 << T2C_LIB_ID; |
| 1870 |
} |
| 1871 |
|
| 1872 |
/* Convert error code to error string. */ |
| 1873 |
char *t2cErrStr(int err_code) { |
| 1874 |
static char *errstrs[] = |
| 1875 |
{ |
| 1876 |
#undef CTL_DCL_ERR |
| 1877 |
#define CTL_DCL_ERR(name, string) string, |
| 1878 |
#include "t2cerr.h" |
| 1879 |
}; |
| 1880 |
return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code]; |
| 1881 |
} |