gdevpsfx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /* Copyright (C) 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpsfx.c,v 1.25 2005/07/30 02:39:45 alexcher Exp $ */
  14. /* Convert Type 1 Charstrings to Type 2 */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gxfixed.h"
  20. #include "gxmatrix.h" /* for gsfont.h */
  21. #include "gxfont.h"
  22. #include "gxfont1.h"
  23. #include "gxtype1.h"
  24. #include "stream.h"
  25. #include "gdevpsf.h"
  26. /* ------ Type 1 Charstring parsing ------ */
  27. /*
  28. * The parsing code handles numbers on its own; it reports callsubr and
  29. * return operators to the caller, but also executes them.
  30. *
  31. * Only the following elements of the Type 1 state are used:
  32. * ostack, os_count, ipstack, ips_count
  33. */
  34. #define CE_OFFSET 32 /* offset for extended opcodes */
  35. typedef struct {
  36. fixed v0, v1; /* coordinates */
  37. ushort index; /* sequential index of hint */
  38. } cv_stem_hint;
  39. typedef struct {
  40. int count;
  41. int current; /* cache cursor for search */
  42. /*
  43. * For dotsection and Type 1 Charstring hint replacement,
  44. * we store active hints at the bottom of the table, and
  45. * replaced hints at the top.
  46. */
  47. int replaced_count; /* # of replaced hints at top */
  48. cv_stem_hint data[max_total_stem_hints];
  49. } cv_stem_hint_table;
  50. /* Skip over the initial bytes in a Charstring, if any. */
  51. private void
  52. skip_iv(gs_type1_state *pcis)
  53. {
  54. int skip = pcis->pfont->data.lenIV;
  55. ip_state_t *ipsp = &pcis->ipstack[pcis->ips_count - 1];
  56. const byte *cip = ipsp->cs_data.bits.data;
  57. crypt_state state = crypt_charstring_seed;
  58. for (; skip > 0; ++cip, --skip)
  59. decrypt_skip_next(*cip, state);
  60. ipsp->ip = cip;
  61. ipsp->dstate = state;
  62. }
  63. /*
  64. * Set up for parsing a Type 1 Charstring.
  65. *
  66. * Only uses the following elements of *pfont:
  67. * data.lenIV
  68. */
  69. private void
  70. type1_next_init(gs_type1_state *pcis, const gs_glyph_data_t *pgd,
  71. gs_font_type1 *pfont)
  72. {
  73. gs_type1_interp_init(pcis, NULL, NULL, NULL, NULL, false, 0, pfont);
  74. pcis->flex_count = flex_max;
  75. pcis->ipstack[0].cs_data = *pgd;
  76. skip_iv(pcis);
  77. }
  78. /* Clear the Type 1 operand stack. */
  79. inline private void
  80. type1_clear(gs_type1_state *pcis)
  81. {
  82. pcis->os_count = 0;
  83. }
  84. /* Execute a callsubr. */
  85. private int
  86. type1_callsubr(gs_type1_state *pcis, int index)
  87. {
  88. gs_font_type1 *pfont = pcis->pfont;
  89. ip_state_t *ipsp1 = &pcis->ipstack[pcis->ips_count];
  90. int code = pfont->data.procs.subr_data(pfont, index, false,
  91. &ipsp1->cs_data);
  92. if (code < 0)
  93. return_error(code);
  94. pcis->ips_count++;
  95. skip_iv(pcis);
  96. return code;
  97. }
  98. /* Add 1 or 3 stem hints. */
  99. private int
  100. type1_stem1(gs_type1_state *pcis, cv_stem_hint_table *psht, const fixed *pv,
  101. fixed lsb, byte *active_hints)
  102. {
  103. fixed v0 = pv[0] + lsb, v1 = v0 + pv[1];
  104. cv_stem_hint *bot = &psht->data[0];
  105. cv_stem_hint *orig_top = bot + psht->count;
  106. cv_stem_hint *top = orig_top;
  107. if (psht->count >= max_total_stem_hints)
  108. return_error(gs_error_limitcheck);
  109. while (top > bot &&
  110. (v0 < top[-1].v0 || (v0 == top[-1].v0 && v1 < top[-1].v1))
  111. ) {
  112. *top = top[-1];
  113. top--;
  114. }
  115. if (top > bot && v0 == top[-1].v0 && v1 == top[-1].v1) {
  116. /* Duplicate hint, don't add it. */
  117. memmove(top, top + 1, (char *)orig_top - (char *)top);
  118. if (active_hints) {
  119. uint index = top[-1].index;
  120. active_hints[index >> 3] |= 0x80 >> (index & 7);
  121. }
  122. return 0;
  123. }
  124. top->v0 = v0;
  125. top->v1 = v1;
  126. psht->count++;
  127. return 0;
  128. }
  129. private void
  130. type1_stem3(gs_type1_state *pcis, cv_stem_hint_table *psht, const fixed *pv3,
  131. fixed lsb, byte *active_hints)
  132. {
  133. type1_stem1(pcis, psht, pv3, lsb, active_hints);
  134. type1_stem1(pcis, psht, pv3 + 2, lsb, active_hints);
  135. type1_stem1(pcis, psht, pv3 + 4, lsb, active_hints);
  136. }
  137. /*
  138. * Get the next operator from a Type 1 Charstring. This procedure handles
  139. * numbers, div, blend, pop, and callsubr/return.
  140. */
  141. private int
  142. type1_next(gs_type1_state *pcis)
  143. {
  144. ip_state_t *ipsp = &pcis->ipstack[pcis->ips_count - 1];
  145. const byte *cip, *cipe;
  146. crypt_state state;
  147. #define CLEAR (csp = pcis->ostack - 1)
  148. fixed *csp = &pcis->ostack[pcis->os_count - 1];
  149. const bool encrypted = pcis->pfont->data.lenIV >= 0;
  150. int c, code, num_results, c0;
  151. load:
  152. cip = ipsp->ip;
  153. cipe = ipsp->cs_data.bits.data + ipsp->cs_data.bits.size;
  154. state = ipsp->dstate;
  155. for (;;) {
  156. if (cip >= cipe)
  157. return_error(gs_error_invalidfont);
  158. c0 = *cip++;
  159. charstring_next(c0, state, c, encrypted);
  160. if (c >= c_num1) {
  161. /* This is a number, decode it and push it on the stack. */
  162. if (c < c_pos2_0) { /* 1-byte number */
  163. decode_push_num1(csp, pcis->ostack, c);
  164. } else if (c < cx_num4) { /* 2-byte number */
  165. decode_push_num2(csp, pcis->ostack, c, cip, state, encrypted);
  166. } else if (c == cx_num4) { /* 4-byte number */
  167. long lw;
  168. decode_num4(lw, cip, state, encrypted);
  169. CS_CHECK_PUSH(csp, pcis->ostack);
  170. *++csp = int2fixed(lw);
  171. } else /* not possible */
  172. return_error(gs_error_invalidfont);
  173. continue;
  174. }
  175. #ifdef DEBUG
  176. if (gs_debug_c('1')) {
  177. const fixed *p;
  178. for (p = pcis->ostack; p <= csp; ++p)
  179. dprintf1(" %g", fixed2float(*p));
  180. if (c == cx_escape) {
  181. crypt_state cstate = state;
  182. int cn;
  183. charstring_next(*cip, cstate, cn, encrypted);
  184. dprintf1(" [*%d]\n", cn);
  185. } else
  186. dprintf1(" [%d]\n", c);
  187. }
  188. #endif
  189. switch ((char_command) c) {
  190. default:
  191. break;
  192. case c_undef0:
  193. case c_undef2:
  194. case c_undef17:
  195. return_error(gs_error_invalidfont);
  196. case c_callsubr:
  197. code = type1_callsubr(pcis, fixed2int_var(*csp) +
  198. pcis->pfont->data.subroutineNumberBias);
  199. if (code < 0)
  200. return_error(code);
  201. ipsp->ip = cip, ipsp->dstate = state;
  202. --csp;
  203. ++ipsp;
  204. goto load;
  205. case c_return:
  206. gs_glyph_data_free(&ipsp->cs_data, "type1_next");
  207. pcis->ips_count--;
  208. --ipsp;
  209. goto load;
  210. case c_undoc15:
  211. /* See gstype1.h for information on this opcode. */
  212. CLEAR;
  213. continue;
  214. case cx_escape:
  215. charstring_next(*cip, state, c, encrypted);
  216. ++cip;
  217. switch ((char1_extended_command) c) {
  218. default:
  219. c += CE_OFFSET;
  220. break;
  221. case ce1_div:
  222. csp[-1] = float2fixed((double)csp[-1] / (double)*csp);
  223. --csp;
  224. continue;
  225. case ce1_undoc15: /* see gstype1.h */
  226. CLEAR;
  227. continue;
  228. case ce1_callothersubr:
  229. switch (fixed2int_var(*csp)) {
  230. case 0:
  231. pcis->ignore_pops = 2;
  232. break; /* pass to caller */
  233. case 3:
  234. pcis->ignore_pops = 1;
  235. break; /* pass to caller */
  236. case 14:
  237. num_results = 1; goto blend;
  238. case 15:
  239. num_results = 2; goto blend;
  240. case 16:
  241. num_results = 3; goto blend;
  242. case 17:
  243. num_results = 4; goto blend;
  244. case 18:
  245. num_results = 6;
  246. blend:
  247. code = gs_type1_blend(pcis, csp, num_results);
  248. if (code < 0)
  249. return code;
  250. csp -= code;
  251. continue;
  252. default:
  253. break; /* pass to caller */
  254. }
  255. break;
  256. case ce1_pop:
  257. if (pcis->ignore_pops != 0) {
  258. pcis->ignore_pops--;
  259. continue;
  260. }
  261. return_error(gs_error_rangecheck);
  262. }
  263. break;
  264. }
  265. break;
  266. }
  267. ipsp->ip = cip, ipsp->dstate = state;
  268. pcis->ips_count = ipsp + 1 - &pcis->ipstack[0];
  269. pcis->os_count = csp + 1 - &pcis->ostack[0];
  270. return c;
  271. }
  272. /* ------ Output ------ */
  273. /* Put 2 or 4 bytes on a stream (big-endian). */
  274. private void
  275. sputc2(stream *s, int i)
  276. {
  277. sputc(s, (byte)(i >> 8));
  278. sputc(s, (byte)i);
  279. }
  280. private void
  281. sputc4(stream *s, int i)
  282. {
  283. sputc2(s, i >> 16);
  284. sputc2(s, i);
  285. }
  286. /* Put a Type 2 operator on a stream. */
  287. private void
  288. type2_put_op(stream *s, int op)
  289. {
  290. if (op >= CE_OFFSET) {
  291. spputc(s, cx_escape);
  292. spputc(s, (byte)(op - CE_OFFSET));
  293. } else
  294. sputc(s, (byte)op);
  295. }
  296. /* Put a Type 2 number on a stream. */
  297. private void
  298. type2_put_int(stream *s, int i)
  299. {
  300. if (i >= -107 && i <= 107)
  301. sputc(s, (byte)(i + 139));
  302. else if (i <= 1131 && i >= 0)
  303. sputc2(s, (c_pos2_0 << 8) + i - 108);
  304. else if (i >= -1131 && i < 0)
  305. sputc2(s, (c_neg2_0 << 8) - i - 108);
  306. else if (i >= -32768 && i <= 32767) {
  307. spputc(s, c2_shortint);
  308. sputc2(s, i);
  309. } else {
  310. /*
  311. * We can't represent this number directly: compute it.
  312. * (This can be done much more efficiently in particular cases;
  313. * we'll do this if it ever seems worthwhile.)
  314. */
  315. type2_put_int(s, i >> 10);
  316. type2_put_int(s, 1024);
  317. type2_put_op(s, CE_OFFSET + ce2_mul);
  318. type2_put_int(s, i & 1023);
  319. type2_put_op(s, CE_OFFSET + ce2_add);
  320. }
  321. }
  322. /* Put a fixed value on a stream. */
  323. private void
  324. type2_put_fixed(stream *s, fixed v)
  325. {
  326. if (fixed_is_int(v))
  327. type2_put_int(s, fixed2int_var(v));
  328. else if (v >= int2fixed(-32768) && v < int2fixed(32768)) {
  329. /* We can represent this as a 16:16 number. */
  330. spputc(s, cx_num4);
  331. sputc4(s, v << (16 - _fixed_shift));
  332. } else {
  333. type2_put_int(s, fixed2int_var(v));
  334. type2_put_fixed(s, fixed_fraction(v));
  335. type2_put_op(s, CE_OFFSET + ce2_add);
  336. }
  337. }
  338. /* Put a stem hint table on a stream. */
  339. private void
  340. type2_put_stems(stream *s, int os_count, const cv_stem_hint_table *psht, int op)
  341. {
  342. fixed prev = 0;
  343. int pushed = os_count;
  344. int i;
  345. for (i = 0; i < psht->count; ++i, pushed += 2) {
  346. fixed v0 = psht->data[i].v0;
  347. fixed v1 = psht->data[i].v1;
  348. if (pushed > ostack_size - 2) {
  349. type2_put_op(s, op);
  350. pushed = 0;
  351. }
  352. type2_put_fixed(s, v0 - prev);
  353. type2_put_fixed(s, v1 - v0);
  354. prev = v1;
  355. }
  356. type2_put_op(s, op);
  357. }
  358. /* Put out a hintmask command. */
  359. private void
  360. type2_put_hintmask(stream *s, const byte *mask, uint size)
  361. {
  362. uint ignore;
  363. type2_put_op(s, c2_hintmask);
  364. sputs(s, mask, size, &ignore);
  365. }
  366. /* ------ Main program ------ */
  367. /*
  368. * Convert a Type 1 Charstring to (unencrypted) Type 2.
  369. * For simplicity, we expand all Subrs in-line.
  370. * We still need to optimize the output using these patterns:
  371. * (vhcurveto hvcurveto)* (vhcurveto hrcurveto | vrcurveto) =>
  372. * vhcurveto
  373. * (hvcurveto vhcurveto)* (hvcurveto vrcurveto | hrcurveto) =>
  374. * hvcurveto
  375. */
  376. #define MAX_STACK ostack_size
  377. int
  378. psf_convert_type1_to_type2(stream *s, const gs_glyph_data_t *pgd,
  379. gs_font_type1 *pfont)
  380. {
  381. gs_type1_state cis;
  382. cv_stem_hint_table hstem_hints; /* horizontal stem hints */
  383. cv_stem_hint_table vstem_hints; /* vertical stem hints */
  384. bool first = true;
  385. bool replace_hints = false;
  386. bool hints_changed = false;
  387. enum {
  388. dotsection_in = 0,
  389. dotsection_out = -1
  390. } dotsection_flag = dotsection_out;
  391. byte active_hints[(max_total_stem_hints + 7) / 8];
  392. byte dot_save_hints[(max_total_stem_hints + 7) / 8];
  393. uint hintmask_size;
  394. #define HINTS_CHANGED()\
  395. BEGIN\
  396. hints_changed = replace_hints;\
  397. if (hints_changed)\
  398. CHECK_OP(); /* see below */\
  399. END
  400. #define CHECK_HINTS_CHANGED()\
  401. BEGIN\
  402. if (hints_changed) {\
  403. type2_put_hintmask(s, active_hints, hintmask_size);\
  404. hints_changed = false;\
  405. }\
  406. END
  407. /*
  408. * In order to combine Type 1 operators, we usually delay writing
  409. * out operators (but not their operands). We must keep track of
  410. * the stack depth so we don't exceed it when combining operators.
  411. */
  412. int depth; /* of operands on stack */
  413. int prev_op; /* operator to write, -1 if none */
  414. #define CLEAR_OP()\
  415. (depth = 0, prev_op = -1)
  416. #define CHECK_OP()\
  417. BEGIN\
  418. if (prev_op >= 0) {\
  419. type2_put_op(s, prev_op);\
  420. CLEAR_OP();\
  421. }\
  422. END
  423. fixed mx0 = 0, my0 = 0; /* See ce1_setcurrentpoint. */
  424. /*
  425. * Do a first pass to collect hints. Note that we must also process
  426. * [h]sbw, because the hint coordinates are relative to the lsb.
  427. */
  428. hstem_hints.count = hstem_hints.replaced_count = hstem_hints.current = 0;
  429. vstem_hints.count = vstem_hints.replaced_count = vstem_hints.current = 0;
  430. type1_next_init(&cis, pgd, pfont);
  431. for (;;) {
  432. int c = type1_next(&cis);
  433. fixed *csp = &cis.ostack[cis.os_count - 1];
  434. switch (c) {
  435. default:
  436. if (c < 0)
  437. return c;
  438. type1_clear(&cis);
  439. continue;
  440. case c1_hsbw:
  441. gs_type1_sbw(&cis, cis.ostack[0], fixed_0, cis.ostack[1], fixed_0);
  442. goto clear;
  443. case cx_hstem:
  444. type1_stem1(&cis, &hstem_hints, csp - 1, cis.lsb.y, NULL);
  445. goto clear;
  446. case cx_vstem:
  447. type1_stem1(&cis, &vstem_hints, csp - 1, cis.lsb.x, NULL);
  448. goto clear;
  449. case CE_OFFSET + ce1_sbw:
  450. gs_type1_sbw(&cis, cis.ostack[0], cis.ostack[1],
  451. cis.ostack[2], cis.ostack[3]);
  452. goto clear;
  453. case CE_OFFSET + ce1_vstem3:
  454. type1_stem3(&cis, &vstem_hints, csp - 5, cis.lsb.x, NULL);
  455. goto clear;
  456. case CE_OFFSET + ce1_hstem3:
  457. type1_stem3(&cis, &hstem_hints, csp - 5, cis.lsb.y, NULL);
  458. clear:
  459. type1_clear(&cis);
  460. continue;
  461. case ce1_callothersubr:
  462. if (*csp == int2fixed(3))
  463. replace_hints = true;
  464. cis.os_count -= 2;
  465. continue;
  466. case CE_OFFSET + ce1_dotsection:
  467. replace_hints = true;
  468. continue;
  469. case CE_OFFSET + ce1_seac:
  470. case cx_endchar:
  471. break;
  472. }
  473. break;
  474. }
  475. /*
  476. * Number the hints for hintmask. We must do this even if we never
  477. * replace hints, because type1_stem# uses the index to set bits in
  478. * active_hints.
  479. */
  480. {
  481. int i;
  482. for (i = 0; i < hstem_hints.count; ++i)
  483. hstem_hints.data[i].index = i;
  484. for (i = 0; i < vstem_hints.count; ++i)
  485. vstem_hints.data[i].index = i + hstem_hints.count;
  486. }
  487. if (replace_hints) {
  488. hintmask_size =
  489. (hstem_hints.count + vstem_hints.count + 7) / 8;
  490. memset(active_hints, 0, hintmask_size);
  491. } else
  492. hintmask_size = 0;
  493. /* Do a second pass to write the result. */
  494. type1_next_init(&cis, pgd, pfont);
  495. CLEAR_OP();
  496. for (;;) {
  497. int c = type1_next(&cis);
  498. fixed *csp = &cis.ostack[cis.os_count - 1];
  499. #define POP(n)\
  500. (csp -= (n), cis.os_count -= (n))
  501. int i;
  502. fixed mx, my;
  503. switch (c) {
  504. default:
  505. if (c < 0)
  506. return c;
  507. if (c >= CE_OFFSET)
  508. return_error(gs_error_rangecheck);
  509. /* The Type 1 use of all other operators is the same in Type 2. */
  510. copy:
  511. CHECK_OP();
  512. CHECK_HINTS_CHANGED();
  513. put:
  514. for (i = 0; i < cis.os_count; ++i)
  515. type2_put_fixed(s, cis.ostack[i]);
  516. depth += cis.os_count;
  517. prev_op = c;
  518. type1_clear(&cis);
  519. continue;
  520. case cx_hstem:
  521. type1_stem1(&cis, &hstem_hints, csp - 1, cis.lsb.y, active_hints);
  522. hint:
  523. HINTS_CHANGED();
  524. type1_clear(&cis);
  525. continue;
  526. case cx_vstem:
  527. type1_stem1(&cis, &vstem_hints, csp - 1, cis.lsb.x, active_hints);
  528. goto hint;
  529. case CE_OFFSET + ce1_vstem3:
  530. type1_stem3(&cis, &vstem_hints, csp - 5, cis.lsb.x, active_hints);
  531. goto hint;
  532. case CE_OFFSET + ce1_hstem3:
  533. type1_stem3(&cis, &hstem_hints, csp - 5, cis.lsb.y, active_hints);
  534. goto hint;
  535. case CE_OFFSET + ce1_dotsection:
  536. if (dotsection_flag == dotsection_out) {
  537. memcpy(dot_save_hints, active_hints, hintmask_size);
  538. memset(active_hints, 0, hintmask_size);
  539. dotsection_flag = dotsection_in;
  540. } else {
  541. memcpy(active_hints, dot_save_hints, hintmask_size);
  542. dotsection_flag = dotsection_out;
  543. }
  544. HINTS_CHANGED();
  545. continue;
  546. case c1_closepath:
  547. continue;
  548. case CE_OFFSET + ce1_setcurrentpoint:
  549. if (first) {
  550. /* A workaround for fonts which use ce1_setcurrentpoint
  551. in an illegal way for shifting a path.
  552. See t1_hinter__setcurrentpoint for more information. */
  553. mx0 = csp[-1], my0 = *csp;
  554. }
  555. continue;
  556. case cx_vmoveto:
  557. mx = 0, my = *csp;
  558. POP(1); goto move;
  559. case cx_hmoveto:
  560. mx = *csp, my = 0;
  561. POP(1); goto move;
  562. case cx_rmoveto:
  563. mx = csp[-1], my = *csp;
  564. POP(2);
  565. move:
  566. CHECK_OP();
  567. if (first) {
  568. if (cis.os_count)
  569. type2_put_fixed(s, *csp); /* width */
  570. mx += cis.lsb.x + mx0, my += cis.lsb.y + my0;
  571. first = false;
  572. }
  573. if (cis.flex_count != flex_max) {
  574. /* We're accumulating points for a flex. */
  575. if (type1_next(&cis) != ce1_callothersubr)
  576. return_error(gs_error_rangecheck);
  577. csp = &cis.ostack[cis.os_count - 1];
  578. if (*csp != int2fixed(2) || csp[-1] != fixed_0)
  579. return_error(gs_error_rangecheck);
  580. cis.flex_count++;
  581. csp[-1] = mx, *csp = my;
  582. continue;
  583. }
  584. CHECK_HINTS_CHANGED();
  585. if (mx == 0) {
  586. type2_put_fixed(s, my);
  587. depth = 1, prev_op = cx_vmoveto;
  588. } else if (my == 0) {
  589. type2_put_fixed(s, mx);
  590. depth = 1, prev_op = cx_hmoveto;
  591. } else {
  592. type2_put_fixed(s, mx);
  593. type2_put_fixed(s, my);
  594. depth = 2, prev_op = cx_rmoveto;
  595. }
  596. type1_clear(&cis);
  597. continue;
  598. case c1_hsbw:
  599. gs_type1_sbw(&cis, cis.ostack[0], fixed_0, cis.ostack[1], fixed_0);
  600. /*
  601. * Leave the l.s.b. on the operand stack for the initial hint,
  602. * moveto, or endchar command.
  603. */
  604. cis.ostack[0] = cis.ostack[1];
  605. sbw:
  606. if (cis.ostack[0] == pfont->data.defaultWidthX)
  607. cis.os_count = 0;
  608. else {
  609. cis.ostack[0] -= pfont->data.nominalWidthX;
  610. cis.os_count = 1;
  611. }
  612. if (hstem_hints.count) {
  613. if (cis.os_count)
  614. type2_put_fixed(s, cis.ostack[0]);
  615. type2_put_stems(s, cis.os_count, &hstem_hints,
  616. (replace_hints ? c2_hstemhm : cx_hstem));
  617. cis.os_count = 0;
  618. }
  619. if (vstem_hints.count) {
  620. if (cis.os_count)
  621. type2_put_fixed(s, cis.ostack[0]);
  622. type2_put_stems(s, cis.os_count, &vstem_hints,
  623. (replace_hints ? c2_vstemhm : cx_vstem));
  624. cis.os_count = 0;
  625. }
  626. continue;
  627. case CE_OFFSET + ce1_seac:
  628. /*
  629. * It is an undocumented feature of the Type 2 CharString
  630. * format that endchar + 4 or 5 operands is equivalent to
  631. * seac with an implicit asb operand + endchar with 0 or 1
  632. * operands. Remove the asb argument from the stack, but
  633. * adjust the adx argument to compensate for the fact that
  634. * Type 2 CharStrings don't have any concept of l.s.b.
  635. */
  636. csp[-3] += cis.lsb.x - csp[-4];
  637. memmove(csp - 4, csp - 3, sizeof(*csp) * 4);
  638. POP(1);
  639. /* (falls through) */
  640. case cx_endchar:
  641. CHECK_OP();
  642. for (i = 0; i < cis.os_count; ++i)
  643. type2_put_fixed(s, cis.ostack[i]);
  644. type2_put_op(s, cx_endchar);
  645. return 0;
  646. case CE_OFFSET + ce1_sbw:
  647. gs_type1_sbw(&cis, cis.ostack[0], cis.ostack[1],
  648. cis.ostack[2], cis.ostack[3]);
  649. cis.ostack[0] = cis.ostack[2];
  650. goto sbw;
  651. case ce1_callothersubr:
  652. CHECK_OP();
  653. switch (fixed2int_var(*csp)) {
  654. default:
  655. return_error(gs_error_rangecheck);
  656. case 0:
  657. /*
  658. * The operand stack contains: delta to reference point,
  659. * 6 deltas for the two curves, fd, final point, 3, 0.
  660. */
  661. csp[-18] += csp[-16], csp[-17] += csp[-15];
  662. memmove(csp - 16, csp - 14, sizeof(*csp) * 11);
  663. cis.os_count -= 6, csp -= 6;
  664. /*
  665. * We could optimize by using [h]flex[1],
  666. * but it isn't worth the trouble.
  667. */
  668. c = CE_OFFSET + ce2_flex;
  669. cis.flex_count = flex_max; /* not inside flex */
  670. cis.ignore_pops = 2;
  671. goto copy;
  672. case 1:
  673. cis.flex_count = 0;
  674. cis.os_count -= 2;
  675. continue;
  676. /*case 2:*/ /* detected in *moveto */
  677. case 3:
  678. memset(active_hints, 0, hintmask_size);
  679. HINTS_CHANGED();
  680. cis.ignore_pops = 1;
  681. cis.os_count -= 2;
  682. continue;
  683. case 12:
  684. case 13:
  685. /* Counter control is not implemented. */
  686. cis.os_count -= 2 + fixed2int(csp[-1]);
  687. continue;
  688. }
  689. /*
  690. * The remaining cases are strictly for optimization.
  691. */
  692. case cx_rlineto:
  693. if (depth > MAX_STACK - 2)
  694. goto copy;
  695. switch (prev_op) {
  696. case cx_rlineto: /* rlineto+ => rlineto */
  697. goto put;
  698. case cx_rrcurveto: /* rrcurveto+ rlineto => rcurveline */
  699. c = c2_rcurveline;
  700. goto put;
  701. default:
  702. goto copy;
  703. }
  704. case cx_hlineto: /* hlineto (vlineto hlineto)* [vlineto] => hlineto */
  705. if (depth > MAX_STACK - 1 ||
  706. prev_op != (depth & 1 ? cx_vlineto : cx_hlineto))
  707. goto copy;
  708. c = prev_op;
  709. goto put;
  710. case cx_vlineto: /* vlineto (hlineto vlineto)* [hlineto] => vlineto */
  711. if (depth > MAX_STACK - 1 ||
  712. prev_op != (depth & 1 ? cx_hlineto : cx_vlineto))
  713. goto copy;
  714. c = prev_op;
  715. goto put;
  716. case cx_hvcurveto: /* hvcurveto (vhcurveto hvcurveto)* => hvcurveto */
  717. /* (vhcurveto hvcurveto)+ => vhcurveto */
  718. /*
  719. * We have to check (depth & 1) because the last curve might
  720. * have 5 parameters rather than 4 (see rrcurveto below).
  721. */
  722. if ((depth & 1) || depth > MAX_STACK - 4 ||
  723. prev_op != (depth & 4 ? cx_vhcurveto : cx_hvcurveto))
  724. goto copy;
  725. c = prev_op;
  726. goto put;
  727. case cx_vhcurveto: /* vhcurveto (hvcurveto vhcurveto)* => vhcurveto */
  728. /* (hvcurveto vhcurveto)+ => hvcurveto */
  729. /* See above re the (depth & 1) check. */
  730. if ((depth & 1) || depth > MAX_STACK - 4 ||
  731. prev_op != (depth & 4 ? cx_hvcurveto : cx_vhcurveto))
  732. goto copy;
  733. c = prev_op;
  734. goto put;
  735. case cx_rrcurveto:
  736. if (depth == 0) {
  737. if (csp[-1] == 0) {
  738. /* A|0 B C D 0 F rrcurveto => [A] B C D F vvcurveto */
  739. c = c2_vvcurveto;
  740. csp[-1] = csp[0];
  741. if (csp[-5] == 0) {
  742. memmove(csp - 5, csp - 4, sizeof(*csp) * 4);
  743. POP(2);
  744. } else
  745. POP(1);
  746. } else if (*csp == 0) {
  747. /* A B|0 C D E 0 rrcurveto => [B] A C D E hhcurveto */
  748. c = c2_hhcurveto;
  749. if (csp[-4] == 0) {
  750. memmove(csp - 4, csp - 3, sizeof(*csp) * 3);
  751. POP(2);
  752. } else {
  753. *csp = csp[-5], csp[-5] = csp[-4], csp[-4] = *csp;
  754. POP(1);
  755. }
  756. }
  757. /*
  758. * We could also optimize:
  759. * 0 B C D E F|0 rrcurveto => B C D E [F] vhcurveto
  760. * A 0 C D E|0 F rrcurveto => A C D F [E] hvcurveto
  761. * but this gets in the way of subsequent optimization
  762. * of multiple rrcurvetos, so we don't do it.
  763. */
  764. goto copy;
  765. }
  766. if (depth > MAX_STACK - 6)
  767. goto copy;
  768. switch (prev_op) {
  769. case c2_hhcurveto: /* hrcurveto (x1 0 x2 y2 x3 0 rrcurveto)* => */
  770. /* hhcurveto */
  771. if (csp[-4] == 0 && *csp == 0) {
  772. memmove(csp - 4, csp - 3, sizeof(*csp) * 3);
  773. c = prev_op;
  774. POP(2);
  775. goto put;
  776. }
  777. goto copy;
  778. case c2_vvcurveto: /* rvcurveto (0 y1 x2 y2 0 y3 rrcurveto)* => */
  779. /* vvcurveto */
  780. if (csp[-5] == 0 && csp[-1] == 0) {
  781. memmove(csp - 5, csp - 4, sizeof(*csp) * 3);
  782. csp[-2] = *csp;
  783. c = prev_op;
  784. POP(2);
  785. goto put;
  786. }
  787. goto copy;
  788. case cx_hvcurveto:
  789. if (depth & 1)
  790. goto copy;
  791. if (!(depth & 4))
  792. goto hrc;
  793. vrc: /* (vhcurveto hvcurveto)+ vrcurveto => vhcurveto */
  794. /* hvcurveto (vhcurveto hvcurveto)* vrcurveto => hvcurveto */
  795. if (csp[-5] != 0)
  796. goto copy;
  797. memmove(csp - 5, csp - 4, sizeof(*csp) * 5);
  798. c = prev_op;
  799. POP(1);
  800. goto put;
  801. case cx_vhcurveto:
  802. if (depth & 1)
  803. goto copy;
  804. if (!(depth & 4))
  805. goto vrc;
  806. hrc: /* (hvcurveto vhcurveto)+ hrcurveto => hvcurveto */
  807. /* vhcurveto (hvcurveto vhcurveto)* hrcurveto => vhcurveto */
  808. if (csp[-4] != 0)
  809. goto copy;
  810. /* A 0 C D E F => A C D F E */
  811. memmove(csp - 4, csp - 3, sizeof(*csp) * 2);
  812. csp[-2] = *csp;
  813. c = prev_op;
  814. POP(1);
  815. goto put;
  816. case cx_rlineto: /* rlineto+ rrcurveto => rlinecurve */
  817. c = c2_rlinecurve;
  818. goto put;
  819. case cx_rrcurveto: /* rrcurveto+ => rrcurveto */
  820. goto put;
  821. default:
  822. goto copy;
  823. }
  824. }
  825. }
  826. }