afmmetrics.ph 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/perl
  2. ## --------------------------------------------------------------------------
  3. ##
  4. ## Copyright 1996-2017 The NASM Authors - All Rights Reserved
  5. ## See the file AUTHORS included with the NASM distribution for
  6. ## the specific copyright holders.
  7. ##
  8. ## Redistribution and use in source and binary forms, with or without
  9. ## modification, are permitted provided that the following
  10. ## conditions are met:
  11. ##
  12. ## * Redistributions of source code must retain the above copyright
  13. ## notice, this list of conditions and the following disclaimer.
  14. ## * Redistributions in binary form must reproduce the above
  15. ## copyright notice, this list of conditions and the following
  16. ## disclaimer in the documentation and/or other materials provided
  17. ## with the distribution.
  18. ##
  19. ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  20. ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  21. ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  31. ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ##
  33. ## --------------------------------------------------------------------------
  34. #
  35. # Parse AFM metric file, returns a reference to fontdata
  36. #
  37. sub parse_afm_file($$) {
  38. my($filename, $filetype) = @_;
  39. my $fontdata = {
  40. widths => {},
  41. kern => {}
  42. };
  43. my $charmetrics = 0;
  44. my $kerndata = 0;
  45. my $charcode, $width, $name;
  46. my $fontfile = $filename.'.'.$filetype;
  47. return undef unless ( -f $fontfile );
  48. $fontdata->{file} = $fontfile;
  49. $fontdata->{type} = $filetype;
  50. $fontdata->{scale} = 1000; # AFM metrics always have scale 1000
  51. return undef unless (open(my $fh, '<', $filename.'.afm'));
  52. while ( my $line = <$fh> ) {
  53. if ( $line =~ /^\s*FontName\s+(.*)\s*$/i ) {
  54. $fontdata->{'name'} = $1;
  55. } elsif ( $line =~ /^\s*StartCharMetrics\b/i ) {
  56. $charmetrics = 1;
  57. } elsif ( $line =~ /^\s*EndCharMetrics\b/i ) {
  58. $charmetrics = 0;
  59. } elsif ( $line =~ /^\s*StartKernPairs\b/i ) {
  60. $kerndata = 1;
  61. } elsif ( $line =~ /^\s*EndKernPairs\b/i ) {
  62. $kerndata = 0;
  63. } elsif ( $charmetrics ) {
  64. my @data = split(/\s*;\s*/, $line);
  65. undef $charcode, $width, $name;
  66. foreach my $d ( @data ) {
  67. my @dd = split(/\s+/, $d);
  68. if ( $dd[0] eq 'C' ) {
  69. $charcode = $dd[1];
  70. } elsif ( $dd[0] eq 'WX' ) {
  71. $width = $dd[1];
  72. } elsif ( $dd[0] eq 'W' ) {
  73. $width = $dd[2];
  74. } elsif ( $dd[0] eq 'N' ) {
  75. $name = $dd[1];
  76. }
  77. }
  78. if ( defined($name) && defined($width) ) {
  79. $fontdata->{widths}{$name} = $width;
  80. }
  81. } elsif ( $kerndata ) {
  82. my($kpx, $a, $b, $adj) = split(/\s+/, $line);
  83. if ( $kpx eq 'KPX' ) {
  84. if (!exists($fontdata->{kern}{$a})) {
  85. $fontdata->{kern}{$a} = {};
  86. }
  87. $fontdata->{kern}{$a}{$b} = $adj;
  88. }
  89. }
  90. }
  91. return $fontdata;
  92. }
  93. 1;