forked from arpruss/simplestopwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpfont.py
executable file
·131 lines (110 loc) · 4.43 KB
/
dumpfont.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from fontTools.ttLib import TTFont
from fontTools.pens.basePen import decomposeQuadraticSegment
from sys import argv
charsToDump = "0123456789:\u2212"
charsToNarrow = ":"
narrowFraction = 0.8
maximizeDigitBounds = True
equalizeWidths = "0123456789"
fontName = "Roboto-Regular.ttf" if len(argv) < 2 else argv[1]
className = "SansDigitsColon" if len(argv) < 3 else argv[2]
ttf = TTFont(fontName)
glyphs = ttf.getGlyphSet()
map = ttf.getBestCmap()
def multiply(m1,m2):
return tuple( tuple(sum(m1[i][j]*m2[j][k] for j in range(len(m1[0]))) for k in range(len(m2[0]))) for i in range(len(m1)) )
def apply2d(m1,v):
if v is None:
return None
else:
return ( m1[0][0]*v[0] + m1[0][1]*v[1] + m1[0][2], m1[1][0]*v[0] + m1[1][1]*v[1] + m1[1][2] )
class MyPen(object):
def __init__(self, indent="", transformation=[[1,0,0],[0,-1,0],[0,0,1]]):
self.transformation = transformation
self.indent = indent
def shiftList(self, points):
return [apply2d(self.transformation,point) for point in points]
def shift(self, point):
return apply2d(self.transformation,point)
def moveTo(self, point):
print(self.indent+"path.moveTo(%gf,%gf);" % self.shift(point))
# def curveTo(self, *points):
# print(self.indent+"curveTo", self.shift(*points))
def qCurveTo(self, *points):
decomp = decomposeQuadraticSegment(points)
for pair in decomp:
shifted = self.shiftList(pair)
print(self.indent+"path.quadTo(%gf,%gf,%gf,%gf);" % tuple(shifted[0]+shifted[1]))
def lineTo(self, point):
print(self.indent+"path.lineTo(%gf,%gf);" % self.shift(point))
def closePath(self):
print(self.indent+"path.close();")
def endPath(self):
print(self.indent+"// endPath")
def addComponent(self, glyphName, t):
print(self.indent+"// addComponent", glyphName, t)
glyphs[glyphName].draw(MyPen(indent=self.indent,transformation=multiply(self.transformation,[ [t[0],t[2],t[4]],[t[1],t[3],t[5]],[0,0,1] ])))
class PointListPen(object):
def __init__(self, transformation=[[1,0,0],[0,-1,0],[0,0,1]], pointList=[]):
self.transformation = transformation
self.pointList = pointList
def update(self, point):
p = apply2d(self.transformation,point)
self.pointList.append(p)
def moveTo(self, point):
self.update(point)
# def curveTo(self, *points):
# print(self.indent+"curveTo", self.shift(*points))
def qCurveTo(self, *points):
decomp = decomposeQuadraticSegment(points)
for pair in decomp:
self.update(pair[1])
def lineTo(self, point):
self.update(point)
def closePath(self):
pass
def endPath(self):
pass
def addComponent(self, glyphName, t):
glyphs[glyphName].draw(PointListPen(pointList=self.pointList, transformation=multiply(self.transformation,[ [t[0],t[2],t[4]],[t[1],t[3],t[5]],[0,0,1] ])))
plPen = PointListPen()
glyphs["M"].draw(plPen)
glyphs["y"].draw(plPen)
minY = min(p[1] for p in plPen.pointList)
maxY = max(p[1] for p in plPen.pointList)
print("""package omegacentauri.mobi.simplestopwatch;
import android.graphics.Path;
public class %s extends MiniFont {
public %s() {
super(%s);
}
public void addFontData() {
defineFontSize(%gf);
""" % (className, className, "true" if maximizeDigitBounds else "false", maxY-minY))
def getGlyph(c):
try:
return glyphs[map[ord(c)]]
except:
if c == '\u2212':
return glyphs[map[ord('-')]]
else:
raise KeyError()
for c in charsToDump:
glyph = getGlyph(c)
print(" addCharacter((char)%d,%gf,%gf,new PathMaker() {" % (ord(c),glyph.width,glyph.lsb))
print(" @Override")
print(" public Path makePath() {")
print(" Path path = new Path();")
glyph.draw(MyPen(indent=" ", transformation=[[1,0,0],[0,-1,0],[0,0,1]]))
print(" return path;")
print(" }")
print(" });")
for c in charsToNarrow:
print(" tweakWidth((char)%d,%gf);" % (ord(c),getGlyph(c).width*narrowFraction))
if equalizeWidths:
maxW = max(getGlyph(c).width for c in equalizeWidths)
for c in equalizeWidths:
if getGlyph(c).width != maxW:
print(" tweakWidth((char)%d,%gf);" % (ord(c),maxW))
print(" }")
print("}")