Debian Codegraph @

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

raw
1 #!/usr/bin/env ../utility/fontforge-interp.sh
2
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, Stevan White"
21 __date__ = "$Date: 2010-09-14 13:02:02 $"
22 __version__ = "$Revision: 1.9 $"
23 __doc__ = """
24 Makes an HTML table showing how many glyphs are in each range in each font,
25 and tries to collate that with the OS/2 character range support bit flags.
26
27 Runs under FontForge.
28 fontforge -script ranges.py
29
30 This is a hack--in no way authoritative.
31 Lots of guesswork; much is wrong; the coding is gross.
32
33 See
34 http://www.w3.org/TR/REC-CSS2/notes.html
35 http://shlimazl.nm.ru/eng/fonts_ttf.htm
36 http://www.evertype.com/standards/iso10646/ucs-collections.html
37
38 The intervals are partly just the assigned interval, but often I have
39 listed the ranges that have characters assigned to them.
40 """
41
42 import fontforge
43 from sys import stderr
44 from time import tzset, strftime
45 from OpenType.UnicodeRanges import *
46
47 def total_intervals( intervals ):
48 num = 0
49 for i in intervals:
50 num += i.len()
51 return num
52
53 def count_glyphs_in_intervals( font, intervals ):
54 num = 0
55 for r in intervals:
56 if r.begin < len( font ) and r.end < len( font ):
57 # select() will throw up if try to select value
58 # beyond the range of the encoding
59 try:
60 font.selection.select( ( 'ranges', None ),
61 r.begin, r.end )
62 g = font.selection.byGlyphs
63 for e in g:
64 num += 1
65 except ValueError:
66 print >> stderr, "interval " + str( r ) \
67 + " not representable in " + font.fontname
68 exit( 1 )
69 return num
70
71 special_TT_points = ( 0x00, 0x01, 0x0D )
72
73 def codepointIsSpecialTT( encoding ):
74 """ See Recommendations for OpenType Fonts
75 http://www.microsoft.com/typography/otspec/recom.htm """
76 return encoding in special_TT_points
77
78 class SupportInfo:
79 def __init__( self, os2bit, supports, total ):
80 self.os2bit = os2bit
81 self.supports = supports
82 self.total = total
83
84 class FontSupport:
85 """ A record of support for all OS/2 ranges within a single font.
86 Uses a dictionary internally, to avoid loss of the index info.
87 """
88 def __init__( self, fontPath, short ):
89 font = fontforge.open( fontPath )
90 self.name = font.fontname
91 self.short = short
92 self.myInfos = {}
93 self.totalGlyphs = 0
94 self.fontTotalGlyphs = 0
95 self.privateUseGlyphs = 0
96
97 r = font.os2_unicoderanges
98
99 # print >> stderr, font.fontname, hex( r[0] ), hex( r[1] ),hex( r[2] ),hex( r[3] );
100
101 nRanges = len( ulUnicodeRange )
102
103 for index in range( 0, nRanges ):
104 byte = index / 32
105 bit = index % 32
106
107 self.collectRangeInfo( font, r[byte], bit, index )
108
109 for g in font.glyphs():
110 self.fontTotalGlyphs += 1
111 cp = g.encoding
112 if ( not codepointIsInSomeRange( cp )
113 and not codepointIsSpecialTT( cp ) ):
114 print >> stderr, font.fontname, \
115 "no range for", hex( cp )
116
117 """ '''Would like to check that special TT slots are
118 present, but don't know how...'''
119 for cp in special_TT_points:
120 font.selection.all()
121 if not cp in font.selection.byGlyphs:
122 print >> stderr, font.fontname, \
123 "special TT glyph missing", hex( cp )
124 """
125
126 def collectRangeInfo( self, font, os2supportbyte, bit, index ):
127 supports = ( os2supportbyte & (1 << bit) ) != 0
128 rangeName = ulUnicodeRange[index][1]
129 intervals = ulUnicodeRange[index][2]
130 nglyphs = count_glyphs_in_intervals( font, intervals )
131 self.setRangeSupport( index, supports, nglyphs )
132 self.totalGlyphs += nglyphs
133 if index == 60 or index == 90:
134 self.privateUseGlyphs += nglyphs
135
136 def setRangeSupport( self, idx, supports, total ):
137 if self.myInfos.has_key( idx ):
138 print >> stderr, "OS/2 index ", idx, " duplicated"
139 exit( 1 )
140 self.myInfos[idx] = SupportInfo( idx, supports, total )
141
142 def getInfo( self, idx ):
143 if not self.myInfos.has_key( idx ):
144 print >> stderr, "OS/2 index ", idx, " not found"
145 exit( 1 )
146 return self.myInfos[ idx ]
147
148 table_head = '''
149 <table class="fontrangereport" cellspacing="0" cellpadding="0" frame="box" rules="all">
150 <caption>
151 OS/2 character ranges vs. font faces
152 </caption>
153 <colgroup>
154 <col /><col /><col />
155 </colgroup>
156 <colgroup>
157 <col class="roman"/><col /><col /><col />
158 <col /><col /><col /><col />
159 </colgroup>
160 <colgroup>
161 <col class="roman"/><col /><col /><col />
162 <col /><col /><col /><col />
163 </colgroup>
164 <colgroup>
165 <col class="roman"/><col /><col /><col />
166 <col /><col /><col /><col />
167 </colgroup>
168 <thead>
169 <tr><th>OS/2 character range</th>
170 <th>range<br />total</th>
171 <td></td>
172 %s
173 </tr>
174 </thead>'''
175
176 def print_font_range_table( fontSupportList ):
177 headings = ''
178 for fsl in fontSupportList:
179 headings += '<th colspan="2">' + fsl.short + '</th>'
180 print table_head % ( headings )
181
182 for r in ulUnicodeRange:
183 idx = r[0]
184 range_name = r[1]
185 intervals = r[2]
186
187 rowclass = ' class="low"'
188 if len( ulUnicodeRange[idx] ) > 3 and ulUnicodeRange[ idx ][3]:
189 rowclass = ' class="high"'
190 if idx == 60 or idx == 90:
191 rowclass = ' class="private"'
192
193 print '<tr%s><td>%s</td>' % ( rowclass, range_name )
194 print '<td class="num">%i</td>' % (
195 total_intervals( intervals ) )
196 print '<td></td>'
197 for fsl in fontSupportList:
198 supportInfo = fsl.getInfo( idx )
199 supportString = ''
200 if supportInfo.supports:
201 supportString = '&bull;'
202 totalStr = '&nbsp;'
203 if supportInfo.total:
204 totalStr = str( supportInfo.total )
205
206 print '<td class="num">%s</td><td>%s</td>' % (
207 totalStr, supportString )
208
209 print '</tr>'
210 print '<tr><th colspan="3">total in Unicode ranges</th>'
211 for fsl in fontSupportList:
212 print '<td class="num" colspan="2">%i&nbsp;</td>' % (
213 fsl.totalGlyphs )
214 print '</tr>'
215 print '<tr><th colspan="3">total in font</th>'
216 for fsl in fontSupportList:
217 print '<td class="num" colspan="2">%i&nbsp;</td>' % (
218 fsl.fontTotalGlyphs )
219 print '</tr>'
220 print '<tr><th colspan="3">total in Private Use</th>'
221 for fsl in fontSupportList:
222 print '<td class="num" colspan="2">%i&nbsp;</td>' % (
223 fsl.privateUseGlyphs )
224 print '</tr>'
225 # Would also like to total glyphs in ranges for each font,
226 # and also print total glyphs in each font.
227 print '</table>'
228
229 table_introduction = """
230 For historical reasons, TrueType classifies Unicode ranges according to
231 an extension of the old OS/2 character ranges. This table shows how many
232 characters FontForge finds in each of the ranges for each face in the family.
233 """
234
235 table_explanation = """
236 <p>
237 Ranges for which (FontForge reports that) the font's OS/2 support
238 bit is set are marked with a bullet.
239 </p>
240 <p>
241 For many ranges, I took the liberty of reducing the set of characters
242 considered to those listed for the range in the current Unicode charts.
243 The number of characters supported can thus be less than the width of the range.
244 </p>
245 <p>
246 The totals include glyphs in the Private Use areas (where there
247 are many ligatures, alternative forms, and glyph components). The glyphs
248 in these areas do not correspond to Unicode characters.
249 </p>
250 <p>
251 Three control characters are inserted at 0x00, 0x01 and 0x0d to satisfy the
252 TrueType recommendations. These are counted under Basic Latin, but other
253 control characters there are not counted.
254 </p>
255 <p>
256 Note that there is a discrepancy in the Greek Symbols, Hebrew Extended and
257 Arabic Extended ranges, between what FontForge reports here and in its Font
258 Info window under OS/2 Character Ranges. I don't know why, but these ranges
259 are also not well defined in the TrueType standard.
260 </p>
261 <p>
262 Note the two characters from Devanagri. These are the danda and double-danda
263 used by other Indic scripts.
264 </p>
265 <p>
266 The ranges <span style="color: #555">beyond Unicode point 0xFFFF</span>, are
267 shaded. </p>
268 """
269
270 html_heading = '''
271 <html>
272 <head>
273 <title>
274 Gnu FreeFont character range support
275 </title>
276 <style type="text/css">
277 tr.high { color: #555 }
278 tr.private { background-color: silver; }
279 td.num { text-align: right }
280 td { padding-right: 0.25ex }
281 th { padding: 0.25ex }
282 .roman { border-left: medium black solid; }
283 caption { font-size: larger; font-weight: bold; }
284 </style>
285 </head>
286 '''
287
288 def print_font_range_report( fontSupportList ):
289 print html_heading
290
291 print '<body>'
292 print '<h1>'
293 print 'Gnu FreeFont support for OpenType OS/2 character ranges'
294 print '</h1>'
295 print '<p>'
296 print table_introduction
297 print '</p>'
298 print_font_range_table( fontSupportList )
299 print '<p>'
300 print table_explanation
301 tzset()
302 print 'Generated by <code>range_report.py</code> on %s.' % (
303 strftime('%X %x %Z') )
304 print '</p>'
305 print '</body>'
306
307 print '</html>'
308
309 supportList = []
310 supportList.append( FontSupport( '../../sfd/FreeSerif.sfd', 'Srf' ) )
311 supportList.append( FontSupport( '../../sfd/FreeSerifItalic.sfd', 'Srf I' ) )
312 supportList.append( FontSupport( '../../sfd/FreeSerifBold.sfd', 'Srf B' ) )
313 supportList.append( FontSupport( '../../sfd/FreeSerifBoldItalic.sfd', 'Srf BI' ) )
314 supportList.append( FontSupport( '../../sfd/FreeSans.sfd', 'Sans' ) )
315 supportList.append( FontSupport( '../../sfd/FreeSansOblique.sfd', 'Sans O' ) )
316 supportList.append( FontSupport( '../../sfd/FreeSansBold.sfd', 'Sans B' ) )
317 supportList.append( FontSupport( '../../sfd/FreeSansBoldOblique.sfd', 'Sans BO' ) )
318 supportList.append( FontSupport( '../../sfd/FreeMono.sfd', 'Mono' ) )
319 supportList.append( FontSupport( '../../sfd/FreeMonoOblique.sfd', 'Mono O' ) )
320 supportList.append( FontSupport( '../../sfd/FreeMonoBold.sfd', 'Mono B' ) )
321 supportList.append( FontSupport( '../../sfd/FreeMonoBoldOblique.sfd', 'Mono BO' ) )
322
323 print_font_range_report( supportList )