home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpsfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  21.2 KB  |  820 lines

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