impath.ps 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. % Copyright (C) 1992, 1996 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: impath.ps,v 1.3 2000/12/28 17:06:07 alexcher Exp $
  18. % impath.ps
  19. % Reverse-rasterize a bitmap to produce a Type 1 outline.
  20. % (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
  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. % Import the Type 1 opcodes.
  54. (type1ops.ps) runlibfile
  55. 100 dict
  56. dup /type1imagepath_dict exch def
  57. begin
  58. /rc { round cvi } bind def
  59. /moving [/rmoveto /hmoveto /vmoveto] def
  60. /drawing [/rlineto /hlineto /vlineto] def
  61. % Convert the current path to a Type 1 token array.
  62. /putxy % x y ops -> cs_elements
  63. { 3 -1 roll dup x sub rc exch /x exch def
  64. 3 -1 roll dup y sub rc exch /y exch def
  65. % stack: ops dx dy
  66. dup 0 eq
  67. { % dy = 0, use hmoveto/lineto
  68. pop exch 1 get
  69. }
  70. { 1 index 0 eq
  71. { % dx = 0, use vmoveto/lineto
  72. exch pop exch 2 get
  73. }
  74. { % use rmoveto/rlineto
  75. 3 -1 roll 0 get
  76. }
  77. ifelse
  78. }
  79. ifelse
  80. } bind def
  81. /pathtotype1 % -> charstack
  82. { 3 dict begin /x 0 def /y 0 def
  83. mark
  84. { moving putxy
  85. }
  86. { drawing putxy
  87. }
  88. { % Convert curve to relative form
  89. x y 3
  90. { exch neg 7 index add rc
  91. exch neg 6 index add rc
  92. 8 -2 roll
  93. }
  94. repeat /y exch def /x exch def
  95. 1 index 0 eq 5 index 0 eq and % dy1=dx3=0, hv
  96. { 5 -1 roll pop exch pop /hvcurveto
  97. }
  98. { dup 0 eq 6 index 0 eq and % dx1=dy3=0, vh
  99. { 6 -1 roll pop pop /vhcurveto
  100. }
  101. { /rrcurveto % none of the above
  102. }
  103. ifelse
  104. }
  105. ifelse
  106. }
  107. { /closepath
  108. }
  109. pathforall end
  110. } bind def
  111. end % type1imagepath_dict
  112. % The main program
  113. /type1imagepath % image width height wx wy ox oy string ->
  114. % substring
  115. { type1imagepath_dict begin
  116. /tsave save def
  117. /ostring exch def
  118. /oy exch def /ox exch def
  119. /wy exch def /wx exch def
  120. /height exch def /width exch def
  121. /data exch def
  122. /ofilter ostring /NullEncode filter def
  123. /raster width 7 add 8 idiv def
  124. % Construct the coordinate transformation.
  125. height dup scale
  126. matrix currentmatrix matrix invertmatrix setmatrix
  127. % Determine the left side bearing.
  128. /lsbx width
  129. 0 1 width 1 sub
  130. { dup dup 8 idiv 0 exch
  131. raster raster height mul 1 sub
  132. { data exch get or }
  133. for exch 8 mod bitshift 128 and 0 ne
  134. { exch pop exit }
  135. if pop
  136. }
  137. for def
  138. % Compute and encode the origin, width, and side bearing.
  139. mark
  140. ox oy dtransform
  141. rc /opty exch def rc /optx exch def
  142. wx wy dtransform
  143. rc /iwy exch def rc /iwx exch def
  144. lsbx ox sub 0 dtransform
  145. rc /ilsby exch def rc /ilsbx exch def
  146. ilsbx
  147. iwy 0 ne ilsby 0 ne or
  148. { ilsby iwx iwy /sbw }
  149. { iwx /hsbw }
  150. ifelse
  151. ofilter charstack_write
  152. % Flip over the Y axis, because the image is stored top-to-bottom.
  153. [1 0 0 -1 0 height] concat
  154. % Account for the character origin.
  155. lsbx oy translate
  156. % Trace the outline.
  157. newpath
  158. width height data .imagepath
  159. gsave matrix setmatrix pathtotype1 grestore
  160. ofilter charstack_write
  161. % Terminate the output
  162. mark /endchar ofilter charstack_write
  163. ofilter .fileposition ofilter closefile % flush buffers
  164. ostring 0 3 -1 roll getinterval
  165. tsave restore
  166. end
  167. } bind def