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