home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Font / AFM.pm
Encoding:
Perl POD Document  |  1999-12-28  |  9.6 KB  |  363 lines

  1.  
  2. package Font::AFM;
  3.  
  4. =head1 NAME
  5.  
  6. Font::AFM - Interface to Adobe Font Metrics files
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.  use Font::AFM;
  11.  $h = new Font::AFM "Helvetica";
  12.  $copyright = $h->Notice;
  13.  $w = $h->Wx->{"aring"};
  14.  $w = $h->stringwidth("Gisle", 10);
  15.  $h->dump;  # for debugging
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. This module implements the Font::AFM class. Objects of this class are
  20. initialised from an AFM-file and allows you to obtain information
  21. about the font and the metrics of the various glyphs in the font.
  22.  
  23. All measurements in AFM files are given in terms of units equal to
  24. 1/1000 of the scale factor of the font being used. To compute actual
  25. sizes in a document, these amounts should be multiplied by (scale
  26. factor of font)/1000.
  27.  
  28. The following methods are available:
  29.  
  30. =over 3
  31.  
  32. =item new($fontname)
  33.  
  34. Object constructor. Takes the name of the font as argument. It will
  35. croak if the font can not be found.
  36.  
  37. =item latin1_wx_table()
  38.  
  39. Returns a 256 element array, where each element contains the width
  40. of the corresponding character in the iso-8859-1 character set.
  41.  
  42. =item stringwidth($string, [$fontsize])
  43.  
  44. Returns the width of the string passed as argument. The string is
  45. assumed to be encoded in the iso-8859-1 character set.  A second
  46. argument can be used to scale the width according to the font size.
  47.  
  48. =item FontName
  49.  
  50. The name of the font as presented to the PostScript language
  51. C<findfont> operator, for instance "Times-Roman".
  52.  
  53. =item FullName
  54.  
  55. Unique, human-readable name for an individual font, for instance
  56. "Times Roman".
  57.  
  58. =item FamilyName
  59.  
  60. Human-readable name for a group of fonts that are stylistic variants
  61. of a single design. All fonts that are member of such a group should
  62. have exactly the same C<FamilyName>. Example of a family name is
  63. "Times".
  64.  
  65. =item Weight
  66.  
  67. Human-readable name for the weight, or "boldness", attribute of a font.
  68. Exampes are C<Roman>, C<Bold>, C<Light>.
  69.  
  70. =item ItalicAngle
  71.  
  72. Angle in degrees counterclockwise from the vertical of the dominant
  73. vertical strokes of the font.
  74.  
  75. =item IsFixedPitch
  76.  
  77. If the value is C<true>, it indicated that the font is a fixed-pitch
  78. (monospaced) font.
  79.  
  80. =item FontBBox
  81.  
  82. A string of four numbers giving the lower-left x, lower-left y,
  83. upper-right x, and upper-right y of the font bounding box. The font
  84. bounding box is the smallest rectangle enclosing the shape that would
  85. result if all the characters of the font were placed with their
  86. origins coincident, and then painted.
  87.  
  88. =item UnderlinePosition
  89.  
  90. Recommended distance from the baseline for positioning underline
  91. stokes. This number is the y coordinate of the center of the stroke.
  92.  
  93. =item UnderlineThickness
  94.  
  95. Recommended stroke width for underlining.
  96.  
  97. =item Version
  98.  
  99. Version number of the font.
  100.  
  101. =item Notice
  102.  
  103. Trademark or copyright notice, if applicable.
  104.  
  105. =item Comment
  106.  
  107. Comments found in the AFM file.
  108.  
  109. =item EncodingScheme
  110.  
  111. The name of the standard encoding scheme for the font. Most Adobe
  112. fonts use the C<AdobeStandardEncoding>. Special fonts might state
  113. C<FontSpecific>.
  114.  
  115. =item CapHeight
  116.  
  117. Usually the y-value of the top of the capital H.
  118.  
  119. =item XHeight
  120.  
  121. Typically the y-value of the top of the lowercase x.
  122.  
  123. =item Ascender
  124.  
  125. Typically the y-value of the top of the lowercase d.
  126.  
  127. =item Descender
  128.  
  129. Typically the y-value of the bottom of the lowercase p.
  130.  
  131. =item Wx
  132.  
  133. Returns a hash table that maps from glyph names to the width of that glyph.
  134.  
  135. =item BBox
  136.  
  137. Returns a hash table that maps from glyph names to bounding box information.
  138. The bounding box consist of 4 numbers: llx, lly, urx, ury.
  139.  
  140. =item dump
  141.  
  142. Dumps the content of the Font::AFM object to STDOUT.  Might sometimes
  143. be useful for debugging.
  144.  
  145. =back
  146.  
  147.  
  148. The AFM specification can be found at:
  149.  
  150.    ftp://ftp.adobe.com/pub/adobe/DeveloperSupport/TechNotes/PSfiles/5004.AFM_Spec.ps
  151.  
  152.  
  153. =head1 ENVIRONMENT
  154.  
  155. =over 10
  156.  
  157. =item METRICS
  158.  
  159. Contains the path to seach for AFM-files.  Format is as for the PATH
  160. environment variable. The default path built into this library is:
  161.  
  162.  /usr/lib/afm:/usr/local/lib/afm:/usr/openwin/lib/fonts/afm/:.
  163.  
  164. =back
  165.  
  166.  
  167. =head1 BUGS
  168.  
  169. Kerning data and composite character data is not yet parsed.
  170. Ligature data is not parsed.
  171.  
  172.  
  173. =head1 COPYRIGHT
  174.  
  175. Copyright 1995 Gisle Aas. All rights reserved.
  176.  
  177. This program is free software; you can redistribute it and/or modify
  178. it under the same terms as Perl itself.
  179.  
  180. =cut
  181.  
  182.  
  183. use Carp;
  184.  
  185. $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
  186. sub ModuleVersion { $VERSION; }
  187.  
  188.  
  189. $metrics_path = $ENV{METRICS} ||
  190.     "/usr/lib/afm:/usr/local/lib/afm:/usr/openwin/lib/fonts/afm/:.";
  191. @metrics_path = split(/:/, $metrics_path);
  192. foreach (@metrics_path) { s,/$,, }    # reove trailing slashes
  193.  
  194. @ISOLatin1Encoding = qw(
  195.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
  196.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
  197.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
  198.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space
  199.  exclam quotedbl numbersign dollar percent ampersand quoteright
  200.  parenleft parenright asterisk plus comma minus period slash zero one
  201.  two three four five six seven eight nine colon semicolon less equal
  202.  greater question at A B C D E F G H I J K L M N O P Q R S
  203.  T U V W X Y Z bracketleft backslash bracketright asciicircum
  204.  underscore quoteleft a b c d e f g h i j k l m n o p q r s
  205.  t u v w x y z braceleft bar braceright asciitilde .notdef .notdef
  206.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
  207.  .notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave
  208.  acute circumflex tilde macron breve dotaccent dieresis .notdef ring
  209.  cedilla .notdef hungarumlaut ogonek caron space exclamdown cent
  210.  sterling currency yen brokenbar section dieresis copyright ordfeminine
  211.  guillemotleft logicalnot hyphen registered macron degree plusminus
  212.  twosuperior threesuperior acute mu paragraph periodcentered cedilla
  213.  onesuperior ordmasculine guillemotright onequarter onehalf threequarters
  214.  questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE
  215.  Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex
  216.  Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis
  217.  multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn
  218.  germandbls agrave aacute acircumflex atilde adieresis aring ae
  219.  ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex
  220.  idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide
  221.  oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis
  222. );
  223.  
  224.  
  225.  
  226. sub new
  227. {
  228.    my($class, $fontname) = @_;
  229.    $fontname =~ s/.amf$//;
  230.    my $file = "$fontname.afm";
  231.    unless ($file =~ m,^/,) {
  232.        foreach (@metrics_path) {
  233.        if (-f "$_/$file") {
  234.            $file = "$_/$file";
  235.            last;
  236.        }
  237.        }
  238.    }
  239.    open(AFM, $file) or croak "Can't find the AFM file for $fontname";
  240.    my $this = bless { }, $class;
  241.    local($/, $_) = ("\n", undef);  # ensure correct $INPUT_RECORD_SEPARATOR
  242.    while (<AFM>) {
  243.        next if /^StartKernData/ .. /^EndKernData/;  # kern data not parsed yet
  244.        next if /^StartComposites/ .. /^EndComposites/; # same for composites
  245.        if (/^StartCharMetrics/ .. /^EndCharMetrics/) {
  246.        next unless /^CH?\s/;
  247.        my($name) = /\bN\s+(\w+)\s*;/;
  248.        my($wx)   = /\bWX\s+(\d+)\s*;/;
  249.        my($bbox)    = /\bB\s+([^;]+)\s*;/;
  250.        $this->{'wx'}{$name} = $wx;
  251.        $this->{'bbox'}{$name} = $bbox;
  252.        next;
  253.        }
  254.        last if /^EndFontMetrics/;
  255.        if (/(^\w+)\s+(.*)/) {
  256.        my($key,$val) = ($1, $2);
  257.        $key = lc $key;
  258.        if (defined $this->{$key}) {
  259.            $this->{$key} = [ $this->{$key} ] unless ref $this->{$key};
  260.            push(@{$this->{$key}}, $val);
  261.        } else {
  262.            $this->{$key} = $val;
  263.        }
  264.        } else {
  265.        print STDERR "Can't parse: $_";
  266.        }
  267.    }
  268.    close(AFM);
  269.    $this->{wx}->{'.notdef'} = 0;
  270.    $this->{bbox}{'.notdef'} = "0 0 0 0";
  271.    $this;
  272. }
  273.  
  274. sub latin1_wx_table
  275. {
  276.     my($this) = @_;
  277.     unless ($this->{'_wx_table'}) {
  278.     $this->{'_wx_table'} =
  279.         [ map {$this->{wx}->{$ISOLatin1Encoding[$_]}} 0..255 ];
  280.     }
  281.     @{ $this->{'_wx_table'} };
  282. }
  283.  
  284. sub stringwidth
  285. {
  286.     my($this, $string, $pointsize) = @_;
  287.     return 0.0 unless defined $string;
  288.     return 0.0 unless length $string;
  289.  
  290.     my @wx = $this->latin1_wx_table;
  291.     my $width = 0.0;
  292.     while ($string =~ /./g) {
  293.     $width += $wx[ord $&];
  294.     }
  295.     if ($pointsize) {
  296.     $width *= $pointsize / 1000;
  297.     }
  298.     $width;
  299. }
  300.  
  301. sub FontName;
  302. sub FullName;
  303. sub FamilyName;
  304. sub Weight;
  305. sub ItalicAngle;
  306. sub IsFixedPitch;
  307. sub FontBBox;
  308. sub UnderlinePosition;
  309. sub UnderlineThickness;
  310. sub Version;
  311. sub Notice;
  312. sub Comment;
  313. sub EncodingScheme;
  314. sub CapHeight;
  315. sub XHeight;
  316. sub Ascender;
  317. sub Descender;
  318. sub Wx;
  319. sub BBox;
  320.  
  321.  
  322. sub AUTOLOAD
  323. {
  324.     if ($AUTOLOAD =~ /::DESTROY$/) {
  325.     eval "sub $AUTOLOAD {}";
  326.     goto &$AUTOLOAD;
  327.     } else {
  328.     my $name = $AUTOLOAD;
  329.     $name =~ s/^.*:://;
  330.     croak "Attribute $name not defined for AFM object"
  331.         unless defined $_[0]->{lc $name};
  332.     return $_[0]->{lc $name};
  333.     }
  334. }
  335.  
  336.  
  337.  
  338. sub dump
  339. {
  340.     my($this) = @_;
  341.     my($key, $val);
  342.     foreach $key (sort keys %$this) {
  343.     if (ref $this->{$key}) {
  344.         if (ref $this->{$key} eq "ARRAY") {
  345.         print "$key = [\n\t", join("\n\t", @{$this->{$key}}), "\n]\n";
  346.         } elsif (ref $this->{$key} eq "HASH") {
  347.         print "$key = {\n";
  348.         my $key2;
  349.         foreach $key2 (sort keys %{$this->{$key}}) {
  350.             print "\t$key2 => $this->{$key}{$key2},\n";
  351.         }
  352.         print "}\n";
  353.         } else {
  354.         print "$key = $this->{$key}\n";
  355.         }
  356.     } else {
  357.         print "$key = $this->{$key}\n";
  358.     }
  359.     }
  360. }
  361.  
  362. 1;
  363.