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