ter-u12n.bdf: fix e? and u+?
[terpinus.git] / bdftopsf.pl
bloba31d09423d68016763415d2a9765cf863b3fff7b
1 #!/usr/bin/perl
3 push @ARGV, "";
5 if($ARGV[0] eq "--help")
7 print STDERR <<EOT;
8 usage: bdftopsf [+h][+u|-u][+1|-1]|[-h[+r|-r]] [+o|-o OUTPUT]
9 [--] [INPUT [TABLE...]]
11 +h PSF mode - write a PSF header. This is the default.
12 +u Write an unicode table. Default for fonts with more
13 than 256 characters.
14 -u Don't write unicode table. Default for fonts with 256
15 or less characters.
16 +1 Use PSF1. Default for fonts with 256 or 512 characters,
17 width 8 and height less than 256.
18 -1 Use PSF2. Default for all other fonts.
20 -h RAW mode - write no PSF header (and no unicode table,
21 of course).
22 +r Reject 512 character fonts. This is the default. Most
23 (if not all) UNIX systems won't handle these properly.
24 -r Allow 512 character fonts.
26 +o Output to INPUT.psf or INPUT.raw (replacing the .bdf in
27 INPUT if any).
28 -o NAME Output to NAME.
30 -- Terminate the option list.
32 INPUT RAW and PSF1: a 256 or 512 character BDF file with
33 width = 8 and height between 1 and 255. PSF2: a BDF
34 with width between 1 and 1200, height between 1 and
35 2400 and number of characters between 1 and 2048.
37 TABLE A duplicate unicodes table. Each line must either be
38 blank or contain two hexadecimal unicodes, each
39 consisting of maximum 4 digits. Used for PSF with
40 unicode data only, otherwise treated as error.
42 If no input or output are specified, the standard output is
43 the standard input and output are used.
45 Any options not specified in the above order are treated as
46 non-option arguments.
47 EOT
48 exit 0;
51 if($ARGV[0] eq "--version")
53 print STDERR <<EOT;
54 bdftopsf.pl 0.2.0, Copyright (C) 2005 Dimitar Toshkov Zhekov
56 This program is free software; you can redistribute it and/or
57 modify it under the terms of the GNU General Public License as
58 published by the Free Software Foundation; either version 2 of
59 the License, or (at your option) any later version.
61 This program is distributed in the hope that it will be useful,
62 but WITHOUT ANY WARRANTY; without even the implied warranty of
63 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 GNU General Public License for more details.
66 Report bugs to jimmy\@is-vn.bg
67 EOT
68 exit 0;
71 if($ARGV[0] eq "-h")
73 $header = 0;
74 $suffix = ".raw";
75 $unicode = 0;
76 $version = 0;
77 shift @ARGV;
79 if($ARGV[0] eq "-r")
81 $reject = 0;
82 shift @ARGV;
84 else
86 $reject = 1;
87 if($ARGV[0] eq "+r") { shift @ARGV; }
90 else
92 $header = 1;
93 $suffix = ".psf";
94 $reject = 0;
95 if($ARGV[0] eq "+h") { shift @ARGV; }
97 if($ARGV[0] eq "+u")
99 $unicode = 1;
100 shift @ARGV;
102 elsif($ARGV[0] eq "-u")
104 $unicode = 0;
105 shift @ARGV;
108 if($ARGV[0] eq "+1")
110 $version = 0;
111 shift @ARGV;
113 elsif($ARGV[0] eq "-1")
115 $version = 1;
116 shift @ARGV;
120 if($ARGV[0] eq "+o")
122 $output = "";
123 shift @ARGV;
125 elsif($ARGV[0] eq "-o")
127 shift @ARGV;
129 $ARGV[0] ne "" || die("$0: -o requires a non-empty argument\n");
130 $output = $ARGV[0];
131 shift @ARGV;
134 if($ARGV[0] eq "--") { shift @ARGV; }
135 elsif($ARGV[0] =~ /^([-+][0-9a-z])$/) { print STDERR "$0: suspicuous $1, use -- to terminate the option list\n"; }
136 pop @ARGV;
138 if($#ARGV < 0) { $ARGV[0] = "-"; }
140 open(BDF, "<$ARGV[0]") || die("$0: $ARGV[0]: $!\n");
142 while(<BDF>)
144 if(/^FONTBOUNDINGBOX\s+([0-9]+)\s+([0-9]+).*$/)
146 $width = $1;
147 $height = $2;
149 elsif(/^CHARS\s+([0-9]+)$/)
151 $chars = $1;
152 last;
156 ($width > 0 && $width <= 1200) || die("$0: $ARGV[0]: width $width zero or too big\n");
157 ($height > 0 && $height <= 2400) || die("$0: $ARGV[0]: height $height zero or too big\n");
158 ($chars > 0 && $chars <= 2048) || die("$0: $ARGV[0]: CHARS $chars zero or too big\n");
160 $minimal = $width != 8 || $height >= 256 || ($chars != 256 && $chars != 512);
161 $header != 0 || $minimal == 0 || die("$0: $ARGV[0]: RAW format: invalid width $width, height $height or CHARS $chars\n");
162 $chars == 256 || $reject == 0 || die("$0: $ARGV[0]: RAW format: CHARS $chars rejected, use -r to accept\n", $chars);
164 if(!defined($unicode)) { $unicode = $chars > 256; }
165 if(!defined($version)) { $version = $minimal; }
166 else { $version >= $minimal || die("$0: $ARGV[0]: requested version ", $version + 1, ", required ", $minimal + 1, "\n"); }
168 if(!defined($output)) { $output = "-"; }
169 elsif($output eq "") { if($ARGV[0] =~ /^(.*).bdf$/) { $output = "$1$suffix" ; } else { $output = "$ARGV[0]$suffix"; } }
171 $linesize = ($width + 7) >> 3;
172 $charsize = $linesize * $height;
174 sub int { $int = $!; }
175 sub bye
177 close OUT;
178 $output ne "-" && unlink($output) != 1 && print STDERR "$0: $output: $!\n";
179 die("@_");
182 open(OUT, ">$output") || die("$0: $output: $!\n");
183 $SIG{INT} = 'int';
184 binmode(OUT) || bye("$0: $output: $!\n");
186 if($header != 0)
188 if($version == 0) { printf OUT "%c%c%c%c", 0x36, 0x04, ($chars == 512) + $unicode * 2, $height; }
189 else
191 printf OUT "%c%c%c%c", 0x72, 0xB5, 0x4A, 0x86;
192 printf OUT "%c%c%c%c", 0x00, 0x00, 0x00, 0x00;
193 printf OUT "%c%c%c%c", 0x20, 0x00, 0x00, 0x00;
194 printf OUT "%c%c%c%c", $unicode, 0x00, 0x00, 0x00;
195 printf OUT "%c%c%c%c", $chars & 0xFF, $chars >> 8, 0x00, 0x00;
196 printf OUT "%c%c%c%c", $charsize & 0xFF, ($charsize >> 8) & 0xFF, $charsize >> 16, 0x00;
197 printf OUT "%c%c%c%c", $height & 0xFF, $height >> 8, 0x00, 0x00;
198 printf OUT "%c%c%c%c", $width & 0xFF, $width >> 8, 0x00, 0x00;
202 $lines = 0;
203 while(<BDF>)
205 $bytes = 0;
206 while(/^([0-9a-fA-F]{2})(([0-9a-fA-F]{2})*)$/)
208 printf OUT "%c", hex($1);
209 $bytes++;
210 $_ = $2;
212 if($bytes != 0)
214 $lines++;
215 $bytes == $linesize || bye("$0: $ARGV[0]: invalid number of bytes $bytes on data line $lines\n");
217 else
219 if(/^ENCODING\s+([0-9]+)$/)
221 push @unimap, $1;
222 if($1 != 65535) { $unidup{$1}[0] = $1; }
224 elsif(/^ENDFONT$/) { last; }
228 close BDF;
230 if($#ARGV > 0)
232 if($unicode)
236 shift @ARGV;
238 open(DUP, "<$ARGV[0]") || bye("$0: $ARGV[0]: $!\n");
239 while(<DUP>)
241 next if /^\s*$/;
242 /^([0-9a-fA-F]{1,4})\s+([0-9a-fA-F]{1,4})$/ || bye("$0: $ARGV[0]: invalid unicode(s) $_\n");
243 $duplicate = hex($2);
244 hex($1) != $duplicate || bye("$0: $ARGV[0]: invalid duplicate entry $_\n");
245 foreach(@{$unidup{hex($1)}}) { if($_ == $duplicate) { $duplicate = 65535; last; } }
246 if($duplicate != 65535) { push @{$unidup{hex($1)}}, $duplicate; }
248 close DUP;
249 } while($#ARGV > 0);
251 else { bye("$0: invalid number of arguments\n"); }
254 @unimap == $chars || bye("$0: $output: invalid number of chars @unimap\n");
255 @unimap * $charsize == $lines * $linesize || bye("$0: $output: invalid number of data lines $lines\n");
257 if($unicode)
259 foreach(@unimap)
261 foreach(@{$unidup{$_}}) {
262 if($version == 0) { printf OUT "%c%c", $_ & 0xFF, $_ >> 8; }
263 elsif($_ <= 0x7F) { printf OUT "%c", $_; }
264 else {
265 if($_ <= 0x7FF) { printf OUT "%c", 0xC0 + ($_ >> 6); }
266 else { printf OUT "%c%c", 0xE0 + ($_ >> 12), 0x80 + (($_ >> 6) & 0x3F); }
267 printf OUT "%c", 0x80 + ($_ & 0x3F);
270 printf OUT "%c", 0xFF;
271 if($version == 0) { printf OUT "%c", 0xFF; }
275 close OUT || bye("$0: $output: $!\n");
276 defined($int) && bye("$0: $int\n");