Debian Codegraph @

tools/fontres

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 if (!@ARGV) {
50 print "Usage: $0 FNT.RES indir\n";
51 print "Reconstructs dumped font directory into a FNT.RES\n";
52 exit 1;
53 }
54 my ($font_file, $indir) = @ARGV;
55
56 if (!defined($indir)) {
57 print "Please define input directory\n";
58 exit 1;
59 }
60
61 my $font_fh;
62 if (!open($font_fh, '>', $font_file)) {
63 print "Error: Unable to open $font_file\n";
64 exit 1;
65 }
66
67 my $buf;
68 my %header;
69 read_file(\%header, File::Spec->catfile($indir, "header.txt"));
70 $buf = pack($FontHeaderPacking, map {$header{$_}} @FontHeaderVars);
71 print $font_fh $buf;
72
73 my @info;
74 my $offset = 0;
75 for (my $i = $header{first_char}; $i <= $header{last_char}; $i++) {
76 my %font_info;
77 read_file(\%font_info, File::Spec->catfile($indir, "$i.txt"));
78 $font_info{bitmap_offset} = $offset;
79 $buf = pack($FontInfoPacking, map {$font_info{$_}} @FontInfoVars);
80 print $font_fh $buf;
81 $offset += $font_info{height} * $font_info{width} + 4;
82 }
83
84 for (my $i = $header{first_char}; $i <= $header{last_char}; $i++) {
85 my $icn_fh;
86 my $infile = File::Spec->catfile($indir, "$i.ICN");
87 if (!open($icn_fh, '<', $infile)) {
88 print "Error: Cannot open $infile\n";
89 exit 1;
90 }
91 my $icn = icn->read_file($icn_fh);
92 $icn->write_file($font_fh);
93 close($icn_fh);
94 }
95
96 close($font_fh);
97
98 exit 0;
99
100 sub read_file {
101 my $h;
102 my $filename;
103 ($h, $filename) = @_;
104 my $fh;
105 if (!open($fh, '<', $filename)) {
106 print "Error: Cannot open $filename\n";
107 exit 1;
108 }
109 while (my $line = <$fh>) {
110 chomp($line);
111 my @parts = split(/=/, $line);
112 if (@parts != 2) {
113 print "Syntax error in $filename\n";
114 exit 1;
115 }
116 $h->{$parts[0]} = $parts[1];
117 }
118 close($fh);
119 }