source: git/lib/make-pixel-font @ a7d4233

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since a7d4233 was 614d60b, checked in by Olly Betts <olly@…>, 10 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
RevLine 
[1aa3fb7]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);
[614d60b]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    }
[1aa3fb7]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) {
[4a6569a]49        $packcode = 'n';
[1aa3fb7]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;
[d4d6909]60    my $pixels_set = 0;
61    my $width = $len * 4;
[1aa3fb7]62    if ($len) {
[d4d6909]63        my @data = ();
[1aa3fb7]64        for (0 .. 15) {
65            my $c = substr($hex, (15 - $_) * $len, $len);
66            my $row = hex($c);
[d4d6909]67            $pixels_set |= $row;
[1aa3fb7]68            if (!defined $start) {
69                # Skip blank rows at the bottom.
70                next unless $row;
71                $start = $_;
72            }
73            $n = $_ if $row;
[d4d6909]74            push @data, $row;
[1aa3fb7]75
76#       my $b = sprintf $fmt, $row;
77#       $b =~ y/01/ @/;
78#       print "\t$b\n";
79        }
[d4d6909]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;
[1aa3fb7]94    }
95
96    if (!defined $start) {
97        # No set pixels.
[d4d6909]98        print O pack 'C', ($len * 4 - 4);
[1aa3fb7]99        die "not really empty!" unless $hex =~ /^0*$/;
100    } else {
101        die "really empty!" if $hex =~ /^0*$/;
102        $n = $n + 1 - $start;
[d4d6909]103        print O pack 'C', (($len / 2) << 6) | $width;
[1aa3fb7]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.