-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_fonts.py
More file actions
199 lines (179 loc) · 6.31 KB
/
test_fonts.py
File metadata and controls
199 lines (179 loc) · 6.31 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
notice = """
Unit tests for fonts
for a Pure Python graphics library
that saves to a bitmap
-----------------------------------
| Copyright 2022 by Joel C. Alcarez |
| [[email protected]] |
|-----------------------------------|
| We make absolutely no warranty |
| of any kind, expressed or implied |
|-----------------------------------|
| The primary author and any |
| any subsequent code contributors |
| shall not be liable in any event |
| for incidental or consequential |
| damages in connection with, or |
| arising out from the use of this |
| code in current form or with any |
| modifications. |
|-----------------------------------|
| This graphics library outputs |
| to a bitmap file. |
-----------------------------------
"""
from array import array
from typing import Callable
import unittest
from os import path
from pythonbmp.BITMAPlib import(
loadBMP,
plotstring2file,
plotstringasdots2file,
plotreversedstring2file,
plotreversedstringasdots2file,
plotupsidedownstring2file,
plotupsidedownstringasdots2file,
plotitalicstringasdots2file,
plotitalicstring2file,
plotreverseditalicstring2file,
plotverticalitalicstring2file,
plotverticalstring2file,
plotverticalstringasdots2file,
plotverticalitalicstringasdots2file,
getX11colorname2RGBdict,
getcolorname2RGBdict,
font8x8,
font8x14
)
rootdir = path.dirname(__file__)
def teststrgen():
s = ""
for i in range(256):
if i != 9 and i != 13 and i != 10:
s += chr(i)
else:
s += " "
if (i + 1) % 16 == 0:
s += "\n"
return s
class TestFontfilefunc(unittest.TestCase):
testimgdir = '/assets/fonts/'
sourcedir = rootdir + testimgdir
outputdir = f'{rootdir}/test_output/'
c = getX11colorname2RGBdict()
c1 = getcolorname2RGBdict()
teststr = teststrgen()
rainbow = (c1['brightred'],
c1['brightorange'],
c1['brightyellow'],
c1['brightgreen'],
c1['cyan'],
c1['brightblue'],
c1['brightmagenta'])
def genname(self,
fn: Callable,
font: array,
color: int,
size: int,
pixspace: int,
spacebetweenchar: int,
backgroundcolor: int = 0,
bits: int = 24) -> str:
return f'8x{font[0]}x{size}px{pixspace}cs{spacebetweenchar}{bits}bit{fn.__name__}bc{backgroundcolor}c{color if type(color) == int else "multi"}'
def dofontfunc(self,
fn: Callable,
font: array,
color: int,
size: int,
pixspace: int,
spacebetweenchar: int,
backgroundcolor: int = 0,
bits: int = 24):
p = self._filepaths(f'{self.genname(fn, font, color, size, pixspace, spacebetweenchar, backgroundcolor, bits)}.bmp')
fn(p[0], self.teststr, size, pixspace, spacebetweenchar,color, font, backgroundcolor, bits)
self.filecmp(*p)
def _filepaths(self, filename: str) -> list[str, str]:
return (f'{self.outputdir}{filename}',
f'{self.sourcedir}{filename}')
def filecmp(self, filename1: str,
filename2: str):
bmp1 = loadBMP(filename1)
bmp2 = loadBMP(filename2)
self.assertIsNotNone(bmp1)
self.assertIsNotNone(bmp2)
self.assertEqual(bmp1, bmp2)
aqua = c['aqua']
darkolive = c['darkolivegreen1']
orange = c['orange']
green = c['green']
yellow = c['yellow']
testcases = (
#0
(plotstring2file, font8x8, aqua, 1, 0, 0),
#1
(plotstring2file, font8x8, 1, 3, 0, 0, 0, 1),
#2
(plotstring2file, font8x8, aqua, 3, 1, 0),
#3
(plotstring2file, font8x14, darkolive, 1, 0, 0),
#4
(plotstring2file, font8x14, green, 3, 0, 0),
#5
(plotstring2file, font8x14, darkolive, 3, 1, 0),
#6
(plotitalicstring2file, font8x8, rainbow, 4, 1, 0),
#7
(plotreversedstring2file, font8x8, aqua, 1, 0, 0),
#8
(plotupsidedownstring2file, font8x8, aqua, 1, 0, 0),
#9
(plotstringasdots2file, font8x8, rainbow, 4, 1, 0),
#10
(plotstringasdots2file, font8x14, darkolive, 4, 1, 0),
#11
(plotreversedstringasdots2file, font8x14, darkolive, 4, 1, 0),
#12
(plotupsidedownstringasdots2file, font8x14, green, 3, 0, 0),
#13
(plotitalicstringasdots2file, font8x14, darkolive, 4, 1, 0),
#14
(plotreverseditalicstring2file, font8x14, yellow, 1, 0, 0),
#15
(plotverticalstring2file, font8x8, orange, 1, 0, 0),
#16
(plotverticalstring2file, font8x8, orange, 3, 1, 0),
#17
(plotverticalitalicstring2file, font8x14, orange, 1, 0, 0),
#18
(plotverticalitalicstring2file, font8x8, yellow, 3, 1, 0),
#19
(plotverticalstringasdots2file, font8x14, orange, 4, 1, 0),
#20
(plotverticalitalicstringasdots2file, font8x8, orange, 4, 1, 2)
)
def test0(self): self.dofontfunc(*self.testcases[0])
def test1(self): self.dofontfunc(*self.testcases[1])
def test2(self): self.dofontfunc(*self.testcases[2])
def test3(self): self.dofontfunc(*self.testcases[3])
def test4(self): self.dofontfunc(*self.testcases[4])
def test5(self): self.dofontfunc(*self.testcases[5])
def test6(self): self.dofontfunc(*self.testcases[6])
def test7(self): self.dofontfunc(*self.testcases[7])
def test8(self): self.dofontfunc(*self.testcases[8])
def test9(self): self.dofontfunc(*self.testcases[9])
def test10(self): self.dofontfunc(*self.testcases[10])
def test11(self): self.dofontfunc(*self.testcases[11])
def test12(self): self.dofontfunc(*self.testcases[12])
def test13(self): self.dofontfunc(*self.testcases[13])
def test14(self): self.dofontfunc(*self.testcases[14])
def test15(self): self.dofontfunc(*self.testcases[15])
def test16(self): self.dofontfunc(*self.testcases[16])
def test17(self): self.dofontfunc(*self.testcases[17])
def test18(self): self.dofontfunc(*self.testcases[18])
def test19(self): self.dofontfunc(*self.testcases[19])
def test20(self): self.dofontfunc(*self.testcases[20])
if __name__ == "__main__":
print(notice)
print('Root directory is: ',rootdir)
unittest.main()