1 % Copyright (C) 1992, 1996 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
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.
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.
16 % $Id: impath.ps,v 1.5 2002/02/21 21:49:28 giles Exp $
18 % Reverse-rasterize a bitmap to produce a Type 1 outline.
19 % (This was formerly a Ghostscript operator implemented in C.)
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
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.
54 % Import the Type 1 opcodes.
55 (type1ops.ps) runlibfile
58 dup /type1imagepath_dict exch def
61 /rc { round cvi } bind def
62 /moving [/rmoveto /hmoveto /vmoveto] def
63 /drawing [/rlineto /hlineto /vlineto] def
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
71 { % dy = 0, use hmoveto/lineto
75 { % dx = 0, use vmoveto/lineto
78 { % use rmoveto/rlineto
85 /pathtotype1 % -> charstack
86 { 3 dict begin /x 0 def /y 0 def
92 { % Convert curve to relative form
94 { exch neg 7 index add rc
95 exch neg 6 index add rc
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
102 { dup 0 eq 6 index 0 eq and % dx1=dy3=0, vh
103 { 6 -1 roll pop pop /vhcurveto
105 { /rrcurveto % none of the above
116 end % type1imagepath_dict
119 /type1imagepath % image width height wx wy ox oy string ->
121 { type1imagepath_dict begin
124 /oy exch def /ox exch def
125 /wy exch def /wx exch def
126 /height exch def /width exch def
129 /ofilter ostring /NullEncode filter def
130 /raster width 7 add 8 idiv def
132 % Construct the coordinate transformation.
134 matrix currentmatrix matrix invertmatrix setmatrix
136 % Determine the left side bearing.
139 { dup dup 8 idiv 0 exch
140 raster raster height mul 1 sub
142 for exch 8 mod bitshift 128 and 0 ne
148 % Compute and encode the origin, width, and side bearing.
151 rc /opty exch def rc /optx exch def
153 rc /iwy exch def rc /iwx exch def
154 lsbx ox sub 0 dtransform
155 rc /ilsby exch def rc /ilsbx exch def
157 iwy 0 ne ilsby 0 ne or
158 { ilsby iwx iwy /sbw }
161 ofilter charstack_write
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.
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
175 ofilter .fileposition ofilter closefile % flush buffers
176 ostring 0 3 -1 roll getinterval