| 1 |
#include "charstring-il.h" |
| 2 |
#include "table/glyf.h" |
| 3 |
|
| 4 |
// Glyph building |
| 5 |
static void ensureThereIsSpace(cff_CharstringIL *il) { |
| 6 |
if (il->free) return; |
| 7 |
il->free = 0x100; |
| 8 |
RESIZE(il->instr, il->length + il->free); |
| 9 |
} |
| 10 |
|
| 11 |
void il_push_operand(cff_CharstringIL *il, double x) { |
| 12 |
ensureThereIsSpace(il); |
| 13 |
il->instr[il->length].type = IL_ITEM_OPERAND; |
| 14 |
il->instr[il->length].d = x; |
| 15 |
il->instr[il->length].arity = 0; |
| 16 |
il->length++; |
| 17 |
il->free--; |
| 18 |
} |
| 19 |
static void il_push_VQ(cff_CharstringIL *il, VQ x) { |
| 20 |
il_push_operand(il, iVQ.getStill(x)); |
| 21 |
} |
| 22 |
void il_push_special(cff_CharstringIL *il, int32_t s) { |
| 23 |
ensureThereIsSpace(il); |
| 24 |
il->instr[il->length].type = IL_ITEM_SPECIAL; |
| 25 |
il->instr[il->length].i = s; |
| 26 |
il->instr[il->length].arity = 0; |
| 27 |
il->length++; |
| 28 |
il->free--; |
| 29 |
} |
| 30 |
void il_push_op(cff_CharstringIL *il, int32_t op) { |
| 31 |
ensureThereIsSpace(il); |
| 32 |
il->instr[il->length].type = IL_ITEM_OPERATOR; |
| 33 |
il->instr[il->length].i = op; |
| 34 |
il->instr[il->length].arity = cff_getStandardArity(op); |
| 35 |
il->length++; |
| 36 |
il->free--; |
| 37 |
} |
| 38 |
static void il_moveto(cff_CharstringIL *il, VQ dx, VQ dy) { |
| 39 |
il_push_VQ(il, dx); |
| 40 |
il_push_VQ(il, dy); |
| 41 |
il_push_op(il, op_rmoveto); |
| 42 |
} |
| 43 |
static void il_lineto(cff_CharstringIL *il, VQ dx, VQ dy) { |
| 44 |
il_push_VQ(il, dx); |
| 45 |
il_push_VQ(il, dy); |
| 46 |
il_push_op(il, op_rlineto); |
| 47 |
} |
| 48 |
static void il_curveto(cff_CharstringIL *il, VQ dx1, VQ dy1, VQ dx2, VQ dy2, VQ dx3, VQ dy3) { |
| 49 |
il_push_VQ(il, dx1); |
| 50 |
il_push_VQ(il, dy1); |
| 51 |
il_push_VQ(il, dx2); |
| 52 |
il_push_VQ(il, dy2); |
| 53 |
il_push_VQ(il, dx3); |
| 54 |
il_push_VQ(il, dy3); |
| 55 |
il_push_op(il, op_rrcurveto); |
| 56 |
} |
| 57 |
|
| 58 |
static void _il_push_maskgroup(cff_CharstringIL *il, // il seq |
| 59 |
glyf_MaskList *masks, // masks array |
| 60 |
uint16_t contours, // contous drawn |
| 61 |
uint16_t points, // points drawn |
| 62 |
uint16_t nh, uint16_t nv, // quantity of stems |
| 63 |
uint16_t *jm, // index of cur mask |
| 64 |
int32_t op) { // mask operator |
| 65 |
shapeid_t n = masks->length; |
| 66 |
while (*jm < n && (masks->items[*jm].contoursBefore < contours || |
| 67 |
(masks->items[*jm].contoursBefore == contours && |
| 68 |
masks->items[*jm].pointsBefore <= points))) { |
| 69 |
il_push_op(il, op); |
| 70 |
uint8_t maskByte = 0; |
| 71 |
uint8_t bits = 0; |
| 72 |
for (uint16_t j = 0; j < nh; j++) { |
| 73 |
maskByte = maskByte << 1 | (masks->items[*jm].maskH[j] & 1); |
| 74 |
bits += 1; |
| 75 |
if (bits == 8) { |
| 76 |
il_push_special(il, maskByte); |
| 77 |
bits = 0; |
| 78 |
} |
| 79 |
} |
| 80 |
for (uint16_t j = 0; j < nv; j++) { |
| 81 |
maskByte = maskByte << 1 | (masks->items[*jm].maskV[j] & 1); |
| 82 |
bits += 1; |
| 83 |
if (bits == 8) { |
| 84 |
il_push_special(il, maskByte); |
| 85 |
bits = 0; |
| 86 |
} |
| 87 |
} |
| 88 |
if (bits) { |
| 89 |
maskByte = maskByte << (8 - bits); |
| 90 |
il_push_special(il, maskByte); |
| 91 |
} |
| 92 |
*jm += 1; |
| 93 |
} |
| 94 |
} |
| 95 |
static void il_push_masks(cff_CharstringIL *il, glyf_Glyph *g, // meta |
| 96 |
uint16_t contours, // contours sofar |
| 97 |
uint16_t points, // points sofar |
| 98 |
uint16_t *jh, // index of pushed cmasks |
| 99 |
uint16_t *jm // index of pushed hmasks |
| 100 |
) { |
| 101 |
if (!g->stemH.length && !g->stemV.length) return; |
| 102 |
_il_push_maskgroup(il, &g->contourMasks, contours, points, // |
| 103 |
g->stemH.length, g->stemV.length, jh, op_cntrmask); |
| 104 |
_il_push_maskgroup(il, &g->hintMasks, contours, points, // |
| 105 |
g->stemH.length, g->stemV.length, jm, op_hintmask); |
| 106 |
} |
| 107 |
|
| 108 |
static void _il_push_stemgroup(cff_CharstringIL *il, // il seq |
| 109 |
glyf_StemDefList *stems, // stem array |
| 110 |
bool hasmask, bool haswidth, int32_t ophm, int32_t oph) { |
| 111 |
if (!stems || !stems->length) return; |
| 112 |
pos_t ref = 0; |
| 113 |
uint16_t nn = haswidth ? 1 : 0; |
| 114 |
for (uint16_t j = 0; j < stems->length; j++) { |
| 115 |
il_push_operand(il, stems->items[j].position - ref); |
| 116 |
il_push_operand(il, stems->items[j].width); |
| 117 |
ref = stems->items[j].position + stems->items[j].width; |
| 118 |
nn++; |
| 119 |
if (nn >= type2_argument_stack) { |
| 120 |
if (hasmask) { |
| 121 |
il_push_op(il, op_hstemhm); |
| 122 |
} else { |
| 123 |
il_push_op(il, op_hstem); |
| 124 |
} |
| 125 |
il->instr[il->length - 1].arity = nn; |
| 126 |
nn = 0; |
| 127 |
} |
| 128 |
} |
| 129 |
if (hasmask) { |
| 130 |
il_push_op(il, ophm); |
| 131 |
} else { |
| 132 |
il_push_op(il, oph); |
| 133 |
} |
| 134 |
il->instr[il->length - 1].arity = nn; |
| 135 |
} |
| 136 |
static void il_push_stems(cff_CharstringIL *il, glyf_Glyph *g, bool hasmask, bool haswidth) { |
| 137 |
_il_push_stemgroup(il, &g->stemH, hasmask, haswidth, op_hstemhm, op_hstem); |
| 138 |
_il_push_stemgroup(il, &g->stemV, hasmask, haswidth, op_vstemhm, op_vstem); |
| 139 |
} |
| 140 |
cff_CharstringIL *cff_compileGlyphToIL(glyf_Glyph *g, uint16_t defaultWidth, |
| 141 |
uint16_t nominalWidth) { |
| 142 |
cff_CharstringIL *il; |
| 143 |
NEW(il); |
| 144 |
// Convert absolute positions to deltas |
| 145 |
glyf_Contour *tempContours = NULL; |
| 146 |
{ |
| 147 |
VQ x = iVQ.neutral(); |
| 148 |
VQ y = iVQ.neutral(); |
| 149 |
NEW(tempContours, g->contours.length); |
| 150 |
for (uint16_t c = 0; c < g->contours.length; c++) { |
| 151 |
glyf_Contour *contour = &(g->contours.items[c]); |
| 152 |
glyf_Contour *newcontour = &(tempContours[c]); |
| 153 |
glyf_iContour.init(newcontour); |
| 154 |
for (shapeid_t j = 0; j < contour->length; j++) { |
| 155 |
glyf_iContour.push(newcontour, glyf_iPoint.dup(contour->items[j])); |
| 156 |
} |
| 157 |
|
| 158 |
if (newcontour->length > 2 && !newcontour->items[newcontour->length - 1].onCurve) { |
| 159 |
// Duplicate first point for proper CurveTo generation |
| 160 |
glyf_iContour.push(newcontour, glyf_iPoint.dup(newcontour->items[0])); |
| 161 |
} |
| 162 |
|
| 163 |
for (shapeid_t j = 0; j < newcontour->length; j++) { |
| 164 |
VQ dx = iVQ.minus(newcontour->items[j].x, x); |
| 165 |
VQ dy = iVQ.minus(newcontour->items[j].y, y); |
| 166 |
iVQ.copyReplace(&x, newcontour->items[j].x); |
| 167 |
iVQ.copyReplace(&y, newcontour->items[j].y); |
| 168 |
iVQ.replace(&newcontour->items[j].x, dx); |
| 169 |
iVQ.replace(&newcontour->items[j].y, dy); |
| 170 |
} |
| 171 |
} |
| 172 |
iVQ.dispose(&x); |
| 173 |
iVQ.dispose(&y); |
| 174 |
} |
| 175 |
|
| 176 |
bool hasmask = |
| 177 |
g->hintMasks.length || g->contourMasks.length; // we have hint masks or contour masks |
| 178 |
const pos_t glyphADWConst = iVQ.getStill(g->advanceWidth); |
| 179 |
bool haswidth = glyphADWConst != defaultWidth; // we have width operand here |
| 180 |
// Write IL |
| 181 |
if (haswidth) { il_push_operand(il, (int)(glyphADWConst) - (int)(nominalWidth)); } |
| 182 |
il_push_stems(il, g, hasmask, haswidth); |
| 183 |
// Write contour |
| 184 |
shapeid_t contoursSofar = 0; |
| 185 |
shapeid_t pointsSofar = 0; |
| 186 |
shapeid_t jh = 0; |
| 187 |
shapeid_t jm = 0; |
| 188 |
if (hasmask) il_push_masks(il, g, contoursSofar, pointsSofar, &jh, &jm); |
| 189 |
for (shapeid_t c = 0; c < g->contours.length; c++) { |
| 190 |
glyf_Contour *contour = &(tempContours[c]); |
| 191 |
shapeid_t n = contour->length; |
| 192 |
if (n == 0) continue; |
| 193 |
il_moveto(il, contour->items[0].x, contour->items[0].y); |
| 194 |
pointsSofar++; |
| 195 |
if (hasmask) il_push_masks(il, g, contoursSofar, pointsSofar, &jh, &jm); |
| 196 |
// TODO: Generate BLENDs |
| 197 |
for (shapeid_t j = 1; j < n; j++) { |
| 198 |
if (contour->items[j].onCurve) { // A line-to |
| 199 |
il_lineto(il, contour->items[j].x, contour->items[j].y); |
| 200 |
pointsSofar += 1; |
| 201 |
} else if (j < n - 2 // have enough points |
| 202 |
&& !contour->items[j + 1].onCurve // next is offcurve |
| 203 |
&& contour->items[j + 2].onCurve // and next is oncurve |
| 204 |
) { // means this is an bezier curve strand |
| 205 |
il_curveto(il, contour->items[j].x, |
| 206 |
contour->items[j].y, // dz1 |
| 207 |
contour->items[j + 1].x, |
| 208 |
contour->items[j + 1].y, // dz2 |
| 209 |
contour->items[j + 2].x, |
| 210 |
contour->items[j + 2].y); // dz3 |
| 211 |
pointsSofar += 3; |
| 212 |
j += 2; |
| 213 |
} else { // invalid offcurve, treat as oncurve |
| 214 |
il_lineto(il, contour->items[j].x, contour->items[j].y); |
| 215 |
pointsSofar++; |
| 216 |
} |
| 217 |
if (hasmask) il_push_masks(il, g, contoursSofar, pointsSofar, &jh, &jm); |
| 218 |
} |
| 219 |
contoursSofar += 1; |
| 220 |
pointsSofar = 0; |
| 221 |
} |
| 222 |
il_push_op(il, op_endchar); |
| 223 |
// delete temp contour array |
| 224 |
for (shapeid_t c = 0; c < g->contours.length; c++) { |
| 225 |
glyf_iContour.dispose(&tempContours[c]); |
| 226 |
} |
| 227 |
FREE(tempContours); |
| 228 |
return il; |
| 229 |
} |
| 230 |
|
| 231 |
// Pattern-based peephole optimization |
| 232 |
static bool il_matchtype(cff_CharstringIL *il, uint32_t j, uint32_t k, cff_InstructionType t) { |
| 233 |
if (k >= il->length) return false; |
| 234 |
for (uint32_t m = j; m < k; m++) { |
| 235 |
if (il->instr[m].type != t) return false; |
| 236 |
} |
| 237 |
return true; |
| 238 |
} |
| 239 |
static bool il_matchop(cff_CharstringIL *il, uint32_t j, int32_t op) { |
| 240 |
if (il->instr[j].type != IL_ITEM_OPERATOR) return false; |
| 241 |
if (il->instr[j].i != op) return false; |
| 242 |
return true; |
| 243 |
} |
| 244 |
static uint8_t zroll(cff_CharstringIL *il, uint32_t j, int32_t op, int32_t op2, ...) { |
| 245 |
uint8_t arity = cff_getStandardArity(op); |
| 246 |
if (arity > 16 || j + arity >= il->length) return 0; |
| 247 |
if ((j == 0 || // We are at the beginning of charstring |
| 248 |
!il_matchtype(il, j - 1, j, |
| 249 |
IL_ITEM_PHANTOM_OPERATOR)) // .. or we are right after a solid operator |
| 250 |
&& il_matchop(il, j + arity, op) // The next operator is <op> |
| 251 |
&& il_matchtype(il, j, j + arity, IL_ITEM_OPERAND) // And we have correct number of operands |
| 252 |
) { |
| 253 |
va_list ap; |
| 254 |
uint8_t check = true; |
| 255 |
uint8_t resultArity = arity; |
| 256 |
bool mask[16]; |
| 257 |
va_start(ap, op2); |
| 258 |
for (uint32_t m = 0; m < arity; m++) { |
| 259 |
int checkzero = va_arg(ap, int); |
| 260 |
mask[m] = checkzero; |
| 261 |
if (checkzero) { |
| 262 |
resultArity -= 1; |
| 263 |
check = check && il->instr[j + m].d == 0; |
| 264 |
} |
| 265 |
} |
| 266 |
va_end(ap); |
| 267 |
if (check) { |
| 268 |
for (uint32_t m = 0; m < arity; m++) { |
| 269 |
if (mask[m]) { il->instr[j + m].type = IL_ITEM_PHANTOM_OPERAND; } |
| 270 |
} |
| 271 |
il->instr[j + arity].i = op2; |
| 272 |
il->instr[j + arity].arity = resultArity; |
| 273 |
return arity; |
| 274 |
} else { |
| 275 |
return 0; |
| 276 |
} |
| 277 |
} else { |
| 278 |
return 0; |
| 279 |
} |
| 280 |
} |
| 281 |
static uint8_t opop_roll(cff_CharstringIL *il, uint32_t j, int32_t op1, int32_t arity, int32_t op2, |
| 282 |
int32_t resultop) { |
| 283 |
if (j + 1 + arity >= il->length) return 0; |
| 284 |
cff_CharstringInstruction *current = &(il->instr[j]); |
| 285 |
cff_CharstringInstruction *nextop = &(il->instr[j + 1 + arity]); |
| 286 |
if (il_matchop(il, j, op1) // match this operator |
| 287 |
&& il_matchtype(il, j + 1, j + 1 + arity, IL_ITEM_OPERAND) // match operands |
| 288 |
&& il_matchop(il, j + 1 + arity, op2) // match next operator |
| 289 |
&& current->arity + nextop->arity <= type2_argument_stack // stack is not full |
| 290 |
) { |
| 291 |
current->type = IL_ITEM_PHANTOM_OPERATOR; |
| 292 |
nextop->i = resultop; |
| 293 |
nextop->arity += current->arity; |
| 294 |
return arity + 1; |
| 295 |
} else { |
| 296 |
return 0; |
| 297 |
} |
| 298 |
} |
| 299 |
static uint8_t hvlineto_roll(cff_CharstringIL *il, uint32_t j) { |
| 300 |
if (j + 3 >= il->length) return 0; |
| 301 |
cff_CharstringInstruction *current = &(il->instr[j]); |
| 302 |
// We will check whether operand <checkdelta> is zero |
| 303 |
// ODD EVEN -- current arity |
| 304 |
// hlineto X Y |
| 305 |
// vlineto Y X |
| 306 |
uint32_t checkdelta = ((bool)(current->arity & 1) ^ (bool)(current->i == op_vlineto) ? 1 : 2); |
| 307 |
if ((il_matchop(il, j, op_hlineto) || il_matchop(il, j, op_vlineto)) // a hlineto/vlineto |
| 308 |
&& il_matchop(il, j + 3, op_rlineto) // followed by a lineto |
| 309 |
&& il_matchtype(il, j + 1, j + 3, IL_ITEM_OPERAND) // have enough operands |
| 310 |
&& il->instr[j + checkdelta].d == 0 // and it is a h/v |
| 311 |
&& current->arity + 1 <= type2_argument_stack // we have enough stack space |
| 312 |
) { |
| 313 |
il->instr[j + checkdelta].type = IL_ITEM_PHANTOM_OPERAND; |
| 314 |
il->instr[j].type = IL_ITEM_PHANTOM_OPERATOR; |
| 315 |
il->instr[j + 3].i = current->i; |
| 316 |
il->instr[j + 3].arity = current->arity + 1; |
| 317 |
return 3; |
| 318 |
} else { |
| 319 |
return 0; |
| 320 |
} |
| 321 |
} |
| 322 |
static uint8_t hvvhcurve_roll(cff_CharstringIL *il, uint32_t j) { |
| 323 |
if (!il_matchop(il, j, op_hvcurveto) && !il_matchop(il, j, op_vhcurveto)) return 0; |
| 324 |
cff_CharstringInstruction *current = &(il->instr[j]); |
| 325 |
// Exit in case of array not long enough or we have already ended |
| 326 |
if (j + 7 >= il->length || current->arity & 1) return 0; |
| 327 |
bool hvcase = (bool)((current->arity >> 2) & 1) ^ (bool)(current->i == op_hvcurveto); |
| 328 |
// We will check whether operand <checkdelta> is zero |
| 329 |
// ODD EVEN -- current arity divided by 4 |
| 330 |
// hvcurveto X Y |
| 331 |
// vhcurveto Y X |
| 332 |
uint32_t checkdelta1 = hvcase ? 2 : 1; |
| 333 |
uint32_t checkdelta2 = hvcase ? 5 : 6; |
| 334 |
if (il_matchop(il, j + 7, op_rrcurveto) // followed by a curveto |
| 335 |
&& il_matchtype(il, j + 1, j + 7, IL_ITEM_OPERAND) // have enough operands |
| 336 |
&& il->instr[j + checkdelta1].d == 0 // and it is a h/v |
| 337 |
) { |
| 338 |
if (il->instr[j + checkdelta2].d == 0 && current->arity + 4 <= type2_argument_stack) { |
| 339 |
// The Standard case |
| 340 |
il->instr[j + checkdelta1].type = IL_ITEM_PHANTOM_OPERAND; |
| 341 |
il->instr[j + checkdelta2].type = IL_ITEM_PHANTOM_OPERAND; |
| 342 |
il->instr[j].type = IL_ITEM_PHANTOM_OPERATOR; |
| 343 |
il->instr[j + 7].i = current->i; |
| 344 |
il->instr[j + 7].arity = current->arity + 4; |
| 345 |
return 7; |
| 346 |
} else if (current->arity + 5 <= type2_argument_stack) { |
| 347 |
// The trailing case |
| 348 |
il->instr[j + checkdelta1].type = IL_ITEM_PHANTOM_OPERAND; |
| 349 |
il->instr[j].type = IL_ITEM_PHANTOM_OPERATOR; |
| 350 |
il->instr[j + 7].i = current->i; |
| 351 |
il->instr[j + 7].arity = current->arity + 5; |
| 352 |
if (hvcase) { |
| 353 |
// Swap the last two operands because hvcurveto's trailing operand is in y-x order |
| 354 |
double t = il->instr[j + 5].d; |
| 355 |
il->instr[j + 5].d = il->instr[j + 6].d; |
| 356 |
il->instr[j + 6].d = t; |
| 357 |
} |
| 358 |
return 7; |
| 359 |
} else { |
| 360 |
return 0; |
| 361 |
} |
| 362 |
} else { |
| 363 |
return 0; |
| 364 |
} |
| 365 |
} |
| 366 |
static uint8_t hhvvcurve_roll(cff_CharstringIL *il, uint32_t j) { |
| 367 |
if (!il_matchop(il, j, op_hhcurveto) && !il_matchop(il, j, op_vvcurveto)) return 0; |
| 368 |
cff_CharstringInstruction *current = &(il->instr[j]); |
| 369 |
// Exit in case of array not long enough or we have already ended |
| 370 |
if (j + 7 >= il->length) return 0; |
| 371 |
bool hh = current->i == op_hhcurveto; |
| 372 |
uint32_t checkdelta1 = hh ? 2 : 1; |
| 373 |
uint32_t checkdelta2 = hh ? 6 : 5; |
| 374 |
if (il_matchop(il, j + 7, op_rrcurveto) // followed by a curveto |
| 375 |
&& il_matchtype(il, j + 1, j + 7, IL_ITEM_OPERAND) // have enough operands |
| 376 |
&& il->instr[j + checkdelta1].d == 0 // and it is a h/v |
| 377 |
&& il->instr[j + checkdelta2].d == 0 // and it is a h/v |
| 378 |
&& current->arity + 4 <= type2_argument_stack) { |
| 379 |
il->instr[j + checkdelta1].type = IL_ITEM_PHANTOM_OPERAND; |
| 380 |
il->instr[j + checkdelta2].type = IL_ITEM_PHANTOM_OPERAND; |
| 381 |
il->instr[j].type = IL_ITEM_PHANTOM_OPERATOR; |
| 382 |
il->instr[j + 7].i = current->i; |
| 383 |
il->instr[j + 7].arity = current->arity + 4; |
| 384 |
return 7; |
| 385 |
} else { |
| 386 |
return 0; |
| 387 |
} |
| 388 |
} |
| 389 |
static uint32_t nextstop(cff_CharstringIL *il, uint32_t j) { |
| 390 |
uint32_t delta = 0; |
| 391 |
for (; j + delta < il->length && il->instr[j + delta].type == IL_ITEM_OPERAND; delta++) |
| 392 |
; |
| 393 |
return delta; |
| 394 |
} |
| 395 |
#define ROLL_FALL(x) \ |
| 396 |
if ((r = (x))) return r; |
| 397 |
static uint8_t decideAdvance(cff_CharstringIL *il, uint32_t j, uint8_t optimizeLevel) { |
| 398 |
uint8_t r = 0; |
| 399 |
ROLL_FALL(zroll(il, j, op_rlineto, op_hlineto, 0, 1)); // rlineto -> hlineto |
| 400 |
ROLL_FALL(zroll(il, j, op_rlineto, op_vlineto, 1, 0)); // rlineto -> vlineto |
| 401 |
ROLL_FALL(zroll(il, j, op_rmoveto, op_hmoveto, 0, 1)); // rmoveto -> hmoveto |
| 402 |
ROLL_FALL(zroll(il, j, op_rmoveto, op_vmoveto, 1, 0)); // rmoveto -> vmoveto |
| 403 |
ROLL_FALL(zroll(il, j, op_rrcurveto, op_hvcurveto, 0, 1, 0, 0, 1, 0)); // rrcurveto->hvcurveto |
| 404 |
ROLL_FALL(zroll(il, j, op_rrcurveto, op_vhcurveto, 1, 0, 0, 0, 0, 1)); // rrcurveto->vhcurveto |
| 405 |
ROLL_FALL(zroll(il, j, op_rrcurveto, op_hhcurveto, 0, 1, 0, 0, 0, 1)); // rrcurveto->hhcurveto |
| 406 |
ROLL_FALL(zroll(il, j, op_rrcurveto, op_vvcurveto, 1, 0, 0, 0, 1, 0)); // rrcurveto->vvcurveto |
| 407 |
ROLL_FALL(opop_roll(il, j, op_rrcurveto, 6, op_rrcurveto, op_rrcurveto)); // rrcurveto roll |
| 408 |
ROLL_FALL(opop_roll(il, j, op_rrcurveto, 2, op_rlineto, op_rcurveline)); // rcurveline roll |
| 409 |
ROLL_FALL(opop_roll(il, j, op_rlineto, 6, op_rrcurveto, op_rlinecurve)); // rlinecurve roll |
| 410 |
ROLL_FALL(opop_roll(il, j, op_rlineto, 2, op_rlineto, op_rlineto)); // rlineto roll |
| 411 |
ROLL_FALL(opop_roll(il, j, op_hstemhm, 0, op_hintmask, op_hintmask)); // hintmask roll |
| 412 |
ROLL_FALL(opop_roll(il, j, op_vstemhm, 0, op_hintmask, op_hintmask)); // hintmask roll |
| 413 |
ROLL_FALL(opop_roll(il, j, op_hstemhm, 0, op_cntrmask, op_cntrmask)); // cntrmask roll |
| 414 |
ROLL_FALL(opop_roll(il, j, op_vstemhm, 0, op_cntrmask, op_cntrmask)); // cntrmask roll |
| 415 |
ROLL_FALL(hvlineto_roll(il, j)); // hlineto-vlineto roll |
| 416 |
ROLL_FALL(hhvvcurve_roll(il, j)); // hhcurveto-vvcurveto roll |
| 417 |
ROLL_FALL(hvvhcurve_roll(il, j)); // hvcurveto-vhcurveto roll |
| 418 |
ROLL_FALL(nextstop(il, j)); // move to next stop for operand match |
| 419 |
return 1; // nothing match |
| 420 |
} |
| 421 |
|
| 422 |
void cff_optimizeIL(cff_CharstringIL *il, const otfcc_Options *options) { |
| 423 |
if (!options->cff_rollCharString) return; |
| 424 |
uint32_t j = 0; |
| 425 |
while (j < il->length) { |
| 426 |
j += decideAdvance(il, j, options->cff_rollCharString); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
// IL to buffer conversion |
| 431 |
caryll_Buffer *cff_build_IL(cff_CharstringIL *il) { |
| 432 |
caryll_Buffer *blob = bufnew(); |
| 433 |
|
| 434 |
for (uint16_t j = 0; j < il->length; j++) { |
| 435 |
switch (il->instr[j].type) { |
| 436 |
case IL_ITEM_OPERAND: { |
| 437 |
cff_mergeCS2Operand(blob, il->instr[j].d); |
| 438 |
break; |
| 439 |
} |
| 440 |
case IL_ITEM_OPERATOR: { |
| 441 |
cff_mergeCS2Operator(blob, il->instr[j].i); |
| 442 |
break; |
| 443 |
} |
| 444 |
case IL_ITEM_SPECIAL: { |
| 445 |
cff_mergeCS2Special(blob, il->instr[j].i); |
| 446 |
break; |
| 447 |
} |
| 448 |
default: |
| 449 |
break; |
| 450 |
} |
| 451 |
} |
| 452 |
return blob; |
| 453 |
} |
| 454 |
|
| 455 |
cff_CharstringIL *cff_shrinkIL(cff_CharstringIL *il) { |
| 456 |
cff_CharstringIL *out; |
| 457 |
NEW(out); |
| 458 |
for (uint16_t j = 0; j < il->length; j++) { |
| 459 |
switch (il->instr[j].type) { |
| 460 |
case IL_ITEM_OPERAND: { |
| 461 |
il_push_operand(out, il->instr[j].d); |
| 462 |
break; |
| 463 |
} |
| 464 |
case IL_ITEM_OPERATOR: { |
| 465 |
il_push_op(out, il->instr[j].i); |
| 466 |
break; |
| 467 |
} |
| 468 |
case IL_ITEM_SPECIAL: { |
| 469 |
il_push_special(out, il->instr[j].i); |
| 470 |
break; |
| 471 |
} |
| 472 |
default: |
| 473 |
break; |
| 474 |
} |
| 475 |
} |
| 476 |
return out; |
| 477 |
} |
| 478 |
|
| 479 |
void cff_ILmergeIL(cff_CharstringIL *self, cff_CharstringIL *il) { |
| 480 |
for (uint16_t j = 0; j < il->length; j++) { |
| 481 |
switch (il->instr[j].type) { |
| 482 |
case IL_ITEM_OPERAND: { |
| 483 |
il_push_operand(self, il->instr[j].d); |
| 484 |
break; |
| 485 |
} |
| 486 |
case IL_ITEM_OPERATOR: { |
| 487 |
il_push_op(self, il->instr[j].i); |
| 488 |
break; |
| 489 |
} |
| 490 |
case IL_ITEM_SPECIAL: { |
| 491 |
il_push_special(self, il->instr[j].i); |
| 492 |
break; |
| 493 |
} |
| 494 |
default: |
| 495 |
break; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
bool instruction_eq(cff_CharstringInstruction *z1, cff_CharstringInstruction *z2) { |
| 501 |
if (z1->type == z2->type) { |
| 502 |
if (z1->type == IL_ITEM_OPERAND || z1->type == IL_ITEM_PHANTOM_OPERAND) { |
| 503 |
return z1->d == z2->d; |
| 504 |
} else { |
| 505 |
return z1->i == z2->i; |
| 506 |
} |
| 507 |
} else { |
| 508 |
return false; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
bool cff_ilEqual(cff_CharstringIL *a, cff_CharstringIL *b) { |
| 513 |
if (!a || !b) return false; |
| 514 |
if (a->length != b->length) return false; |
| 515 |
for (uint32_t j = 0; j < a->length; j++) |
| 516 |
if (!instruction_eq(a->instr + j, b->instr + j)) { return false; } |
| 517 |
return true; |
| 518 |
} |