| 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 |  | 
|---|
| 32 | use strict; | 
|---|
| 33 |  | 
|---|
| 34 | my $skip; | 
|---|
| 35 |  | 
|---|
| 36 | my $width; | 
|---|
| 37 | my $height; | 
|---|
| 38 | my $offset_x; | 
|---|
| 39 | my $offset_y; | 
|---|
| 40 |  | 
|---|
| 41 | my $gwidth; | 
|---|
| 42 | my $gheight; | 
|---|
| 43 | my $goffset_x; | 
|---|
| 44 | my $goffset_y; | 
|---|
| 45 |  | 
|---|
| 46 | my $index; | 
|---|
| 47 | my @glyphs; | 
|---|
| 48 | my @chars; | 
|---|
| 49 |  | 
|---|
| 50 | open(BDF, "u_vga16.bdf") or die("Unable to open source font\n"); | 
|---|
| 51 |  | 
|---|
| 52 | READHEADER: 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 |  | 
|---|
| 62 | READCHARS: 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 |  | 
|---|
| 100 | close(BDF); | 
|---|
| 101 |  | 
|---|
| 102 | @chars = sort { $a <=> $b } (@chars); | 
|---|
| 103 |  | 
|---|
| 104 | print "#define FONT_GLYPHS     " . (@chars + 1). "\n"; | 
|---|
| 105 | print "#define FONT_SCANLINES  " . $height . "\n"; | 
|---|
| 106 |  | 
|---|
| 107 | print "\n"; | 
|---|
| 108 | print "uint16_t fb_font_glyph(const wchar_t ch)\n"; | 
|---|
| 109 | print "{\n"; | 
|---|
| 110 | print "\tif (ch == 0x0000)\n"; | 
|---|
| 111 | print "\t\treturn 0;\n\n"; | 
|---|
| 112 |  | 
|---|
| 113 | my $pos = 0; | 
|---|
| 114 | my $start = -1; | 
|---|
| 115 | my $start_pos = 0; | 
|---|
| 116 | my $prev = 0; | 
|---|
| 117 | for $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 |  | 
|---|
| 139 | print "\treturn " . @chars . ";\n"; | 
|---|
| 140 | print "}\n"; | 
|---|
| 141 |  | 
|---|
| 142 | print "\n"; | 
|---|
| 143 | print "uint8_t fb_font[FONT_GLYPHS][FONT_SCANLINES] = {"; | 
|---|
| 144 |  | 
|---|
| 145 | for $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 |  | 
|---|
| 157 | print "\n\t\n\t/* Special glyph for unknown character */\n\t{"; | 
|---|
| 158 | my $y; | 
|---|
| 159 | for ($y = 0; $y < $height; $y++) { | 
|---|
| 160 | print ", " if ($y > 0); | 
|---|
| 161 | printf "0x%.2x", $glyphs[63]->[$y]; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | print "}\n};\n"; | 
|---|