-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkana.py
58 lines (53 loc) · 1.7 KB
/
pkana.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from libotd.unicode import IsKana
from libotd.transform import Transform, ChangeAdvanceWidth
def GetLookupPalt(font):
palt = []
if 'GPOS' not in font:
return palt
gpos = font['GPOS']
lookups = gpos['lookups']
for l in lookups:
if 'palt' in l:
palt.append(lookups[l])
return palt
# multiple scalars may be mapped to same glyph
def GetUnicodeScalars(name, font):
result = []
for (u, n) in font['cmap'].items():
if n == name:
result.append(int(u))
return result
def ProportionalizeKana(font):
for palt in GetLookupPalt(font):
for sub in palt['subtables']:
for (n, d) in sub.items():
if any([ IsKana(ch) for ch in GetUnicodeScalars(n, font) ]):
glyph = font['glyf'][n]
if 'dx' in d:
Transform(glyph, 1, 0, 0, 1, d['dx'], 0)
if 'dWidth' in d:
ChangeAdvanceWidth(glyph, d['dWidth'])
def ApplyPalt(font):
for palt in GetLookupPalt(font):
for sub in palt['subtables']:
for (n, d) in sub.items():
glyph = font['glyf'][n]
if 'dx' in d:
Transform(glyph, 1, 0, 0, 1, d['dx'], 0)
if 'dWidth' in d:
ChangeAdvanceWidth(glyph, d['dWidth'])
def NowarApplyPaltMultiplied(font, multiplier):
for palt in GetLookupPalt(font):
for sub in palt['subtables']:
for (n, d) in sub.items():
glyph = font['glyf'][n]
if any([ IsKana(ch) for ch in GetUnicodeScalars(n, font) ]):
if 'dx' in d:
Transform(glyph, 1, 0, 0, 1, d['dx'], 0)
if 'dWidth' in d:
ChangeAdvanceWidth(glyph, d['dWidth'])
else:
if 'dx' in d:
Transform(glyph, 1, 0, 0, 1, d['dx'] * multiplier, 0, roundToInt = True)
if 'dWidth' in d:
ChangeAdvanceWidth(glyph, round(d['dWidth'] * multiplier))