forked from masterzorag/xbm_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenXBMfonts.sh
executable file
·162 lines (118 loc) · 4.05 KB
/
genXBMfonts.sh
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
#!/bin/bash
# file: genXBMfonts.sh
# brief: generate font glyphs and C header file from XBM images
# author: 2015, [email protected]
# This script uses ImageMagick convert to generate images of glyphs
# 1. rebuild $fontDestDir/xbm_font.h
# ./genXBMfonts.sh Razor_1911/ 16 16
# 2. use generated XBM fonts: hardcode in xbm_print
# cp Razor_1911/xbm/xbm_font.h xbm_font.h
# 3. rebuild xbm_tools
# make clean && make
if [ -z $1 ]; then
echo "$0 FontDir Font_W Font_H"
echo "$0 Razor_1911 16 16"
exit
fi
### arguments, keep 16x16 as default
FontDir=$1
Font_W=$2
Font_H=$3
### process argv end here ###
fonts="$fonts $(echo "$FontDir"/*.ttf)"
# ImageMagick supported extension: pnm, png, bmp, xpm, pbm... here we deal with xbm
type=xbm
echo "Found" $(echo "$fonts" | wc -w) "fonts"
# for each font
for i in $fonts
do
echo "$i"
fontDestDir="$FontDir/$type"
mkdir -p "$fontDestDir"
t=$fontDestDir/temp
out=$fontDestDir/xbm_font.h
echo "$t, $out"
if [ -f "$t" ]
then
rm $t
rm $out
fi
# keep track of array index
n="0"
# chars=" ! a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + - / # . , \*"
# for c in $chars
# Better: do a printable range set, start from char ' ' (space): ASCII 32
# ASCII 127 is NOT printable and will write output.h file in binary form!
# keep it under 127 for now...
for c in `seq 32 126`
do
#printf "%d\t\x%x\n" $c $c
# compose decimal representation and label
d="$(printf %.3d $c)" #printf "\x$(printf %x $c)"
D="$(printf "\x$(printf %x $c)")"
# 1. build commented label header: array idx, ascii code, glyph
echo "{ /* $n: ASCII $d [$D] bits */" >> $t
# Generate (Font_W x Font_H) images, 1bpp of each character in ASCII range
# call imagemagick tool to convert bitmap
convert \
+antialias \
-depth 1 -colors 2 \
-size "$Font_W"x"$Font_H" \
-background white -fill black \
-font "$i" \
-pointsize 17 \
-density 72 \
-gravity center \
label:$D \
"$fontDestDir/$d.$type" &> /dev/null
# 2. check to build data bits
if [ -f "$fontDestDir/$d.$type" ]
then
echo "$n: $d.$type [$D]"
# extra: dump single XBM to console
./xbm_dump "$fontDestDir/$d.$type"
# 3a. strip data bits and push to output
tail -n+4 "$fontDestDir/$d.$type" | head --bytes -5 >> $t
else
echo "$n: $d.$type does not exists!"
# 3b. build zeroed data bit header for a missing ASCII code
printf "\t0x00" >> $t
fi
# 4. close data bits footer
printf "\n},\n" >> $t
# increase array count
((n+=1))
done
printf "I: range of %d ASCII codes\n" $n
# 1. build top C header
printf "#ifndef __XBM_FONT_H__\n" > $out
printf "#define __XBM_FONT_H__\n" >> $out
printf "\n/*\n\t%s bits\n" $fonts >> $out
printf "\tgenerated with genXBMfonts, https://github.com/masterzorag/xbm_tools\n" >> $out
printf "\t2015, [email protected]\n*/\n\n" >> $out
printf "#define LOWER_ASCII_CODE %d\n" 32 >> $out
printf "#define UPPER_ASCII_CODE %d\n" 126 >> $out
printf "#define FONT_W %d\n" $Font_W >> $out
printf "#define FONT_H %d\n" $Font_H >> $out
printf "#define BITS_IN_BYTE %d\n\n" 8 >> $out
echo "char xbmFont[$n][(FONT_W * FONT_H) / BITS_IN_BYTE] = {" >> $out
# 2. fix: remove last ','
head --bytes -2 $t >> $out
# 3. build bottom C header: add "};"
printf "\n};\n" >> $out
printf "#endif //__XBM_FONT_H__\n" >> $out
# extra: cleanup from temp
rm $t
done # for i in fonts
# count and report exported
n=$(ls $fontDestDir/*.$type | wc -l)
printf "I: succesfully parsed %d ASCII codes\nDone\n\n" $n
# inquiry
file $out
# preview outputted C header
head -14 $out
echo "..."
tail -6 $out
# extra: look at exported XBM(s)
viewnior $fontDestDir &> /dev/null
exit