forked from lvgl/lv_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex2lvfont.rb
More file actions
267 lines (229 loc) · 7.6 KB
/
hex2lvfont.rb
File metadata and controls
267 lines (229 loc) · 7.6 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# coding: utf-8
# USAGE EXAMPLE:
# --------------
#
# ruby hex2lvfont.rb unscii-16.hex -r basic -h 16 -n lv_font_unscii_16
#
#
require 'optparse'
PROGNAME = $0.split('/')[-1]
RANGE_ALIAS = {
'basic' => [ 0x0020, 0x007e ],
'latin_sup' => [ 0x00a0, 0x00ff ],
'cyrillic' => [ 0x0410, 0x044f ],
'all' => [ 0x0000, 0xffff ],
}
#
# Command line parsing
#
$opts = {
:range => 0x0020 .. 0x007e,
:set => nil,
:height => nil,
:name => nil,
:bpp => 1,
}
OptionParser.new do |opts|
opts.banner = "Usage: #{PROGNAME} [options] font.hex"
opts.on("-r", "--range [RANGE]", "Range selection") do |r|
if r = case r
when /^(\d+)\.\.(\d+)$/ then [ $1.to_i, $2.to_i ]
when /^(.)-(.)$/ then [ $1, $2 ]
else RANGE_ALIAS[r]
end
f, l = r.sort
$opts[:range ] = f .. l
else
warn "Invalid range provided, can be:"
warn " - character range: a-z"
warn " - integer range : 32..127"
warn " - alias : basic, latin_sup, cyrillic"
exit
end
end
opts.on("-s", "--set [STRING]", "Set selection") do |s|
$opts[:set] = s.split('')
end
opts.on("-b", "--bpp [1,2,4,8]", Integer, "Bit-pet-pixel") do |b|
if [ 1, 2, 4, 8 ].include?(b)
$opts[:bpp] = b
else
warn "Only BPP of 1, 2, 4 or 8 are supported"
exit
end
end
opts.on("-n", "--name [STRING]", "Font name") do |n|
$opts[:name] = n
end
opts.on("-H", "--height [INTEGER]", Integer, "Font height") do |h|
$opts[:height] = h
end
end.parse!
$file = ARGV[0]
if $opts[:name].nil?
$opts[:name] = $file.split('/')[-1]
.sub(/\.[^.]+/, '')
.gsub(/[^a-z0-9_]/, '_')
end
if $opts[:height].nil?
warn "Original font height need to be specified"
exit
end
if $opts[:bpp] != 1
warn "For now only 1 BPP is supported"
exit
end
#
# FontConverter
#
class FontConverter
def self.unicode(c)
"Unicode: U+%04X (%s)" % [
c, c > 32 ? c.chr(Encoding::UTF_8) : '[]'
]
end
def read(fontfile, height)
regexp = /^(?<code>[0-9a-f]+):(?<data>[0-9a-f]+)$/i
@height = height
@glyphs = Hash[File.foreach(fontfile).with_index.map {|line, idx|
unless m = regexp.match(line)
warn "Parsing error line #{idx} (skipping glyph)"
next
end
[ m[:code].to_i(16), [ m[:data] ].pack('H*') ]
}]
@list = @glyphs.keys
end
def filter(range: nil, set: nil)
return if range.nil? && set.nil?
@list = []
@list += set.to_a if set
@list += range.to_a if range
@list = @list.map {|i| case i
when String then i.ord
when Integer then i
end }
.compact.sort.uniq
@list.reject! {|code| code < 32 } # Reject control characters
@list &= @glyphs.keys # Ensure that glyphs exist
end
def build(bpp = [ 1 ])
lists = @list.slice_when {|a,b| (a+1) != b }
.map {|list| list.first .. list.last }
@sparse = lists.size > 1
if @sparse
warn "Glyph selection result in a sparse font"
warn " => increasing size by #{4 * (@list.size+1)} bytes"
end
@bpp = bpp
@c_data = {}
bpp.each {|b|
index = 0;
@c_data[b] = d = {}
@list.each {|code|
data = @glyphs[code]
bitstring = data.unpack1("B*")
width = bitstring.size / @height
text = bitstring.tr('01', '.#').scan(/.{#{width}}/)
bytestring = data.unpack("C*")
data = bytestring.each_slice(width/8).map {|row|
row.map {|b| "0x%02x" % b }.join(', ') }
d[code] = {
:width => width,
:text => text,
:data => data,
:index => index,
}
index += bytestring.size
}
}
end
def header(name)
puts "#include <lvgl/lv_misc/lv_font.h>"
puts ""
puts "#if USE_#{name.upcase} != 0 /* Can be enabled in lv_conf.h */"
end
def footer(name)
puts ""
puts "#endif"
end
def output(name)
puts ""
puts "/* Store the image of the letters (glyph) */"
puts "static const uint8_t #{name}_glyph_bitmap[] = "
puts "{"
@bpp.each {|b|
puts "#if USE_#{name.upcase} == #{b}"
@c_data[b].each {| code, width:, text:, data:, index: |
unicode = self.class::unicode(code)
puts " /* #{unicode}, Width: #{width} */"
0.upto(@height-1) {|i|
puts " %s, // %s" % [ data[i], text[i] ]
}
puts
puts
}
puts "#endif"
}
puts "};"
# Glyph description
puts ""
puts "/* Store the glyph descriptions */"
puts "static const lv_font_glyph_dsc_t #{name}_glyph_dsc[] = "
puts "{"
@bpp.each {|b|
puts "#if USE_#{name.upcase} == #{b}"
@c_data[b].each {| code, width:, index:, ** |
unicode = self.class::unicode(code)
puts " { .w_px = %2d, .glyph_index = %4d }, /* %s */" % [
width, index, unicode ]
}
puts "#endif"
}
puts "};"
if @sparse
# Unicode mapping
puts ""
puts "/* List of unicode characters */"
puts "static const uint32_t #{name}_unicode_list[] = {"
@list.each {|code|
unicode = self.class::unicode(code)
puts " %4d, /* %s */" % [ code, unicode ]
}
puts " 0, /* End indicator */"
puts "};"
unicode_list = "#{name}_unicode_list"
get_bitmap = "lv_font_get_bitmap_sparse"
get_width = "lv_font_get_width_sparse"
else
unicode_list = "NULL"
get_bitmap = "lv_font_get_bitmap_continuous"
get_width = "lv_font_get_width_continuous"
end
puts ""
puts "lv_font_t #{name} = "
puts "{"
puts " .unicode_first = #{@list.first},"
puts " .unicode_last = #{@list.last},"
puts " .h_px = #{@height},"
puts " .glyph_bitmap = #{name}_glyph_bitmap,"
puts " .glyph_dsc = #{name}_glyph_dsc,"
puts " .unicode_list = #{unicode_list},"
puts " .get_bitmap = #{get_bitmap},"
puts " .get_width = #{get_width},"
@bpp.each {|b|
puts "#if USE_#{name.upcase} == #{b}"
puts " .bpp = #{b},"
puts "#endif"
}
puts " .next_page = NULL,"
puts "};"
end
end
$fc = FontConverter.new
$fc.read($file, $opts[:height])
$fc.filter(range: $opts[:range], set: $opts[:set])
$fc.header($opts[:name])
$fc.build([1])
$fc.output($opts[:name])
$fc.footer($opts[:name])