Debian Codegraph @

tools/test/findUseMyMetrics.py

raw
1 #!/usr/bin/env ../utility/fontforge-interp.sh
2 from __future__ import print_function
3 __license__ = """
4 This file is part of GNU FreeFont.
5
6 GNU FreeFont is free software: you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation, either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
17 """
18 __author__ = "Stevan White"
19 __email__ = "[email protected]"
20 __copyright__ = "Copyright 2016, 2018 Stevan White"
21 __date__ = "$Date: $"
22 __version__ = "$Revision: 3050 $"
23
24 __doc__ = """
25 Many applications, especially on Windows, have bugs regarding the TrueType
26 flag USE_MY_METRICS.
27
28 If a glyph reference is rotated, and the flag is set for it, these
29 applications behave as if the advance width of the containing glyph
30 were negative.
31
32 This seems to be endemic on Windows -- it seems to be a deep problem,
33 likely in Cleartype itself. And the behaviour is inconsistent...
34
35 Official statement from MS in OpenType spec:
36 Note that the behavior of the USE_MY_METRICS operation is undefined for
37 rotated composite components.
38
39 """
40
41 #FIXME Tragically, the FontForge Python function useRefsMetrics always
42 # returns true, regardless the value of the USE_MY_METRICS flag
43 # for that reference.
44 # Therefore this script parses the SFD file directly.
45
46 from sys import argv, exit
47 import re
48 from os import path
49
50 problem = False
51
52 class glyphref:
53 def __init__( self, codept ):
54 self.codept = codept
55 self.tmatrix = ()
56 self.offset = ()
57 self.use_my_metrics = False
58
59 class glyph:
60 def __init__( self, name ):
61 self.name = name.strip()
62 self.codept = -1
63 self.refs = []
64 def setCodept( self, codept ):
65 self.codept = codept
66
67 def ref_with_use_my_metrics( self ):
68 for r in self.refs:
69 if r.use_my_metrics:
70 return r
71 return None
72 def __str__( self ):
73 return self.name + " [" + str( hex( self.codept ) ) + "]"
74
75 encoding_re = re.compile( "Encoding: (\d+) (\d+) (\d+)" )
76 _flt_re = "-?\d+(?:/.\d*)?"
77 _rre = ( "Refer: (\d+) (\d+) ([NS])"
78 + " (" + _flt_re + " " + _flt_re + " " + _flt_re + " " + _flt_re + ")"
79 + " (" + _flt_re + " " + _flt_re + ")"
80 + " (\d+)"
81 )
82 refer_re = re.compile( _rre )
83 _flt_grp = "(" + _flt_re + ")"
84 _tmx = _flt_grp + " " + _flt_grp + " " + _flt_grp + " " + _flt_grp
85 tmatrix_re = re.compile( _tmx )
86 _off = _flt_grp + " " + _flt_grp
87 offset_re = re.compile( _tmx )
88
89 def checkUseMyMetrics( fontDir, fontFile ):
90 if isinstance( fontFile, ( list, tuple ) ):
91 print( "In directory " + fontDir )
92 for fontName in fontFile:
93 if fontName.endswith( '.sfd' ):
94 checkUseMyMetrics( fontDir, fontName )
95 else:
96 print( "Argument must be a FontForge SFD file." )
97 exit( 1 )
98 return
99
100 print( "Checking for rotated references with use_my_metrics in "
101 + fontFile )
102 sfd_file = open( path.join( fontDir, fontFile ) )
103
104 glyphs = collectGlyphsAndRefs( sfd_file )
105
106 for glyphname in glyphs:
107 g = glyphs[glyphname]
108 r = g.ref_with_use_my_metrics()
109 if r:
110 if r.tmatrix[0] != 1.0 or r.tmatrix[1] != 0.0:
111 print( g )
112 problem = True
113
114 def collectGlyphsAndRefs( sfd_file ):
115 glyphs = {}
116 curglyph = None
117 for line in sfd_file:
118 if line.startswith( "StartChar: " ):
119 hlen = len( "StartChar: " )
120 glyphname = line[hlen:]
121 curglyph = glyph( glyphname )
122 elif line.startswith( "Encoding" ):
123 m = encoding_re.match( line )
124 if m and curglyph:
125 curglyph.codept = int( m.group( 1 ) )
126 elif line.startswith( "Refer" ):
127 m = refer_re.match( line )
128 if m:
129 index = int( m.group( 1 ) )
130 codept = int( m.group( 2 ) )
131 ref = glyphref( codept )
132 selected = m.group( 3 ) == "S"
133 tm = tmatrix_re.match( m.group( 4 ) )
134 if tm:
135 ref.tmatrix = ( float( tm.group( 1 ) ),
136 float( tm.group( 2 ) ),
137 float( tm.group( 3 ) ),
138 float( tm.group( 4 ) ) )
139 om = offset_re.match( m.group( 5 ) )
140 if om:
141 ref.offset = ( float( om.group( 1 ) ),
142 float( om.group( 2 ) ) )
143 flags = int( m.group( 6 ) )
144 ref.use_my_metrics = flags & 1
145
146 if curglyph:
147 curglyph.refs.append( ref )
148 elif line.startswith( "EndChar" ):
149 if curglyph:
150 glyphs[curglyph.name] = curglyph
151 curglyph = None
152 return glyphs
153
154
155 # --------------------------------------------------------------------------
156 args = argv[1:]
157
158 if len( args ) < 1 or len( args[0].strip() ) == 0:
159 checkUseMyMetrics( '../../sfd/',
160 ( 'FreeSerif.sfd', 'FreeSerifItalic.sfd',
161 'FreeSerifBold.sfd', 'FreeSerifBoldItalic.sfd',
162 'FreeSans.sfd', 'FreeSansOblique.sfd',
163 'FreeSansBold.sfd', 'FreeSansBoldOblique.sfd',
164 'FreeMono.sfd', 'FreeMonoOblique.sfd',
165 'FreeMonoBold.sfd', 'FreeMonoBoldOblique.sfd' ) )
166 else:
167 checkUseMyMetrics( args[0], args[1:] )
168
169 if problem:
170 exit( 1 )