Debian Codegraph @

texmf-dist/doc/fonts/gnu-freefont/tools/report/ligatureLookups.py

raw
1 #!/usr/bin/env ../utility/fontforge-interp.sh
2 __license__ = """
3 This file is part of Gnu FreeFont.
4
5 Gnu FreeFont is free software: you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation, either version 3 of the License, or (at your option) any later
8 version.
9
10 Gnu FreeFont is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 Gnu FreeFont. If not, see <http://www.gnu.org/licenses/>.
16 """
17 __author__ = "Stevan White"
18 __email__ = "[email protected]"
19 __copyright__ = "Copyright 2009, 2010, 2012 Stevan White"
20 __date__ = "$Date: 2012-03-28 21:24:32 +0200 (Wed, 28 Mar 2012) $"
21 __version__ = "$Revision: 2184 $"
22
23 __doc__ = """
24 ligaturelookups
25
26 fontforge -script ligature.ookups font_file_path...
27
28 Output is HTML showing all the ligature lookups in the font.
29
30 To display the ligature, the HTML entities for the component Unicode charaters
31 are printed together. Then to show the components sparately they, are printed
32 with intervening spaces.
33
34 Most web browsers do not display any character unless it is Unicode.
35 It may replace a sequence of Unicode characters by a ligature, however.
36
37 Some of the ligatures in Indic ranges expand to (are made of) non-Unicode
38 characters, which themselves are ligatures. Ultimately, they all should
39 resolve to Unicode characters, although there isn't any real limit to how many
40 steps it may take.
41
42 The resulting string of Unicode characters can then be put into HTML, which
43 should be properly rendered by a browser.
44
45 """
46
47 __usage = """Usage:
48 fontforge -script ligaturelookups.py font-path-1 font-path-2 ...
49 """
50
51 import fontforge
52 from sys import stdout, stderr, argv, exit
53
54 def get_ligature_lookups( font ):
55 try:
56 tables = []
57 for lookup in font.gsub_lookups:
58 if font.getLookupInfo( lookup )[0] == 'gsub_ligature':
59 sts = font.getLookupSubtables( lookup )
60 for st in sts:
61 tables.append( st )
62 return tables
63 except EnvironmentError, ( e ):
64 print >> stderr, 'EnvironmentError ' + str( e )
65 except TypeError, ( t ):
66 print >> stderr, 'TypeError ' + str( t )
67 return None
68
69 _preamble= """<?xml version="1.0" encoding="utf-8"?>
70 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
71 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
72 <html xmlns="http://www.w3.org/1999/xhtml">
73 <head>
74 <title>Ligatures</title>
75 <style type="text/css">
76 .nonchar { background-color: red; }
77 table, tr, td { font-family: inherit; }
78 table, tr, td { font-style: inherit; }
79 table, tr, td { font-weight: inherit; }
80 td { text-align: right; }
81 td { line-height: 1; }
82 .ligatures td { width: 2em; }
83 .ligatures th { text-align: left; font-family: freemono, monospace; }
84 </style>
85 </head>
86 <body>
87 """
88
89 _postamble="""
90 </body>
91 </html>
92 """
93
94 _style_div_html = """<div style="font-family: '%s';%s%s">"""
95 _lig_header_html = '<h2>Ligatures in %s</h2>'
96
97 def print_ligatures( fontPath ):
98 subtables = []
99 font = fontforge.open( fontPath )
100
101 style = ''
102 if font.italicangle != 0.0:
103 style = "font-style: italic; "
104 weight = ''
105 if font.weight == 'Bold':
106 weight = "font-weight: bold; "
107
108 print _style_div_html % ( font.familyname, style, weight )
109 print _lig_header_html % ( font.fontname )
110
111 subtable_names = get_ligature_lookups( font )
112 for subtable_name in subtable_names:
113 subtables.append( makeLigatureSubtable( font, subtable_name ) )
114 for subtable in subtables:
115 out = htmlListOfLigSubtable( font, subtable, subtables )
116 stdout.writelines( out )
117 stdout.flush()
118 print '</div>'
119
120 class Ligature:
121 def __init__( self, glyph ):
122 self.glyph = glyph
123 self.parts = []
124 def setParts( self, parts ):
125 self.parts = parts
126 def append( self, part ):
127 self.parts.append( part )
128
129 class LigatureSubtable:
130 def __init__( self, tablename, name ):
131 self.tablename = tablename
132 self.name = name
133 self.ligatures = []
134 def append( self, ligature ):
135 self.ligatures.append( ligature )
136 def findLigatureGlyph( self, g ):
137 for p in self.ligatures:
138 if g == p.glyph.encoding:
139 return p
140 return False
141
142 def findLigatureGlyph( g, subtables ):
143 for s in subtables:
144 lig = s.findLigatureGlyph( g )
145 if lig:
146 return lig
147 return False
148
149 def makeLigatureSubtable( font, subtable_name ):
150 """
151 From FontForge Python scripting doc
152
153 glyph.getPosSub( lookup-subtable-name )
154
155 Returns any positioning/substitution data attached to the glyph
156 controlled by the lookup-subtable. If the name is "*" then returns
157 data from all subtables.
158
159 The data are returned as a tuple of tuples.
160 The first element of the subtuples is the name of the lookup-subtable.
161 The second element will be one of the strings:
162 "Position", "Pair", "Substitution", "AltSubs", "MultSubs","Ligature".
163 ...
164 Ligature data will be followed by several strings each containing
165 the name of a ligature component glyph.
166
167
168 BUT...
169 this info is attached to glyphs...
170 which glyph is it attached to? ones in the range, or the ligatures?
171 how to get the glyphs in the range referred to by the lookup??
172
173 Evidently, the library has stuff arranged internally to do the search
174 efficiently in the backwards direction, from glyph to subtable.
175
176 font.getLookupInfo gets a feature-script-lang-tuple, which in principle
177 should be able to resolve a glyph list... but can't see how to use it...
178
179 """
180 subtable = LigatureSubtable( "", subtable_name )
181 for g in font.glyphs():
182 ligs = g.getPosSub( subtable_name )
183 if ligs:
184 ligature = Ligature( g )
185 for lr in ligs:
186 if len( lr ) < 3 or lr[1] != 'Ligature':
187 print >> stderr, font.fullname, '- non-ligature: ', g.glyphname
188 break
189 i = 2
190 while i < len( lr ):
191 ligature.append( lr[i] )
192 i += 1
193
194 subtable.append( ligature )
195 return subtable
196
197 _table_head_html = '''<table class="ligatures" rules="groups">
198 <caption>%s</caption>
199 <colgroup>
200 <col style="width: 50ex" />
201 </colgroup>
202 <colgroup>
203 <col style="width: 4ex" />
204 </colgroup>
205 '''
206
207 def htmlListOfLigSubtable( font, subtable, subtables ):
208 out = [ _table_head_html % ( subtable.name ) ]
209 for lig in subtable.ligatures:
210 out += [ '<tr>\n<th>' ]
211
212 # FIXME this will fail for high Unicode
213 if lig.glyph.unicode > -1:
214 s = font.findEncodingSlot( lig.glyph.unicode )
215 out += [ '%s%0.4x%s' %( "U+", s, " " ) ]
216 else:
217 out += [ '%s%0.4x%s' %( "#", lig.glyph.encoding, " " ) ]
218 out += [ lig.glyph.glyphname ]
219 out += [ '</th>' ]
220
221 out += [ '<td>' ]
222 for p in lig.parts:
223 out += [ nestedEntity( font, subtable, p, subtables ) ]
224 out += [ '</td>' ]
225
226 for p in lig.parts:
227 out += [ '<td>' ]
228 out += [ nestedEntity( font, subtable, p, subtables ) ]
229 out += [ '</td>' ]
230 out += [ '</tr>\n' ]
231 out += [ "</table>" ]
232 return out
233
234 def nestedEntity( font, subtable, a, subtables ):
235 """
236 Expands each ligature, then checks each component to see if it's
237 Unicode.
238 If not, it looks through all the ligature tables to expand it,
239 and so on recursively until only Unicode characters remain.
240 """
241 s = font.findEncodingSlot( a )
242 if s >= 0xe000 and s <= 0xf8ff: # Unicode only
243 lig = findLigatureGlyph( s, subtables )
244 if lig:
245 #print >> stderr, 'Nested glyph found: ' + a
246 for p in lig.parts:
247 return nestedEntity( font, subtable, p, subtables )
248 else:
249 print >> stderr, font.fullname, '- No nested glyph: ', a
250 return '<span class="nonchar">&nbsp;</span>'
251 else:
252 return entityHTML( font, a )
253
254 def entityHTML( font, a ):
255 s = font.findEncodingSlot( a )
256 if s == -1:
257 print >> stderr, font.fullname, '- Missing glyph: ', a
258 return '<span class="nonchar">&nbsp;</span>'
259 else:
260 return formatted_hex_value( s )
261
262 def formatted_hex_value( n ):
263 return '%s%0.4x%s' %( "&#x", n, ";" )
264
265 # --------------------------------------------------------------------------
266 args = argv[1:]
267
268 if len( args ) < 1 or len( args[0].strip() ) == 0:
269 print >> stderr, __usage
270 exit( 0 )
271
272 print _preamble
273 for font_name in args:
274 print_ligatures( font_name )
275 print _postamble
276