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

  1. %    Copyright (C) 1999 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: addxchar.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
  14. % Add the Central European and other Adobe extended Latin characters to a
  15. % Type 1 font.
  16. % Requires -dWRITESYSTEMDICT to disable access protection.
  17.  
  18. (type1ops.ps) runlibfile
  19.  
  20. % ---------------- Utilities ---------------- %
  21.  
  22. /addce_dict 50 dict def
  23. addce_dict begin
  24.  
  25. % Define the added copyright notice.
  26. /addednotice (. Portions Copyright (C) 1999 Aladdin Enterprises.) def 
  27.  
  28. % Open a font for modification by removing the FID and changing the
  29. % FontName.  Removing UniqueID and XUID is not necessary, since we
  30. % will only be adding characters.
  31. /openfont {        % <name> <font> openfont <name> <font'>
  32.   dup length dict copy
  33.   dup /FID undef
  34.   dup /FontName 3 index put
  35. } def
  36.  
  37. % Do the equivalent of false charpath for a glyph.
  38. % This should really be an operator!
  39. /glyphpath {        % <glyph> glyphpath -
  40.   currentfont /Encoding get 0 3 -1 roll put
  41.   <00> false charpath
  42. } def
  43.  
  44. % Do the equivalent of charpath + pathbbox for a glyph.
  45. /glyphbbox {        % <glyph> glyphbbox <llx> <lly> <urx> <ury>
  46.     % We cache this value, because it's expensive to compute.
  47.   BBoxes 1 index .knownget {
  48.     exch pop
  49.   } {
  50.     gsave newpath 0 0 moveto dup glyphpath [pathbbox] grestore
  51.     BBoxes 3 -1 roll 2 index put
  52.   } ifelse aload pop
  53. } def
  54.  
  55. % Get the side bearing and width for a glyph.
  56. /glyphsbw {        % <glyph> glyphsbw <lsbx> <wx>
  57.     % We cache this value, because it's expensive to compute.
  58.   SBW 1 index .knownget {
  59.     exch pop
  60.   } {
  61.     dup glyphcs { dup /hsbw eq { pop exit } if } forall
  62.     2 array astore
  63.     SBW 3 -1 roll 2 index put
  64.   } ifelse aload pop
  65. } def
  66.  
  67. % Get the CharString for a glyph, as an array.
  68. /glyphcs {        % <glyph> glyphcs <array>
  69.   CharStrings exch get
  70.   4330 exch dup length string .type1decrypt exch pop
  71.   dup length lenIV sub lenIV exch getinterval
  72.   0 () /SubFileDecode filter [ exch charstack_read ]
  73. } def
  74.  
  75. % Find an occurrence of a value in an array.
  76. /asearch {        % <array> <value> asearch <index> true
  77.             % <array> <value> asearch false
  78.   false 0 4 2 roll exch {
  79.         % Stack: false index value element
  80.     2 copy eq { pop pop exch not exch dup exit } if
  81.     exch 1 add exch
  82.   } forall pop pop
  83. } def
  84.  
  85. % Convert an array back to a CharString.
  86. /csdef {        % <glyph> <array> csdef -
  87.   charproc_string
  88.   4330 exch dup .type1encrypt exch pop readonly
  89.   CharStrings 3 1 roll put
  90. } def
  91.  
  92. % Split an accented character name.
  93. /splitaccented {    % <Baccent> splitaccented <Baccent> <B> <accent>
  94.     dup =string cvs
  95.     dup 0 1 getinterval cvn
  96.     exch dup length 1 sub 1 exch getinterval cvn
  97. } def
  98.  
  99. % Begin the definition of a 'seac' character.
  100. % Defines accent, base, abox, bbox.
  101. % The initial dx lines up the origins of the base and the accent.
  102. /beginseac {        % <bchar> <achar> beginseac
  103.             %   -mark- <lsbx> <wx> /hsbw <asb> <dx>
  104.   /accent exch def /base exch def
  105.   /abox [accent glyphbbox] def
  106.   /bbox [base glyphbbox] def
  107.   [ base glyphsbw /hsbw accent glyphsbw pop
  108.   dup 4 index sub
  109. } def
  110.  
  111. % Center the accent over the base of a 'seac' character.
  112. /centeraccent {        % <dx> centeraccent <adx>
  113.   bbox 2 get bbox 0 get add 2 div
  114.   abox 2 get abox 0 get add 2 div
  115.   sub add
  116. } def
  117.  
  118. % Finish the definition of a 'seac' character.
  119. /finishseac {        % <charname> -mark- ... <adx> <ady> finishseac -
  120.   exch cvi exch cvi
  121.   charindex base get charindex accent get /seac ] csdef
  122. } def
  123.  
  124. % ---------------- Main program ---------------- %
  125.  
  126. % Define accented characters that can be made with seac,
  127. % with the accent centered over the character.
  128. /seacchars [
  129.   /Abreve /Amacron
  130.   /Cacute /Ccaron /Dcaron
  131.   /Ecaron /Edotaccent /Emacron
  132.   /Gbreve
  133.   /Idotaccent /Imacron
  134.   /Lacute
  135.   /Nacute /Ncaron
  136.   /Ohungarumlaut /Omacron
  137.   /Racute /Rcaron
  138.   /Sacute /Scedilla
  139.   /Tcaron
  140.   /Uhungarumlaut /Umacron /Uogonek /Uring
  141.   /Zacute /Zdotaccent
  142.   /abreve /amacron
  143.   /cacute /ccaron
  144.   /ecaron /edotaccent /emacron
  145.   /gbreve
  146.   /lacute
  147.   /nacute /ncaron
  148.   /ohungarumlaut /omacron
  149.   /racute /rcaron
  150.   /sacute /scedilla
  151.   /uhungarumlaut /umacron /uring
  152.   /zacute /zdotaccent
  153. ] def
  154.  
  155. % Define seac characters where the accent lines up with the right
  156. % edge of the character.
  157. /seacrightchars [
  158.   /Aogonek /Eogonek /Iogonek /aogonek /eogonek /iogonek /uogonek
  159. ] def
  160.  
  161. % Define seac characters where the caron becomes an appended quoteright.
  162. /seaccaronchars [
  163.   /dcaron /lcaron /tcaron
  164. ] def
  165.  
  166. % Define seac characters using commaaccent.
  167. /seaccommachars [
  168.   /Gcommaaccent /Kcommaaccent /Lcommaaccent /Ncommaaccent /Rcommaaccent
  169.   /Scommaaccent /Tcommaaccent
  170.   /gcommaaccent /kcommaaccent /lcommaaccent /ncommaaccent /rcommaaccent
  171.   /scommaaccent /tcommaaccent
  172. ] def
  173.  
  174. % Define the characters copied from the Symbol font.
  175. /symbolchars [
  176.   /Delta /greaterequal /lessequal /lozenge /notequal /partialdiff
  177.   /summation
  178. ] def
  179.  
  180. % Define the procedures for editing the commaaccent character.
  181. % Delete all the hints, since it's too hard to adjust them.
  182. /caedit mark
  183.   /rmoveto { exch commatop sub cvi exch }
  184.   /hstem { pop pop pop }
  185.   /vstem 1 index
  186.   /callothersubr {
  187.     dup 3 eq { 4 { pop } repeat /skip true def } if
  188.   }
  189.   /pop { skip { pop /skip false def } if }
  190. .dicttomark def
  191.  
  192. /addce {        % <name> <font> addce <font'>
  193.   20 dict begin
  194.   /origfont 1 index def
  195.   openfont
  196.   dup /CharStrings 2 copy get dup length dict copy put
  197.   dup /Encoding 2 copy get dup length array copy put
  198.   dup /FontInfo 2 copy get dup length dict copy put
  199.   definefont /font exch def
  200.   currentdict font end begin begin
  201.   font 1000 scalefont setfont
  202.   /symbolfont /Symbol findfont def
  203.   /BBoxes CharStrings length dict def
  204.   /SBW CharStrings length dict def
  205.  
  206.   /italfactor FontInfo /ItalicAngle .knownget {
  207.     neg dup sin exch cos div
  208.   } {
  209.     0
  210.   } ifelse def
  211.  
  212.     % Invert the Encoding (needed for seac).
  213.  
  214.   /charindex 256 dict def
  215.   0 1 255 {
  216.     charindex exch Encoding 1 index get exch put
  217.   } for
  218.  
  219.     % Add the commaaccent character, by moving the comma downward.
  220.  
  221.   /comma glyphbbox /commatop exch def pop pop pop
  222.   /comma glyphcs
  223.     /skip false def
  224.     [ exch { caedit 1 index .knownget { exec } if } forall ]
  225.   /commaaccent exch csdef
  226.  
  227.     % Add the accented characters that can be made with seac.
  228.  
  229.   seacchars {
  230.     splitaccented beginseac
  231.       centeraccent
  232.         % If the accent would collide with the base character,
  233.         % raise it a little.
  234.       abox 1 get bbox 3 get sub dup 0 le {
  235.         % ... but not if the accent is in the low position.
  236.     abox 1 get 0 gt {
  237.       neg 60 add
  238.         % Adjust the X position if italic.
  239.       dup italfactor mul 3 -1 roll add exch
  240.     } {
  241.       pop 0
  242.     } ifelse
  243.       } {
  244.     pop 0
  245.       } ifelse
  246.     finishseac
  247.   } forall
  248.  
  249.   seacrightchars {
  250.     splitaccented beginseac
  251.     bbox 2 get abox 2 get sub add    % line up right edges
  252.     0 finishseac
  253.   } forall
  254.  
  255.   /dcroat /d /hyphen beginseac
  256.     bbox 2 get abox 2 get sub add    % line up right edges
  257.   0 finishseac
  258.  
  259.   /imacron /dotlessi /macron beginseac
  260.     centeraccent
  261.   0 finishseac
  262.  
  263.   /Lcaron /L /quoteright beginseac
  264.     bbox 2 get abox 2 get sub add    % line up right edges
  265.   0 finishseac
  266.  
  267.   seaccaronchars {
  268.     dup =string cvs 0 1 getinterval cvn /quoteright beginseac
  269.         % Move the quote to the right of the character.
  270.     bbox 2 get abox 0 get sub 50 add add
  271.         % Adjust the character width as well.
  272.     4 -1 roll abox 2 get abox 0 get sub 50 add add cvi 4 1 roll
  273.     0 finishseac
  274.   } forall
  275.  
  276.   seaccommachars {
  277.     dup =string cvs 0 1 getinterval cvn /comma beginseac
  278.       centeraccent
  279.       commatop neg
  280.         % Lower the accent if the character extends below
  281.         % the baseline
  282.       bbox 1 get 0 .min add
  283.     finishseac
  284.   } forall
  285.  
  286.     % Add the characters from the Symbol font.
  287.     % We should scale them to match the FontBBox, but we don't.
  288.  
  289.   symbolchars {
  290.     symbolfont /CharStrings get 1 index get
  291.     CharStrings 3 1 roll put
  292.   } forall
  293.  
  294.     % Add the one remaining character.
  295.  
  296.   CharStrings /Dcroat CharStrings /Eth get put
  297.  
  298.     % Recompute the FontBBox, since some of the accented characters
  299.     % may have enlarged it.
  300.  
  301.   /llx 1000 def /lly 1000 def /urx 0 def /ury 0 def
  302.   CharStrings {
  303.     pop glyphbbox
  304.     ury .max /ury exch def urx .max /urx exch def
  305.     lly .min /lly exch def llx .min /llx exch def
  306.   } forall
  307.   /FontBBox llx cvi lly cvi urx ceiling cvi ury ceiling cvi 4 packedarray def
  308.  
  309.     % Restore the Encoding and wrap up.
  310.  
  311.   [/Copyright /Notice] {
  312.     FontInfo 1 index .knownget {
  313.       addednotice concatstrings FontInfo 3 1 roll put
  314.     } {
  315.       pop
  316.     } ifelse
  317.   } forall
  318.   FontName font openfont
  319.   dup /Encoding origfont /Encoding get put
  320.   definefont
  321.  
  322.   end end
  323. } def
  324.  
  325. currentdict end readonly pop    % addce_dict
  326.  
  327. /addce { addce_dict begin addce end } def
  328.  
  329. % ---------------- Integration ---------------- %
  330.  
  331. % We would like to patch the font loader so that it adds the extended
  332. % Latin characters automatically.  We haven't done this yet.
  333.  
  334. % ---------------- Test program ---------------- %
  335.  
  336. /TEST where { pop TEST } { false } ifelse {
  337.   /FONT where { pop } { /FONT /Palatino-Italic def } ifelse
  338.   (unprot.ps) runlibfile
  339.   unprot
  340.   (wrfont.ps) runlibfile
  341.   wrfont_dict begin
  342.     /eexec_encrypt true def
  343.     /binary_CharStrings true def
  344.   end
  345.   save
  346.     FONT findfont
  347.     /Latin-CE exch addce setfont
  348.     (t.ce.pfb) (w) file dup writefont closefile
  349.   restore
  350.   (prfont.ps) runlibfile
  351.   (t.ce.pfb) (r) file .loadfont
  352.   /Latin-CE DoFont
  353.   quit
  354. } if
  355.