1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gstype2.c 8250 2007-09-25 13:31:24Z giles $ */
15
/* Adobe Type 2 charstring interpreter */
32
/* NOTE: The following are not yet implemented:
33
* Registry items other than 0
34
* Counter masks (but they are parsed correctly)
38
/* ------ Internal routines ------ */
41
* Set the character width. This is provided as an optional extra operand
42
* on the stack for the first operator. After setting the width, we remove
43
* the extra operand, and back up the interpreter pointer so we will
44
* re-execute the operator when control re-enters the interpreter.
46
#define check_first_operator(explicit_width)\
48
if ( pcis->init_done < 0 )\
49
{ ipsp->ip = cip, ipsp->dstate = state;\
50
return type2_sbw(pcis, csp, cstack, ipsp, explicit_width);\
54
type2_sbw(gs_type1_state * pcis, cs_ptr csp, cs_ptr cstack, ip_state_t * ipsp,
57
t1_hinter *h = &pcis->h;
58
fixed sbx = fixed_0, sby = fixed_0, wx, wy = fixed_0;
62
wx = cstack[0] + pcis->pfont->data.nominalWidthX;
63
memmove(cstack, cstack + 1, (csp - cstack) * sizeof(*cstack));
66
wx = pcis->pfont->data.defaultWidthX;
67
if (pcis->seac_accent < 0) {
69
pcis->origin_offset.x = pcis->lsb.x - sbx;
70
pcis->origin_offset.y = pcis->lsb.y - sby;
74
if (pcis->width_set) {
79
code = t1_hinter__sbw(h, sbx, sby, wx, wy);
82
gs_type1_sbw(pcis, fixed_0, fixed_0, wx, fixed_0);
83
/* Back up the interpretation pointer. */
85
decrypt_skip_previous(*ipsp->ip, ipsp->dstate);
86
/* Save the interpreter state. */
87
pcis->os_count = csp + 1 - cstack;
88
pcis->ips_count = ipsp - &pcis->ipstack[0] + 1;
89
memcpy(pcis->ostack, cstack, pcis->os_count * sizeof(cstack[0]));
90
if (pcis->init_done < 0) { /* Finish init when we return. */
93
return type1_result_sbw;
96
type2_vstem(gs_type1_state * pcis, cs_ptr csp, cs_ptr cstack)
100
t1_hinter *h = &pcis->h;
103
for (ap = cstack; ap + 1 <= csp; x += ap[1], ap += 2) {
104
code = t1_hinter__vstem(h, x += ap[0], ap[1]);
108
pcis->num_hints += (csp + 1 - cstack) >> 1;
112
/* ------ Main interpreter ------ */
115
* Continue interpreting a Type 2 charstring. If str != 0, it is taken as
116
* the byte string to interpret. Return 0 on successful completion, <0 on
117
* error, or >0 when client intervention is required (or allowed). The int*
118
* argument is only for compatibility with the Type 1 charstring interpreter.
121
gs_type2_interpret(gs_type1_state * pcis, const gs_glyph_data_t *pgd,
124
gs_font_type1 *pfont = pcis->pfont;
125
gs_type1_data *pdata = &pfont->data;
126
t1_hinter *h = &pcis->h;
127
bool encrypted = pdata->lenIV >= 0;
128
fixed cstack[ostack_size];
130
#define clear CLEAR_CSTACK(cstack, csp)
131
ip_state_t *ipsp = &pcis->ipstack[pcis->ips_count - 1];
132
register const byte *cip;
133
register crypt_state state;
139
/****** FAKE THE REGISTRY ******/
145
Registry[0].values = pcis->pfont->data.WeightVector.values;
147
switch (pcis->init_done) {
149
t1_hinter__init(h, pcis->path);
152
gs_type1_finish_init(pcis); /* sets origin */
153
code = t1_hinter__set_mapping(h, &pcis->pis->ctm,
154
&pfont->FontMatrix, &pfont->base->FontMatrix,
155
pcis->scale.x.log2_unit, pcis->scale.x.log2_unit,
156
pcis->scale.x.log2_unit - pcis->log2_subpixels.x,
157
pcis->scale.y.log2_unit - pcis->log2_subpixels.y,
158
pcis->origin.x, pcis->origin.y,
159
gs_currentaligntopixels(pfont->dir));
162
code = t1_hinter__set_font_data(h, 2, pdata, pcis->no_grid_fitting,
163
pcis->pfont->is_resource);
167
default /*case 1 */ :
170
INIT_CSTACK(cstack, csp, pcis);
174
ipsp->cs_data = *pgd;
175
cip = pgd->bits.data;
176
call:state = crypt_charstring_seed;
178
int skip = pdata->lenIV;
180
/* Skip initial random bytes */
181
for (; skip > 0; ++cip, --skip)
182
decrypt_skip_next(*cip, state);
186
state = ipsp->dstate;
190
charstring_next(c0, state, c, encrypted);
192
/* This is a number, decode it and push it on the stack. */
194
if (c < c_pos2_0) { /* 1-byte number */
195
decode_push_num1(csp, cstack, c);
196
} else if (c < cx_num4) { /* 2-byte number */
197
decode_push_num2(csp, cstack, c, cip, state, encrypted);
198
} else if (c == cx_num4) { /* 4-byte number */
201
decode_num4(lw, cip, state, encrypted);
202
/* 32-bit numbers are 16:16. */
203
CS_CHECK_PUSH(csp, cstack);
204
*++csp = arith_rshift(lw, 16 - _fixed_shift);
205
} else /* not possible */
206
return_error(gs_error_invalidfont);
207
pushed:if_debug3('1', "[1]%d: (%d) %f\n",
208
(int)(csp - cstack), c, fixed2float(*csp));
213
static const char *const c2names[] =
214
{char2_command_names};
217
dlprintf2("[1]0x%lx: %02x??\n", (ulong) (cip - 1), c);
219
dlprintf3("[1]0x%lx: %02x %s\n", (ulong) (cip - 1), c,
223
switch ((char_command) c) {
224
#define cnext clear; goto top
226
/* Commands with identical functions in Type 1 and Type 2, */
227
/* except for 'escape'. */
232
return_error(gs_error_invalidfont);
234
c = fixed2int_var(*csp) + pdata->subroutineNumberBias;
235
code = pdata->procs.subr_data
236
(pfont, c, false, &ipsp[1].cs_data);
238
/* Calling a Subr with an out-of-range index is clearly a error:
239
* the Adobe documentation says the results of doing this are
240
* undefined. However, we have seen a PDF file produced by Adobe
241
* PDF Library 4.16 that included a Type 2 font that called an
242
* out-of-range Subr, and Acrobat Reader did not signal an error.
243
* Therefore, we ignore such calls.
249
ipsp->ip = cip, ipsp->dstate = state;
251
cip = ipsp->cs_data.bits.data;
254
gs_glyph_data_free(&ipsp->cs_data, "gs_type2_interpret");
258
/* See gstype1.h for information on this opcode. */
261
/* Commands with similar but not identical functions */
262
/* in Type 1 and Type 2 charstrings. */
269
check_first_operator(csp > cstack);
270
code = t1_hinter__rmoveto(h, 0, *csp);
277
for (ap = cstack; ap + 1 <= csp; ap += 2) {
278
code = t1_hinter__rlineto(h, ap[0], ap[1]);
289
hvl:for (ap = cstack; ap <= csp; vertical = !vertical, ++ap) {
291
code = t1_hinter__rlineto(h, 0, ap[0]);
293
code = t1_hinter__rlineto(h, ap[0], 0);
300
for (ap = cstack; ap + 5 <= csp; ap += 6) {
301
code = t1_hinter__rcurveto(h, ap[0], ap[1], ap[2],
302
ap[3], ap[4], ap[5]);
309
* It is a feature of Type 2 CharStrings that if endchar is
310
* invoked with 4 or 5 operands, it is equivalent to the
311
* Type 1 seac operator. In this case, the asb operand of
312
* seac is missing: we assume it is the same as the
313
* l.s.b. of the accented character. This feature was
314
* undocumented until the 16 March 2000 version of the Type
315
* 2 Charstring Format specification, but, thankfully, is
316
* described in that revision.
318
if (csp >= cstack + 3) {
319
check_first_operator(csp > cstack + 3);
320
code = gs_type1_seac(pcis, cstack, 0, ipsp);
324
cip = ipsp->cs_data.bits.data;
328
* This might be the only operator in the charstring.
329
* In this case, there might be a width on the stack.
331
check_first_operator(csp >= cstack);
332
if (pcis->seac_accent < 0) {
333
code = t1_hinter__endglyph(h);
336
code = gx_setcurrentpoint_from_path(pcis->pis, pcis->path);
340
t1_hinter__setcurrentpoint(h, pcis->save_adxy.x + pcis->origin_offset.x,
341
pcis->save_adxy.y + pcis->origin_offset.y);
342
code = t1_hinter__end_subglyph(h);
346
code = gs_type1_endchar(pcis);
349
* Reset the total hint count so that hintmask will
350
* parse its following data correctly.
351
* (gs_type1_endchar already reset the actual hint
355
/* do accent of seac */
356
ipsp = &pcis->ipstack[pcis->ips_count - 1];
357
cip = ipsp->cs_data.bits.data;
362
/* See vmoveto above re closing the subpath. */
363
check_first_operator(!((csp - cstack) & 1));
364
if (csp > cstack + 1) {
365
/* Some Type 2 charstrings omit the vstemhm operator before rmoveto,
366
even though this is only allowed before hintmask and cntrmask.
367
Thanks to Felix Pahl.
369
type2_vstem(pcis, csp - 2, cstack);
370
cstack [0] = csp [-1];
371
cstack [1] = csp [ 0];
374
code = t1_hinter__rmoveto(h, csp[-1], *csp);
377
/* See vmoveto above re closing the subpath. */
378
check_first_operator(csp > cstack);
379
code = t1_hinter__rmoveto(h, *csp, 0);
386
hvc:for (ap = cstack; ap + 3 <= csp; vertical = !vertical, ap += 4) {
387
gs_fixed_point pt[2] = {{0, 0}, {0, 0}};
399
code = t1_hinter__rcurveto(h, pt[0].x, pt[0].y, ap[1], ap[2], pt[1].x, pt[1].y);
405
/***********************
406
* New Type 2 commands *
407
***********************/
411
int n = fixed2int_var(*csp);
412
int num_values = csp - cstack;
413
gs_font_type1 *pfont = pcis->pfont;
414
int k = pfont->data.WeightVector.count;
418
base = csp - 1 - num_values;
419
deltas = base + n - 1;
420
for (j = 0; j < n; j++, base++, deltas += k - 1)
421
for (i = 1; i < k; i++)
422
*base += (fixed)(deltas[i] *
423
pfont->data.WeightVector.values[i]);
427
hstem:check_first_operator(!((csp - cstack) & 1));
431
for (ap = cstack; ap + 1 <= csp; x += ap[1], ap += 2) {
432
code = t1_hinter__hstem(h, x += ap[0], ap[1]);
437
pcis->num_hints += (csp + 1 - cstack) >> 1;
441
* A hintmask at the beginning of the CharString is
442
* equivalent to vstemhm + hintmask. For simplicity, we use
443
* this interpretation everywhere.
446
check_first_operator(!((csp - cstack) & 1));
447
type2_vstem(pcis, csp, cstack);
449
* We should clear the stack here only if this is the
450
* initial mask operator that includes the implicit
451
* vstemhm, but currently this is too much trouble to
456
byte mask[max_total_stem_hints / 8];
459
for (i = 0; i < pcis->num_hints; ++cip, i += 8) {
460
charstring_next(*cip, state, mask[i >> 3], encrypted);
461
if_debug1('1', " 0x%02x", mask[i >> 3]);
463
if_debug0('1', "\n");
465
ipsp->dstate = state;
466
if (c == c2_cntrmask) {
468
} else { /* hintmask or equivalent */
469
if_debug0('1', "[1]hstem hints:\n");
470
if_debug0('1', "[1]vstem hints:\n");
471
code = t1_hinter__hint_mask(h, mask);
478
vstem:check_first_operator(!((csp - cstack) & 1));
479
type2_vstem(pcis, csp, cstack);
482
for (ap = cstack; ap + 5 <= csp; ap += 6) {
483
code = t1_hinter__rcurveto(h, ap[0], ap[1], ap[2], ap[3],
488
code = t1_hinter__rlineto(h, ap[0], ap[1]);
491
for (ap = cstack; ap + 7 <= csp; ap += 2) {
492
code = t1_hinter__rlineto(h, ap[0], ap[1]);
496
code = t1_hinter__rcurveto(h, ap[0], ap[1], ap[2], ap[3],
502
int n = csp + 1 - cstack;
503
fixed dxa = (n & 1 ? *ap++ : 0);
505
for (; ap + 3 <= csp; ap += 4) {
506
code = t1_hinter__rcurveto(h, dxa, ap[0], ap[1], ap[2],
517
int n = csp + 1 - cstack;
518
fixed dya = (n & 1 ? *ap++ : 0);
520
for (; ap + 3 <= csp; ap += 4) {
521
code = t1_hinter__rcurveto(h, ap[0], dya, ap[1], ap[2],
533
charstring_next(*cip, state, c1, encrypted);
535
charstring_next(*cip, state, c2, encrypted);
537
CS_CHECK_PUSH(csp, cstack);
538
*++csp = int2fixed((((c1 ^ 0x80) - 0x80) << 8) + c2);
542
c = fixed2int_var(*csp) + pdata->gsubrNumberBias;
543
code = pdata->procs.subr_data
544
(pfont, c, true, &ipsp[1].cs_data);
547
charstring_next(*cip, state, c, encrypted);
550
if (gs_debug['1'] && c < char2_extended_command_count) {
551
static const char *const ce2names[] =
552
{char2_extended_command_names};
554
if (ce2names[c] == 0)
555
dlprintf2("[1]0x%lx: %02x??\n", (ulong) (cip - 1), c);
557
dlprintf3("[1]0x%lx: %02x %s\n", (ulong) (cip - 1), c,
561
switch ((char2_extended_command) c) {
563
csp[-1] = ((csp[-1] != 0) & (*csp != 0) ? fixed_1 : 0);
567
csp[-1] = (csp[-1] | *csp ? fixed_1 : 0);
571
*csp = (*csp ? 0 : fixed_1);
575
int i, n = fixed2int_var(*csp);
576
float *to = Registry[fixed2int_var(csp[-3])].values +
577
fixed2int_var(csp[-2]);
579
pcis->transient_array + fixed2int_var(csp[-1]);
581
for (i = 0; i < n; ++i)
582
to[i] = fixed2float(from[i]);
599
csp[-1] = float2fixed((double)csp[-1] / *csp);
603
/* The specification says there is no j (starting index */
604
/* in registry array) argument.... */
606
int i, n = fixed2int_var(*csp);
607
const float *from = Registry[fixed2int_var(csp[-2])].values;
609
pcis->transient_array + fixed2int_var(csp[-1]);
611
for (i = 0; i < n; ++i)
612
to[i] = float2fixed(from[i]);
620
csp[-1] = (csp[-1] == *csp ? fixed_1 : 0);
627
pcis->transient_array[fixed2int_var(*csp)] = csp[-1];
631
*csp = pcis->transient_array[fixed2int_var(*csp)];
639
CS_CHECK_PUSH(csp, cstack);
645
double prod = fixed2float(csp[-1]) * *csp;
648
(prod > max_fixed ? max_fixed :
649
prod < min_fixed ? min_fixed : (fixed)prod);
655
*csp = float2fixed(sqrt(fixed2float(*csp)));
658
CS_CHECK_PUSH(csp, cstack);
666
*csp = csp[-1], csp[-1] = top;
671
(*csp < 0 ? csp[-1] : csp[-1 - fixed2int_var(csp[-1])]);
675
int distance = fixed2int_var(*csp);
676
int count = fixed2int_var(csp[-1]);
680
if (count < 0 || count > csp + 1 - cstack)
681
return_error(gs_error_invalidfont);
685
distance = count - (-distance % count);
686
bot = csp + 1 - count;
687
while (--distance >= 0) {
690
memmove(bot + 1, bot,
691
(count - 1) * sizeof(fixed));
697
csp[6] = fixed_half; /* fd/100 */
698
csp[4] = *csp, csp[5] = 0; /* dx6, dy6 */
699
csp[2] = csp[-1], csp[3] = -csp[-4]; /* dx5, dy5 */
700
*csp = csp[-2], csp[1] = 0; /* dx4, dy4 */
701
csp[-2] = csp[-3], csp[-1] = 0; /* dx3, dy3 */
702
csp[-3] = csp[-4], csp[-4] = csp[-5]; /* dx2, dy2 */
703
csp[-5] = 0; /* dy1 */
707
*csp /= 100; /* fd/100 */
709
fixed x_join = csp[-12] + csp[-10] + csp[-8];
710
fixed y_join = csp[-11] + csp[-9] + csp[-7];
711
fixed x_end = x_join + csp[-6] + csp[-4] + csp[-2];
712
fixed y_end = y_join + csp[-5] + csp[-3] + csp[-1];
717
gs_distance_transform(fixed2float(x_join),
719
&ctm_only(pcis->pis),
722
gs_distance_transform(fixed2float(x_end),
724
&ctm_only(pcis->pis),
729
* Use the X or Y distance depending on whether
730
* the curve is more horizontal or more
733
if (any_abs(end.y) > any_abs(end.x))
737
if (fabs(flex_depth) < fixed2float(*csp)) {
738
/* Do flex as line. */
739
code = t1_hinter__rlineto(h, x_end, y_end);
742
* Do flex as curve. We can't jump to rrc,
743
* because the flex operators don't clear
746
code = t1_hinter__rcurveto(h,
747
csp[-12], csp[-11], csp[-10],
748
csp[-9], csp[-8], csp[-7]);
751
code = t1_hinter__rcurveto(h,
752
csp[-6], csp[-5], csp[-4],
753
csp[-3], csp[-2], csp[-1]);
761
csp[4] = fixed_half; /* fd/100 */
762
csp[2] = *csp; /* dx6 */
763
csp[3] = -(csp[-7] + csp[-5] + csp[-1]); /* dy6 */
764
*csp = csp[-2], csp[1] = csp[-1]; /* dx5, dy5 */
765
csp[-2] = csp[-3], csp[-1] = 0; /* dx4, dy4 */
766
csp[-3] = 0; /* dy3 */
771
fixed dx = csp[-10] + csp[-8] + csp[-6] + csp[-4] + csp[-2];
772
fixed dy = csp[-9] + csp[-7] + csp[-5] + csp[-3] + csp[-1];
774
if (any_abs(dx) > any_abs(dy))
775
csp[1] = -dy; /* d6 is dx6 */
777
csp[1] = *csp, *csp = -dx; /* d6 is dy6 */
779
csp[2] = fixed_half; /* fd/100 */
785
/* Fill up the dispatch up to 32. */
788
default: /* pacify compiler */
789
return_error(gs_error_invalidfont);