| 1 |
#!/usr/bin/fontforge -script |
| 2 |
|
| 3 |
__author__ = "Stevan White <[email protected]>" |
| 4 |
|
| 5 |
import fontforge |
| 6 |
import sys |
| 7 |
from OpenType.UnicodeRanges import * |
| 8 |
|
| 9 |
def get_kern_subtables( font ): |
| 10 |
try: |
| 11 |
tables = [] |
| 12 |
for lookup in font.gpos_lookups: |
| 13 |
if font.getLookupInfo( lookup )[0] == 'gpos_pair': |
| 14 |
sts = font.getLookupSubtables( lookup ) |
| 15 |
for st in sts: |
| 16 |
if font.isKerningClass( st ): |
| 17 |
tables.append( st ) |
| 18 |
return tables |
| 19 |
except EnvironmentError, ( e ): |
| 20 |
print >> sys.stderr, 'EnvironmentError ' + str( e ) |
| 21 |
except TypeError, ( t ): |
| 22 |
print >> sys.stderr, 'TypeError ' + str( t ) |
| 23 |
return None |
| 24 |
preamble = """ |
| 25 |
<html> |
| 26 |
<head> |
| 27 |
<style type="text/css"> |
| 28 |
.nonexistent { background-color: red; } |
| 29 |
td { text-align: right; font-family: inherit; } |
| 30 |
.I td { font-style: italic; } |
| 31 |
.B td { font-weight: bold; } |
| 32 |
.BI td { font-weight: bold; font-style: italic; } |
| 33 |
td { line-height: 1; } |
| 34 |
.classes td { text-align: left; vertical-align: top; } |
| 35 |
td span { font-weight: normal; font-style: normal; font-size: smaller; color: lime; } |
| 36 |
td span.pos { color: magenta; } |
| 37 |
td.zero { color: gray; } |
| 38 |
</style> |
| 39 |
</head> |
| 40 |
<body> |
| 41 |
""" |
| 42 |
postamble=""" |
| 43 |
</body> |
| 44 |
</html> |
| 45 |
""" |
| 46 |
|
| 47 |
def print_kerns( fontPath ): |
| 48 |
font = fontforge.open( fontPath ) |
| 49 |
print '<h2>Kerning classes in ' + font.fontname + '</h2>' |
| 50 |
weight = '' |
| 51 |
if font.os2_weight > 500: |
| 52 |
weight = 'B' |
| 53 |
style = '' |
| 54 |
if font.italicangle < 0.0: |
| 55 |
style = 'I' |
| 56 |
print '<div style="font-family: ' + font.familyname + '" ' \ |
| 57 |
+ 'class="' + weight + style + '">' |
| 58 |
subtables = get_kern_subtables( font ) |
| 59 |
for st in subtables: |
| 60 |
print '<h3>Subtable ' + st + '</h3>' |
| 61 |
printKernsOfSubtable( font, st ) |
| 62 |
print '</div>' |
| 63 |
sys.stdout.flush() |
| 64 |
|
| 65 |
def printKernsOfSubtable( font, subtable ): |
| 66 |
kclass = font.getKerningClass( subtable ) |
| 67 |
n = 0 |
| 68 |
leftclasses = kclass[0] |
| 69 |
rightclasses = kclass[1] |
| 70 |
kerns = kclass[2] |
| 71 |
nr = len( rightclasses ) |
| 72 |
print '<table class="classes"><tr>' |
| 73 |
print '<th>left classes: </th>' |
| 74 |
print '<th>right classes: </th>' |
| 75 |
print '<tr><td>' |
| 76 |
for lc in leftclasses: |
| 77 |
if lc: |
| 78 |
for c in lc: |
| 79 |
printentity( font, c ) |
| 80 |
print "<br />" |
| 81 |
print "</td>" |
| 82 |
print "<td>" |
| 83 |
for rc in rightclasses: |
| 84 |
if rc: |
| 85 |
for c in rc: |
| 86 |
printentity( font, c ) |
| 87 |
print "<br />" |
| 88 |
print "</td>" |
| 89 |
print "</tr>" |
| 90 |
print "</table>" |
| 91 |
print "<table>" |
| 92 |
print "<tr>" |
| 93 |
print "<th></th>" |
| 94 |
for rc in rightclasses: |
| 95 |
if rc: |
| 96 |
sys.stdout.write( "<th>" ) |
| 97 |
printentity( font, rc[0] ) |
| 98 |
sys.stdout.write( "</th>" ) |
| 99 |
print "</tr>" |
| 100 |
for lc in leftclasses: |
| 101 |
m = 0 |
| 102 |
if lc: |
| 103 |
print "<tr>" |
| 104 |
sys.stdout.write( "<th>" ) |
| 105 |
printentity( font, lc[0] ) |
| 106 |
sys.stdout.write( "</th>" ) |
| 107 |
for rc in rightclasses: |
| 108 |
kern = kerns[ n * nr + m ] |
| 109 |
if rc: |
| 110 |
ccolor = '' |
| 111 |
ncolor = '' |
| 112 |
if kern > 0: |
| 113 |
ncolor = ' class="pos"' |
| 114 |
if kern == 0: |
| 115 |
ccolor = ' class="zero"' |
| 116 |
sys.stdout.write( '<td' + ccolor + '><span' + ncolor + '>' ) |
| 117 |
if kern == 0: |
| 118 |
sys.stdout.write( ' ' ) |
| 119 |
else: |
| 120 |
sys.stdout.write( str( kern ) ) |
| 121 |
sys.stdout.write( '</span><br />' ) |
| 122 |
printpair( font, lc[0], rc[0] ) |
| 123 |
sys.stdout.write( '</td>' ) |
| 124 |
m += 1 |
| 125 |
print "</tr>" |
| 126 |
n += 1 |
| 127 |
print "</table>" |
| 128 |
|
| 129 |
def printentity( font, a ): |
| 130 |
s = font.findEncodingSlot( a ) |
| 131 |
v = formatted_hex_value( s ) |
| 132 |
if s == -1: |
| 133 |
v = '<span class="nonexistent"> </span>' |
| 134 |
print >> sys.stderr, font.fullname, 'Missing glyph: ' + a |
| 135 |
elif not codepointIsInSomeRange( s ): |
| 136 |
print >> sys.stderr, font.fullname, 'Non-unicode: ' + v |
| 137 |
sys.stdout.write( v ) |
| 138 |
|
| 139 |
def printpair( font, p, q ): |
| 140 |
printentity( font, p ) |
| 141 |
printentity( font, q ) |
| 142 |
sys.stdout.write( ' ' ) |
| 143 |
|
| 144 |
def formatted_hex_value( n ): |
| 145 |
return '%s%0.4x%s' %( "&#x", n, ";" ) |
| 146 |
|
| 147 |
def printlist( lst ): |
| 148 |
s = '' |
| 149 |
delim = '' |
| 150 |
for m in lst: |
| 151 |
s += delim + m |
| 152 |
delim = ' ' |
| 153 |
print s |
| 154 |
|
| 155 |
print preamble |
| 156 |
#print_kerns( '/home/swhite/font_stuff/urwsr-ttf/URWPalladioL-Roman-test.sfd' ) |
| 157 |
print_kerns( '../../sfd/FreeSerif.sfd' ) |
| 158 |
print_kerns( '../../sfd/FreeSerifItalic.sfd' ) |
| 159 |
print_kerns( '../../sfd/FreeSerifBold.sfd' ) |
| 160 |
print_kerns( '../../sfd/FreeSerifBoldItalic.sfd' ) |
| 161 |
print_kerns( '../../sfd/FreeSans.sfd' ) |
| 162 |
print_kerns( '../../sfd/FreeSansOblique.sfd' ) |
| 163 |
print_kerns( '../../sfd/FreeSansBold.sfd' ) |
| 164 |
print_kerns( '../../sfd/FreeSansBoldOblique.sfd' ) |
| 165 |
print_kerns( '../../sfd/FreeMono.sfd' ) |
| 166 |
print_kerns( '../../sfd/FreeMonoOblique.sfd' ) |
| 167 |
print_kerns( '../../sfd/FreeMonoBold.sfd' ) |
| 168 |
print_kerns( '../../sfd/FreeMonoBoldOblique.sfd' ) |
| 169 |
print postamble |