home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Impressora_PDF / converter.exe / GPLGS / impath.ps < prev    next >
Text File  |  2002-02-22  |  6KB  |  180 lines

  1. %    Copyright (C) 1992, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: impath.ps,v 1.5 2002/02/21 21:49:28 giles Exp $
  14. % impath.ps
  15. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  16. % (This was formerly a Ghostscript operator implemented in C.)
  17.  
  18. %    <image> <width> <height> <wx> <wy> <ox> <oy> <string>
  19. %      type1imagepath <substring>
  20. %        Converts an image (bitmap) description of a character into
  21. %          a scalable description in Adobe Type 1 format.  The
  22. %          current transformation matrix should be the same as the
  23. %          FontMatrix of the font in which this character will be
  24. %          used: this establishes the scaling relationship between
  25. %          image pixels (the image is assumed to be 1 unit high in
  26. %          user space) and the character coordinate system used in
  27. %          the scalable description.  wx and wy are the character
  28. %          width, and ox and oy are the character origin relative
  29. %          to the lower left corner of the bitmap, in *pixels*.
  30. %        The image is assumed to be stored in left-to-right,
  31. %          top-to-bottom order.  Note that this is not consistent
  32. %          with the `image' operator's interpretation of the CTM.
  33. %        All coordinates in the scalable description are rounded to
  34. %          integers, so the coefficients in the FontMatrix should
  35. %          be on the order of 1/N for some value of N that is
  36. %          either a multiple of the height/width or is large
  37. %          compared to the width and height.  (There is a
  38. %          convention, which some PostScript programs rely on, that
  39. %          N=1000.)
  40. %        Note that the encoded description has *not* been subjected
  41. %          to CharString encryption, which is necessary before the
  42. %          description can be given to type1addpath: to do this,
  43. %          follow the type1imagepath with
  44. %            4330 exch dup .type1encrypt exch pop
  45. %        If the description is too complex to fit into the supplied
  46. %          string, a limitcheck error results.  A good rule of
  47. %          thumb is that the size of the string should be about 6
  48. %          times the number of 1-bits in the image that are not
  49. %          completely surrounded by other 1-bits.
  50.  
  51. % Import the Type 1 opcodes.
  52. (type1ops.ps) runlibfile
  53.  
  54. 100 dict
  55. dup /type1imagepath_dict exch def
  56. begin
  57.  
  58. /rc { round cvi } bind def
  59. /moving [/rmoveto /hmoveto /vmoveto] def
  60. /drawing [/rlineto /hlineto /vlineto] def
  61.  
  62. % Convert the current path to a Type 1 token array.
  63. /putxy            % x y ops -> cs_elements
  64.  { 3 -1 roll dup x sub rc exch /x exch def
  65.    3 -1 roll dup y sub rc exch /y exch def
  66.    % stack: ops dx dy
  67.    dup 0 eq
  68.     { % dy = 0, use hmoveto/lineto
  69.       pop exch 1 get
  70.     }
  71.     { 1 index 0 eq
  72.        { % dx = 0, use vmoveto/lineto
  73.          exch pop exch 2 get
  74.        }
  75.        { % use rmoveto/rlineto
  76.          3 -1 roll 0 get
  77.        }
  78.       ifelse
  79.     }
  80.    ifelse
  81.  } bind def
  82. /pathtotype1        % -> charstack
  83.  { 3 dict begin /x 0 def /y 0 def
  84.    mark
  85.    { moving putxy
  86.    }
  87.    { drawing putxy
  88.    }
  89.    { % Convert curve to relative form
  90.      x y 3
  91.       { exch neg 7 index add rc
  92.     exch neg 6 index add rc
  93.     8 -2 roll
  94.       }
  95.      repeat /y exch def /x exch def
  96.      1 index 0 eq 5 index 0 eq and    % dy1=dx3=0, hv
  97.       { 5 -1 roll pop exch pop /hvcurveto
  98.       }
  99.       { dup 0 eq 6 index 0 eq and    % dx1=dy3=0, vh
  100.          { 6 -1 roll pop pop /vhcurveto
  101.      }
  102.      { /rrcurveto            % none of the above
  103.      }
  104.     ifelse
  105.       }
  106.      ifelse
  107.    }
  108.    { /closepath
  109.    }
  110.    pathforall end
  111.  } bind def
  112.  
  113. end    % type1imagepath_dict
  114.  
  115. % The main program
  116. /type1imagepath        % image width height wx wy ox oy string ->
  117.             % substring
  118.  { type1imagepath_dict begin
  119.    /tsave save def
  120.    /ostring exch def
  121.    /oy exch def   /ox exch def
  122.    /wy exch def   /wx exch def
  123.    /height exch def   /width exch def
  124.    /data exch def
  125.  
  126.    /ofilter ostring /NullEncode filter def
  127.    /raster width 7 add 8 idiv def
  128.  
  129. % Construct the coordinate transformation.
  130.    height dup scale
  131.      matrix currentmatrix matrix invertmatrix setmatrix
  132.  
  133. % Determine the left side bearing.
  134.    /lsbx width
  135.    0 1 width 1 sub
  136.     { dup dup 8 idiv 0 exch
  137.       raster raster height mul 1 sub
  138.        { data exch get or }
  139.       for exch 8 mod bitshift 128 and 0 ne
  140.        { exch pop exit }
  141.       if pop
  142.     }
  143.    for def
  144.  
  145. % Compute and encode the origin, width, and side bearing.
  146.    mark
  147.    ox oy dtransform
  148.      rc /opty exch def   rc /optx exch def
  149.    wx wy dtransform
  150.      rc /iwy exch def    rc /iwx exch def
  151.    lsbx ox sub 0 dtransform
  152.      rc /ilsby exch def   rc /ilsbx exch def
  153.    ilsbx
  154.    iwy 0 ne ilsby 0 ne or
  155.     { ilsby iwx iwy /sbw }
  156.     { iwx /hsbw }
  157.    ifelse
  158.    ofilter charstack_write
  159.  
  160. % Flip over the Y axis, because the image is stored top-to-bottom.
  161.    [1 0 0 -1 0 height] concat
  162. % Account for the character origin.
  163.    lsbx oy translate
  164. % Trace the outline.
  165.    newpath
  166.    width height data .imagepath
  167.    gsave matrix setmatrix pathtotype1 grestore
  168.    ofilter charstack_write
  169. % Terminate the output
  170.    mark /endchar ofilter charstack_write
  171.  
  172.    ofilter .fileposition ofilter closefile    % flush buffers
  173.    ostring 0 3 -1 roll getinterval
  174.    tsave restore
  175.    end
  176.  } bind def
  177.