Debian Codegraph @

tools/report/kernclasses.py

raw
1 #!/usr/bin/fontforge -script
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 2009, 2010, 2011, 2017, 2018 Stevan White"
21 __date__ = "$Date: 2018-06-03 22:07:11 +0200 (So, 03. Jun 2018) $"
22 __version__ = "$Revision: 3916 $"
23
24
25 import fontforge
26 from sys import stdout as out, stderr as err
27 from OpenType.UnicodeRanges import *
28
29 def get_kern_subtables( font ):
30 try:
31 tables = []
32 for lookup in font.gpos_lookups:
33 if font.getLookupInfo( lookup )[0] == 'gpos_pair':
34 sts = font.getLookupSubtables( lookup )
35 for st in sts:
36 if font.isKerningClass( st ):
37 tables.append( st )
38 return tables
39 except EnvironmentError as e:
40 print( 'EnvironmentError', e, file=err )
41 except TypeError as t:
42 print( 'TypeError', t, file=err )
43 return None
44 preamble = """<!doctype html>
45 <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
46 <head>
47 <meta charset="UTF-8" />
48 <title>glyph kerning classes in GNU FreeFont
49 </title>
50 <style type="text/css">
51 .nonexistent { background-color: red; }
52 td { text-align: right; font-family: inherit; }
53 .I td { font-style: italic; }
54 .B td { font-weight: bold; }
55 .BI td { font-weight: bold; font-style: italic; }
56 td { line-height: 1; }
57 .classes td { text-align: left; vertical-align: top; }
58 td span { font-weight: normal; font-style: normal; font-size: smaller; color: lime; }
59 td span.pos { color: magenta; }
60 td.zero { color: gray; }
61 </style>
62 </head>
63 <body>
64 """
65 postamble="""
66 </body>
67 </html>
68 """
69
70 def print_kerns( fontPath ):
71 font = fontforge.open( fontPath )
72 print( '<h2>Kerning classes in ' + font.fontname + '</h2>' )
73 weight = ''
74 if font.os2_weight > 500:
75 weight = 'B'
76 style = ''
77 if font.italicangle < 0.0:
78 style = 'I'
79 print( '<div style="font-family: ' + font.familyname + '" '
80 + 'class="' + weight + style + '">' )
81 subtables = get_kern_subtables( font )
82 for st in subtables:
83 print( '<h3>Subtable ' + st + '</h3>' )
84 printKernsOfSubtable( font, st )
85 print( '</div>' )
86 out.flush()
87
88 def printKernsOfSubtable( font, subtable ):
89 kclass = font.getKerningClass( subtable )
90 n = 0
91 leftclasses = kclass[0]
92 rightclasses = kclass[1]
93 kerns = kclass[2]
94 nr = len( rightclasses )
95 print( '<table class="classes"><tr>' )
96 print( '<th>left classes: </th>' )
97 print( '<th>right classes: </th>' )
98 print( '<tr><td>' )
99 for lc in leftclasses:
100 if lc:
101 for c in lc:
102 writeentity( font, c )
103 print( "<br />" )
104 print( "</td>" )
105 print( "<td>" )
106 for rc in rightclasses:
107 if rc:
108 for c in rc:
109 writeentity( font, c )
110 print( "<br />" )
111 print( "</td>" )
112 print( "</tr>" )
113 print( "</table>" )
114 print( "<table>" )
115 print( "<tr>" )
116 print( "<th></th>" )
117 for rc in rightclasses:
118 if rc:
119 out.write( "<th>" + entitystr( font, rc[0] ) + "</th>" )
120 print( "</tr>" )
121 for lc in leftclasses:
122 m = 0
123 if lc:
124 print( "<tr>" )
125 out.write( "<th>" + entitystr( font, lc[0] ) + "</th>" )
126 for rc in rightclasses:
127 kern = kerns[ n * nr + m ]
128 if rc:
129 ccolor = ''
130 ncolor = ''
131 if kern > 0:
132 ncolor = ' class="pos"'
133 if kern == 0:
134 ccolor = ' class="zero"'
135 out.write( '<td' + ccolor + '><span' + ncolor + '>' )
136 if kern == 0:
137 out.write( '&nbsp;' )
138 else:
139 out.write( str( kern ) )
140 out.write( '</span><br />' )
141 printpair( font, lc[0], rc[0] )
142 out.write( '</td>' )
143 m += 1
144 print( "</tr>" )
145 n += 1
146 print( "</table>" )
147
148 def writeentity( font, a ):
149 out.write( entitystr( font, a ) )
150
151 def entitystr( font, a ):
152 s = font.findEncodingSlot( a )
153 v = formatted_hex_value( s )
154 if s == -1:
155 v = '<span class="nonexistent">&nbsp;</span>'
156 print( font.fullname, 'Missing glyph: ' + a, file=err )
157 elif not codepointIsInSomeRange( s ):
158 print( font.fullname, 'Non-unicode: ' + v, file=err )
159 return v
160
161 def printpair( font, p, q ):
162 writeentity( font, p )
163 writeentity( font, q )
164 out.write( ' ' )
165
166 def formatted_hex_value( n ):
167 return '%s%0.4x%s' %( "&#x", n, ";" )
168
169 def printlist( lst ):
170 s = ''
171 delim = ''
172 for m in lst:
173 s += delim + m
174 delim = ' '
175 print( s )
176
177 print( preamble )
178 print_kerns( '../../sfd/FreeSerif.sfd' )
179 print_kerns( '../../sfd/FreeSerifItalic.sfd' )
180 print_kerns( '../../sfd/FreeSerifBold.sfd' )
181 print_kerns( '../../sfd/FreeSerifBoldItalic.sfd' )
182 print_kerns( '../../sfd/FreeSans.sfd' )
183 print_kerns( '../../sfd/FreeSansOblique.sfd' )
184 print_kerns( '../../sfd/FreeSansBold.sfd' )
185 print_kerns( '../../sfd/FreeSansBoldOblique.sfd' )
186 print_kerns( '../../sfd/FreeMono.sfd' )
187 print_kerns( '../../sfd/FreeMonoOblique.sfd' )
188 print_kerns( '../../sfd/FreeMonoBold.sfd' )
189 print_kerns( '../../sfd/FreeMonoBoldOblique.sfd' )
190 print( postamble )