-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHeeksFontLines.py
More file actions
125 lines (104 loc) · 4.64 KB
/
HeeksFontLines.py
File metadata and controls
125 lines (104 loc) · 4.64 KB
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
import geom
import cad
import os
def cchr(i):
c = chr(i)
if c == '\\': c = 'backslash'
return c
class Character:
def __init__(self, c):
self.c = c
self.pos = None
self.width = 0.0
self.line_strips = []
def AddSketch(self, s):
a = s.GetArea()
c = a.GetCurves()[0]
ls = LineStrip()
for v in c.GetVertices():
ls.pts.append(v.p)
self.line_strips.append(ls)
class LineStrip:
def __init__(self):
self.pts = []
def Transform(self, mat):
for p in self.pts:
p.Transform(mat)
CONVERT_LINES_MODE_OPENGL = 0
CONVERT_LINES_MODE_GFX_C = 1
def ConvertHeeksLines(mode = CONVERT_LINES_MODE_OPENGL):
this_dir = os.path.dirname(os.path.realpath(__file__))
chars = {}
chars[' '] = Character(' ')
for object in cad.GetObjects():
if object.GetType() == cad.OBJECT_TYPE_SKETCH:
title = object.GetTitle()
if len(title) == 2 and title[0] == '_':
box = object.GetBox()
chars[title[1]].pos = geom.Point(box.MinX(), box.MinY())
chars[title[1]].width = box.Width()
else:
c = title[0]
if c not in chars:
chars[c] = Character(c)
sketch = object
sketch.__class__ = cad.Sketch
chars[c].AddSketch(sketch)
# now write the file
if mode == CONVERT_LINES_MODE_OPENGL:
f = open(this_dir + '/CAD/HeeksFontLineData.h', 'w')
f.write('// HeeksFontLineData.h written automatically by HeeksFontLines.py\n\n')
f.write('double RenderCharLines(char c)\n{\n\tswitch(c){\n')
elif mode == CONVERT_LINES_MODE_GFX_C:
f = open(this_dir + '/GfxLinesFont.h', 'w')
f.write('// GfxLinesFont.h written automatically by HeeksFontLines.py\n\n')
f.write('uint8_t RenderLinesChar20(char c, uint16_t x, uint16_t y, uint8_t* color);\n')
f.write('void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint8_t* color);\n')
f.write('uint8_t GetLinesCharWidth20(char c);\n\n')
f.write('uint8_t RenderLinesChar20(char c, uint16_t x, uint16_t y, uint8_t* color)\n{\n\tswitch(c){\n')
char_height = 20
scale = 0.0133333
for i in range(32, 127):
c = chr(i)
if c in chars:
f.write('\t\tcase ' + str(i) + ':\n')
ch = chars[c]
if mode == CONVERT_LINES_MODE_OPENGL:
for line_strip in ch.line_strips:
f.write('\t\t\tglBegin(GL_LINE_STRIP);\n')
for pt in line_strip.pts:
f.write('\t\t\tglVertex2d(' + str((pt.x - ch.pos.x) * scale) + ', ' + str((pt.y - ch.pos.y) * scale) + ');\n')
f.write('\t\t\tglEnd();\n')
f.write('\t\t\treturn ' + str(ch.width * scale) + ';\n')
elif mode == CONVERT_LINES_MODE_GFX_C:
for line_strip in ch.line_strips:
prev_pt = None
for pt in line_strip.pts:
if prev_pt != None:
f.write('\t\t\tdrawLine(x + ' + str(int((prev_pt.x - ch.pos.x) * scale * char_height)) + ', y + ' + str(int(char_height - (prev_pt.y - ch.pos.y) * scale * char_height)) + ', x + ' + str(int((pt.x - ch.pos.x) * scale * char_height)) + ', y + ' + str(int(char_height - (pt.y - ch.pos.y) * scale * char_height)) + ', color);\n')
prev_pt = pt
f.write('\t\t\treturn ' + str(int(ch.width * scale * char_height)) + ';\n')
else:
print('missing char: ' + c)
f.write('\t\tdefault:\n\t\t\treturn 0.0;')
f.write('\t}\n}\n')
f.write('\n\n')
f.write('uint8_t GetLinesCharWidth(char c)\n{\n\tswitch(c){\n')
for i in range(32, 127):
c = chr(i)
if c in chars:
f.write('\t\tcase ' + str(i) + ':\n')
ch = chars[c]
if mode == CONVERT_LINES_MODE_OPENGL:
f.write('\t\t\treturn ' + str(ch.width * scale) + ';\n')
elif mode == CONVERT_LINES_MODE_GFX_C:
f.write('\t\t\treturn ' + str(int(ch.width * scale * char_height)) + ';\n')
else:
print('missing char: ' + c)
f.write('\t\tdefault:\n\t\t\treturn 0.0;')
f.write('\t}\n}\n')
f.close()
if mode == CONVERT_LINES_MODE_OPENGL:
print('successfully created HeeksFontLineData.h')
elif mode == CONVERT_LINES_MODE_GFX_C:
print('successfully created GfxLinesFont.h')