home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSCHAR.C < prev    next >
C/C++ Source or Header  |  1994-07-31  |  32KB  |  1,009 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gschar.c */
  20. /* Character writing operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gxfixed.h"            /* ditto */
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gzstate.h"
  30. #include "gxcoord.h"
  31. #include "gxdevmem.h"
  32. #include "gxchar.h"
  33. #include "gxfont.h"
  34. #include "gxfont0.h"
  35. #include "gxfcache.h"
  36. #include "gzpath.h"
  37.  
  38. /* Structure descriptors */
  39. private_st_gs_show_enum();
  40. #define eptr ((gs_show_enum *)vptr)
  41. private ENUM_PTRS_BEGIN(show_enum_enum_ptrs) {
  42.     if ( index > eptr->fstack.depth + 4 )
  43.       return 0;
  44.     *pep = eptr->fstack.items[index - 4].font;
  45.     break;
  46.     }
  47.     ENUM_PTR(0, gs_show_enum, pgs);
  48.     ENUM_CONST_STRING_PTR(1, gs_show_enum, str);
  49.     ENUM_PTR(2, gs_show_enum, dev_cache);
  50.     ENUM_PTR(3, gs_show_enum, dev_null);
  51. ENUM_PTRS_END
  52. private RELOC_PTRS_BEGIN(show_enum_reloc_ptrs) {
  53.     int i;
  54.     RELOC_PTR(gs_show_enum, pgs);
  55.     RELOC_CONST_STRING_PTR(gs_show_enum, str);
  56.     RELOC_PTR(gs_show_enum, dev_cache);
  57.     RELOC_PTR(gs_show_enum, dev_null);
  58.     for ( i = 0; i <= eptr->fstack.depth; i++ )
  59.       RELOC_PTR(gs_show_enum, fstack.items[i].font);
  60. } RELOC_PTRS_END
  61. #undef eptr
  62.  
  63. /* Forward declarations */
  64. private int continue_kshow(P1(gs_show_enum *));
  65. private int continue_show(P1(gs_show_enum *));
  66. private int continue_show_update(P1(gs_show_enum *));
  67. private int show_setup(P3(gs_show_enum *, gs_state *, const char *));
  68. private void show_set_scale(P1(gs_show_enum *));
  69. private int show_cache_setup(P1(gs_show_enum *));
  70. private int show_state_setup(P1(gs_show_enum *));
  71. private int show_origin_setup(P4(gs_state *, fixed, fixed, int));
  72. private int stringwidth_setup(P3(gs_show_enum *, gs_state *, const char *));
  73.  
  74. /* Print the ctm if debugging */
  75. #define print_ctm(s,pgs)\
  76.   dprintf7("[p]%sctm=[%g %g %g %g %g %g]\n", s,\
  77.        pgs->ctm.xx, pgs->ctm.xy, pgs->ctm.yx, pgs->ctm.yy,\
  78.        pgs->ctm.tx, pgs->ctm.ty)
  79.  
  80. /* ------ String writing operators ------ */
  81.  
  82. /* Allocate a show enumerator. */
  83. gs_show_enum *
  84. gs_show_enum_alloc(gs_memory_t *mem, client_name_t cname)
  85. {    return gs_alloc_struct(mem, gs_show_enum, &st_gs_show_enum, cname);
  86. }
  87.  
  88. /* Setup macros for show operators */
  89. #define setup_show_n()\
  90.   penum->str.size = size
  91. #define setup_a()\
  92.   penum->add = 1, penum->ax = ax, penum->ay = ay,\
  93.   penum->slow_show = 1
  94. #define setup_width()\
  95.   penum->wchr = chr, penum->wcx = cx, penum->wcy = cy,\
  96.   penum->slow_show = 1
  97.  
  98. /* show[_n] */
  99. int
  100. gs_show_n_init(register gs_show_enum *penum,
  101.    gs_state *pgs, const char *str, uint size)
  102. {    setup_show_n();
  103.     penum->slow_show = 0;
  104.     return show_setup(penum, pgs, str);
  105. }
  106. int
  107. gs_show_init(gs_show_enum *penum, gs_state *pgs, const char *str)
  108. {    return gs_show_n_init(penum, pgs, str, strlen(str));
  109. }
  110.  
  111. /* ashow[_n] */
  112. int
  113. gs_ashow_n_init(register gs_show_enum *penum,
  114.   gs_state *pgs, floatp ax, floatp ay, const char *str, uint size)
  115. {    int code;
  116.     setup_show_n();
  117.     code = show_setup(penum, pgs, str);
  118.     setup_a();
  119.     return code;
  120. }
  121. int
  122. gs_ashow_init(gs_show_enum *penum,
  123.   gs_state *pgs, floatp ax, floatp ay, const char *str)
  124. {    return gs_ashow_n_init(penum, pgs, ax, ay, str, strlen(str));
  125. }
  126.  
  127. /* widthshow[_n] */
  128. int
  129. gs_widthshow_n_init(register gs_show_enum *penum,
  130.   gs_state *pgs, floatp cx, floatp cy, gs_char chr, const char *str, uint size)
  131. {    int code;
  132.     setup_show_n();
  133.     code = show_setup(penum, pgs, str);
  134.     setup_width();
  135.     return code;
  136. }
  137. int
  138. gs_widthshow_init(gs_show_enum *penum,
  139.   gs_state *pgs, floatp cx, floatp cy, gs_char chr, const char *str)
  140. {    return gs_widthshow_n_init(penum, pgs, cx, cy, chr, str, strlen(str));
  141. }
  142.  
  143. /* awidthshow[_n] */
  144. int
  145. gs_awidthshow_n_init(register gs_show_enum *penum,
  146.   gs_state *pgs, floatp cx, floatp cy, gs_char chr, floatp ax, floatp ay,
  147.   const char *str, uint size)
  148. {    int code;
  149.     setup_show_n();
  150.     code = show_setup(penum, pgs, str);
  151.     setup_a();
  152.     setup_width();
  153.     return code;
  154. }
  155. int
  156. gs_awidthshow_init(gs_show_enum *penum,
  157.   gs_state *pgs, floatp cx, floatp cy, gs_char chr, floatp ax, floatp ay,
  158.   const char *str)
  159. {    return gs_awidthshow_n_init(penum, pgs, cx, cy, chr, ax, ay,
  160.                     str, strlen(str));
  161. }
  162.  
  163. /* kshow[_n] */
  164. int
  165. gs_kshow_n_init(register gs_show_enum *penum,
  166.   gs_state *pgs, const char *str, uint size)
  167. {    int code;
  168.     if ( pgs->font->FontType == ft_composite)
  169.         return_error(gs_error_invalidfont);
  170.     setup_show_n();
  171.     code = show_setup(penum, pgs, str);
  172.     penum->do_kern = penum->slow_show = 1;
  173.     return code;
  174. }
  175. int
  176. gs_kshow_init(gs_show_enum *penum, gs_state *pgs, const char *str)
  177. {    return gs_kshow_n_init(penum, pgs, str, strlen(str));
  178. }
  179.  
  180. /* xyshow[_n] */
  181. int
  182. gs_xyshow_n_init(register gs_show_enum *penum,
  183.    gs_state *pgs, const char *str, uint size)
  184. {    int code;
  185.     setup_show_n();
  186.     code = show_setup(penum, pgs, str);
  187.     penum->do_kern = -1;
  188.     penum->slow_show = 1;
  189.     return code;
  190. }
  191. int
  192. gs_xyshow_init(gs_show_enum *penum, gs_state *pgs, const char *str)
  193. {    return gs_xyshow_n_init(penum, pgs, str, strlen(str));
  194. }
  195.  
  196. /* glyphshow */
  197. private font_proc_encode_char(gs_glyphshow_encode_char);
  198. int
  199. gs_glyphshow_init(gs_show_enum *penum, gs_state *pgs, gs_glyph glyph)
  200. {    int code;
  201.     if ( pgs->font->FontType == ft_composite)
  202.         return_error(gs_error_invalidfont);
  203.     penum->str.size = 1;
  204.     penum->slow_show = 0;
  205.     code = show_setup(penum, pgs, "\000");    /* arbitrary char */
  206.     penum->current_glyph = glyph;
  207.     penum->encode_char = gs_glyphshow_encode_char;
  208.     return code;
  209. }
  210. private gs_glyph
  211. gs_glyphshow_encode_char(gs_show_enum *penum, gs_font *pfont, gs_char *pchr)
  212. {    /* We just nil out the character, and return the pre-loaded glyph. */
  213.     *pchr = gs_no_char;
  214.     return penum->current_glyph;
  215. }
  216.  
  217. /* ------ Related operators ------ */
  218.  
  219. /* cshow[_n] */
  220. int
  221. gs_cshow_n_init(register gs_show_enum *penum,
  222.    gs_state *pgs, const char *str, uint size)
  223. {    int code;
  224.     setup_show_n();
  225.     code = show_setup(penum, pgs, str);
  226.     penum->do_kern = -1;
  227.     penum->stringwidth_flag = -1;
  228.     penum->slow_show = 1;
  229.     return code;
  230. }
  231. int
  232. gs_cshow_init(gs_show_enum *penum, gs_state *pgs, const char *str)
  233. {    return gs_cshow_n_init(penum, pgs, str, strlen(str));
  234. }
  235.  
  236. /* stringwidth[_n] */
  237. int
  238. gs_stringwidth_n_init(gs_show_enum *penum, gs_state *pgs, const char *str, uint size)
  239. {    setup_show_n();
  240.     return stringwidth_setup(penum, pgs, str);
  241. }
  242. int
  243. gs_stringwidth_init(gs_show_enum *penum, gs_state *pgs, const char *str)
  244. {    return gs_stringwidth_n_init(penum, pgs, str, strlen(str));
  245. }
  246.  
  247. /* Common code for stringwidth[_n] */
  248. private int
  249. stringwidth_setup(gs_show_enum *penum, gs_state *pgs, const char *str)
  250. {    int code = (penum->slow_show = 0, show_setup(penum, pgs, str));
  251.     gs_memory_t *mem = pgs->memory;
  252.     gx_device_null *dev_null;
  253.     if ( code < 0 )
  254.       return code;
  255.     dev_null = gs_alloc_struct(mem, gx_device_null, &st_device_null,
  256.                    "stringwidth_setup");
  257.     if ( dev_null == 0 )
  258.       return_error(gs_error_VMerror);
  259.     penum->stringwidth_flag = 1;
  260.     /* Do an extra gsave and suppress output */
  261.     if ( (code = gs_gsave(pgs)) < 0 ) return code;
  262.     penum->level = pgs->level;    /* for level check in show_update */
  263.     /* Set up a null device that forwards xfont requests properly. */
  264.     gs_make_null_device(dev_null, mem);
  265.     dev_null->target = gs_currentdevice_inline(pgs);
  266.     pgs->device = (gx_device *)dev_null;
  267.     penum->dev_null = dev_null;
  268.     /* Establish an arbitrary current point. */
  269.     return gx_path_add_point(pgs->path, pgs->ctm.tx_fixed, pgs->ctm.ty_fixed);
  270. }
  271.  
  272. /* charpath[_n] */
  273. int
  274. gs_charpath_n_init(gs_show_enum *penum, gs_state *pgs,
  275.   const char *str, uint size, int stroke_path)
  276. {    int code;
  277.     setup_show_n();
  278.     code = show_setup(penum, pgs, str);
  279.     penum->charpath_flag = (stroke_path ? 2 : 1);
  280.     penum->can_cache = 0;
  281.     return code;
  282. }
  283. int
  284. gs_charpath_init(gs_show_enum *penum, gs_state *pgs,
  285.   const char *str, int stroke_path)
  286. {    return gs_charpath_n_init(penum, pgs, str, strlen(str), stroke_path);
  287. }
  288.  
  289. /* ------ Width/cache operators ------ */
  290.  
  291. private int set_cache_device(P3(gs_show_enum *, gs_state *,
  292.   const float * /*[4]*/));
  293.  
  294. /* setcachedevice */
  295. /* The elements of pw are: wx, wy, llx, lly, urx, ury. */
  296. int
  297. gs_setcachedevice(gs_show_enum *penum, gs_state *pgs, const float *pw)
  298. {    int wcode = gs_setcharwidth(penum, pgs, pw[0], pw[1]);    /* default is don't cache */
  299.     int code;
  300.     if ( wcode < 0 ) return code;
  301.     code = set_cache_device(penum, pgs, pw + 2);
  302.     return (code < 0 ? code : wcode);
  303. }
  304.  
  305. /* setcachedevice2 */
  306. /* The elements of pw2 are: w0x, w0y, llx, lly, urx, ury, w1x, w1y, vx, vy. */
  307. int
  308. gs_setcachedevice2(gs_show_enum *penum, gs_state *pgs, const float *pw2)
  309. {    int wcode, code;
  310.     if ( gs_rootfont(pgs)->WMode )
  311.     {    float vx = pw2[8], vy = pw2[9];
  312.         gs_fixed_point pvxy, dvxy;
  313.         cached_char *cc;
  314.         if ( (code = gs_point_transform2fixed(&pgs->ctm, -vx, -vy, &pvxy)) < 0 ||
  315.              (code = gs_distance_transform2fixed(&pgs->ctm, vx, vy, &dvxy)) < 0 ||
  316.              (code = wcode = gs_setcharwidth(penum, pgs, pw2[6], pw2[7])) < 0
  317.            )
  318.           return code;
  319.         /* Adjust the origin by (vx, vy). */
  320.         gx_translate_to_fixed(pgs, pvxy.x, pvxy.y);
  321.         code = set_cache_device(penum, pgs, pw2 + 2);
  322.         if ( code != 1 )
  323.           return (code < 0 ? code : wcode);
  324.         /* Adjust the character origin too. */
  325.         cc = penum->cc;
  326.         cc->offset.x += dvxy.x;
  327.         cc->offset.y += dvxy.y;
  328.         return wcode;
  329.     }
  330.     else
  331.     {    wcode = gs_setcharwidth(penum, pgs, pw2[0], pw2[1]);
  332.         if ( wcode < 0 )
  333.           return wcode;
  334.         code = set_cache_device(penum, pgs, pw2 + 2);
  335.         return (code < 0 ? code : wcode);
  336.     }
  337. }
  338.  
  339. /* Set up the cache device if relevant. */
  340. /* Return 1 if we just set up a cache device. */
  341. /* Used by setcachedevice and setcachedevice2. */
  342. /* The elements of pb are: llx, lly, urx, ury. */
  343. private int
  344. set_cache_device(register gs_show_enum *penum, gs_state *pgs, const float *pb)
  345. {    float llx = pb[0], lly = pb[1], urx = pb[2], ury = pb[3];
  346.     /* See if we want to cache this character. */
  347.     if ( pgs->in_cachedevice )        /* no recursion! */
  348.         return 0;
  349.     pgs->in_cachedevice = 1;    /* disable color/gray/image operators */
  350.     /* We can only use the cache if ctm is unchanged */
  351.     /* (aside from a possible translation), */
  352.     /* and if the extent of the box is non-negative. */
  353.     if ( !penum->can_cache || !pgs->char_tm_valid ||
  354.          llx > urx || lly > ury
  355.        )
  356.         return 0;
  357.        {    gs_font_dir *dir = pgs->font->dir;
  358.         gs_fixed_point cbox_ll, cbox_ur, cdim;
  359.         gs_int_point log2_scale;
  360.         static const fixed max_cdim[3] = {
  361. #define max_cd(n)\
  362.   (fixed_1 << (arch_sizeof_short * 8 - n)) - (fixed_1 >> n) * 3
  363.             max_cd(0), max_cd(1), max_cd(2)
  364. #undef max_cd
  365.         };
  366.         ushort iwidth, iheight;
  367.         cached_char *cc;
  368.         gs_fixed_rect clip_box;
  369.         int code;
  370.         if ( (code = gs_distance_transform2fixed(&pgs->ctm, llx, lly, &cbox_ll)) < 0 ||
  371.              (code = gs_distance_transform2fixed(&pgs->ctm, urx, ury, &cbox_ur))
  372.            )
  373.           return code;
  374.         cdim.x = cbox_ur.x - cbox_ll.x;
  375.         cdim.y = cbox_ur.y - cbox_ll.y;
  376.         if ( cdim.x < 0 ) cdim.x = -cdim.x;
  377.         if ( cdim.y < 0 ) cdim.y = -cdim.y;
  378.         show_set_scale(penum);
  379.         log2_scale.x = penum->log2_suggested_scale.x;
  380.         log2_scale.y = penum->log2_suggested_scale.y;
  381. #ifdef DEBUG
  382. if ( gs_debug_c('k') )
  383.    {    dprintf6("[k]cbox=[%g %g %g %g] scale=%dx%d\n",
  384.          fixed2float(cbox_ll.x), fixed2float(cbox_ll.y),
  385.          fixed2float(cbox_ur.x), fixed2float(cbox_ur.y),
  386.          1 << log2_scale.x, 1 << log2_scale.y);
  387.     print_ctm("  ", pgs);
  388.    }
  389. #endif
  390.         if ( cdim.x > max_cdim[log2_scale.x] ||
  391.              cdim.y > max_cdim[log2_scale.y]
  392.            )
  393.           return 0;        /* much too big */
  394.         iwidth = ((ushort)fixed2int_var(cdim.x)  + 2) << log2_scale.x;
  395.         iheight = ((ushort)fixed2int_var(cdim.y) + 2) << log2_scale.y;
  396.         if ( penum->dev_cache == 0 )
  397.           {    code = show_cache_setup(penum);
  398.             if ( code < 0 )
  399.               return code;
  400.           }
  401.         cc = gx_alloc_char_bits(dir, penum->dev_cache,
  402.                     iwidth, iheight);
  403.         if ( cc == 0 )
  404.             return 0;    /* too big for cache */
  405.         /* The mins handle transposed coordinate systems.... */
  406.         /* Truncate the offsets to avoid artifacts later. */
  407.         cc->offset.x = fixed_ceiling(-min(cbox_ll.x, cbox_ur.x));
  408.         cc->offset.y = fixed_ceiling(-min(cbox_ll.y, cbox_ur.y));
  409.         if_debug4('k', "[k]width=%u, height=%u, offset=[%g %g]\n",
  410.               (uint)iwidth, (uint)iheight,
  411.               fixed2float(cc->offset.x),
  412.               fixed2float(cc->offset.y));
  413.         if ( (code = gs_gsave(pgs)) < 0 )
  414.            {    gx_free_cached_char(dir, cc);
  415.             return code;
  416.            }
  417.         /* Nothing can go wrong now.... */
  418.         penum->cc = cc;
  419.         cc->code = gs_show_current_glyph(penum);
  420.         cc->wmode = gs_rootfont(pgs)->WMode;
  421.         cc->depth = 1;
  422.         cc->wxy = penum->wxy;
  423.         /* Install the device */
  424.         pgs->device = (gx_device *)penum->dev_cache;
  425.         /* Adjust the transformation in the graphics context */
  426.         /* so that the character lines up with the cache. */
  427.         gx_translate_to_fixed(pgs,
  428.                       cc->offset.x << log2_scale.x,
  429.                       cc->offset.y << log2_scale.y);
  430.         if ( (log2_scale.x | log2_scale.y) != 0 )
  431.             gx_scale_char_matrix(pgs, 1 << log2_scale.x,
  432.                          1 << log2_scale.y);
  433.         /* Set the initial matrix for the cache device. */
  434.         penum->dev_cache->initial_matrix = ctm_only(pgs);
  435.         /* Set the oversampling factor. */
  436.         penum->log2_current_scale.x = log2_scale.x;
  437.         penum->log2_current_scale.y = log2_scale.y;
  438.         /* Reset the clipping path to match the metrics. */
  439.         clip_box.p.x = clip_box.p.y = 0;
  440.         clip_box.q.x = int2fixed(iwidth);
  441.         clip_box.q.y = int2fixed(iheight);
  442.         if ( (code = gx_clip_to_rectangle(pgs, &clip_box)) < 0 )
  443.           return code;
  444.         gx_set_black(pgs);    /* Set the color to black. */
  445.        }
  446.     penum->width_status = sws_cache;
  447.     return 1;
  448. }
  449.  
  450. /* setcharwidth */
  451. int
  452. gs_setcharwidth(register gs_show_enum *penum, gs_state *pgs,
  453.   floatp wx, floatp wy)
  454. {    int code;
  455.     if ( penum->width_status != sws_none )
  456.       return_error(gs_error_undefined);
  457.     if ( (code = gs_distance_transform2fixed(&pgs->ctm, wx, wy, &penum->wxy)) < 0 )
  458.       return code;
  459.     /* Check whether we're setting the scalable width */
  460.     /* for a cached xfont character. */
  461.     if ( penum->cc != 0 )
  462.       {    penum->cc->wxy = penum->wxy;
  463.         penum->width_status = sws_cache_width_only;
  464.       }
  465.     else
  466.       {    penum->width_status = sws_no_cache;
  467.       }
  468.     return (penum->stringwidth_flag != 0 ? 1 : 0);
  469. }
  470.  
  471. /* ------ Enumerator ------ */
  472.  
  473. /* Do the next step of a show (or stringwidth) operation */
  474. int
  475. gs_show_next(gs_show_enum *penum)
  476. {    return (*penum->continue_proc)(penum);
  477. }
  478.  
  479. /* Continuation procedures */
  480. #define show_fast_move(wxy, pgs)\
  481.   gx_path_add_rel_point_inline(pgs->path, wxy.x, wxy.y)
  482. private int show_update(P1(gs_show_enum *penum));
  483. private int show_move(P1(gs_show_enum *penum));
  484. private int show_proceed(P1(gs_show_enum *penum));
  485. private int show_finish(P1(gs_show_enum *penum));
  486. private int
  487. continue_show_update(register gs_show_enum *penum)
  488. {    int code = show_update(penum);
  489.     if ( code < 0 ) return code;
  490.     code = show_move(penum);
  491.     if ( code != 0 ) return code;
  492.     return show_proceed(penum);
  493. }
  494. private int
  495. continue_show(register gs_show_enum *penum)
  496. {    return show_proceed(penum);
  497. }
  498. /* For kshow, the CTM or font may have changed, so we have to reestablish */
  499. /* the cached values in the enumerator. */
  500. private int
  501. continue_kshow(register gs_show_enum *penum)
  502. {    int code = show_state_setup(penum);
  503.     if ( code < 0 ) return code;
  504.     return show_proceed(penum);
  505. }
  506.  
  507. /* Update position */
  508. private int
  509. show_update(register gs_show_enum *penum)
  510. {    register gs_state *pgs = penum->pgs;
  511.     cached_char *cc = penum->cc;
  512.     int code;
  513.     /* Update position for last character */
  514.     switch ( penum->width_status )
  515.        {
  516.     case sws_none:
  517.         /* Adobe interpreters assume a character width of 0, */
  518.         /* even though the documentation says this is an error.... */
  519.         penum->wxy.x = penum->wxy.y = 0;
  520.         break;
  521.     case sws_cache:
  522.         /* Finish installing the cache entry. */
  523.         /* If the BuildChar/BuildGlyph procedure did a save and a */
  524.         /* restore, it already undid the gsave in setcachedevice. */
  525.         /* We have to check for this by comparing levels. */
  526.         switch ( pgs->level - penum->level )
  527.            {
  528.         default:
  529.             return_error(gs_error_invalidfont);    /* WRONG */
  530.         case 2:
  531.             code = gs_grestore(pgs);
  532.             if ( code < 0 ) return code;
  533.         case 1:
  534.             ;
  535.            }
  536.         gx_add_cached_char(pgs->font->dir, penum->dev_cache,
  537.                    cc, gx_lookup_fm_pair(pgs->font, pgs),
  538.                    &penum->log2_current_scale);
  539.         if ( penum->stringwidth_flag || penum->charpath_flag )
  540.             break;
  541.         /* falls through */
  542.     case sws_cache_width_only:
  543.         /* Copy the bits to the real output device. */
  544.         code = gs_grestore(pgs);
  545.         if ( code < 0 ) return code;
  546.         code = gx_color_load(pgs->dev_color, pgs);
  547.         if ( code < 0 ) return code;
  548.         return gx_image_cached_char(penum, cc);
  549.     case sws_no_cache:
  550.         ;
  551.        }
  552.     if ( penum->charpath_flag )
  553.     {    /* Move back to the character origin, so that */
  554.         /* show_move will get us to the right place. */
  555.         code = gx_path_add_point(pgs->show_gstate->path,
  556.                      penum->origin.x, penum->origin.y);
  557.         if ( code < 0 ) return code;
  558.     }
  559.     return gs_grestore(pgs);
  560. }
  561.  
  562. /* Move to next character */
  563. private int
  564. show_move(register gs_show_enum *penum)
  565. {    register gs_state *pgs = penum->pgs;
  566.     if ( penum->do_kern < 0 )
  567.     {    /* xyshow or cshow */
  568.         penum->continue_proc = continue_show;
  569.         return gs_show_move;
  570.     }
  571.     if ( penum->add )
  572.         gs_rmoveto(pgs, penum->ax, penum->ay);
  573.     if ( penum->wchr != gs_no_char )
  574.     {    gs_char chr = penum->current_char;
  575.         int fdepth = penum->fstack.depth;
  576.         if ( fdepth > 0 )
  577.         {    /* Add in the shifted font number. */
  578.             uint fidx = penum->fstack.items[fdepth].index;
  579.             switch ( ((gs_font_type0 *)(penum->fstack.items[fdepth - 1].font))->data.FMapType )
  580.             {
  581.             case fmap_1_7:
  582.             case fmap_9_7:
  583.                 chr += fidx << 7;
  584.                 break;
  585.             default:
  586.                 chr += fidx << 8;
  587.             }
  588.         }
  589.         if ( chr == penum->wchr )
  590.             gs_rmoveto(pgs, penum->wcx, penum->wcy);
  591.     }
  592.     /* wxy is in device coordinates */
  593.        {    int code = show_fast_move(penum->wxy, pgs);
  594.         if ( code < 0 ) return code;
  595.        }
  596.     /* Check for kerning, but not on the last character. */
  597.     if ( penum->do_kern && penum->index < penum->str.size )
  598.        {    penum->continue_proc = continue_kshow;
  599.         return gs_show_kern;
  600.        }
  601.     return 0;
  602. }
  603. /* Process next character */
  604. private int
  605. show_proceed(register gs_show_enum *penum)
  606. {    register gs_state *pgs = penum->pgs;
  607.     gs_font *pfont;
  608.     cached_fm_pair *pair = 0;
  609.     gs_font *rfont = gs_rootfont(pgs);
  610.     int wmode = rfont->WMode;
  611.     font_proc_next_char((*next_char)) = rfont->procs.next_char;
  612.     gs_char chr;
  613.     gs_glyph glyph;
  614.     int code;
  615.     cached_char *cc;
  616.     code = gx_color_load(pgs->dev_color, pgs);
  617.     if ( code < 0 ) return code;
  618. more:    /* Proceed to next character */
  619.     pfont = (penum->fstack.depth < 0 ? pgs->font :
  620.          penum->fstack.items[penum->fstack.depth].font);
  621.     if ( penum->can_cache )
  622.     {    /* Loop with cache */
  623.         if ( pair == 0 )
  624.             pair = gx_lookup_fm_pair(pfont, pgs);
  625.         for ( ; ; )
  626.         {   switch ( (code = (*next_char)(penum, &chr)) )
  627.             {
  628.             default:            /* error */
  629.             return code;
  630.             case 2:                /* done */
  631.             return show_finish(penum);
  632.             case 1:                /* font change */
  633.             pfont = penum->fstack.items[penum->fstack.depth].font;
  634.             pgs->char_tm_valid = 0;
  635.             show_state_setup(penum);
  636.             pair = gx_lookup_fm_pair(pfont, pgs);
  637.             /* falls through */
  638.             case 0:                /* plain char */
  639.             glyph = (*penum->encode_char)(penum, pfont, &chr);
  640.             if ( glyph == gs_no_glyph )
  641.                 goto no_cache;
  642.             cc = gx_lookup_cached_char(pfont, pair, glyph, wmode, 1);
  643.             if ( cc == 0 )
  644.               { /* Character is not in cache. */
  645.                 /* If possible, try for an xfont before */
  646.                 /* rendering from the outline. */
  647.                 if ( pfont->ExactSize == fbit_use_outlines )
  648.                   goto no_cache;
  649.                 if ( pfont->BitmapWidths )
  650.                   { cc = gx_lookup_xfont_char(pgs, pair, chr,
  651.                     glyph, pfont->procs.glyph_name, wmode);
  652.                 if ( cc == 0 )
  653.                   goto no_cache;
  654.                   }
  655.                 else
  656.                   { if ( penum->stringwidth_flag != 0 )
  657.                   goto no_cache;
  658.                 /* We might have an xfont, but we still */
  659.                 /* want the scalable widths. */
  660.                 cc = gx_lookup_xfont_char(pgs, pair, chr,
  661.                     glyph, pfont->procs.glyph_name, wmode);
  662.                 /* Run the scaler up to the point of */
  663.                 /* setcharwidth or setcachedevice, */
  664.                 /* just as for stringwidth. */
  665.                 /* This is the only case in which we can */
  666.                 /* to go no_cache with cc != 0. */
  667.                 goto no_cache;
  668.                   }
  669.               }
  670.             /* Character is in cache. */
  671.             if ( !penum->stringwidth_flag )
  672.               { code = gx_image_cached_char(penum, cc);
  673.                 if ( code < 0 ) return code;
  674.                 else if ( code > 0 )
  675.                   { cc = 0;
  676.                 goto no_cache;
  677.                   }
  678.               }
  679.             if ( penum->slow_show )
  680.               { penum->current_char = chr; /* for widthshow */
  681.                 /* Split up the assignment so that the */
  682.                 /* Watcom compiler won't reserve esi/edi. */
  683.                 penum->wxy.x = cc->wxy.x;
  684.                 penum->wxy.y = cc->wxy.y;
  685.                 code = show_move(penum);
  686.               }
  687.             else
  688.                 code = show_fast_move(cc->wxy, pgs);
  689.             if ( code )
  690.               { /* Might be kshow, so store the state. */
  691.                 penum->current_char = chr;
  692.                 penum->current_glyph = glyph;
  693.                 return code;
  694.               }
  695.             }
  696.         }
  697.     }
  698.     else
  699.     {    /* Can't use cache */
  700.         switch ( (code = (*next_char)(penum, &chr)) )
  701.         {
  702.         default:
  703.             return code;
  704.         case 2:
  705.             return show_finish(penum);
  706.         case 1:
  707.             pfont = penum->fstack.items[penum->fstack.depth].font;
  708.             show_state_setup(penum);
  709.         case 0:
  710.             ;
  711.         }
  712.         glyph = (*penum->encode_char)(penum, pfont, &chr);
  713.         cc = 0;
  714.     }
  715. no_cache:
  716.     /*
  717.      * We must call the client's rendering code.  Normally,
  718.      * we only do this if the character is not cached (cc = 0);
  719.      * however, we also must do this if we have an xfont but
  720.      * are using scalable widths.  In this case, and only this case,
  721.      * we get here with cc != 0.
  722.      */
  723.     penum->current_char = chr;
  724.     penum->current_glyph = glyph;
  725.     if ( (code = gs_gsave(pgs)) < 0 )
  726.         return code;
  727.     /* Set the font to the current descendant font. */
  728.     pgs->font = pfont;
  729.     /* Reset the in_cachedevice flag, so that a recursive show */
  730.     /* will use the cache properly. */
  731.     pgs->in_cachedevice = 0;
  732.     /* Reset the sampling scale. */
  733.     penum->log2_current_scale.x = penum->log2_current_scale.y = 0;
  734.     /* Set the charpath flag in the graphics context if necessary, */
  735.     /* so that fill and stroke will add to the path */
  736.     /* rather than having their usual effect. */
  737.     pgs->in_charpath = penum->charpath_flag;
  738.     pgs->stroke_adjust = 0;        /* per specification */
  739.        {    gs_fixed_point cpt;
  740.         gx_path *ppath = pgs->path;
  741.         if ( (code = gx_path_current_point_inline(ppath, &cpt)) < 0 )
  742.             return code;
  743.         cpt.x -= pgs->ctm.tx_fixed;
  744.         cpt.y -= pgs->ctm.ty_fixed;
  745.         gs_settocharmatrix(pgs);
  746.         penum->origin.x = cpt.x += pgs->ctm.tx_fixed;
  747.         penum->origin.y = cpt.y += pgs->ctm.ty_fixed;
  748.         gs_newpath(pgs);
  749.         code = show_origin_setup(pgs, cpt.x, cpt.y,
  750.                      penum->charpath_flag);
  751.         if ( code < 0 ) return code;
  752.        }
  753.     penum->width_status = sws_none;
  754.     penum->continue_proc = continue_show_update;
  755.     /* Try using the build procedure in the font. */
  756.     /* < 0 means error, 0 means success, 1 means failure. */
  757.     code = (*pfont->procs.build_char)(penum, pgs, pfont, chr, glyph);
  758.     if ( code < 0 ) return_error(code);
  759.     if ( code == 0 )
  760.        {    code = show_update(penum);
  761.         if ( code < 0 ) return code;
  762.         code = show_move(penum);
  763.         if ( code ) return code;
  764.         goto more;
  765.        }
  766.     penum->cc = cc;
  767.     return gs_show_render;
  768. }
  769.  
  770. /* Finish show or stringwidth */
  771. private int
  772. show_finish(gs_show_enum *penum)
  773. {    gs_state *pgs = penum->pgs;
  774.     gs_memory_t *mem = pgs->memory;
  775.     int code;
  776.     if ( penum->dev_cache != 0 )
  777.       {    gs_free_object(mem, penum->dev_cache,
  778.                    "show_finish(dev_cache)");
  779.         penum->dev_cache = 0;
  780.       }
  781.     if ( penum->dev_null != 0 )
  782.       {    gs_free_object(mem, penum->dev_null,
  783.                    "show_finish(dev_null)");
  784.         penum->dev_null = 0;
  785.       }
  786.     if ( penum->stringwidth_flag <= 0 )    /* could be cshow */
  787.         return 0;
  788.     /* Save the accumulated width before returning, */
  789.     /* and undo the extra gsave. */
  790.     code = gs_currentpoint(pgs, &penum->width);
  791.     if ( code < 0 )
  792.       return code;
  793.     return gs_grestore(pgs);
  794. }
  795.  
  796. /* Return the current character for rendering. */
  797. gs_char
  798. gs_show_current_char(const gs_show_enum *penum)
  799. {    return penum->current_char;
  800. }
  801.  
  802. /* Return the current glyph for rendering. */
  803. gs_glyph
  804. gs_show_current_glyph(const gs_show_enum *penum)
  805. {    return penum->current_glyph;
  806. }
  807.  
  808. /* Return the width of the just-enumerated character (for cshow). */
  809. int
  810. gs_show_current_width(const gs_show_enum *penum, gs_point *ppt)
  811. {    return gs_idtransform(penum->pgs, fixed2float(penum->wxy.x),
  812.                   fixed2float(penum->wxy.y), ppt);
  813. }
  814.  
  815. /* Return the just-displayed character for kerning. */
  816. gs_char
  817. gs_kshow_previous_char(const gs_show_enum *penum)
  818. {    return penum->current_char;
  819. }
  820.  
  821. /* Return the about-to-be-displayed character for kerning. */
  822. gs_char
  823. gs_kshow_next_char(const gs_show_enum *penum)
  824. {    return penum->str.data[penum->index];
  825. }
  826.  
  827. /* ------ Miscellaneous accessors ------ */
  828.  
  829. /* Return the accumulated width for stringwidth. */
  830. void
  831. gs_show_width(const gs_show_enum *penum, gs_point *ppt)
  832. {    *ppt = penum->width;
  833. }
  834.  
  835. /* Return the charpath flag. */
  836. int
  837. gs_show_in_charpath(const gs_show_enum *penum)
  838. {    return penum->charpath_flag;
  839. }
  840.  
  841. /* Return true if we only need the width from the rasterizer, */
  842. /* false if we need the actual character bits. */
  843. /* This is only meaningful just before calling gs_setcharwidth or */
  844. /* gs_setcachedevice[2]. */
  845. bool
  846. gs_show_width_only(const gs_show_enum *penum)
  847. {    /* penum->cc will be non-zero iff we are calculating */
  848.     /* the scalable width for an xfont character. */
  849.     return (penum->stringwidth_flag != 0 || penum->cc != 0);
  850. }
  851.  
  852. /* ------ Internal routines ------ */
  853.  
  854. /* Initialize a show enumerator. */
  855. private int
  856. show_setup(register gs_show_enum *penum, gs_state *pgs, const char *str)
  857. {    int code;
  858.     gs_font *pfont;
  859.     gx_set_dev_color(pgs);
  860.     pfont = pgs->font;
  861.     penum->pgs = pgs;
  862.     penum->level = pgs->level;
  863.     penum->str.data = (const byte *)str;    /* avoid signed chars */
  864.     penum->wchr = gs_no_char;
  865.     penum->add = 0;
  866.     penum->do_kern = 0;
  867.     penum->charpath_flag = 0;
  868.     penum->stringwidth_flag = 0;
  869.     penum->dev_cache = 0;
  870.     penum->dev_null = 0;
  871.     penum->index = 0;
  872.     penum->continue_proc = continue_show;
  873.     code = (*pfont->procs.init_fstack)(penum, pfont);
  874.     if ( code < 0 )
  875.       return code;
  876.     penum->can_cache = 1;        /* show_state_setup may reset */
  877.     code = show_state_setup(penum);
  878.     if ( code < 0 )
  879.       return code;
  880.     pgs->show_gstate = pgs;
  881.     return 0;
  882. }
  883.  
  884. /* Initialize the gstate-derived parts of a show enumerator. */
  885. /* We do this both when starting the show operation, */
  886. /* and when returning from the kshow callout. */
  887. private int
  888. show_state_setup(gs_show_enum *penum)
  889. {    gs_state *pgs = penum->pgs;
  890.     const gs_font *pfont;
  891.     if ( penum->fstack.depth <= 0 )
  892.     {    pfont = pgs->font;
  893.         gs_currentcharmatrix(pgs, NULL, 1); /* make char_tm valid */
  894.     }
  895.     else
  896.     {    /* We have to concatenate the parent's FontMatrix as well. */
  897.         gs_matrix mat;
  898.         const gx_font_stack_item *pfsi =
  899.             &penum->fstack.items[penum->fstack.depth];
  900.         pfont = pfsi->font;
  901.         gs_matrix_multiply(&pfsi[-1].font->FontMatrix,
  902.                    &pfont->FontMatrix, &mat);
  903.         gs_setcharmatrix(pgs, &mat);
  904.     }
  905.     if ( (penum->can_cache &= /* no skewing or non-rectangular rotation */
  906.         (is_fzero2(pgs->char_tm.xy, pgs->char_tm.yx) ||
  907.          is_fzero2(pgs->char_tm.xx, pgs->char_tm.yy))) )
  908.        {    gs_fixed_rect cbox;
  909.         gx_cpath_box_for_check(pgs->clip_path, &cbox);
  910.         /* Since characters occupy an integral number of pixels, */
  911.         /* we can (and should) round the inner clipping box */
  912.         /* outward rather than inward. */
  913.         penum->ibox.p.x = fixed2int_var(cbox.p.x);
  914.         penum->ibox.p.y = fixed2int_var(cbox.p.y);
  915.         penum->ibox.q.x = fixed2int_var_ceiling(cbox.q.x);
  916.         penum->ibox.q.y = fixed2int_var_ceiling(cbox.q.y);
  917.         gx_path_bbox(&pgs->clip_path->path, &cbox);
  918.         penum->obox.p.x = fixed2int_var(cbox.p.x);
  919.         penum->obox.p.y = fixed2int_var(cbox.p.y);
  920.         penum->obox.q.x = fixed2int_var_ceiling(cbox.q.x);
  921.         penum->obox.q.y = fixed2int_var_ceiling(cbox.q.y);
  922.         penum->ftx = (int)fixed2long(pgs->char_tm.tx_fixed - pgs->ctm.tx_fixed);
  923.         penum->fty = (int)fixed2long(pgs->char_tm.ty_fixed - pgs->ctm.ty_fixed);
  924.        }
  925.     penum->encode_char = pfont->procs.encode_char;
  926.     return 0;
  927. }
  928.  
  929. /* Set the suggested oversampling scale for character rendering. */
  930. private void
  931. show_set_scale(gs_show_enum *penum)
  932. {    /* Decide whether to oversample. */
  933.     /* We have to decide this each time setcachedevice is called. */
  934.     const gs_state *pgs = penum->pgs;
  935.     if ( !penum->charpath_flag &&
  936.          !penum->stringwidth_flag &&
  937.          gx_path_is_void_inline(pgs->path)
  938.        )
  939.       {    const gs_font_base *pfont = (gs_font_base *)pgs->font;
  940.         gs_fixed_point extent;
  941.         int code = gs_distance_transform2fixed(&pgs->char_tm,
  942.             pfont->FontBBox.q.x - pfont->FontBBox.p.x,
  943.             pfont->FontBBox.q.y - pfont->FontBBox.p.y,
  944.             &extent);
  945.         if ( code >= 0 )
  946.           {    penum->log2_suggested_scale.x =
  947.               (extent.x != 0 && any_abs(extent.x) < int2fixed(16) ? 2 : 0);
  948.             penum->log2_suggested_scale.y =
  949.               (extent.y != 0 && any_abs(extent.y) < int2fixed(16) ? 2 : 0);
  950.             return;
  951.           }
  952.       }
  953.     /* By default, don't scale. */
  954.     penum->log2_suggested_scale.x =
  955.       penum->log2_suggested_scale.y = 0;
  956. }
  957.  
  958. /* Set up the cache device and related information. */
  959. private int
  960. show_cache_setup(gs_show_enum *penum)
  961. {    gs_state *pgs = penum->pgs;
  962.     gs_memory_t *mem = pgs->memory;
  963.     gx_device_memory *dev =
  964.       gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  965.               "show_cache_setup");
  966.     if ( dev == 0 )
  967.       return_error(gs_error_VMerror);
  968.     gs_make_mem_mono_device(dev, mem);
  969.     dev->target = gs_currentdevice_inline(pgs);
  970.     dev->cached.white = 1;
  971.     dev->cached.black = 1;
  972.     penum->dev_cache = dev;
  973.     return 0;
  974. }
  975.  
  976. /* Set the character origin as the origin of the coordinate system. */
  977. /* Used before rendering characters, and for moving the origin */
  978. /* in setcachedevice2 when WMode=1. */
  979. private int
  980. show_origin_setup(gs_state *pgs, fixed cpt_x, fixed cpt_y, int charpath_flag)
  981. {    if ( !charpath_flag )
  982.       { /* Round the translation in the graphics state. */
  983.         /* This helps prevent rounding artifacts later. */
  984.         cpt_x = fixed_rounded(cpt_x);
  985.         cpt_y = fixed_rounded(cpt_y);
  986.       }
  987.     gx_translate_to_fixed(pgs, cpt_x, cpt_y);
  988.     return gx_path_add_point(pgs->path, pgs->ctm.tx_fixed,
  989.                  pgs->ctm.ty_fixed);
  990. }
  991.  
  992. /* Default fstack initialization procedure. */
  993. int
  994. gs_default_init_fstack(gs_show_enum *penum, gs_font *pfont)
  995. {    penum->fstack.depth = -1;
  996.     return 0;
  997. }
  998.  
  999. /* Default next-character procedure. */
  1000. int
  1001. gs_default_next_char(gs_show_enum *penum, gs_char *pchr)
  1002. {    if ( penum->index == penum->str.size )
  1003.         return 2;
  1004.     else
  1005.     {    *pchr = penum->str.data[penum->index++];
  1006.         return 0;
  1007.     }
  1008. }
  1009.