home *** CD-ROM | disk | FTP | other *** search
/ The Net Power 1997 April / NETDisc0497.iso / pc / demos / mmedia / xres / data.z / FONT.PS < prev    next >
Encoding:
Text File  |  1996-06-16  |  9.1 KB  |  415 lines

  1. %!
  2. %%VMusage: 3888 3888
  3. %%VMlocation: global
  4. %%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. %%                                                                   %%
  7. %% This file implements the Font category resource.                  %%
  8. %%                                                                   %%
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10.  
  11. <<
  12.  
  13.     /Category /Font
  14.  
  15.     /ResourceFileName
  16.     {
  17.  
  18.         TProFontFileName
  19.  
  20.     } bind
  21.  
  22.   /ReadSfnts {
  23.     10 dict begin
  24.     dup status {
  25.       pop pop exch pop
  26.     } {
  27.       0
  28.     } ifelse
  29.     /bytes exch def
  30.     (r) file
  31.     mark exch
  32.    {
  33.         {
  34.           dup
  35.           bytes 65536 gt {65536} {bytes} ifelse
  36.           bytes 1 index sub /bytes exch def
  37.           string readstring pop
  38.           exch
  39.           bytes 0 eq {exit} if
  40.        } loop
  41.     } stopped
  42.  
  43.     {
  44.         cleartomark
  45.         stop
  46.     }
  47.     {
  48.         pop ]
  49.     } ifelse
  50.     end
  51.   } bind
  52.  
  53.     /ResourceRead
  54.     {
  55.         dup (.ttf) search
  56.         {
  57.             pop pop pop
  58. %            ReadSfnts /sfnts exch def
  59. %            10 dict begin
  60. %            /FontName fontname
  61. %            dup type /nametype ne
  62. %            {
  63. %                dup gcheck currentglobal ne
  64. %                {
  65. %                    dup length string copy
  66. %                } if def
  67. %            } if
  68. %            /Encoding 256 array def
  69. %            0 1 255 {Encoding exch /.notdef put} for
  70. %            /CharStrings 256 dict def
  71. %            /FontMatrix [1 0 0 1 0 0] def
  72. %            /FontBBox [-1 -1 1 1] cvx def
  73. %            /PaintType 0 def
  74. %            /FontType 42 def
  75. %            /WindowsTTF true def
  76. %            /sfnts sfnts def
  77. %            currentdict end
  78.             readsfntfile
  79.             fontname exch definefont pop
  80.         }
  81.         {
  82.             (.pfb) search
  83.             {
  84.                 pop pop pop
  85.                 (r) file /PFBDecode filter cvx exec
  86.             }
  87.             {
  88.                 pop
  89.                 (r) file cvx exec
  90.             } ifelse
  91.         } ifelse
  92.     } bind
  93.  
  94.  
  95.     /FindResource
  96.     {
  97.         10 dict begin
  98.         /fontname exch def
  99.         FontDirectory fontname known
  100.         {
  101.             /font FontDirectory fontname get def
  102.         }
  103.         {
  104. %            (%%[ Finding ) print fontname dup length string cvs print
  105. %            ( ]%%) = flush
  106.             currentglobal
  107.             fontname TProFontSubstitution
  108.             {
  109.                 % Tell the user that font substitution is occurring and
  110.                 % why.
  111.                 {
  112. %                    dup
  113. %                    (%%[ ) print fontname dup length string cvs print
  114. %                    ( not found, using ) print
  115. %                    dup length string cvs print
  116. %                    ( ]%%) = flush
  117.                 }
  118.                 {
  119.                     dup fontname
  120.                     (%%[ Substituting ) print
  121.                     dup length string cvs print
  122.                     ( with ) print
  123.                     dup length string cvs print
  124.                     ( ]%%) = flush
  125.                 }
  126.                 ifelse
  127.  
  128.                 % Do the actual font substitution.
  129.                 fontname exch SubstituteResource
  130.             }
  131.             {
  132.                 /filename fontname 256 string ResourceFileName def
  133.                 filename status
  134.                 {
  135.                     pop pop pop pop
  136.                     filename FontGCheck setglobal
  137.                     countdictstack
  138.                     mark filename {ResourceRead} stopped
  139.                     {
  140.                         (%%[ Possibly corrupt font file ]%%) = flush
  141.                     } if
  142.                     cleartomark
  143.                     countdictstack exch sub {end} repeat
  144.                 }
  145.                 {
  146.                     false setglobal
  147.                 } ifelse
  148.             } ifelse
  149.  
  150.             FontDirectory fontname known
  151.             {
  152.                 /font FontDirectory fontname get def
  153.             }
  154.             {
  155.                 % If we get this far, an unrecoverable problem has occurred.
  156.                 (%%[ ) print fontname dup length string cvs print
  157.                 ( could not be read, stopping ]%%) = flush
  158.                 stop
  159.             } ifelse
  160.  
  161.             setglobal
  162.         } ifelse
  163.         font
  164.         end
  165.     } bind
  166.  
  167.  
  168.     /DefineResource
  169.     {
  170.         definefont
  171.     } bind
  172.  
  173.     /UndefineResource
  174.     {
  175.         undefinefont
  176.     } bind
  177.  
  178.   /ResourceStatus {
  179.     FontDirectory 1 index known {
  180.       pop 0 -1 true
  181.     } {
  182.       256 string ResourceFileName
  183.       dup status {
  184.         pop pop pop pop
  185.         (r) file 256 string readstring pop
  186. %% Is this good enough? string is only 256 long....
  187.         (%%VMusage:) search {
  188.           pop pop 2 exch
  189.           token pop exch token pop exch pop
  190.       2 copy lt {exch} if pop
  191.           true
  192.         } {
  193.           pop 2 -1 true
  194.         } ifelse
  195.       } {
  196.         pop false
  197.       } ifelse
  198.     } ifelse
  199.   } bind
  200.  
  201.   /ResourceForAll {
  202.     currentdict end
  203.     10 dict begin
  204.     /cat exch def
  205.     userdict /##_RFA_TEMP_## currentdict put
  206.     /scratch exch def
  207.     /proc exch def
  208.     /template exch def
  209.     /done false def
  210.     currentglobal not {
  211.       FontDirectory {
  212.         gcheck not {
  213.           scratch cvs dup template stringmatch {
  214.             /proc load
  215.             end stopped
  216.             ##_RFA_TEMP_## begin
  217.             {
  218.               $error /errorname get dup /invalidexit eq {
  219.                 pop /done true def exit
  220.               } {
  221.                 $error /command get exch
  222.                 errordict exch get exec
  223.               } ifelse
  224.             } if
  225.           } {
  226.             pop
  227.           } ifelse
  228.         } if
  229.       } forall
  230.     } if
  231.     done not {
  232.       FontDirectory {
  233.         gcheck {
  234.           scratch cvs dup template stringmatch {
  235.             /proc load
  236.             end stopped
  237.             ##_RFA_TEMP_## begin
  238.             {
  239.               $error /errorname get dup /invalidexit eq {
  240.                 pop /done true def exit
  241.               } {
  242.                 $error /command get exch
  243.                 errordict exch get exec
  244.               } ifelse
  245.             } if
  246.           } {
  247.             pop
  248.           } ifelse
  249.         } if
  250.       } forall
  251.     } if
  252.     done not {
  253.       [(%font%*) (%atmfont%*) (%ttfont%*)] {
  254.         {
  255.           false cat /Directory get {
  256.             3 index eq {3 1 roll pop pop true exit} {pop} ifelse
  257.           } forall
  258.           {
  259.             dup template stringmatch {
  260.               FontDirectory 1 index known
  261.               1 index (%font%_dir_) eq or not {
  262.                 scratch cvs
  263.                 /proc load
  264.                 end stopped
  265.                 ##_RFA_TEMP_## begin
  266.                 {
  267.                   $error /errorname get dup /invalidexit eq {
  268.                     pop /done true def exit
  269.                   } {
  270.                     $error /command get exch
  271.                     errordict exch get exec
  272.                   } ifelse
  273.                 } if
  274.               } {
  275.                 pop
  276.               } ifelse
  277.             } {
  278.               pop
  279.             } ifelse
  280.           } {
  281.             pop
  282.           } ifelse
  283.         } 256 string filenameforall
  284.       } forall
  285.     } if
  286.     end
  287.     ##_RFA_TEMP_## /cat get begin
  288.     userdict /##_RFA_TEMP_## undef
  289.   } bind
  290.  
  291. %%
  292. %% FontGCheck tries to decide whether the font should live in local or global
  293. %% VM.  Type 3 fonts go in local VM, and most Type 1 go in global VM.
  294. %% Hybrid fonts, such as Optima, have to go in local VM so that save
  295. %% and restore do the right thing.  The Black Book suggests that we
  296. %% look for an occurrence of /hires before /Subrs to decide that.  However,
  297. %% that is in the encoded portion of the font.  It appears that Optima
  298. %% is the only font that currently needs this, so we explicitly check
  299. %% the name.
  300. %%
  301.     /HybridNames [ (Optima) ]
  302.     /FGnonpsfont
  303.     {
  304.  
  305.         dup (.pfb) search
  306.         {
  307.             pop pop pop true
  308.         }
  309.         {
  310.             (.ttf) search
  311.             {
  312.                 pop pop pop true
  313.             }
  314.             {
  315.                 pop false
  316.             } ifelse
  317.         } ifelse
  318.  
  319.     } bind
  320.  
  321.     /FontGCheck
  322.     {
  323.  
  324.         FGnonpsfont
  325.         {
  326.             pop true
  327.         }
  328.         {
  329.       4 dict begin
  330.       (r) file /ffile exch def
  331.       /glob null def
  332.       /ss 1024 string def
  333.       {
  334.         ffile ss readline not {pop exit} if
  335.         (eexec) search {pop pop pop exit} if
  336.         (/Private) search {pop pop pop exit} if
  337.         {token} stopped {pop pop false} if {
  338.           dup /FontType eq {
  339.             pop token pop exch pop dup 1 eq exch 0 eq or {
  340.               /glob true def
  341.             } {
  342.               /glob false def exit
  343.             } ifelse
  344.           } {
  345.             /FamilyName eq {
  346.               token pop exch pop HybridNames {
  347.                 1 index eq {
  348.                   /glob false def exit
  349.                 } if
  350.               } forall
  351.               pop
  352.               glob false eq {exit} if
  353.             } {
  354.               pop
  355.             } ifelse
  356.           } ifelse
  357.         } if
  358.       } loop
  359.       glob null ne {glob} {false} ifelse
  360.       end
  361.     } ifelse
  362.   }bind
  363.  
  364. %% SubstituteResource is used for the situation where the font supplied
  365. %% by the foundry has a different name to the standard PostScript font
  366. %% name.  Call it with /PostScriptName /FoundryName SubstituteResource.
  367. %% After it has finished, both fonts will have been defined in FontDirectory.
  368. %% [A possible optimisation might be to change Bdefinefont() so that it
  369. %% could add a previously defined font to the FontDirectory; currently
  370. %% we have to copy and redefine the font.]
  371.     /SubstituteResource
  372.     {
  373.         % Save a copy of the foundry name.
  374.         dup /foundryname exch def
  375.  
  376.         FindResource
  377.         dup gcheck setglobal
  378.         dup length dict exch
  379.         {
  380.             1 index /FID eq
  381.             {
  382.                 pop pop
  383.             }
  384.             {
  385.                 1 index /OrigFontName eq
  386.                 {
  387.                     pop 2 index dup type /stringtype eq {cvn} if
  388.                 }
  389.                 {
  390.                     1 index /FontName eq
  391.                     {
  392.                         pop 2 index dup type /stringtype eq {cvn} if
  393.                     } if
  394.                 } ifelse
  395.                 2 index 3 1 roll put
  396.             } ifelse
  397.         } forall
  398.  
  399.         % Keep the originally requested font name around too.
  400.         dup /TProFontName known not
  401.         {
  402.             dup /TProFontName foundryname dup length string cvs cvn put
  403.         } if
  404.  
  405.         definefont pop
  406.     } bind
  407.  
  408.   /Directory 1 dict
  409.  
  410. >>
  411.  
  412. /Font exch /Category defineresource
  413.  
  414.  
  415.