Skip to content

Commit 2303efe

Browse files
committed
✨ generate .h headers from .ttf font files
1 parent d077f5b commit 2303efe

File tree

12 files changed

+88
-1064
lines changed

12 files changed

+88
-1064
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.vscode/
22

3-
# Executables
3+
# executable
44
*.exe
55
*.out
66

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ git clone --recurse-submodules --remote-submodule [email protected]:userElaina/Bad-
5050

5151
How to build OpenCV with MinGW-W64
5252

53+
Test
54+
5355
### License
5456

5557
The code is available under the [MIT license](./LICENSE).
@@ -72,9 +74,11 @@ The code is available under the [MIT license](./LICENSE).
7274
pip install Pillow
7375
```
7476

75-
##### Compile and Run
77+
##### Interpret and Run
7678

77-
Usually, as long as you can compile the CPP.
79+
```sh
80+
python -u "font.py"
81+
```
7882

7983
#### Windows-FFmpeg
8084

base.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <ctime>
66

77
#include "winux.hpp"
8-
#include "consola_0_0ff.h"
8+
#include "font/consola_ascii_0_ff.h"
99

1010
#define LL long long
1111
#define B unsigned char
@@ -45,7 +45,7 @@ class Font {
4545
}
4646
inline void init() {
4747
for (auto i = 0; i < MAXCOL; i++) {
48-
std::memcpy(o[i], default_map[i], sizeof(default_map[i]));
48+
std::memcpy(o[i], map65536[i], sizeof(map65536[i]));
4949
}
5050
}
5151
inline void init(const char *s) {
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
char default_map[0x100][0x101]={
3+
char map65536[0x100][0x101]={
44
" ........................-------------------,,,,,,,,,,,,,,,,_______________________________________________________________uuuuuuuuuuuuuuuaaaaaaayyyyyyyyyywwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwgggggggggggggggggggggg",
55
" ........................-------------------,,,,,,,,,,,,,,,,______________________________________________________________uuuuuuuuuuuuuuuuaaaaaaayyyyyyyyyywwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwgggggggggggggggggggggg",
66
" ........................-------------------,,,,,,,,,,,,,,,,______________________________++++++++++++++++++++++++++++++++uuuuuuuuuuuuuuuuaaaaaaayyyyyyyyyywwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwgggggggggggggggggggggg",
@@ -257,4 +257,4 @@ char default_map[0x100][0x101]={
257257
"*********************ffffffffffffffffffffffffffffffffffffffffffffffffffffffffMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM00000000000000000000000000000&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
258258
"*********************ffffffffffffffffffffffffffffffffffffffffffffffffffffffffMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM00000000000000000000000000000&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
259259
"*********************ffffffffffffffffffffffffffffffffffffffffffffffffffffffffMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM00000000000000000000000000000&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
260-
};
260+
};

font/font.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import os
2+
import string
3+
from PIL import Image, ImageDraw, ImageFont, ImageStat
4+
5+
# config consola
6+
dpi = 128
7+
x_ascii = 15
8+
y_ascii = 32
9+
fout_size = 26
10+
offset = 5
11+
f_font = './consola.ttf'
12+
char_type = 'ascii'
13+
# cmd: offset=5
14+
15+
# preprocess
16+
x_ascii *= dpi
17+
y_ascii *= dpi >> 1
18+
fout_size *= dpi
19+
offset *= dpi
20+
fnt = ImageFont.truetype(f_font, fout_size)
21+
22+
list_ascii = sorted(string.printable[:-5])
23+
24+
if char_type == 'ascii':
25+
None
26+
else:
27+
raise Exception('Unsupported char type: %s' % char_type)
28+
29+
ans_ascii = dict()
30+
max_ascii = 0
31+
min_ascii = 255
32+
for c in list_ascii:
33+
i = Image.new('L', (x_ascii, y_ascii))
34+
d = ImageDraw.Draw(i)
35+
d.text((0, offset), c, font=fnt, fill=255)
36+
l = ImageStat.Stat(i).mean[0]
37+
max_ascii = max(max_ascii, l)
38+
min_ascii = min(min_ascii, l)
39+
40+
i = Image.new('L', (x_ascii, y_ascii))
41+
d = ImageDraw.Draw(i)
42+
d.text((0, offset - y_ascii), c, font=fnt, fill=255)
43+
r = ImageStat.Stat(i).mean[0]
44+
max_ascii = max(max_ascii, r)
45+
min_ascii = min(min_ascii, r)
46+
47+
ans_ascii[c] = (l, r)
48+
print(c, l, r, flush=True)
49+
50+
print()
51+
52+
lc_ascii = max_ascii-min_ascii
53+
for k in ans_ascii:
54+
ans_ascii[k] = ((ans_ascii[k][0]-min_ascii)/lc_ascii*255,
55+
(ans_ascii[k][1]-min_ascii)/lc_ascii*255)
56+
57+
ans_65536 = list()
58+
for i in range(256):
59+
ans_256 = ''
60+
for j in range(256):
61+
ans_ = ''
62+
d = 512
63+
for k in ans_ascii:
64+
_d = abs(ans_ascii[k][0] - i) + abs(ans_ascii[k][1] - j)
65+
if _d < d:
66+
ans_ = k
67+
d = _d
68+
ans_256 += ans_
69+
ans_65536.append(ans_256)
70+
71+
s = '#pragma once\n\nchar map65536[0x100][0x101]={'
72+
for i in ans_65536:
73+
s += '\n "'+i.replace('\\', '\\\\').replace('"', '\\"')+'",'
74+
s = s[:-1]+'\n};\n'
75+
76+
open(os.path.join(os.path.dirname(os.path.abspath(f_font)), os.path.basename(
77+
f_font).rsplit('.', 1)[0]+'_'+char_type+'_0_ff.h'), 'w').write(s)

0 commit comments

Comments
 (0)