home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSCHAR0.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  10KB  |  336 lines

  1. /* Copyright (C) 1991, 1992, 1993 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. /* gschar0.c */
  20. /* Composite font decoding for Ghostscript library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxfixed.h"
  26. #include "gxdevice.h"
  27. #include "gxdevmem.h"            /* for gxchar.h */
  28. #include "gxchar.h"
  29. #include "gxfont.h"
  30. #include "gxfont0.h"
  31.  
  32. /* Stack up modal composite fonts, down to a non-modal or base font. */
  33. private int
  34. gs_stack_modal_fonts(gs_show_enum *penum)
  35. {    int fdepth = penum->fstack.depth;
  36.     gs_font *cfont = penum->fstack.items[fdepth].font;
  37. #define cmfont ((gs_font_type0 *)cfont)
  38.     while ( cfont->FontType == ft_composite &&
  39.         fmap_type_is_modal(cmfont->data.FMapType)
  40.           )
  41.     {    if ( fdepth == max_font_depth )
  42.             return_error(gs_error_invalidfont);
  43.         fdepth++;
  44.         cfont = cmfont->data.FDepVector[cmfont->data.Encoding[0]];
  45.         penum->fstack.items[fdepth].font = cfont;
  46.         penum->fstack.items[fdepth].index = 0;
  47.         if_debug2('j', "[j]stacking depth=%d font=0x%lx\n",
  48.               fdepth, (ulong)cfont);
  49.     }
  50.     penum->fstack.depth = fdepth;
  51.     return 0;
  52. #undef cmfont
  53. }
  54. /* Initialize the composite font stack for a show enumerator. */
  55. int
  56. gs_type0_init_fstack(gs_show_enum *penum, gs_font *pfont)
  57. {    if_debug1('j', "[j]stacking depth=0 font=0x%lx\n",
  58.           (ulong)pfont);
  59.     penum->fstack.depth = 0;
  60.     penum->fstack.items[0].font = pfont;
  61.     penum->fstack.items[0].index = 0;
  62.     return gs_stack_modal_fonts(penum);
  63. }
  64.  
  65. /* Select the appropriate descendant of a font. */
  66. /* Uses free variables: penum. */
  67. /* Uses pdata, uses & updates fdepth, sets pfont. */
  68. #define select_descendant(pfont, pdata, fidx, fdepth)\
  69.   if ( fidx >= pdata->encoding_size )\
  70.     return_error(gs_error_rangecheck);\
  71.   if ( fdepth == max_font_depth )\
  72.     return_error(gs_error_invalidfont);\
  73.   pfont = pdata->FDepVector[pdata->Encoding[fidx]];\
  74.   if ( ++fdepth > orig_depth || pfont != penum->fstack.items[fdepth].font )\
  75.     penum->fstack.items[fdepth].font = pfont,\
  76.     changed = 1;\
  77.   penum->fstack.items[fdepth].index = fidx
  78.  
  79. /* Get the next character from a composite string. */
  80. /* If we run off the end of the string in the middle of a */
  81. /* multi-byte sequence, return gs_error_rangecheck. */
  82. /* If the string is empty, return 2. */
  83. /* If the current (base) font changed, return 1.  Otherwise, return 0. */
  84. int
  85. gs_type0_next_char(register gs_show_enum *penum, gs_char *pchr)
  86. {    const byte *p = penum->str.data + penum->index;
  87.     const byte *end = penum->str.data + penum->str.size;
  88.     int fdepth = penum->fstack.depth;
  89.     int orig_depth = fdepth;
  90.     gs_font *pfont;
  91. #define pfont0 ((gs_font_type0 *)pfont)
  92.     gs_type0_data *pdata;
  93.     uint fidx;
  94.     gs_char chr;
  95.     int changed = 0;
  96.     bool first = true;
  97. #define need_left(n)\
  98.   if ( end - p < n ) return_error(gs_error_rangecheck)
  99. #define root_EscChar\
  100.   (((gs_font_type0 *)(penum->fstack.items[0].font))->data.EscChar)    /* root overrides */
  101.  
  102.     /*
  103.      * Although the Adobe documentation doesn't say anything about this,
  104.      * if the root font is modal and the very first character of the
  105.      * string being decoded is an escape or shift character, then
  106.      * font selection via the escape mechanism works down from the root,
  107.      * rather than up from the lowest modal font.  (This was first
  108.      * reported by Norio Katayama, and confirmed by someone at Adobe.)
  109.      */
  110.  
  111.     if ( penum->index == 0 )
  112.       { int idepth = 0;
  113.         pfont = penum->fstack.items[0].font;
  114.         for ( ; pfont->FontType == ft_composite; )
  115.           { fmap_type fmt = (pdata = &pfont0->data)->FMapType;
  116.         if ( p == end )
  117.           return 2;
  118.         chr = *p;
  119.         switch ( fmt )
  120.           {
  121.           case fmap_escape:
  122.             if ( chr != root_EscChar )
  123.               break;
  124.             need_left(2);
  125.             fidx = p[1];
  126.             p += 2;
  127.             if_debug1('j', "[j]from root: escape %d\n", fidx);
  128. rdown:            select_descendant(pfont, pdata, fidx, idepth);
  129.             if_debug2('j', "[j]... new depth=%d, new font=0x%lx\n",
  130.                   idepth, (ulong)pfont);
  131.             continue;
  132.           case fmap_double_escape:
  133.             if ( chr != root_EscChar )
  134.               break;
  135.             need_left(2);
  136.             fidx = p[1];
  137.             p += 2;
  138.             if ( fidx == chr )
  139.               {    need_left(1);
  140.             fidx = *p++ + 256;
  141.               }
  142.             if_debug1('j', "[j]from root: double escape %d\n", fidx);
  143.             goto rdown;
  144.           case fmap_shift:
  145.             if ( chr == pdata->ShiftIn )
  146.               fidx = 0;
  147.             else if ( chr == pdata->ShiftOut )
  148.               fidx = 1;
  149.             else break;
  150.             p++;
  151.             if_debug1('j', "[j]from root: shift %d\n", fidx);
  152.             goto rdown;
  153.           default:
  154.             break;
  155.           }
  156.         break;
  157.           }
  158.         /* If we saw any initial escapes or shifts, */
  159.         /* compute a new initial base font. */
  160.         if ( idepth != 0 )
  161.           { int code;
  162.         penum->fstack.depth = idepth;
  163.         code = gs_stack_modal_fonts(penum);
  164.         if ( code < 0 )
  165.           return code;
  166.         if ( penum->fstack.depth > idepth )
  167.           changed = 1;
  168.         orig_depth = fdepth = penum->fstack.depth;
  169.           }
  170.       }
  171.  
  172.         /* Handle initial escapes or shifts. */
  173.  
  174. up:    if ( p == end )
  175.         return 2;
  176.     chr = *p;
  177.     while ( fdepth > 0 )
  178.     {    pfont = penum->fstack.items[fdepth - 1].font;
  179.         pdata = &pfont0->data;
  180.         switch ( pdata->FMapType )
  181.         {
  182.         default:            /* non-modal */
  183.             fdepth--;
  184.             continue;
  185.  
  186.         case fmap_escape:
  187.             if ( chr != root_EscChar )
  188.                 break;
  189.             need_left(2);
  190.             fidx = *++p;
  191.             if_debug1('j', "[j]next: escape %d\n", fidx);
  192.             /* Per Adobe, if we get an escape at the root, */
  193.             /* treat it as an ordinary character (font index). */
  194.             if ( fidx == chr && fdepth > 1 )
  195.             {    fdepth--;
  196.                 goto up;
  197.             }
  198. down:            if ( ++p == end )
  199.                 return 2;
  200.             chr = *p;
  201.             fdepth--;
  202.             do
  203.             {    select_descendant(pfont, pdata, fidx, fdepth);
  204.                 if_debug3('j', "[j]down from modal: new depth=%d, index=%d, new font=0x%lx\n",
  205.                       fdepth, fidx, (ulong)pfont);
  206.                 if ( pfont->FontType != ft_composite )
  207.                     break;
  208.                 pdata = &pfont0->data;
  209.                 fidx = 0;
  210.             }
  211.             while ( pdata->FMapType == fmap_escape );
  212.             continue;
  213.  
  214.         case fmap_double_escape:
  215.             if ( chr != root_EscChar )
  216.                 break;
  217.             need_left(2);
  218.             fidx = *++p;
  219.             if ( fidx == chr )
  220.             {    need_left(2);
  221.                 fidx = *++p + 256;
  222.             }
  223.             if_debug1('j', "[j]next: double escape %d\n", fidx);
  224.             goto down;
  225.  
  226.         case fmap_shift:
  227.             if ( chr == pdata->ShiftIn )
  228.                 fidx = 0;
  229.             else if ( chr == pdata->ShiftOut )
  230.                 fidx = 1;
  231.             else break;
  232.             if_debug1('j', "[j]next: shift %d\n", fidx);
  233.             goto down;
  234.         }
  235.         break;
  236.     }
  237.     p++;
  238.  
  239.         /* Now handle non-modal descendants. */
  240.  
  241.     while ( (pfont = penum->fstack.items[fdepth].font)->FontType == ft_composite )
  242.     {    pdata = &pfont0->data;
  243.         switch ( pdata->FMapType )
  244.         {
  245.         default:            /* can't happen */
  246.             return_error(gs_error_invalidfont);
  247.  
  248.         case fmap_8_8:
  249.             need_left(1);
  250.             fidx = chr;
  251.             chr = *p++;
  252.             if_debug2('J', "[J]8/8 index=%d, char=%ld\n",
  253.                   fidx, chr);
  254.             break;
  255.  
  256.         case fmap_1_7:
  257.             if ( first )
  258.             {    fidx = chr >> 7;
  259.                 chr &= 0x7f;
  260.                 if_debug2('J', "[J]1/7 index=%d, char=%ld\n",
  261.                       fidx, chr);
  262.                 break;
  263.             }
  264.             /* falls through */
  265.  
  266.         case fmap_9_7:
  267.             need_left(1);
  268.             fidx = (first ? ((uint)chr << 1) + (*p >> 7) : chr);
  269.             chr = *p & 0x7f;
  270.             if_debug2('J', "[J]1/7 index=%d, char=%ld\n",
  271.                   fidx, chr);
  272.             p++;
  273.             break;
  274.  
  275.         case fmap_SubsVector:
  276.         {    int width = pdata->subs_width;
  277.             uint subs_count = pdata->subs_size;
  278.             const byte *psv = pdata->SubsVector.data;
  279. #define subs_loop(subs_elt, width)\
  280.   while ( subs_count != 0 && tchr >= (schr = subs_elt) )\
  281.     subs_count--, tchr -= schr, psv += width;\
  282.   chr = tchr; p += width - 1; break
  283.             switch ( width )
  284.             {
  285.             default:        /* can't happen */
  286.                 return_error(gs_error_invalidfont);
  287.             case 1:
  288.             {    byte tchr = (byte)chr, schr;
  289.                 subs_loop(*psv, 1);
  290.             }
  291.             case 2:
  292.                 need_left(1);
  293. #define w2(p) (((ushort)*p << 8) + p[1])
  294.             {    ushort tchr = ((ushort)chr << 8) + *p, schr;
  295.                 subs_loop(w2(psv), 2);
  296.             }
  297.             case 3:
  298.                 need_left(2);
  299. #define w3(p) (((ulong)*p << 16) + ((uint)p[1] << 8) + p[2])
  300.             {    ulong tchr = ((ulong)chr << 16) + w2(p), schr;
  301.                 subs_loop(w3(psv), 3);
  302.             }
  303.             case 4:
  304.                 need_left(3);
  305. #define w4(p) (((ulong)*p << 24) + ((ulong)p[1] << 16) + ((uint)p[2] << 8) + p[3])
  306.             {    ulong tchr = ((ulong)chr << 24) + w3(p), schr;
  307.                 subs_loop(w4(psv), 4);
  308.             }
  309. #undef w2
  310. #undef w3
  311. #undef w4
  312. #undef subs_loop
  313.             }
  314.             fidx = pdata->subs_size - subs_count;
  315.             if_debug2('J', "[J]SubsVector index=%d, char=%ld\n",
  316.                   fidx, chr);
  317.             break;
  318.         }
  319.  
  320.         }
  321.  
  322.         select_descendant(pfont, pdata, fidx, fdepth);
  323.         if_debug2('J', "... new depth=%d, new font=0x%lx\n",
  324.               fdepth, (ulong)pfont);
  325.         first = false;
  326.     }
  327.     *pchr = chr;
  328.     penum->index = p - penum->str.data;
  329.     penum->fstack.depth = fdepth;
  330.     if_debug4('J', "[J]depth=%d font=0x%lx index=%d changed=%d\n",
  331.           fdepth, (ulong)penum->fstack.items[fdepth].font,
  332.           penum->fstack.items[fdepth].index, changed);
  333.     return changed;
  334. #undef pfont0
  335. }
  336.