Debian Codegraph @

tools/fontdump

raw
1 #!/usr/bin/perl
2 #
3 # Seven Kingdoms: Ancient Adversaries
4 #
5 # Copyright 1997,1998 Enlight Software Ltd.
6 # Copyright 2018 Jesse Allen
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21 #
22
23 use warnings;
24 use strict;
25
26 use FindBin;
27 use lib $FindBin::Bin;
28 use File::Path qw(make_path);
29 use icn;
30
31 my @FontHeaderVars = qw(
32 max_width
33 max_height
34 std_height
35 first_char
36 last_char
37 );
38 my $FontHeaderPacking = 'SSSSS';
39 my $FontHeaderSize = 10;
40 my @FontInfoVars = qw(
41 offset_y
42 width
43 height
44 bitmap_offset
45 );
46 my $FontInfoPacking = 'cCCL';
47 my $FontInfoSize = 7;
48
49
50 if (!@ARGV) {
51 print "Usage: $0 FNT.RES outdir\n";
52 print "Dump information from FNT.RES and optionally write data to outdir.\n";
53 exit 0;
54 }
55 my ($font_file, $outdir) = @ARGV;
56
57 my $dumping_font = 0;
58 if (defined($outdir)) {
59 eval { make_path($outdir) };
60 if ($@) {
61 print "Error: Cannot create $outdir\n";
62 exit 1;
63 }
64 $dumping_font = 1;
65 }
66
67 my $font_fh;
68 if (!open($font_fh, '<', $font_file)) {
69 print "Error: Unable to open $font_file\n";
70 exit 1;
71 }
72
73 my $buf;
74 my @elements;
75 read($font_fh, $buf, $FontHeaderSize);
76 @elements = unpack($FontHeaderPacking, $buf);
77 my %header = map { $FontHeaderVars[$_] => $elements[$_] } 0..$#FontHeaderVars;
78
79 print "font header: max_width=$header{max_width} max_height=$header{max_height} std_height=$header{std_height} first_char=$header{first_char} last_char=$header{last_char}\n";
80 if ($dumping_font) {
81 write_file(\%header, File::Spec->catfile($outdir, "header.txt"));
82 }
83
84 my @info;
85 for (my $i = $header{first_char}; $i <= $header{last_char}; $i++) {
86 read($font_fh, $buf, $FontInfoSize);
87 @elements = unpack($FontInfoPacking, $buf);
88 my %font_info = map { $FontInfoVars[$_] => $elements[$_] } 0..$#FontInfoVars;
89 push(@info, \%font_info);
90
91 print "font info $i: offset_y=$font_info{offset_y} width=$font_info{width} height=$font_info{height} bitmap_offset=$font_info{bitmap_offset}\n";
92 if ($dumping_font) {
93 write_file(\%font_info, File::Spec->catfile($outdir, "$i.txt"));
94 }
95 }
96
97 for (my $i = $header{first_char}; $i <= $header{last_char}; $i++) {
98 if (eof($font_fh)) {
99 print "bad end of file\n";
100 exit 1;
101 }
102 my $icn = icn->read_file($font_fh);
103 print "char $i bmp of h=", $icn->height(), " w=", $icn->width(), "\n";
104 $icn->print_ascii(*STDOUT);
105 if ($dumping_font) {
106 my $outfile = File::Spec->catfile($outdir, "$i.ICN");
107 my $out_fh;
108 if (!open($out_fh, '>', $outfile)) {
109 print "Error: Cannot open $outfile\n";
110 exit 1;
111 }
112 $icn->write_file($out_fh);
113 close($out_fh);
114 }
115 }
116
117 close($font_fh);
118
119 exit 0;
120
121 sub write_file {
122 my $h;
123 my $filename;
124 ($h, $filename) = @_;
125 my $fh;
126 if (!open($fh, '>', $filename)) {
127 print "Error: Cannot open $filename\n";
128 exit 1;
129 }
130 print $fh map {"$_=$h->{$_}\n"} keys(%$h);
131 close($fh);
132 }