-
Notifications
You must be signed in to change notification settings - Fork 100
/
fontconverter
executable file
·115 lines (99 loc) · 3.02 KB
/
fontconverter
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
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $fontname = "Terminal11x16";
## https://github.com/moononournation/Arduino_GFX/blob/master/src/Arduino_GFX.cpp: baseline = yadvance * 2 / 3
## so we have to set yadvance = (baseline * 3 / 2).
## baseofs is baseline offset from bottom, i.e. baseline = height - baseofs
## so lets try... (for Terminal11x16, baseline is 14 pixel from top, height is 16)
my $baseofs = 4;
my @data;
# read font bitmap
while(<>) {
my @dt = map { $_=~/(0x..)/ ? (hex($1)) : () } split(/,/,$_);
push @data, @dt;
}
my $width = shift @data;
my $height = shift @data;
my $first = shift @data;
my $count = shift @data;
my $nbrows = int(($height+7)/8);
my $charofs = $width * $nbrows + 1;
## process all glyphs
my @bitmap;
my @glyph;
my $bitofs = 0;
#$count=2;
sub optimize_glyph {
my $w = shift;
my $h = shift;
my $pix = shift;
my $xofs = 0;
my $yofs = 0;
# shrink 2D pixel array if possible
while(@$pix) {
if ( @{$pix->[0]} == grep { $_ == 0 } @{$pix->[0]} ) { shift(@$pix); $yofs++; $h--; }
else { last; }
}
while(@$pix) {
if ( @{$pix->[-1]} == grep { $_ == 0 } @{$pix->[-1]} ) { pop(@$pix); $h--; }
else { last; }
}
# transform 2D pixel array into compact byte sequence
my @all;
for(@$pix) { push @all, @$_; }
#print("pix size: ".(@all)."\n");
my @res;
while(@all) {
my @b = splice(@all,0,8);
while(@b<8) { push @b,0; }
my $res = 0;
while(@b) { $res = ($res*2) + (shift(@b)?1:0); }
push @res,$res;
}
return ($w, $h, \@res, $xofs, $yofs);
}
for my $c ($first .. $first+$count-1) {
# Convert char $c
my @gly = splice(@data,0,$charofs);
my $glywidth = shift @gly;
my @pixmap;
# print("Converting $c ($width x $height => nbrows=$nbrows)");
for my $x (0..$glywidth-1) {
for my $y (0..$height-1) {
my $g = $gly[$x * $nbrows + int($y/8)];
$pixmap[$y]->[$x] = (($g >> ($y%8))&1) ? 1 : 0;
#print("$x, $y : $pixmap[$y]->[$x]\n");
}
}
# pixmap is now my glyph
my ($w, $h, $d, $xofs, $yofs) = optimize_glyph($glywidth, $height, \@pixmap);
my $glyph = {
"offset" => $bitofs,
"width" => $w,
"height" => $h,
"xadv" => $w+1,
"xofs" => $xofs,
"yofs" => -$height + $yofs + $baseofs,
"c" => $c,
};
push @bitmap, @$d;
$bitofs += @$d;
push @glyph, $glyph;
#print(Dumper($glyph,$d));
}
#print(Dumper(\@glyph));
printf("const uint8_t %sBitmap[] = {\n", $fontname);
while(my @bm = splice(@bitmap,0,12)) { print( " ".join(", ", map { sprintf("0x%02X",$_) } @bm ).",\n" ); }
print("};\n\n");
printf("const GFXglyph %sGlyphs[] = {\n", $fontname);
for my $gl (@glyph) {
printf(" { %5d, %3d, %3d, %3d, %4d, %4d }, // 0x%02x '%c'\n ", $gl->{"offset"}, $gl->{"width"}, $gl->{"height"},
$gl->{"xadv"}, $gl->{"xofs"}, $gl->{"yofs"}, $gl->{"c"}, $gl->{"c"});
}
print("};\n");
printf("const GFXfont %sFont = {\n", $fontname);
printf(" (uint8_t *)%sBitmap,\n", $fontname);
printf(" (GFXglyph *)%sGlyphs,\n", $fontname);
printf(" 0x%02X, 0x%02X, %d };\n", $first, $first+$count-1, ($height-$baseofs)*3/2);