aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/fonts.src/terminus-font/bdftopsf.pl
blob: b3607ddfa8836786628bfb2ce3565a839f7ed413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/perl

push @ARGV, "";

if ($ARGV[0] eq "--help")
{
print STDERR <<EOT;
usage:	bdftopsf [[+h] [+u|-u] [+1|-1]]|[-h [+r|-r]]
	[+o|-o NAME] [--] [INPUT [TABLE...]]

+h	PSF mode - write a PSF header. This is the default.
  +u	Write an unicode table. Default for fonts with more
	than 256 characters.
  -u	Don't write unicode table. Default for fonts with 256
	or less characters.
  +1	Use PSF1. Default for fonts with 256 or 512 characters,
	width 8 and height less than 256.
  -1	Use PSF2. Default for all other fonts.

-h	RAW mode - write no PSF header (and no unicode table,
	of course).
  +r	Reject 512 character fonts. This is the default. Most
	systems won't handle these properly without some sort
	of header.
  -r	Allow 512 character fonts.

+o	Output to INPUT.psf or INPUT.raw (replacing the .bdf in
	INPUT if any).
-o NAME	Output to NAME.

--	Terminate the option list.

INPUT	RAW and PSF1: a 256 or 512 character BDF file with
	width = 8 and height between 1 and 255; PSF2: a BDF
	file with width between 1 and 128, height between 1 and
	256 and number of characters between 1 and 65536. If no
	input is specified, the standard input is used.

TABLE	A duplicate unicodes table. Each line must either be
	blank or contain two hexadecimal unicodes, each
	consisting of maximum 4 digits. Used for PSF with
	unicode table only, otherwise treated as error.

If no output is specified, the standard output is used.

Any options not specified in the above order are treated as
non-option arguments.
EOT
	exit 0;
}

if ($ARGV[0] eq "--version")
{
print STDERR <<EOT;
bdftopsf.pl 0.2.1, Copyright (C) 2008 Dimitar Toshkov Zhekov

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Report bugs to jimmy\@is-vn.bg
EOT
	exit 0;
}

if ($ARGV[0] eq "-h")
{
	$header = 0;
	$suffix = ".raw";
	$unicode = 0;
	$version = 0;
	shift @ARGV;

	if ($ARGV[0] eq "-r")
	{
		$reject = 0;
		shift @ARGV;
	}
	else
	{
		$reject = 1;
		if ($ARGV[0] eq "+r") { shift @ARGV; }
	}
}
else
{
	$header = 1;
	$suffix = ".psf";
	$reject = 0;
	if ($ARGV[0] eq "+h") { shift @ARGV; }

	if ($ARGV[0] eq "+u")
	{
		$unicode = 1;
		shift @ARGV;
	}
	elsif ($ARGV[0] eq "-u")
	{
		$unicode = 0;
		shift @ARGV;
	}

	if ($ARGV[0] eq "+1")
	{
		$version = 0;
		shift @ARGV;
	}
	elsif ($ARGV[0] eq "-1")
	{
		$version = 1;
		shift @ARGV;
	}
}

if ($ARGV[0] eq "+o")
{
	$output = "";
	shift @ARGV;
}
elsif ($ARGV[0] eq "-o")
{
	shift @ARGV;
	$#ARGV > 0 || die("$0: -o requires an argument\n");
	$output = $ARGV[0];
	shift @ARGV;
}
elsif ($ARGV[0] =~ /^-o(.*)$/)
{
	$output = $1;
	shift @ARGV;
}

if ($ARGV[0] eq "--") { shift @ARGV; }
elsif ($ARGV[0] =~ /^([-+][0-9a-z])$/) { print STDERR "$0: suspicuous $1, use -- to terminate the option list\n"; }
pop @ARGV;

if ($#ARGV < 0) { $ARGV[0] = "-"; }

open(BDF, "<$ARGV[0]") || die("$0: $ARGV[0]: $!\n");

while (<BDF>)
{
	if (/^FONTBOUNDINGBOX\s+([0-9]+)\s+([0-9]+).*$/)
	{
		$width = $1;
		$height = $2;
	}
	elsif (/^CHARS\s+([0-9]+)$/)
	{
		$chars = $1;
		last;
	}
}

(defined($width) && defined($chars)) || die("$0: $ARGV[0]: missing width/height or CHARS\n");
# psf2 can handle almost anything, but there must be some reasonable limits
($width > 0 && $width <= 128) || die("$0: $ARGV[0]: width $width out of range\n");
($height > 0 && $height <= 256) || die("$0: $ARGV[0]: height $height out of range\n");
($chars > 0 && $chars <= 65536) || die("$0: $ARGV[0]: CHARS $chars out of range\n");

$minimal = $width != 8 || $height >= 256 || ($chars != 256 && $chars != 512);
$header != 0 || $minimal == 0 || die("$0: $ARGV[0]: RAW format: invalid width $width, height $height or CHARS $chars\n");
$chars == 256 || $reject == 0 || die("$0: $ARGV[0]: RAW format: CHARS $chars rejected, use -r to accept\n", $chars);

if (!defined($unicode)) { $unicode = $chars > 256; }
if (!defined($version)) { $version = $minimal; }
else { $version >= $minimal || die("$0: $ARGV[0]: requested version ", $version + 1,  ", required ", $minimal + 1, "\n"); }

if (!defined($output)) { $output = "-"; }
elsif ($output eq "") { if ($ARGV[0] =~ /^(.*).bdf$/) { $output = "$1$suffix" ; } else { $output = "$ARGV[0]$suffix"; } }

$linesize = ($width + 7) >> 3;
$charsize = $linesize * $height;

sub int { $int = $!; }
sub bye
{
	close OUT;
	$output ne "-" && unlink($output) != 1 && print STDERR "$0: $output: $!\n";
	die("@_");
}

open(OUT, ">$output") || die("$0: $output: $!\n");
$SIG{INT} = 'int';
binmode(OUT) || bye("$0: $output: $!\n");

if ($header != 0)
{
	if ($version == 0) { printf OUT "%c%c%c%c", 0x36, 0x04, ($chars == 512) + $unicode * 2, $height; }
	else
	{
		printf OUT "%c%c%c%c", 0x72, 0xB5, 0x4A, 0x86;
		printf OUT "%c%c%c%c", 0x00, 0x00, 0x00, 0x00;
		printf OUT "%c%c%c%c", 0x20, 0x00, 0x00, 0x00;
		printf OUT "%c%c%c%c", $unicode, 0x00, 0x00, 0x00;
		printf OUT "%c%c%c%c", $chars & 0xFF, $chars >> 8, 0x00, 0x00;
		printf OUT "%c%c%c%c", $charsize & 0xFF, ($charsize >> 8) & 0xFF, $charsize >> 16, 0x00;
		printf OUT "%c%c%c%c", $height & 0xFF, $height >> 8, 0x00, 0x00;
		printf OUT "%c%c%c%c", $width & 0xFF, $width >> 8, 0x00, 0x00;
	}
}

$lines = 0;
while (<BDF>)
{
	$bytes = 0;
	while (/^([0-9a-fA-F]{2})(([0-9a-fA-F]{2})*)$/)
	{
		printf OUT "%c", hex($1);
		$bytes++;
		$_ = $2;
	}
	if ($bytes != 0)
	{
		$lines++;
		$bytes == $linesize || bye("$0: $ARGV[0]: invalid number of bytes $bytes on data line $lines\n");
	}
	else
	{
		if (/^ENCODING\s+([0-9]+)$/)
		{
			push @unimap, $1;
			if ($1 != 65535) { $unidup{$1}[0] = $1; }
		}
		elsif (/^ENDFONT$/) { last; }
	}
}

close BDF;

if ($#ARGV > 0)
{
	if ($unicode)
	{
		do
		{
			shift @ARGV;

			open(DUP, "<$ARGV[0]") || bye("$0: $ARGV[0]: $!\n");
			while (<DUP>)
			{
				next if /^\s*$/;
				/^([0-9a-fA-F]{1,4})\s+([0-9a-fA-F]{1,4})$/ || bye("$0: $ARGV[0]: invalid unicode(s) $_\n");
				$duplicate = hex($2);
				hex($1) != $duplicate || bye("$0: $ARGV[0]: invalid duplicate entry $_\n");
				foreach (@{$unidup{hex($1)}}) { if ($_ == $duplicate) { $duplicate = 65535; last; } }
				if ($duplicate != 65535) { push @{$unidup{hex($1)}}, $duplicate; }
			}
			close DUP;
		} while ($#ARGV > 0);
	}
	else { bye("$0: invalid number of arguments\n"); }
}

@unimap == $chars || bye("$0: $output: invalid number of chars @unimap\n");
@unimap * $charsize == $lines * $linesize || bye("$0: $output: invalid number of data lines $lines\n");

if ($unicode)
{
	foreach (@unimap)
	{
		foreach (@{$unidup{$_}}) {
			if ($version == 0) { printf OUT "%c%c", $_ & 0xFF, $_ >> 8; }
			elsif ($_ <= 0x7F) { printf OUT "%c", $_; }
			else
			{
				if ($_ <= 0x7FF) { printf OUT "%c", 0xC0 + ($_ >> 6); }
				else { printf OUT "%c%c", 0xE0 + ($_ >> 12), 0x80 + (($_ >> 6) & 0x3F); }
				printf OUT "%c", 0x80 + ($_ & 0x3F);
			}
		}
		printf OUT "%c", 0xFF;
		if ($version == 0) { printf OUT "%c", 0xFF; }
	}
}

close OUT || bye("$0: $output: $!\n");
defined($int) && bye("$0: $int\n");