-
Notifications
You must be signed in to change notification settings - Fork 2
/
psf2bdf.pl
132 lines (115 loc) · 4.47 KB
/
psf2bdf.pl
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
#!/usr/bin/env perl
# Convert PC Screen Font (PSF) font to Glyph Bitmap Distribution Format (BDF).
# Copyright (c) 2015 Kacper Gutowski
# Copyright (c) 2015 Susanne Oberhauser-Hirschoff
# The MIT License applies (http://opensource.org/licenses/MIT)
# https://gist.github.com/mwgamera/6ad86e71e002e5aef1b8/01ffe8208b2763dd353ea7afacedf83072477372
# with minor fixes
use strict;
use constant {
PSF1_MODE512 => 0x01,
PSF1_MODEHASTAB => 0x02,
PSF2_HAS_UNICODE_TABLE => 0x01,
};
push @ARGV, '-' unless scalar @ARGV;
for (@ARGV) {
my $fn = /^-$/ ? 'stdin' : $_;
eval {
my ($length, $width, $height);
my (@glyphs, @unicode);
my $unilen = 0;
open my $fh, $_ or die $!;
binmode $fh;
read $fh, $_, 4 or die $!;
if (0x0436 == unpack 'v') { # psf1
my ($mode, $size) = unpack 'x2CC';
$length = $mode & PSF1_MODE512 ? 512 : 256;
$height = $size;
$width = 8;
read $fh, $_, $length * $size;
@glyphs = unpack "(a$size)$length";
if ($mode & PSF1_MODEHASTAB) {
for my $i (0 .. $length - 1) {
my ($u, @u) = 0;
do {
read $fh, $_, 2;
$u = unpack 'v';
push @u, $u if $u < 0xFFFE;
} while $u < 0xFFFE;
while ($u != 0xFFFF) {
read $fh, $_, 2;
$u = unpack 'v';
warn 'Unicode sequence ignored' if $u == 0xFFFE;
}
$unicode[$i] = [@u];
$unilen += (scalar @u) - 1 if (scalar @u);
}
}
}
elsif (0x864ab572 == unpack 'V') { # psf2
read $fh, $_, 28 or die $!;
(my ($ver, $hlen, $flg), $length, my $size, $height, $width) = unpack 'V7';
die "Unknown version $ver\n" unless $ver == 0;
warn "Unexpected glyph size $size bytes for ${width}×$height px\n"
unless $size == $height * int(($width + 7) / 8);
read $fh, $_, $hlen - 32; # skip to data
read $fh, $_, $length * $size;
@glyphs = unpack "(a$size)$length";
if ($flg & PSF2_HAS_UNICODE_TABLE) {
my $buf = do { local $/; <$fh>; };
for my $i (0 .. $length - 1) {
$buf =~ m/\G([^\xfe\xff]*+)(?:\xfe[^\xfe\xff]++)*\xff/sg;
utf8::decode(my $str = $1);
$unicode[$i] = [map ord, split //, $str];
$unilen += (scalar @{$unicode[$i]}) - 1 if (scalar @{$unicode[$i]});
}
}
}
else {
die "Bad format\n";
}
print "STARTFONT 2.1\n";
printf "FONT %s\n", '-psf-';
printf "SIZE %u 72 72\n", $height;
printf "FONTBOUNDINGBOX %u %u 0 0\n", $width, $height;
printf "STARTPROPERTIES %u\n", 6 + 2 * !!@unicode;
printf "PIXEL_SIZE %u\n", $height;
printf "POINT_SIZE %u\n", 10 * $height;
printf "FONT_ASCENT %u\n", $height;
print "FONT_DESCENT 0\n";
print "RESOLUTION_X 72\n";
print "RESOLUTION_Y 72\n";
if (@unicode) {
print "CHARSET_REGISTRY \"ISO10646\"\n";
print "CHARSET_ENCODING \"1\"\n";
}
print "ENDPROPERTIES\n";
printf "CHARS %u\n", $length + $unilen;
for my $i (0 .. $length - 1) {
my @encodings = ($i);
my $is_unicode = 0;
if(@unicode && @{$unicode[$i]}) {
@encodings = @{$unicode[$i]};
$is_unicode = 1;
}
foreach my $e (@encodings) {
printf "STARTCHAR psf%03x-%04x\n", $i, $e;
if ($is_unicode) {
printf "ENCODING %u\n", $e;
}
else {
printf "ENCODING -1 %u\n", $e;
}
printf "SWIDTH %u 0\n", $width * 1000 / $height;
printf "DWIDTH %u 0\n", $width;
printf "BBX %u %u 0 0\n", $width, $height;
my $bw = (($width + 7) & ~7) >> 3;
printf "BITMAP\n%s\n", join "\n", map unpack('H*', $_), unpack "(a$bw)*", $glyphs[$i];
printf "ENDCHAR\n";
}
}
print "ENDFONT\n";
};
warn "$fn: $@" if $@;
last;
}