impath.ps 5.4 KB

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