| 1 |
/* |
| 2 |
Parser/Linter of CFF font, notable references: |
| 3 |
* Technical Note #5176: The Compact Font Format Specification |
| 4 |
* Technical Note #5177: The Type 2 Charstring Format |
| 5 |
* Adobe TinTin |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <math.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "libcff.h" |
| 15 |
|
| 16 |
static void parse_encoding(cff_File *cff, int32_t offset, cff_Encoding *enc) { |
| 17 |
uint8_t *data = cff->raw_data; |
| 18 |
|
| 19 |
if (offset == cff_ENC_STANDARD) |
| 20 |
enc->t = cff_ENC_STANDARD; |
| 21 |
else if (offset == cff_ENC_EXPERT) |
| 22 |
enc->t = cff_ENC_EXPERT; |
| 23 |
else { |
| 24 |
switch (data[offset]) { |
| 25 |
case 0: |
| 26 |
enc->t = cff_ENC_FORMAT0; |
| 27 |
{ |
| 28 |
enc->f0.format = 0; |
| 29 |
enc->f0.ncodes = data[offset + 1]; |
| 30 |
NEW(enc->f0.code, enc->f0.ncodes); |
| 31 |
|
| 32 |
for (uint32_t i = 0; i < enc->f0.ncodes; i++) |
| 33 |
enc->f0.code[i] = data[offset + 2 + i]; |
| 34 |
} |
| 35 |
break; |
| 36 |
case 1: |
| 37 |
enc->t = cff_ENC_FORMAT1; |
| 38 |
{ |
| 39 |
enc->f1.format = 1; |
| 40 |
enc->f1.nranges = data[offset + 1]; |
| 41 |
NEW(enc->f1.range1, enc->f1.nranges); |
| 42 |
|
| 43 |
for (uint32_t i = 0; i < enc->f1.nranges; i++) |
| 44 |
enc->f1.range1[i].first = data[offset + 2 + i * 2], |
| 45 |
enc->f1.range1[i].nleft = data[offset + 3 + i * 2]; |
| 46 |
} |
| 47 |
break; |
| 48 |
default: |
| 49 |
enc->t = cff_ENC_FORMAT_SUPPLEMENT; |
| 50 |
{ |
| 51 |
enc->ns.nsup = data[offset]; |
| 52 |
NEW(enc->ns.supplement, enc->ns.nsup); |
| 53 |
|
| 54 |
for (uint32_t i = 0; i < enc->ns.nsup; i++) |
| 55 |
enc->ns.supplement[i].code = data[offset + 1 + i * 3], |
| 56 |
enc->ns.supplement[i].glyph = gu2(data, offset + 2 + i * 3); |
| 57 |
} |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void parse_cff_bytecode(cff_File *cff, const otfcc_Options *options) { |
| 64 |
uint32_t pos; |
| 65 |
int32_t offset; |
| 66 |
|
| 67 |
/* Header */ |
| 68 |
cff->head.major = gu1(cff->raw_data, 0); |
| 69 |
cff->head.minor = gu1(cff->raw_data, 1); |
| 70 |
cff->head.hdrSize = gu1(cff->raw_data, 2); |
| 71 |
cff->head.offSize = gu1(cff->raw_data, 3); |
| 72 |
|
| 73 |
/* Name INDEX */ |
| 74 |
pos = cff->head.hdrSize; |
| 75 |
cff_iIndex.parse(cff->raw_data, pos, &cff->name); |
| 76 |
|
| 77 |
/* Top Dict INDEX */ |
| 78 |
pos = 4 + cff_iIndex.getLength(&cff->name); |
| 79 |
cff_iIndex.parse(cff->raw_data, pos, &cff->top_dict); |
| 80 |
|
| 81 |
/** LINT CFF FONTSET **/ |
| 82 |
|
| 83 |
if (cff->name.count != cff->top_dict.count) |
| 84 |
logWarning("[libcff] Bad CFF font: (%d, name), (%d, top_dict).\n", cff->name.count, |
| 85 |
cff->top_dict.count); |
| 86 |
|
| 87 |
/* String INDEX */ |
| 88 |
pos = 4 + cff_iIndex.getLength(&cff->name) + cff_iIndex.getLength(&cff->top_dict); |
| 89 |
cff_iIndex.parse(cff->raw_data, pos, &cff->string); |
| 90 |
|
| 91 |
/* Global Subr INDEX */ |
| 92 |
pos = 4 + cff_iIndex.getLength(&cff->name) + cff_iIndex.getLength(&cff->top_dict) + |
| 93 |
cff_iIndex.getLength(&cff->string); |
| 94 |
cff_iIndex.parse(cff->raw_data, pos, &cff->global_subr); |
| 95 |
|
| 96 |
if (cff->top_dict.data != NULL) { |
| 97 |
int32_t offset; |
| 98 |
|
| 99 |
// cff->topdict.dict = calloc(1, sizeof(cff_Dict)); |
| 100 |
// cff->topdict.dict = cff_extract_Dict(cff->top_dict.data, cff->top_dict.offset[1] - |
| 101 |
// cff->top_dict.offset[0]); |
| 102 |
|
| 103 |
/* CharStrings INDEX */ |
| 104 |
offset = |
| 105 |
cff_iDict |
| 106 |
.parseDictKey(cff->top_dict.data, cff->top_dict.offset[1] - cff->top_dict.offset[0], |
| 107 |
op_CharStrings, 0) |
| 108 |
.i; |
| 109 |
|
| 110 |
if (offset != -1) { |
| 111 |
cff_iIndex.parse(cff->raw_data, offset, &cff->char_strings); |
| 112 |
cff->cnt_glyph = cff->char_strings.count; |
| 113 |
} else { |
| 114 |
cff_iIndex.empty(&cff->char_strings); |
| 115 |
logWarning("[libcff] Bad CFF font: no any glyph data.\n"); |
| 116 |
} |
| 117 |
|
| 118 |
/* Encodings */ |
| 119 |
offset = |
| 120 |
cff_iDict |
| 121 |
.parseDictKey(cff->top_dict.data, cff->top_dict.offset[1] - cff->top_dict.offset[0], |
| 122 |
op_Encoding, 0) |
| 123 |
.i; |
| 124 |
|
| 125 |
if (offset != -1) { |
| 126 |
parse_encoding(cff, offset, &cff->encodings); |
| 127 |
} else { |
| 128 |
cff->encodings.t = cff_ENC_UNSPECED; |
| 129 |
} |
| 130 |
|
| 131 |
/* Charsets */ |
| 132 |
offset = cff_iDict |
| 133 |
.parseDictKey(cff->top_dict.data, |
| 134 |
cff->top_dict.offset[1] - cff->top_dict.offset[0], op_charset, 0) |
| 135 |
.i; |
| 136 |
|
| 137 |
if (offset != -1) { |
| 138 |
cff_extract_Charset(cff->raw_data, offset, cff->char_strings.count, &cff->charsets); |
| 139 |
} else { |
| 140 |
cff->charsets.t = cff_CHARSET_UNSPECED; |
| 141 |
} |
| 142 |
|
| 143 |
/* FDSelect */ |
| 144 |
offset = |
| 145 |
cff_iDict |
| 146 |
.parseDictKey(cff->top_dict.data, cff->top_dict.offset[1] - cff->top_dict.offset[0], |
| 147 |
op_FDSelect, 0) |
| 148 |
.i; |
| 149 |
|
| 150 |
if (cff->char_strings.count && offset != -1) { |
| 151 |
cff_extract_FDSelect(cff->raw_data, offset, cff->char_strings.count, &cff->fdselect); |
| 152 |
} else { |
| 153 |
cff->fdselect.t = cff_FDSELECT_UNSPECED; |
| 154 |
} |
| 155 |
|
| 156 |
/* Font Dict INDEX */ |
| 157 |
offset = cff_iDict |
| 158 |
.parseDictKey(cff->top_dict.data, |
| 159 |
cff->top_dict.offset[1] - cff->top_dict.offset[0], op_FDArray, 0) |
| 160 |
.i; |
| 161 |
|
| 162 |
if (offset != -1) { |
| 163 |
cff_iIndex.parse(cff->raw_data, offset, &cff->font_dict); |
| 164 |
} else { |
| 165 |
cff_iIndex.empty(&cff->font_dict); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/* Private and Local Subr */ |
| 170 |
{ |
| 171 |
int32_t private_len = -1; |
| 172 |
int32_t private_off = -1; |
| 173 |
|
| 174 |
if (cff->top_dict.data != NULL) { |
| 175 |
private_len = |
| 176 |
cff_iDict |
| 177 |
.parseDictKey(cff->top_dict.data, |
| 178 |
cff->top_dict.offset[1] - cff->top_dict.offset[0], op_Private, 0) |
| 179 |
.i; |
| 180 |
private_off = |
| 181 |
cff_iDict |
| 182 |
.parseDictKey(cff->top_dict.data, |
| 183 |
cff->top_dict.offset[1] - cff->top_dict.offset[0], op_Private, 1) |
| 184 |
.i; |
| 185 |
} |
| 186 |
|
| 187 |
if (private_off != -1 && private_len != -1) { |
| 188 |
offset = |
| 189 |
cff_iDict.parseDictKey(cff->raw_data + private_off, private_len, op_Subrs, 0).i; |
| 190 |
|
| 191 |
if (offset != -1) |
| 192 |
cff_iIndex.parse(cff->raw_data, private_off + offset, &cff->local_subr); |
| 193 |
else |
| 194 |
cff_iIndex.empty(&cff->local_subr); |
| 195 |
} else { |
| 196 |
cff_iIndex.empty(&cff->local_subr); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
cff_File *cff_openStream(uint8_t *data, uint32_t len, const otfcc_Options *options) { |
| 202 |
cff_File *file; |
| 203 |
NEW(file); |
| 204 |
|
| 205 |
NEW(file->raw_data, len); |
| 206 |
memcpy(file->raw_data, data, len); |
| 207 |
file->raw_length = len; |
| 208 |
file->cnt_glyph = 0; |
| 209 |
parse_cff_bytecode(file, options); |
| 210 |
|
| 211 |
return file; |
| 212 |
} |
| 213 |
|
| 214 |
void cff_close(cff_File *file) { |
| 215 |
if (file != NULL) { |
| 216 |
if (file->raw_data != NULL) FREE(file->raw_data); |
| 217 |
|
| 218 |
cff_iIndex.dispose(&file->name); |
| 219 |
cff_iIndex.dispose(&file->top_dict); |
| 220 |
cff_iIndex.dispose(&file->string); |
| 221 |
cff_iIndex.dispose(&file->global_subr); |
| 222 |
cff_iIndex.dispose(&file->char_strings); |
| 223 |
cff_iIndex.dispose(&file->font_dict); |
| 224 |
cff_iIndex.dispose(&file->local_subr); |
| 225 |
|
| 226 |
switch (file->encodings.t) { |
| 227 |
case cff_ENC_STANDARD: |
| 228 |
case cff_ENC_EXPERT: |
| 229 |
case cff_ENC_UNSPECED: |
| 230 |
break; |
| 231 |
case cff_ENC_FORMAT0: |
| 232 |
if (file->encodings.f0.code != NULL) FREE(file->encodings.f0.code); |
| 233 |
break; |
| 234 |
case cff_ENC_FORMAT1: |
| 235 |
if (file->encodings.f1.range1 != NULL) FREE(file->encodings.f1.range1); |
| 236 |
break; |
| 237 |
case cff_ENC_FORMAT_SUPPLEMENT: |
| 238 |
if (file->encodings.ns.supplement != NULL) FREE(file->encodings.ns.supplement); |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
cff_close_Charset(file->charsets); |
| 243 |
cff_close_FDSelect(file->fdselect); |
| 244 |
|
| 245 |
FREE(file); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
uint8_t cff_parseSubr(uint16_t idx, uint8_t *raw, cff_Index fdarray, cff_FDSelect select, |
| 250 |
cff_Index *subr) { |
| 251 |
uint8_t fd = 0; |
| 252 |
int32_t off_private, len_private; |
| 253 |
int32_t off_subr; |
| 254 |
|
| 255 |
switch (select.t) { |
| 256 |
case cff_FDSELECT_FORMAT0: |
| 257 |
fd = select.f0.fds[idx]; |
| 258 |
break; |
| 259 |
case cff_FDSELECT_FORMAT3: |
| 260 |
for (int i = 0; i < select.f3.nranges - 1; i++) |
| 261 |
if (idx >= select.f3.range3[i].first && idx < select.f3.range3[i + 1].first) |
| 262 |
fd = select.f3.range3[i].fd; |
| 263 |
if (idx >= select.f3.range3[select.f3.nranges - 1].first && idx < select.f3.sentinel) |
| 264 |
fd = select.f3.range3[select.f3.nranges - 1].fd; |
| 265 |
break; |
| 266 |
case cff_FDSELECT_UNSPECED: |
| 267 |
fd = 0; |
| 268 |
break; |
| 269 |
} |
| 270 |
|
| 271 |
off_private = cff_iDict |
| 272 |
.parseDictKey(fdarray.data + fdarray.offset[fd] - 1, |
| 273 |
fdarray.offset[fd + 1] - fdarray.offset[fd], op_Private, 1) |
| 274 |
.i; |
| 275 |
len_private = cff_iDict |
| 276 |
.parseDictKey(fdarray.data + fdarray.offset[fd] - 1, |
| 277 |
fdarray.offset[fd + 1] - fdarray.offset[fd], op_Private, 0) |
| 278 |
.i; |
| 279 |
|
| 280 |
if (off_private != -1 && len_private != -1) { |
| 281 |
off_subr = cff_iDict.parseDictKey(raw + off_private, len_private, op_Subrs, 0).i; |
| 282 |
|
| 283 |
if (off_subr != -1) { |
| 284 |
cff_iIndex.parse(raw, off_private + off_subr, subr); |
| 285 |
} else { |
| 286 |
cff_iIndex.empty(subr); |
| 287 |
} |
| 288 |
} else { |
| 289 |
cff_iIndex.empty(subr); |
| 290 |
} |
| 291 |
|
| 292 |
return fd; |
| 293 |
} |
| 294 |
|
| 295 |
static inline uint16_t compute_subr_bias(uint16_t cnt) { |
| 296 |
if (cnt < 1240) |
| 297 |
return 107; |
| 298 |
else if (cnt < 33900) |
| 299 |
return 1131; |
| 300 |
else |
| 301 |
return 32768; |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
CharString program: |
| 306 |
w? {hs* vs* cm* hm* mt subpath}? {mt subpath}* endchar |
| 307 |
*/ |
| 308 |
static void reverseStack(cff_Stack *stack, uint8_t left, uint8_t right) { |
| 309 |
cff_Value *p1 = stack->stack + left; |
| 310 |
cff_Value *p2 = stack->stack + right; |
| 311 |
while (p1 < p2) { |
| 312 |
cff_Value temp = *p1; |
| 313 |
*p1 = *p2; |
| 314 |
*p2 = temp; |
| 315 |
p1++; |
| 316 |
p2--; |
| 317 |
} |
| 318 |
} |
| 319 |
static void callback_nopSetWidth(void *context, double width) {} |
| 320 |
static void callback_nopNewContour(void *context) {} |
| 321 |
static void callback_nopLineTo(void *context, double x1, double y1) {} |
| 322 |
static void callback_nopCurveTo(void *context, double x1, double y1, double x2, double y2, |
| 323 |
double x3, double y3) {} |
| 324 |
static void callback_nopsetHint(void *context, bool isVertical, double position, double width) {} |
| 325 |
static void callback_nopsetMask(void *context, bool isContourMask, bool *mask) { |
| 326 |
FREE(mask); |
| 327 |
} |
| 328 |
static double callback_nopgetrand(void *context) { |
| 329 |
return 0; |
| 330 |
} |
| 331 |
#define CHECK_STACK_TOP(op, n) \ |
| 332 |
{ \ |
| 333 |
if (stack->index < n) { \ |
| 334 |
logWarning("[libcff] Stack cannot provide enough parameters for %s (%04x). This " \ |
| 335 |
"operation is ignored.\n", \ |
| 336 |
#op, op); \ |
| 337 |
break; \ |
| 338 |
} \ |
| 339 |
} |
| 340 |
|
| 341 |
// CFF charastring parser |
| 342 |
void cff_parseOutline(uint8_t *data, uint32_t len, cff_Index gsubr, cff_Index lsubr, |
| 343 |
cff_Stack *stack, void *outline, cff_IOutlineBuilder methods, |
| 344 |
const otfcc_Options *options) { |
| 345 |
uint16_t gsubr_bias = compute_subr_bias(gsubr.count); |
| 346 |
uint16_t lsubr_bias = compute_subr_bias(lsubr.count); |
| 347 |
uint8_t *start = data; |
| 348 |
uint32_t advance, i, cnt_bezier; |
| 349 |
cff_Value val; |
| 350 |
|
| 351 |
void (*setWidth)(void *context, double width) = methods.setWidth; |
| 352 |
void (*newContour)(void *context) = methods.newContour; |
| 353 |
void (*lineTo)(void *context, double x1, double y1) = methods.lineTo; |
| 354 |
void (*curveTo)(void *context, double x1, double y1, double x2, double y2, double x3, |
| 355 |
double y3) = methods.curveTo; |
| 356 |
void (*setHint)(void *context, bool isVertical, double position, double width) = |
| 357 |
methods.setHint; |
| 358 |
void (*setMask)(void *context, bool isContourMask, bool *mask) = methods.setMask; |
| 359 |
double (*getrand)(void *context) = methods.getrand; |
| 360 |
|
| 361 |
if (!setWidth) setWidth = callback_nopSetWidth; |
| 362 |
if (!newContour) newContour = callback_nopNewContour; |
| 363 |
if (!lineTo) lineTo = callback_nopLineTo; |
| 364 |
if (!curveTo) curveTo = callback_nopCurveTo; |
| 365 |
if (!setHint) setHint = callback_nopsetHint; |
| 366 |
if (!setMask) setMask = callback_nopsetMask; |
| 367 |
if (!getrand) getrand = callback_nopgetrand; |
| 368 |
|
| 369 |
while (start < data + len) { |
| 370 |
advance = cff_decodeCS2Token(start, &val); |
| 371 |
|
| 372 |
switch (val.t) { |
| 373 |
case CS2_OPERATOR: |
| 374 |
switch (val.i) { |
| 375 |
case op_hstem: |
| 376 |
case op_vstem: |
| 377 |
case op_hstemhm: |
| 378 |
case op_vstemhm: |
| 379 |
if (stack->index % 2) setWidth(outline, stack->stack[0].d); |
| 380 |
stack->stem += stack->index >> 1; |
| 381 |
double hintBase = 0; |
| 382 |
for (uint16_t j = stack->index % 2; j < stack->index; j += 2) { |
| 383 |
double pos = stack->stack[j].d; |
| 384 |
double width = stack->stack[j + 1].d; |
| 385 |
setHint(outline, (val.i == op_vstem || val.i == op_vstemhm), |
| 386 |
pos + hintBase, width); |
| 387 |
hintBase += pos + width; |
| 388 |
} |
| 389 |
stack->index = 0; |
| 390 |
break; |
| 391 |
case op_hintmask: |
| 392 |
case op_cntrmask: { |
| 393 |
if (stack->index % 2) setWidth(outline, stack->stack[0].d); |
| 394 |
bool isVertical = stack->stem > 0; |
| 395 |
stack->stem += stack->index >> 1; |
| 396 |
double hintBase = 0; |
| 397 |
for (uint16_t j = stack->index % 2; j < stack->index; j += 2) { |
| 398 |
double pos = stack->stack[j].d; |
| 399 |
double width = stack->stack[j + 1].d; |
| 400 |
setHint(outline, isVertical, pos + hintBase, width); |
| 401 |
hintBase += pos + width; |
| 402 |
} |
| 403 |
uint32_t maskLength = (stack->stem + 7) >> 3; |
| 404 |
bool *mask; |
| 405 |
NEW(mask, stack->stem + 7); |
| 406 |
for (uint32_t byte = 0; byte < maskLength; byte++) { |
| 407 |
uint8_t maskByte = start[advance + byte]; |
| 408 |
mask[(byte << 3) + 0] = maskByte >> 7 & 1; |
| 409 |
mask[(byte << 3) + 1] = maskByte >> 6 & 1; |
| 410 |
mask[(byte << 3) + 2] = maskByte >> 5 & 1; |
| 411 |
mask[(byte << 3) + 3] = maskByte >> 4 & 1; |
| 412 |
mask[(byte << 3) + 4] = maskByte >> 3 & 1; |
| 413 |
mask[(byte << 3) + 5] = maskByte >> 2 & 1; |
| 414 |
mask[(byte << 3) + 6] = maskByte >> 1 & 1; |
| 415 |
mask[(byte << 3) + 7] = maskByte >> 0 & 1; |
| 416 |
} |
| 417 |
setMask(outline, (val.i == op_cntrmask), mask); |
| 418 |
advance += maskLength; |
| 419 |
stack->index = 0; |
| 420 |
break; |
| 421 |
} |
| 422 |
|
| 423 |
case op_vmoveto: { |
| 424 |
CHECK_STACK_TOP(op_vmoveto, 1); |
| 425 |
if (stack->index > 1) setWidth(outline, stack->stack[stack->index - 2].d); |
| 426 |
newContour(outline); |
| 427 |
lineTo(outline, 0.0, stack->stack[stack->index - 1].d); |
| 428 |
stack->index = 0; |
| 429 |
break; |
| 430 |
} |
| 431 |
case op_rmoveto: { |
| 432 |
CHECK_STACK_TOP(op_rmoveto, 2); |
| 433 |
if (stack->index > 2) setWidth(outline, stack->stack[stack->index - 3].d); |
| 434 |
newContour(outline); |
| 435 |
lineTo(outline, stack->stack[stack->index - 2].d, |
| 436 |
stack->stack[stack->index - 1].d); |
| 437 |
stack->index = 0; |
| 438 |
break; |
| 439 |
} |
| 440 |
case op_hmoveto: { |
| 441 |
CHECK_STACK_TOP(op_hmoveto, 1); |
| 442 |
if (stack->index > 1) setWidth(outline, stack->stack[stack->index - 2].d); |
| 443 |
newContour(outline); |
| 444 |
lineTo(outline, stack->stack[stack->index - 1].d, 0.0); |
| 445 |
stack->index = 0; |
| 446 |
break; |
| 447 |
} |
| 448 |
case op_endchar: { |
| 449 |
if (stack->index > 0) setWidth(outline, stack->stack[stack->index - 1].d); |
| 450 |
break; |
| 451 |
} |
| 452 |
case op_rlineto: { |
| 453 |
for (i = 0; i < stack->index; i += 2) |
| 454 |
lineTo(outline, stack->stack[i].d, stack->stack[i + 1].d); |
| 455 |
stack->index = 0; |
| 456 |
break; |
| 457 |
} |
| 458 |
case op_vlineto: { |
| 459 |
if (stack->index % 2 == 1) { |
| 460 |
lineTo(outline, 0.0, stack->stack[0].d); |
| 461 |
for (i = 1; i < stack->index; i += 2) { |
| 462 |
lineTo(outline, stack->stack[i].d, 0.0); |
| 463 |
lineTo(outline, 0.0, stack->stack[i + 1].d); |
| 464 |
} |
| 465 |
} else { |
| 466 |
for (i = 0; i < stack->index; i += 2) { |
| 467 |
lineTo(outline, 0.0, stack->stack[i].d); |
| 468 |
lineTo(outline, stack->stack[i + 1].d, 0.0); |
| 469 |
} |
| 470 |
} |
| 471 |
stack->index = 0; |
| 472 |
break; |
| 473 |
} |
| 474 |
case op_hlineto: { |
| 475 |
if (stack->index % 2 == 1) { |
| 476 |
lineTo(outline, stack->stack[0].d, 0.0); |
| 477 |
for (i = 1; i < stack->index; i += 2) { |
| 478 |
lineTo(outline, 0.0, stack->stack[i].d); |
| 479 |
lineTo(outline, stack->stack[i + 1].d, 0.0); |
| 480 |
} |
| 481 |
} else { |
| 482 |
for (i = 0; i < stack->index; i += 2) { |
| 483 |
lineTo(outline, stack->stack[i].d, 0.0); |
| 484 |
lineTo(outline, 0.0, stack->stack[i + 1].d); |
| 485 |
} |
| 486 |
} |
| 487 |
stack->index = 0; |
| 488 |
break; |
| 489 |
} |
| 490 |
case op_rrcurveto: { |
| 491 |
for (i = 0; i < stack->index; i += 6) |
| 492 |
curveTo(outline, stack->stack[i].d, stack->stack[i + 1].d, |
| 493 |
stack->stack[i + 2].d, stack->stack[i + 3].d, |
| 494 |
stack->stack[i + 4].d, stack->stack[i + 5].d); |
| 495 |
stack->index = 0; |
| 496 |
break; |
| 497 |
} |
| 498 |
case op_rcurveline: { |
| 499 |
for (i = 0; i < stack->index - 2; i += 6) |
| 500 |
curveTo(outline, stack->stack[i].d, stack->stack[i + 1].d, |
| 501 |
stack->stack[i + 2].d, stack->stack[i + 3].d, |
| 502 |
stack->stack[i + 4].d, stack->stack[i + 5].d); |
| 503 |
lineTo(outline, stack->stack[stack->index - 2].d, |
| 504 |
stack->stack[stack->index - 1].d); |
| 505 |
stack->index = 0; |
| 506 |
break; |
| 507 |
} |
| 508 |
case op_rlinecurve: { |
| 509 |
for (i = 0; i < stack->index - 6; i += 2) |
| 510 |
lineTo(outline, stack->stack[i].d, stack->stack[i + 1].d); |
| 511 |
curveTo(outline, stack->stack[stack->index - 6].d, |
| 512 |
stack->stack[stack->index - 5].d, stack->stack[stack->index - 4].d, |
| 513 |
stack->stack[stack->index - 3].d, stack->stack[stack->index - 2].d, |
| 514 |
stack->stack[stack->index - 1].d); |
| 515 |
stack->index = 0; |
| 516 |
break; |
| 517 |
} |
| 518 |
case op_vvcurveto: { |
| 519 |
if (stack->index % 4 == 1) { |
| 520 |
curveTo(outline, stack->stack[0].d, stack->stack[1].d, |
| 521 |
stack->stack[2].d, stack->stack[3].d, 0.0, stack->stack[4].d); |
| 522 |
for (i = 5; i < stack->index; i += 4) |
| 523 |
curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, |
| 524 |
stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); |
| 525 |
} else { |
| 526 |
for (i = 0; i < stack->index; i += 4) |
| 527 |
curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, |
| 528 |
stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); |
| 529 |
} |
| 530 |
stack->index = 0; |
| 531 |
break; |
| 532 |
} |
| 533 |
case op_hhcurveto: { |
| 534 |
if (stack->index % 4 == 1) { |
| 535 |
curveTo(outline, stack->stack[1].d, stack->stack[0].d, |
| 536 |
stack->stack[2].d, stack->stack[3].d, stack->stack[4].d, 0.0); |
| 537 |
for (i = 5; i < stack->index; i += 4) |
| 538 |
curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, |
| 539 |
stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); |
| 540 |
} else { |
| 541 |
for (i = 0; i < stack->index; i += 4) |
| 542 |
curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, |
| 543 |
stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); |
| 544 |
} |
| 545 |
stack->index = 0; |
| 546 |
break; |
| 547 |
} |
| 548 |
case op_vhcurveto: { |
| 549 |
if (stack->index % 4 == 1) |
| 550 |
cnt_bezier = (stack->index - 5) / 4; |
| 551 |
else |
| 552 |
cnt_bezier = (stack->index / 4); |
| 553 |
|
| 554 |
for (i = 0; i < 4 * cnt_bezier; i += 4) { |
| 555 |
if ((i / 4) % 2 == 0) |
| 556 |
curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, |
| 557 |
stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); |
| 558 |
else |
| 559 |
curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, |
| 560 |
stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); |
| 561 |
} |
| 562 |
if (stack->index % 8 == 5) { |
| 563 |
curveTo( |
| 564 |
outline, 0.0, stack->stack[stack->index - 5].d, |
| 565 |
stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, |
| 566 |
stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); |
| 567 |
} |
| 568 |
if (stack->index % 8 == 1) { |
| 569 |
curveTo( |
| 570 |
outline, stack->stack[stack->index - 5].d, 0.0, |
| 571 |
stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, |
| 572 |
stack->stack[stack->index - 1].d, stack->stack[stack->index - 2].d); |
| 573 |
} |
| 574 |
stack->index = 0; |
| 575 |
break; |
| 576 |
} |
| 577 |
case op_hvcurveto: { |
| 578 |
if (stack->index % 4 == 1) |
| 579 |
cnt_bezier = (stack->index - 5) / 4; |
| 580 |
else |
| 581 |
cnt_bezier = (stack->index / 4); |
| 582 |
|
| 583 |
for (i = 0; i < 4 * cnt_bezier; i += 4) { |
| 584 |
if ((i / 4) % 2 == 0) |
| 585 |
curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, |
| 586 |
stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); |
| 587 |
else |
| 588 |
curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, |
| 589 |
stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); |
| 590 |
} |
| 591 |
|
| 592 |
if (stack->index % 8 == 5) { |
| 593 |
curveTo( |
| 594 |
outline, stack->stack[stack->index - 5].d, 0.0, |
| 595 |
stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, |
| 596 |
stack->stack[stack->index - 1].d, stack->stack[stack->index - 2].d); |
| 597 |
} |
| 598 |
if (stack->index % 8 == 1) { |
| 599 |
curveTo( |
| 600 |
outline, 0.0, stack->stack[stack->index - 5].d, |
| 601 |
stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, |
| 602 |
stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); |
| 603 |
} |
| 604 |
stack->index = 0; |
| 605 |
break; |
| 606 |
} |
| 607 |
case op_hflex: { |
| 608 |
CHECK_STACK_TOP(op_hflex, 7); |
| 609 |
curveTo(outline, stack->stack[0].d, 0.0, stack->stack[1].d, |
| 610 |
stack->stack[2].d, stack->stack[3].d, 0.0); |
| 611 |
curveTo(outline, stack->stack[4].d, 0.0, stack->stack[5].d, |
| 612 |
-stack->stack[2].d, stack->stack[6].d, 0.0); |
| 613 |
stack->index = 0; |
| 614 |
break; |
| 615 |
} |
| 616 |
case op_flex: { |
| 617 |
CHECK_STACK_TOP(op_flex, 12); |
| 618 |
curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, |
| 619 |
stack->stack[3].d, stack->stack[4].d, stack->stack[5].d); |
| 620 |
curveTo(outline, stack->stack[6].d, stack->stack[7].d, stack->stack[8].d, |
| 621 |
stack->stack[9].d, stack->stack[10].d, stack->stack[11].d); |
| 622 |
stack->index = 0; |
| 623 |
break; |
| 624 |
} |
| 625 |
case op_hflex1: { |
| 626 |
CHECK_STACK_TOP(op_hflex1, 9); |
| 627 |
curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, |
| 628 |
stack->stack[3].d, stack->stack[4].d, 0.0); |
| 629 |
curveTo(outline, stack->stack[5].d, 0.0, stack->stack[6].d, |
| 630 |
stack->stack[7].d, stack->stack[8].d, |
| 631 |
-(stack->stack[1].d + stack->stack[3].d + stack->stack[7].d)); |
| 632 |
stack->index = 0; |
| 633 |
break; |
| 634 |
} |
| 635 |
case op_flex1: { |
| 636 |
CHECK_STACK_TOP(op_flex1, 11); |
| 637 |
double dx = stack->stack[0].d + stack->stack[2].d + stack->stack[4].d + |
| 638 |
stack->stack[6].d + stack->stack[8].d; |
| 639 |
double dy = stack->stack[1].d + stack->stack[3].d + stack->stack[5].d + |
| 640 |
stack->stack[7].d + stack->stack[9].d; |
| 641 |
if (fabs(dx) > fabs(dy)) { |
| 642 |
dx = stack->stack[10].d; |
| 643 |
dy = -dy; |
| 644 |
} else { |
| 645 |
dx = -dx; |
| 646 |
dy = stack->stack[10].d; |
| 647 |
} |
| 648 |
curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, |
| 649 |
stack->stack[3].d, stack->stack[4].d, stack->stack[5].d); |
| 650 |
curveTo(outline, stack->stack[6].d, stack->stack[7].d, stack->stack[8].d, |
| 651 |
stack->stack[9].d, dx, dy); |
| 652 |
stack->index = 0; |
| 653 |
break; |
| 654 |
} |
| 655 |
case op_and: { |
| 656 |
CHECK_STACK_TOP(op_and, 2); |
| 657 |
double num1 = stack->stack[stack->index - 1].d; |
| 658 |
double num2 = stack->stack[stack->index - 2].d; |
| 659 |
stack->stack[stack->index - 2].d = (num1 && num2) ? 1.0 : 0.0; |
| 660 |
stack->index -= 1; |
| 661 |
break; |
| 662 |
} |
| 663 |
case op_or: { |
| 664 |
CHECK_STACK_TOP(op_or, 2); |
| 665 |
double num1 = stack->stack[stack->index - 1].d; |
| 666 |
double num2 = stack->stack[stack->index - 2].d; |
| 667 |
stack->stack[stack->index - 2].d = (num1 || num2) ? 1.0 : 0.0; |
| 668 |
stack->index -= 1; |
| 669 |
break; |
| 670 |
} |
| 671 |
case op_not: { |
| 672 |
CHECK_STACK_TOP(op_not, 1); |
| 673 |
double num = stack->stack[stack->index - 1].d; |
| 674 |
stack->stack[stack->index - 1].d = num ? 0.0 : 1.0; |
| 675 |
break; |
| 676 |
} |
| 677 |
case op_abs: { |
| 678 |
CHECK_STACK_TOP(op_abs, 1); |
| 679 |
double num = stack->stack[stack->index - 1].d; |
| 680 |
stack->stack[stack->index - 1].d = (num < 0.0) ? -num : num; |
| 681 |
break; |
| 682 |
} |
| 683 |
case op_add: { |
| 684 |
CHECK_STACK_TOP(op_add, 2); |
| 685 |
double num1 = stack->stack[stack->index - 1].d; |
| 686 |
double num2 = stack->stack[stack->index - 2].d; |
| 687 |
stack->stack[stack->index - 2].d = num1 + num2; |
| 688 |
stack->index -= 1; |
| 689 |
break; |
| 690 |
} |
| 691 |
case op_sub: { |
| 692 |
CHECK_STACK_TOP(op_sub, 2); |
| 693 |
double num1 = stack->stack[stack->index - 2].d; |
| 694 |
double num2 = stack->stack[stack->index - 1].d; |
| 695 |
stack->stack[stack->index - 2].d = num1 - num2; |
| 696 |
stack->index -= 1; |
| 697 |
break; |
| 698 |
} |
| 699 |
case op_div: { |
| 700 |
CHECK_STACK_TOP(op_div, 2); |
| 701 |
double num1 = stack->stack[stack->index - 2].d; |
| 702 |
double num2 = stack->stack[stack->index - 1].d; |
| 703 |
stack->stack[stack->index - 2].d = num1 / num2; |
| 704 |
stack->index -= 1; |
| 705 |
break; |
| 706 |
} |
| 707 |
case op_neg: { |
| 708 |
CHECK_STACK_TOP(op_neg, 1); |
| 709 |
double num = stack->stack[stack->index - 1].d; |
| 710 |
stack->stack[stack->index - 1].d = -num; |
| 711 |
break; |
| 712 |
} |
| 713 |
case op_eq: { |
| 714 |
CHECK_STACK_TOP(op_eq, 2); |
| 715 |
double num1 = stack->stack[stack->index - 1].d; |
| 716 |
double num2 = stack->stack[stack->index - 2].d; |
| 717 |
stack->stack[stack->index - 2].d = (num1 == num2) ? 1.0 : 0.0; |
| 718 |
stack->index -= 1; |
| 719 |
break; |
| 720 |
} |
| 721 |
case op_drop: { |
| 722 |
CHECK_STACK_TOP(op_drop, 1); |
| 723 |
stack->index -= 1; |
| 724 |
break; |
| 725 |
} |
| 726 |
case op_put: { |
| 727 |
CHECK_STACK_TOP(op_put, 2); |
| 728 |
double val = stack->stack[stack->index - 2].d; |
| 729 |
int32_t i = (int32_t)stack->stack[stack->index - 1].d; |
| 730 |
stack->transient[i % type2_transient_array].d = val; |
| 731 |
stack->index -= 2; |
| 732 |
break; |
| 733 |
} |
| 734 |
case op_get: { |
| 735 |
CHECK_STACK_TOP(op_get, 1); |
| 736 |
int32_t i = (int32_t)stack->stack[stack->index - 1].d; |
| 737 |
stack->stack[stack->index - 1].d = |
| 738 |
stack->transient[i % type2_transient_array].d; |
| 739 |
break; |
| 740 |
} |
| 741 |
case op_ifelse: { |
| 742 |
CHECK_STACK_TOP(op_ifelse, 4); |
| 743 |
double v2 = stack->stack[stack->index - 1].d; |
| 744 |
double v1 = stack->stack[stack->index - 2].d; |
| 745 |
double s2 = stack->stack[stack->index - 3].d; |
| 746 |
double s1 = stack->stack[stack->index - 4].d; |
| 747 |
stack->stack[stack->index - 4].d = (v1 <= v2) ? s1 : s2; |
| 748 |
stack->index -= 3; |
| 749 |
break; |
| 750 |
} |
| 751 |
case op_random: { |
| 752 |
// Chosen from a fair dice |
| 753 |
// TODO: use a real randomizer |
| 754 |
stack->stack[stack->index].t = cff_DOUBLE; |
| 755 |
stack->stack[stack->index].d = getrand(outline); |
| 756 |
stack->index += 1; |
| 757 |
break; |
| 758 |
} |
| 759 |
case op_mul: { |
| 760 |
CHECK_STACK_TOP(op_mul, 2); |
| 761 |
double num1 = stack->stack[stack->index - 1].d; |
| 762 |
double num2 = stack->stack[stack->index - 2].d; |
| 763 |
stack->stack[stack->index - 2].d = num1 * num2; |
| 764 |
stack->index -= 1; |
| 765 |
break; |
| 766 |
} |
| 767 |
case op_sqrt: { |
| 768 |
CHECK_STACK_TOP(op_sqrt, 1); |
| 769 |
double num = stack->stack[stack->index - 1].d; |
| 770 |
stack->stack[stack->index - 1].d = sqrt(num); |
| 771 |
break; |
| 772 |
} |
| 773 |
case op_dup: { |
| 774 |
CHECK_STACK_TOP(op_dup, 1); |
| 775 |
stack->stack[stack->index] = stack->stack[stack->index - 1]; |
| 776 |
stack->index += 1; |
| 777 |
break; |
| 778 |
} |
| 779 |
case op_exch: { |
| 780 |
CHECK_STACK_TOP(op_exch, 2); |
| 781 |
double num1 = stack->stack[stack->index - 1].d; |
| 782 |
double num2 = stack->stack[stack->index - 2].d; |
| 783 |
stack->stack[stack->index - 1].d = num2; |
| 784 |
stack->stack[stack->index - 2].d = num1; |
| 785 |
break; |
| 786 |
} |
| 787 |
case op_index: { |
| 788 |
CHECK_STACK_TOP(op_index, 2); |
| 789 |
uint8_t n = stack->index - 1; |
| 790 |
uint8_t j = n - 1 - (uint8_t)(stack->stack[n].d) % n; |
| 791 |
stack->stack[n] = stack->stack[j]; |
| 792 |
break; |
| 793 |
} |
| 794 |
case op_roll: { |
| 795 |
CHECK_STACK_TOP(op_roll, 2); |
| 796 |
int32_t j = stack->stack[stack->index - 1].d; |
| 797 |
uint32_t n = stack->stack[stack->index - 2].d; |
| 798 |
CHECK_STACK_TOP(op_roll, 2 + n); |
| 799 |
j = -j % n; |
| 800 |
if (j < 0) j += n; |
| 801 |
if (!j) break; |
| 802 |
uint8_t last = stack->index - 3; |
| 803 |
uint8_t first = stack->index - 2 - n; |
| 804 |
|
| 805 |
reverseStack(stack, first, last); |
| 806 |
reverseStack(stack, last - j + 1, last); |
| 807 |
reverseStack(stack, first, last - j); |
| 808 |
stack->index -= 2; |
| 809 |
break; |
| 810 |
} |
| 811 |
case op_return: |
| 812 |
return; |
| 813 |
case op_callsubr: { |
| 814 |
CHECK_STACK_TOP(op_callsubr, 1); |
| 815 |
uint32_t subr = (uint32_t)stack->stack[--(stack->index)].d; |
| 816 |
cff_parseOutline(lsubr.data + lsubr.offset[lsubr_bias + subr] - 1, |
| 817 |
lsubr.offset[lsubr_bias + subr + 1] - |
| 818 |
lsubr.offset[lsubr_bias + subr], |
| 819 |
gsubr, lsubr, stack, outline, methods, options); |
| 820 |
break; |
| 821 |
} |
| 822 |
case op_callgsubr: { |
| 823 |
CHECK_STACK_TOP(op_callgsubr, 1); |
| 824 |
uint32_t subr = (uint32_t)stack->stack[--(stack->index)].d; |
| 825 |
cff_parseOutline(gsubr.data + gsubr.offset[gsubr_bias + subr] - 1, |
| 826 |
gsubr.offset[gsubr_bias + subr + 1] - |
| 827 |
gsubr.offset[gsubr_bias + subr], |
| 828 |
gsubr, lsubr, stack, outline, methods, options); |
| 829 |
break; |
| 830 |
} |
| 831 |
default: { |
| 832 |
logWarning("Warning: unknown operator %d occurs in Type 2 CharString. It " |
| 833 |
"may caused by file corruption.", |
| 834 |
val.i); |
| 835 |
return; |
| 836 |
} |
| 837 |
} |
| 838 |
break; |
| 839 |
case CS2_OPERAND: |
| 840 |
case CS2_FRACTION: |
| 841 |
stack->stack[(stack->index)++] = val; |
| 842 |
break; |
| 843 |
} |
| 844 |
|
| 845 |
start += advance; |
| 846 |
} |
| 847 |
} |