11module AFM
2-
3- ISO_LATIN1_ENCODING = %w(
4- .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
5- .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
6- .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
7- .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space
8- exclam quotedbl numbersign dollar percent ampersand quoteright
9- parenleft parenright asterisk plus comma minus period slash zero one
10- two three four five six seven eight nine colon semicolon less equal
11- greater question at A B C D E F G H I J K L M N O P Q R S
12- T U V W X Y Z bracketleft backslash bracketright asciicircum
13- underscore quoteleft a b c d e f g h i j k l m n o p q r s
14- t u v w x y z braceleft bar braceright asciitilde .notdef .notdef
15- .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
16- .notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave
17- acute circumflex tilde macron breve dotaccent dieresis .notdef ring
18- cedilla .notdef hungarumlaut ogonek caron space exclamdown cent
19- sterling currency yen brokenbar section dieresis copyright ordfeminine
20- guillemotleft logicalnot hyphen registered macron degree plusminus
21- twosuperior threesuperior acute mu paragraph periodcentered cedilla
22- onesuperior ordmasculine guillemotright onequarter onehalf threequarters
23- questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE
24- Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex
25- Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis
26- multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn
27- germandbls agrave aacute acircumflex atilde adieresis aring ae
28- ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex
29- idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide
30- oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis
31- )
32-
33-
2+ ISO_LATIN1_ENCODING = %w[
3+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
4+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
5+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
6+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef space
7+ exclam quotedbl numbersign dollar percent ampersand quoteright
8+ parenleft parenright asterisk plus comma minus period slash zero one
9+ two three four five six seven eight nine colon semicolon less equal
10+ greater question at A B C D E F G H I J K L M N O P Q R S
11+ T U V W X Y Z bracketleft backslash bracketright asciicircum
12+ underscore quoteleft a b c d e f g h i j k l m n o p q r s
13+ t u v w x y z braceleft bar braceright asciitilde .notdef .notdef
14+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef .notdef
15+ .notdef .notdef .notdef .notdef .notdef .notdef .notdef dotlessi grave
16+ acute circumflex tilde macron breve dotaccent dieresis .notdef ring
17+ cedilla .notdef hungarumlaut ogonek caron space exclamdown cent
18+ sterling currency yen brokenbar section dieresis copyright ordfeminine
19+ guillemotleft logicalnot hyphen registered macron degree plusminus
20+ twosuperior threesuperior acute mu paragraph periodcentered cedilla
21+ onesuperior ordmasculine guillemotright onequarter onehalf threequarters
22+ questiondown Agrave Aacute Acircumflex Atilde Adieresis Aring AE
23+ Ccedilla Egrave Eacute Ecircumflex Edieresis Igrave Iacute Icircumflex
24+ Idieresis Eth Ntilde Ograve Oacute Ocircumflex Otilde Odieresis
25+ multiply Oslash Ugrave Uacute Ucircumflex Udieresis Yacute Thorn
26+ germandbls agrave aacute acircumflex atilde adieresis aring ae
27+ ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex
28+ idieresis eth ntilde ograve oacute ocircumflex otilde odieresis divide
29+ oslash ugrave uacute ucircumflex udieresis yacute thorn ydieresis
30+ ]
31+
3432 class Font
3533 attr_reader :metadata , :char_metrics , :char_metrics_by_code , :kern_pairs
36-
34+
3735 # Loading a Font Metrics file by absolute path (no automatic font path resolution)
3836 def initialize ( filename )
3937 @metadata = { }
@@ -43,61 +41,62 @@ def initialize(filename)
4341 File . open ( filename ) do |file |
4442 mode = :meta
4543 file . each_line do |line |
46- case ( line )
47- when /^StartFontMetrics/ ; mode = :meta
48- when /^StartCharMetrics/ ; mode = :char_metrics
49- when /^EndCharMetrics/ ; mode = :meta
50- when /^StartKernData/ ; mode = :kern_data
51- when /^StartKernPairs/ ; mode = :kern_pairs
52- when /^EndKernPairs/ ; mode = :kern_data
53- when /^EndKernData/ ; mode = :meta
44+ case line
45+ when /^StartFontMetrics/ then mode = :meta
46+ when /^StartCharMetrics/ then mode = :char_metrics
47+ when /^EndCharMetrics/ then mode = :meta
48+ when /^StartKernData/ then mode = :kern_data
49+ when /^StartKernPairs/ then mode = :kern_pairs
50+ when /^EndKernPairs/ then mode = :kern_data
51+ when /^EndKernData/ then mode = :meta
5452 else
55- case ( mode )
53+ case mode
5654 when :meta
57- if match = line . match ( /^([\w ]+) (.*)$/ )
55+ if ( match = line . match ( /^([\w ]+) (.*)$/ ) )
5856 @metadata [ match [ 1 ] ] = match [ 2 ]
5957 end
6058 when :char_metrics
6159 metrics = { }
62- metrics [ :charcode ] = match [ 1 ] . to_i if match = line . match ( /C (-?\d +) *?;/ )
63- metrics [ :wx ] = match [ 1 ] . to_i if match = line . match ( /WX (-?\d +) *?;/ )
64- metrics [ :name ] = match [ 1 ] if match = line . match ( /N ([.\w ]+) *?;/ )
65- if match = line . match ( /B (-?\d +) (-?\d +) (-?\d +) (-?\d +) *?;/ )
66- metrics [ :boundingbox ] = [ match [ 1 ] . to_i , match [ 2 ] . to_i , match [ 3 ] . to_i , match [ 4 ] . to_i ]
60+ metrics [ :charcode ] = match [ 1 ] . to_i if ( match = line . match ( /C (-?\d +) *?;/ ) )
61+ metrics [ :wx ] = match [ 1 ] . to_i if ( match = line . match ( /WX (-?\d +) *?;/ ) )
62+ metrics [ :name ] = match [ 1 ] if ( match = line . match ( /N ([.\w ]+) *?;/ ) )
63+ if ( match = line . match ( /B (-?\d +) (-?\d +) (-?\d +) (-?\d +) *?;/ ) )
64+ metrics [ :boundingbox ] = [ match [ 1 ] . to_i , match [ 2 ] . to_i , match [ 3 ] . to_i , match [ 4 ] . to_i ]
6765 end
6866 @char_metrics [ metrics [ :name ] ] = metrics if metrics [ :name ]
6967 @char_metrics_by_code [ metrics [ :charcode ] ] = metrics if metrics [ :charcode ] && metrics [ :charcode ] > 0
7068 when :kern_pairs
71- if match = line . match ( /^KPX ([.\w ]+) ([.\w ]+) (-?\d +)$/ )
69+ if ( match = line . match ( /^KPX ([.\w ]+) ([.\w ]+) (-?\d +)$/ ) )
7270 @kern_pairs << [ match [ 1 ] , match [ 2 ] , match [ 3 ] . to_i ]
7371 end
7472 end
7573 end
7674 end
7775 end
7876 end
79-
80- #
77+
78+ #
8179 # alias for new()
8280 def self . from_file ( file )
83- self . new ( file )
81+ new ( file )
8482 end
85-
83+
8684 #
8785 # Get metadata by key
8886 def []( key )
8987 @metadata [ key ]
9088 end
89+
9190 #
9291 # Get metrics for character. Takes an integer (charcode) or
9392 # a one-char string. currently works only for Latin1 strings,
9493 # since we only have a chartable for the Latin1 charset so far.
9594 # (shamelessly stolen from AFM.pm by Gisle Aas)
9695 def metrics_for ( char )
97- glyph = if ( char . kind_of ?( Integer ) )
96+ glyph = if char . is_a ?( Integer )
9897 ISO_LATIN1_ENCODING [ char ]
9998 else
100- ISO_LATIN1_ENCODING [ char . unpack ( "C*" ) . first ]
99+ ISO_LATIN1_ENCODING [ char . unpack1 ( "C" ) ]
101100 end
102101 @char_metrics [ glyph ]
103102 end
0 commit comments