source: mainline/contrib/font/bdf2c.pl@ 2fce275

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2fce275 was 2fce275, checked in by Martin Decky <martin@…>, 16 years ago

sync with sources

  • Property mode set to 100755
File size: 4.0 KB
Line 
1#!/usr/bin/perl -w
2
3#
4# Copyright (c) 2000 Dmitry Bolkhovityanov
5# Copyright (c) 2009 Martin Decky
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11#
12# - Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14# - Redistributions in binary form must reproduce the above copyright
15# notice, this list of conditions and the following disclaimer in the
16# documentation and/or other materials provided with the distribution.
17# - The name of the author may not be used to endorse or promote products
18# derived from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31
32use strict;
33
34my $skip;
35
36my $width;
37my $height;
38my $offset_x;
39my $offset_y;
40
41my $gwidth;
42my $gheight;
43my $goffset_x;
44my $goffset_y;
45
46my $index;
47my @glyphs;
48my @chars;
49
50open(BDF, "u_vga16.bdf") or die("Unable to open source font\n");
51
52READHEADER: while (<BDF>) {
53 /^FONTBOUNDINGBOX\s/ and do {
54 ($skip, $width, $height, $offset_x, $offset_y) = (split);
55
56 die("Font width is not 8px\n") if ($width != 8);
57 die("Font height is not 16px\n") if ($height != 16);
58 };
59 /^CHARS\s/ && last READHEADER;
60}
61
62READCHARS: while (<BDF>) {
63 /^ENCODING\s+([0-9]+)\s*$/ && do {
64 $index = $1;
65 };
66 /^BBX\s/ && do {
67 ($skip, $gwidth, $gheight, $goffset_x, $goffset_y) = (split);
68 };
69 /^BITMAP/ && do {
70 my @glyph = ();
71 my $y;
72
73 # Add empty lines at top
74 my $empties = $height + $offset_y - $goffset_y - $gheight;
75
76 for ($y = 0; $y < $empties; $y++) {
77 $glyph[$y] = 0;
78 }
79
80 # Scan the hex bitmap
81 for ($y = $empties; $y < $empties + $gheight; $y++) {
82 $_ = <BDF>;
83 $glyph[$y] = hex(substr($_, 0, 2)) >> $goffset_x;
84 }
85
86 # Add empty lines at bottom
87 my $fill = $height - $gheight - $empties;
88 for ($y = $empties + $gheight; $y < $empties + $gheight + $fill; $y++) {
89 $glyph[$y] = 0;
90 }
91
92 if ($index != 0) {
93 $glyphs[$index] = (\@glyph);
94 push(@chars, $index);
95 }
96 };
97 /^ENDFONT/ && last READCHARS;
98}
99
100close(BDF);
101
102@chars = sort { $a <=> $b } (@chars);
103
104print "#define FONT_GLYPHS " . (@chars + 1). "\n";
105print "#define FONT_SCANLINES " . $height . "\n";
106
107print "\n";
108print "uint16_t fb_font_glyph(const wchar_t ch)\n";
109print "{\n";
110print "\tif (ch == 0x0000)\n";
111print "\t\treturn 0;\n\n";
112
113my $pos = 0;
114my $start = -1;
115my $start_pos = 0;
116my $prev = 0;
117for $index (@chars) {
118 if ($prev + 1 < $index) {
119 if ($start != -1) {
120 if ($start == $prev) {
121 printf "\tif (ch == 0x%.4x)\n", $start;
122 print "\t\treturn " . $start_pos . ";\n";
123 } else {
124 printf "\tif ((ch >= 0x%.4x) && (ch <= 0x%.4x))\n", $start, $prev;
125 print "\t\treturn (ch - " . ($start - $start_pos) . ");\n";
126 }
127
128 print "\t\n";
129 }
130
131 $start = $index;
132 $start_pos = $pos;
133 }
134
135 $pos++;
136 $prev = $index;
137}
138
139print "\treturn " . @chars . ";\n";
140print "}\n";
141
142print "\n";
143print "uint8_t fb_font[FONT_GLYPHS][FONT_SCANLINES] = {";
144
145for $index (@chars) {
146 print "\n\t{";
147
148 my $y;
149 for ($y = 0; $y < $height; $y++) {
150 print ", " if ($y > 0);
151 printf "0x%.2x", $glyphs[$index]->[$y];
152 }
153
154 print "},";
155}
156
157print "\n\t\n\t/* Special glyph for unknown character */\n\t{";
158my $y;
159for ($y = 0; $y < $height; $y++) {
160 print ", " if ($y > 0);
161 printf "0x%.2x", $glyphs[63]->[$y];
162}
163
164print "}\n};\n";
Note: See TracBrowser for help on using the repository browser.