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

  1. /* Copyright (C) 1989, 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. /* gxccache.c */
  20. /* Fast case character cache routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gpcheck.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"
  27. #include "gzstate.h"
  28. #include "gzpath.h"
  29. #include "gxcpath.h"
  30. #include "gxdevmem.h"
  31. #include "gxchar.h"
  32. #include "gxfont.h"
  33. #include "gxfcache.h"
  34. #include "gxxfont.h"
  35. #include "gscspace.h"            /* for gsimage.h */
  36. #include "gsimage.h"
  37.  
  38. /* Define a scale factor of 1. */
  39. static const gs_log2_scale_point scale_log2_1 = { 0, 0 };
  40.  
  41. /* Look up, and if necessary add, a font/matrix pair in the cache */
  42. cached_fm_pair *
  43. gx_lookup_fm_pair(gs_font *pfont, register const gs_state *pgs)
  44. {    float    mxx = pgs->char_tm.xx, mxy = pgs->char_tm.xy,
  45.         myx = pgs->char_tm.yx, myy = pgs->char_tm.yy;
  46.     gs_font *font = pfont;
  47.     register gs_font_dir *dir = font->dir;
  48.     register cached_fm_pair *pair =
  49.         dir->fmcache.mdata + dir->fmcache.mnext;
  50.     int count = dir->fmcache.mmax;
  51.     gs_uid uid;
  52.     if ( font->FontType == ft_composite )
  53.     {    uid_set_invalid(&uid);
  54.     }
  55.     else
  56.     {    uid = ((gs_font_base *)font)->UID;
  57.         if ( uid_is_valid(&uid) )
  58.             font = 0;
  59.     }
  60.     while ( count-- )
  61.     {    if ( pair == dir->fmcache.mdata )
  62.             pair += dir->fmcache.mmax;
  63.         pair--;
  64.         /* We have either a non-zero font and an invalid UID, */
  65.         /* or a zero font and a valid UID. */
  66.         /* We have to break up the test */
  67.         /* because of a bug in the Zortech compiler. */
  68.         if ( font != 0 )
  69.         {    if ( pair->font != font ) continue;
  70.         }
  71.         else
  72.         {    if ( !uid_equal(&pair->UID, &uid) ) continue;
  73.         }
  74.         if (    pair->mxx == mxx && pair->mxy == mxy &&
  75.             pair->myx == myx && pair->myy == myy
  76.            )
  77.         {    if ( pair->font == 0 )
  78.             {    pair->font = pfont;
  79.                 if_debug2('k', "[k]updating pair 0x%lx with font 0x%lx\n",
  80.                       (ulong)pair, (ulong)pfont);
  81.             }
  82.             else
  83.             {    if_debug2('k', "[k]found pair 0x%lx: font=0x%lx\n",
  84.                       (ulong)pair, (ulong)pair->font);
  85.             }
  86.             return pair;
  87.         }
  88.     }
  89.     return gx_add_fm_pair(dir, pfont, &uid, pgs);
  90. }
  91.  
  92. /* Look up a glyph in the cache. */
  93. /* The character depth must be either 1 or alt_depth. */
  94. /* Return the cached_char or 0. */
  95. cached_char *
  96. gx_lookup_cached_char(const gs_font *pfont, const cached_fm_pair *pair,
  97.   gs_glyph glyph, int wmode, int alt_depth)
  98. {    gs_font_dir *dir = pfont->dir;
  99.     register cached_char *cc = *chars_head(dir, glyph, pair);
  100.     while ( cc != 0 )
  101.     {    if ( cc->code == glyph && cc->head.pair == pair &&
  102.              cc->wmode == wmode && (cc->depth == 1 || cc->depth == alt_depth)
  103.            )
  104.         {    if_debug4('K', "[K]found 0x%lx (depth=%d) for glyph=0x%lx, wmode=%d\n",
  105.                   (ulong)cc, cc->depth, (ulong)glyph, wmode);
  106.             return cc;
  107.         }
  108.         cc = cc->next;
  109.     }
  110.     if_debug3('K', "[K]not found: glyph=0x%lx, wmode=%d, alt_depth=%d\n",
  111.           (ulong)glyph, wmode, alt_depth);
  112.     return 0;
  113. }
  114.  
  115. /* Look up a character in an external font. */
  116. /* Return the cached_char or 0. */
  117. cached_char *
  118. gx_lookup_xfont_char(const gs_state *pgs, cached_fm_pair *pair,
  119.   gs_char chr, gs_glyph glyph, gs_proc_glyph_name_t name_proc, int wmode)
  120. {    gs_font *font = pair->font;
  121.     int enc_index;
  122.     gx_xfont *xf;
  123.     gx_xglyph xg;
  124.     gs_point wxy;
  125.     gs_int_rect bbox;
  126.     cached_char *cc;
  127.     if ( font == 0 )
  128.         return NULL;
  129.     enc_index =
  130.         (font->FontType == ft_composite ? -1 :
  131.          ((gs_font_base *)font)->nearest_encoding_index);
  132.     if ( !pair->xfont_tried )
  133.     {    /* Look for an xfont now. */
  134.         gx_lookup_xfont(pgs, pair, enc_index);
  135.         pair->xfont_tried = true;
  136.     }
  137.     xf = pair->xfont;
  138.     if ( xf == 0 )
  139.         return NULL;
  140.     /***** The following is wrong.  We should only use the nearest *****/
  141.     /***** registered encoding if the character is really the same. *****/
  142.     xg = (*xf->common.procs->char_xglyph)(xf, chr, enc_index, glyph, name_proc);
  143.     if ( xg == gx_no_xglyph )
  144.         return NULL;
  145.     if ( (*xf->common.procs->char_metrics)(xf, xg, wmode, &wxy, &bbox) < 0 )
  146.         return NULL;
  147.     cc = gx_alloc_char_bits(font->dir, NULL, bbox.q.x - bbox.p.x,
  148.                 bbox.q.y - bbox.p.y);
  149.     if ( cc == 0 )
  150.         return NULL;
  151.     /* Success.  Make the cache entry. */
  152.     cc->code = glyph;
  153.     cc->wmode = wmode;
  154.     cc->depth = 1;
  155.     cc->xglyph = xg;
  156.     cc->wxy.x = float2fixed(wxy.x);
  157.     cc->wxy.y = float2fixed(wxy.y);
  158.     cc->offset.x = int2fixed(-bbox.p.x);
  159.     cc->offset.y = int2fixed(-bbox.p.y);
  160.     if_debug5('k', "[k]xfont %s char %d/0x%x#0x%lx=>0x%lx\n",
  161.           font->font_name.chars, enc_index, (int)chr,
  162.           (ulong)glyph, (ulong)xg);
  163.     if_debug6('k', "     wxy=(%g,%g) bbox=(%d,%d),(%d,%d)\n",
  164.           wxy.x, wxy.y, bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
  165.     gx_add_cached_char(font->dir, NULL, cc, pair, &scale_log2_1);
  166.     return cc;
  167. }
  168.  
  169. /* Copy a cached character to the screen. */
  170. /* Assume the caller has already done gx_color_load. */
  171. /* Return 0 if OK, 1 if we couldn't do the operation but no error */
  172. /* should be signalled, or a negative error code. */
  173. int
  174. gx_image_cached_char(register gs_show_enum *penum, register cached_char *cc)
  175. {    register gs_state *pgs = penum->pgs;
  176.     int x, y, w, h;
  177.     int code;
  178.     gs_fixed_point pt;
  179.     gx_device *dev = gs_currentdevice_inline(pgs);
  180.     gx_device_clip cdev;
  181.     gx_xglyph xg = cc->xglyph;
  182.     gx_xfont *xf;
  183. top:    code = gx_path_current_point_inline(pgs->path, &pt);
  184.     if ( code < 0 ) return code;
  185.     /* If the character doesn't lie entirely within the */
  186.     /* quick-check clipping rectangle, we have to */
  187.     /* set up an intermediate clipping device. */
  188.     pt.x -= cc->offset.x;
  189.     x = fixed2int_var_rounded(pt.x) + penum->ftx;
  190.     pt.y -= cc->offset.y;
  191.     y = fixed2int_var_rounded(pt.y) + penum->fty;
  192.     w = cc->width;
  193.     h = cc->height;
  194. #ifdef DEBUG
  195.     if ( gs_debug_c('K') )
  196.     {    if ( cc_has_bits(cc) )
  197.             debug_dump_bytes(cc_bits(cc),
  198.                      cc_bits(cc) + cc->raster * h,
  199.                      "[K]bits");
  200.         else
  201.             dputs("[K]no bits\n");
  202.         dprintf3("[K]copying 0x%lx, offset=(%g,%g)\n", (ulong)cc,
  203.              fixed2float(-cc->offset.x),
  204.              fixed2float(-cc->offset.y));
  205.         dprintf6("   at (%g,%g)+(%d,%d)->(%d,%d)\n",
  206.              fixed2float(pt.x), fixed2float(pt.y),
  207.              penum->ftx, penum->fty, x, y);
  208.     }
  209. #endif
  210.     if (    (x < penum->ibox.p.x || x + w > penum->ibox.q.x ||
  211.          y < penum->ibox.p.y || y + h > penum->ibox.q.y) &&
  212.         dev != (gx_device *)&cdev    /* might be 2nd time around */
  213.        )
  214.     {    /* Check for the character falling entirely outside */
  215.         /* the clipping region. */
  216.         if ( x >= penum->obox.q.x || x + w <= penum->obox.p.x ||
  217.              y >= penum->obox.q.y || y + h <= penum->obox.p.y
  218.            )
  219.             return 0;        /* nothing to do */
  220.         gx_make_clip_device(&cdev, &cdev, &pgs->clip_path->list);
  221.         cdev.target = dev;
  222.         dev = (gx_device *)&cdev;
  223.         (*dev_proc(dev, open_device))(dev);
  224.         if_debug0('K', "[K](clipping)\n");
  225.     }
  226.     /* If an xfont can render this character, use it. */
  227.     if ( xg != gx_no_xglyph && (xf = cc->head.pair->xfont) != 0 )
  228.     {    int cx = x + fixed2int(cc->offset.x);
  229.         int cy = y + fixed2int(cc->offset.y);
  230.         if ( color_is_pure(pgs->dev_color) )
  231.         {    code = (*xf->common.procs->render_char)(xf, xg,
  232.                 dev, cx, cy,
  233.                 pgs->dev_color->colors.pure, 0);
  234.             if_debug8('K', "[K]render_char display: xfont=0x%lx, glyph=0x%lx\n\tdev=0x%lx(%s) x,y=%d,%d, color=0x%lx => %d\n",
  235.                   (ulong)xf, (ulong)xg, (ulong)dev,
  236.                   dev->dname, cx, cy,
  237.                   (ulong)pgs->dev_color->colors.pure, code);
  238.             if ( code == 0 )
  239.                 return_check_interrupt(0);
  240.         }
  241.         /* Can't render directly.  If we don't have a bitmap yet, */
  242.         /* get it from the xfont now. */
  243.         if ( !cc_has_bits(cc) )
  244.         {    gx_device_memory mdev;
  245.             gs_make_mem_mono_device(&mdev, 0);
  246.             mdev.target = dev;
  247.             gx_open_cache_device(&mdev, cc);
  248.             code = (*xf->common.procs->render_char)(xf, xg,
  249.                 (gx_device *)&mdev, cx - x, cy - y,
  250.                 (gx_color_index)1, 1);
  251.             if_debug7('K', "[K]render_char to bits: xfont=0x%lx, glyph=0x%lx\n\tdev=0x%lx(%s) x,y=%d,%d => %d\n",
  252.                   (ulong)xf, (ulong)xg, (ulong)&mdev,
  253.                   mdev.dname, cx - x, cy - y, code);
  254.             if ( code != 0 )
  255.                 return_check_interrupt(1);
  256.             gx_add_char_bits(cc->head.pair->font->dir,
  257.                      &mdev, cc, &scale_log2_1);
  258.             /* gx_add_char_bits may change width, height, */
  259.             /* raster, and/or offset.  It's easiest to */
  260.             /* start over from the top.  Clear xg so that */
  261.             /* we don't waste time trying render_char again. */
  262.             xg = gx_no_xglyph;
  263.             goto top;
  264.         }
  265.     }
  266.     /* No xfont.  Render from the cached bits. */
  267.     if ( color_is_pure(pgs->dev_color) )
  268.     {    code = (*dev_proc(dev, copy_mono))
  269.             (dev, cc_bits(cc), 0, cc->raster, cc->id,
  270.             x, y, w, h,
  271.             gx_no_color_index, pgs->dev_color->colors.pure);
  272.     }
  273.     else
  274.     {    /* Use imagemask to render the character. */
  275.         gs_image_enum *pie = gs_image_enum_alloc(&gs_memory_default,
  276.                         "image_char(image_enum)");
  277.         gs_matrix mat;
  278.         int iy;
  279.         uint used;
  280.         if ( pie == 0 )
  281.             return 1;    /* VMerror, but recoverable */
  282.         /* Make a matrix that will place the image */
  283.         /* at (x,y) with no transformation. */
  284.         gs_make_translation((floatp)-x, (floatp)-y, &mat);
  285.         gs_matrix_multiply(&ctm_only(pgs), &mat, &mat);
  286.         code = gs_imagemask_init(pie, pgs, w, h, false, &mat, false);
  287.         switch ( code )
  288.         {
  289.         case 1:            /* empty image */
  290.             code = 0;
  291.         default:
  292.             break;
  293.         case 0:
  294.             for ( iy = 0; iy < h && code >= 0; iy++ )
  295.               code = gs_image_next(pie, cc_bits(cc) + iy *
  296.                 cc->raster, (w + 7) >> 3, &used);
  297.             gs_image_cleanup(pie);
  298.         }
  299.         gs_free_object(&gs_memory_default, pie,
  300.                    "image_char(image_enum)");
  301.     }
  302.     if ( code > 0 )
  303.       code = 0;
  304.     return_check_interrupt(code);
  305. }
  306.