source: git/lib/make-pixel-font @ 614d60b

line_contentsproj-api-updatestereo
Last change on this file since 614d60b was 614d60b, checked in by Olly Betts <olly@…>, 9 years ago

lib/Makefile.am,lib/make-pixel-font,src/glbitmapfont.cc,
src/glbitmapfont.h: Embed the font data for the first 256 Unicode
codepoints into the compiled aven binary to reduce start up
overhead. Any additional codepoints are loaded from a data file
only if/when a character >= U+100 is actually needed (as before).

  • Property mode set to 100755
File size: 2.4 KB
Line 
1#!/usr/bin/perl
2use warnings;
3use strict;
4
5my %bitno = (0 => 0);
6for (0 .. 31) {
7    $bitno{1 << $_} = $_;
8}
9
10my $last_ch = -1;
11open U, '<', '/usr/share/unifont/unifont.hex' or die $!;
12open O, '>', 'unifont.pixelfont' or die $!;
13while (<U>) {
14    my ($ch, $hex) = /^([[:xdigit:]]+):([[:xdigit:]]+)$/;
15    if (!defined $ch) {
16        print "Bad line: $_";
17        next;
18    }
19    $ch = hex($ch);
20    if ($ch == 256) {
21        close O;
22        open O, '<', 'unifont.pixelfont' or die $!;
23        open P, '>', 'preload_font.h' or die $!;
24        print P "static const unsigned char fontdata_preloaded[] = {";
25        my $c = 0;
26        while (<O>) {
27            for (split //, $_) {
28                if ($c++ % 8 == 0) {
29                    print P "\n   ";
30                }
31                printf P "% 4d,", ord $_;
32            }
33        }
34        print P "\n};\n";
35        close P;
36        close O;
37        open O, '>', 'unifont.pixelfont' or die $!;
38    }
39
40    while (++$last_ch < $ch) {
41        # print "Missing entry for ".($last_ch - 1 )."\n";
42        print O pack 'C', 0x00;
43    }
44
45    # $len is 0, 2 or 4
46    my $len = length($hex) / 16;
47    my $packcode;
48    if ($len == 4) {
49        $packcode = 'n';
50    } elsif ($len == 2) {
51        $packcode = 'C';
52    } elsif ($len == 0) {
53        $packcode = '';
54    } else {
55        die "No handling for len = $len\n";
56    }
57    my $data = '';
58    my $start;
59    my $n = 0;
60    my $pixels_set = 0;
61    my $width = $len * 4;
62    if ($len) {
63        my @data = ();
64        for (0 .. 15) {
65            my $c = substr($hex, (15 - $_) * $len, $len);
66            my $row = hex($c);
67            $pixels_set |= $row;
68            if (!defined $start) {
69                # Skip blank rows at the bottom.
70                next unless $row;
71                $start = $_;
72            }
73            $n = $_ if $row;
74            push @data, $row;
75
76#       my $b = sprintf $fmt, $row;
77#       $b =~ y/01/ @/;
78#       print "\t$b\n";
79        }
80
81        my $left_gap = 0;
82        my $lsb_used = $pixels_set & -$pixels_set;
83        my $msb_used = 1 << ($len * 4 - 1);
84        while ($msb_used > $pixels_set) {
85            $msb_used >>= 1;
86            ++$left_gap;
87        }
88        $data = join '', map {pack($packcode, $_ << $left_gap)} @data;
89        my $right_gap = $bitno{$lsb_used} + $left_gap;
90        $right_gap = 0 if ($left_gap == 16);
91        $width -= $right_gap;
92    } else {
93        $width -= 2;
94    }
95
96    if (!defined $start) {
97        # No set pixels.
98        print O pack 'C', ($len * 4 - 4);
99        die "not really empty!" unless $hex =~ /^0*$/;
100    } else {
101        die "really empty!" if $hex =~ /^0*$/;
102        $n = $n + 1 - $start;
103        print O pack 'C', (($len / 2) << 6) | $width;
104        print O pack 'C', ($start << 4) | ($n - 1);
105        die "too little data (".length($data)." < ".$n."*".($len/2).")" if length($data) < $n * $len/2;
106        print O substr($data, 0, $n * $len / 2);
107    }
108}
109close O or die $!;
Note: See TracBrowser for help on using the repository browser.