| 1 |
#!/usr/bin/python |
| 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: 2020-09-05 23:45:28 +0200 (Sa, 05. Sep 2020) $" |
| 22 |
__version__ = "$Revision: 4234 $" |
| 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 exit, 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( "interval", r, "not representable in", |
| 67 |
font.fontname, file=stderr ) |
| 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( font.fontname, hex( r[0] ), hex( r[1] ),hex( r[2] ),hex( r[3] ) file=stderr ); |
| 100 |
|
| 101 |
nRanges = len( ulUnicodeRange ) |
| 102 |
|
| 103 |
# FIXME this is a really bad way to index the entries. |
| 104 |
# only works if indexes start at 0 |
| 105 |
for index in range( 0, nRanges ): |
| 106 |
byte = index // 32 |
| 107 |
bit = index % 32 |
| 108 |
|
| 109 |
self.collectRangeInfo( font, r[byte], bit, index ) |
| 110 |
|
| 111 |
for g in font.glyphs(): |
| 112 |
self.fontTotalGlyphs += 1 |
| 113 |
cp = g.encoding |
| 114 |
if ( not codepointIsInSomeRange( cp ) |
| 115 |
and not codepointIsSpecialTT( cp ) ): |
| 116 |
print( font.fontname, |
| 117 |
"no range for", hex( cp ), file=stderr ) |
| 118 |
|
| 119 |
""" '''Would like to check that special TT slots are |
| 120 |
present, but don't know how...''' |
| 121 |
for cp in special_TT_points: |
| 122 |
font.selection.all() |
| 123 |
if not cp in font.selection.byGlyphs: |
| 124 |
print >> stderr, font.fontname, \ |
| 125 |
"special TT glyph missing", hex( cp ) |
| 126 |
""" |
| 127 |
|
| 128 |
def collectRangeInfo( self, font, os2supportbyte, bit, index ): |
| 129 |
supports = ( os2supportbyte & (1 << bit) ) != 0 |
| 130 |
rangeName = ulUnicodeRange[index][1] |
| 131 |
intervals = ulUnicodeRange[index][2] |
| 132 |
nglyphs = count_glyphs_in_intervals( font, intervals ) |
| 133 |
self.setRangeSupport( index, supports, nglyphs ) |
| 134 |
self.totalGlyphs += nglyphs |
| 135 |
if index == 60 or index == 90: |
| 136 |
self.privateUseGlyphs += nglyphs |
| 137 |
|
| 138 |
def setRangeSupport( self, idx, supports, total ): |
| 139 |
if idx in self.myInfos: |
| 140 |
print( "OS/2 index", idx, "duplicated", file=stderr ) |
| 141 |
exit( 1 ) |
| 142 |
self.myInfos[idx] = SupportInfo( idx, supports, total ) |
| 143 |
|
| 144 |
def getInfo( self, idx ): |
| 145 |
if not idx in self.myInfos: |
| 146 |
print( "OS/2 index", idx, "not found", file=stderr ) |
| 147 |
exit( 1 ) |
| 148 |
return self.myInfos[ idx ] |
| 149 |
|
| 150 |
table_head = ''' |
| 151 |
<table class="fontrangereport"> |
| 152 |
<caption> |
| 153 |
OS/2 character ranges vs. font faces |
| 154 |
</caption> |
| 155 |
<colgroup> |
| 156 |
<col /><col /> |
| 157 |
</colgroup> |
| 158 |
<colgroup> |
| 159 |
<col class="roman"/><col /><col /><col /> |
| 160 |
<col /><col /><col /><col /> |
| 161 |
</colgroup> |
| 162 |
<colgroup> |
| 163 |
<col class="roman"/><col /><col /><col /> |
| 164 |
<col /><col /><col /><col /> |
| 165 |
</colgroup> |
| 166 |
<colgroup> |
| 167 |
<col class="roman"/><col /><col /><col /> |
| 168 |
<col /><col /><col /><col /> |
| 169 |
</colgroup> |
| 170 |
<thead> |
| 171 |
<tr><th>OS/2 character range</th> |
| 172 |
<th>range<br />total</th> |
| 173 |
%s |
| 174 |
</tr> |
| 175 |
</thead>''' |
| 176 |
|
| 177 |
def print_font_range_table( fontSupportList ): |
| 178 |
headings = '' |
| 179 |
for fsl in fontSupportList: |
| 180 |
headings += '<th colspan="2">' + fsl.short + '</th>' |
| 181 |
print( table_head % ( headings ) ) |
| 182 |
|
| 183 |
for r in ulUnicodeRange: |
| 184 |
idx = r[0] |
| 185 |
range_name = r[1] |
| 186 |
intervals = r[2] |
| 187 |
|
| 188 |
rowclass = ' class="low"' |
| 189 |
if idx > 122: |
| 190 |
rowclass = ' class="other"' |
| 191 |
else: |
| 192 |
if len( ulUnicodeRange[idx] ) > 3 and ulUnicodeRange[ idx ][3]: |
| 193 |
rowclass = ' class="high"' |
| 194 |
if idx == 60 or idx == 90: |
| 195 |
rowclass = ' class="private"' |
| 196 |
|
| 197 |
print( '<tr%s><td>%s</td>' % ( rowclass, range_name ) ) |
| 198 |
print( '<td>%i</td>' % ( |
| 199 |
total_intervals( intervals ) ) ) |
| 200 |
for fsl in fontSupportList: |
| 201 |
supportInfo = fsl.getInfo( idx ) |
| 202 |
supportString = '' |
| 203 |
if supportInfo.supports: |
| 204 |
supportString = '•' |
| 205 |
totalStr = ' ' |
| 206 |
if supportInfo.total: |
| 207 |
totalStr = str( supportInfo.total ) |
| 208 |
|
| 209 |
print( '<td>%s</td><td>%s</td>' % ( |
| 210 |
totalStr, supportString ) ) |
| 211 |
|
| 212 |
print( '</tr>' ) |
| 213 |
print( '<tr><th colspan="2">total in Unicode ranges</th>' ) |
| 214 |
for fsl in fontSupportList: |
| 215 |
print( '<td colspan="2">%i </td>' % ( |
| 216 |
fsl.totalGlyphs ) ) |
| 217 |
print( '</tr>' ) |
| 218 |
print( '<tr><th colspan="2">total in font</th>' ) |
| 219 |
for fsl in fontSupportList: |
| 220 |
print( '<td colspan="2">%i </td>' % ( |
| 221 |
fsl.fontTotalGlyphs ) ) |
| 222 |
print( '</tr>' ) |
| 223 |
print( '<tr><th colspan="2">total in Private Use</th>' ) |
| 224 |
for fsl in fontSupportList: |
| 225 |
print( '<td colspan="2">%i </td>' % ( |
| 226 |
fsl.privateUseGlyphs ) ) |
| 227 |
print( '</tr>' ) |
| 228 |
# Would also like to total glyphs in ranges for each font, |
| 229 |
# and also print total glyphs in each font. |
| 230 |
print( '</table>' ) |
| 231 |
|
| 232 |
table_introduction = """ |
| 233 |
For historical reasons, TrueType classifies Unicode ranges according to |
| 234 |
an extension of the old OS/2 character ranges. This table shows how many |
| 235 |
characters FontForge finds in each of the ranges for each face in the family. |
| 236 |
Notice that many modern Unicode ranges do not correspond to any of the OS/2 |
| 237 |
ranges. |
| 238 |
""" |
| 239 |
|
| 240 |
table_explanation = """ |
| 241 |
<p> |
| 242 |
Ranges for which (FontForge reports that) the font's OS/2 support |
| 243 |
bit is set are marked with a bullet. |
| 244 |
</p> |
| 245 |
<p> |
| 246 |
For many ranges, I took the liberty of reducing the set of characters |
| 247 |
considered to those listed for the range in the current Unicode charts. |
| 248 |
The number of characters supported can thus be less than the width of the range. |
| 249 |
</p> |
| 250 |
<p> |
| 251 |
The totals include glyphs in the Private Use areas (where there |
| 252 |
are many ligatures, alternative forms, and glyph components). The glyphs |
| 253 |
in these areas do not correspond to Unicode characters. |
| 254 |
</p> |
| 255 |
<p> |
| 256 |
Three control characters are inserted at 0x00, 0x01 and 0x0d to satisfy the |
| 257 |
TrueType recommendations. These are counted under Basic Latin, but other |
| 258 |
control characters there are not counted. |
| 259 |
</p> |
| 260 |
<p> |
| 261 |
Note that there is a discrepancy in the Greek Symbols, Hebrew Extended and |
| 262 |
Arabic Extended ranges, between what FontForge reports here and in its Font |
| 263 |
Info window under OS/2 Character Ranges. I don't know why, but these ranges |
| 264 |
are also not well defined in the TrueType standard. |
| 265 |
</p> |
| 266 |
<p> |
| 267 |
Note the two characters from Devanagri. These are the danda and double-danda |
| 268 |
used by other Indic scripts. |
| 269 |
</p> |
| 270 |
<p> |
| 271 |
The ranges <span style="color: #555">beyond Unicode point 0xFFFF</span>, are |
| 272 |
shaded. </p> |
| 273 |
""" |
| 274 |
|
| 275 |
html_heading = '''<!doctype html> |
| 276 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> |
| 277 |
<head> |
| 278 |
<meta charset="UTF-8" /> |
| 279 |
<title> |
| 280 |
GNU FreeFont character range support |
| 281 |
</title> |
| 282 |
<style type="text/css"> |
| 283 |
table { padding: 0; border: thin black solid; |
| 284 |
border-collapse: collapse; } |
| 285 |
th, td { padding: 0; border: thin black solid; } |
| 286 |
thead { border-bottom: medium black solid; } |
| 287 |
tr.high { color: #555 } |
| 288 |
tr.private { background-color: silver; } |
| 289 |
tr.other { background-color: #CCF; } |
| 290 |
td { text-align: right; } |
| 291 |
td:first-child { text-align: left; } |
| 292 |
td { padding-right: 0.25ex } |
| 293 |
th { padding: 0.25ex } |
| 294 |
.roman { border-left: medium black solid; } |
| 295 |
caption { font-size: larger; font-weight: bold; } |
| 296 |
</style> |
| 297 |
</head> |
| 298 |
''' |
| 299 |
|
| 300 |
def print_font_range_report( fontSupportList ): |
| 301 |
print( html_heading ) |
| 302 |
|
| 303 |
print( '<body>' ) |
| 304 |
print( '<h1>' ) |
| 305 |
print( 'GNU FreeFont support for OpenType OS/2 character ranges' ) |
| 306 |
print( '</h1>' ) |
| 307 |
print( '<p>' ) |
| 308 |
print( table_introduction ) |
| 309 |
print( '</p>' ) |
| 310 |
print_font_range_table( fontSupportList ) |
| 311 |
print( '<p>' ) |
| 312 |
print( table_explanation ) |
| 313 |
tzset() |
| 314 |
print( '<p>' ) |
| 315 |
print( 'Generated by <code>range_report.py</code> on %s.' % ( |
| 316 |
strftime('%X %x %Z') ) ) |
| 317 |
print( '</p>' ) |
| 318 |
print( '</body>' ) |
| 319 |
|
| 320 |
print( '</html>' ) |
| 321 |
|
| 322 |
supportList = [] |
| 323 |
supportList.append( FontSupport( '../../sfd/FreeSerif.sfd', 'Srf' ) ) |
| 324 |
supportList.append( FontSupport( '../../sfd/FreeSerifItalic.sfd', 'Srf I' ) ) |
| 325 |
supportList.append( FontSupport( '../../sfd/FreeSerifBold.sfd', 'Srf B' ) ) |
| 326 |
supportList.append( FontSupport( '../../sfd/FreeSerifBoldItalic.sfd', 'Srf BI' ) ) |
| 327 |
supportList.append( FontSupport( '../../sfd/FreeSans.sfd', 'Sans' ) ) |
| 328 |
supportList.append( FontSupport( '../../sfd/FreeSansOblique.sfd', 'Sans O' ) ) |
| 329 |
supportList.append( FontSupport( '../../sfd/FreeSansBold.sfd', 'Sans B' ) ) |
| 330 |
supportList.append( FontSupport( '../../sfd/FreeSansBoldOblique.sfd', 'Sans BO' ) ) |
| 331 |
supportList.append( FontSupport( '../../sfd/FreeMono.sfd', 'Mono' ) ) |
| 332 |
supportList.append( FontSupport( '../../sfd/FreeMonoOblique.sfd', 'Mono O' ) ) |
| 333 |
supportList.append( FontSupport( '../../sfd/FreeMonoBold.sfd', 'Mono B' ) ) |
| 334 |
supportList.append( FontSupport( '../../sfd/FreeMonoBoldOblique.sfd', 'Mono BO' ) ) |
| 335 |
|
| 336 |
print_font_range_report( supportList ) |