blib/script/afpmkfontdb.pl untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 298 CC 6 SCAR 16.2
CC
6
Cov
0%
write_record
20.0
FNC
6.0
FNI
6.0
CRAP 5.0
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use Encode;
5 use File::Glob 'bsd_glob';
6 use File::Basename;
7 use DBI qw(:sql_types);
8 use DBD::SQLite;
9 use Parse::AFP;
10 use Getopt::Long;
11  
12 use constant GCG_Elements => [
13 qw( Increment Ascend Descend ASpace BSpace CSpace BaseOffset _FNMCount )
14 ];
15 use constant FNI_Elements => [
16 qw( Width Height _FNGOffset )
17 ];
18 use constant +{ map { +GCG_Elements->[$_] => $_ } 0..$#{+GCG_Elements} };
19 use constant +{ map { +FNI_Elements->[$_] => $_ } 0..$#{+FNI_Elements} };
20  
21 $|++;
22  
23 die "Usage: $0 [ -o fonts.fdb | fdbdir ] [ dir | file... ] \n" unless @ARGV >= 1 or -d 'dir';
24  
25 my $default_output;
26 GetOptions(
27 'o|output:s' => \$default_output,
28 );
29  
30 my ($dbh, $file, $output);
31  
32 our (%GCG, %FNI, %SpaceIncrement, @FNM, $FNG);
33 our ($FontName, $Rotation, $Resolution);
34  
35 my (%initialized, $is_child);
36 my @inputs = sort map { (-d $_) ? bsd_glob("$_/*") : $_ } (@ARGV ? @ARGV : 'dir');
37  
38 foreach my $i (@inputs) {
39 $file = $i;
40 $file =~ /X0.*afp$/i or next;
41 if (-d $default_output) {
42 $output = "$default_output/".basename((substr($file, 0, -3) . 'fdb'));
43 }
44 elsif ($default_output) {
45 $output = $default_output;
46 }
47 else {
48 $output = substr($file, 0, -3) . 'fdb';
49 }
50  
51 if (!$initialized{$output}++) {
52 unlink $output if -e $output;
53 $dbh = DBI->connect("dbi:SQLite:dbname=$output") or die $DBI::errstr;
54 init_db();
55 $dbh->disconnect;
56 }
57  
58 if (my $pid = fork) {
59 waitpid($pid, 0);
60 }
61 else {
62 $is_child = 1;
63 last;
64 }
65 }
66 $is_child or exit;
67  
68 $dbh = DBI->connect("dbi:SQLite:dbname=$output") or die $DBI::errstr;
69  
70 basename($file) =~ /^(X0([^.]+))/ or exit;
71 $FontName = $1;
72  
73 $dbh->begin_work;
74 init_table();
75  
76 my $name = $2;
77 print "Parsing font $name.";
78 Parse::AFP->new($file, { lazy => 1 })->callback_members([qw( CFC CFI )]);
79  
80 $dbh->commit;
81 $dbh->begin_work;
82  
83 # Heuristic: If Variations <= 3, we think it's fixed width font.
84 my $dimension = "Width || ',' || Height";
85 my $variations = $dbh->selectall_arrayref(qq(
86 SELECT Width, Height, COUNT($dimension)
87 FROM $FontName
88 GROUP BY $dimension
89 ORDER BY Count($dimension) DESC
90 ));
91  
92 $dbh->do(
93 "INSERT INTO Fonts VALUES (?, ?, ?, ?)", {},
94 $FontName, $Resolution, (@$variations > 3) ? (0, 0) : @{$variations->[0]}[0, 1]
95 );
96  
97 $dbh->commit;
98  
99 # Heuristic: If there is no "\x40" <= 3, fill in a fake record
100 my @has_0x40 = @{$dbh->selectcol_arrayref(
101 "SELECT Width FROM $FontName WHERE Character = '\x40'"
102 )||[]};
103  
104 if (!@has_0x40 and my $inc = $SpaceIncrement{'0000'}{$FontName}) {
105 $dbh->begin_work;
106  
107 $dbh->do(
108 "INSERT INTO $FontName VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", {},
109 "\x40",
110 $inc,
111 0, 0,
112 0, $inc, 0, 0,
113 '',
114 $inc,
115 $inc,
116 );
117  
118 foreach my $rotation (qw( 2D 5A 87 )) {
119 my $rot_inc = $SpaceIncrement{$rotation.'00'}{$FontName} or next;
120 my $angle = hex($rotation) * 2;
121 $dbh->do(
122 "INSERT INTO RotationInfo VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", {},
123 "\x40",
124 $rot_inc,
125 0, 0,
126 0, $rot_inc, 0, 0,
127 $FontName, $angle
128 );
129 }
130  
131 $dbh->commit;
132 }
133  
134 $dbh->disconnect;
135  
136 print "\n";
137  
138 exit;
139  
140 my $CFIRepeatingGroupLength;
141 sub CFC {
142 $CFIRepeatingGroupLength = $_[0]->CFIRepeatingGroupLength;
143 }
144  
145 sub CFI {
146 my $data = $_[0]->Data;
147 my $offset = 0;
148 while (my $CFIRepeatingGroup = substr($data, $offset, $CFIRepeatingGroupLength)) {
149 my ($fcs_name, $cp_name, $section) = unpack("a8a8x8C", $CFIRepeatingGroup);
150  
151 %GCG = %FNI = @FNM = (); $FNG = ''; $Rotation = 0;
152  
153 $cp_name = dirname($file)."/".Encode::decode( cp500 => $cp_name ).".afp";
154 $fcs_name = dirname($file)."/".Encode::decode( cp500 => $fcs_name ).".afp";
155  
156 Parse::AFP->new($cp_name, { lazy => 1 })->callback_members([qw( CPC CPI )]);
157 Parse::AFP->new($fcs_name, { lazy => 1 })->callback_members([qw( FNC FNI FNM FNG FNO )]);
158  
159 write_record($section);
160  
161 $offset += $CFIRepeatingGroupLength;
162  
163 print ".";
164 }
165 }
166  
167 sub write_record {
168 my $section = shift;
169 while (my ($rotation, $fni) = each %FNI) {
170 my $sth = $dbh->prepare_cached(
171 $rotation
172 ? "INSERT INTO RotationInfo VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
173 : "INSERT INTO $FontName VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
174 );
175  
176 foreach my $codepoint (keys %GCG) {
177 my $gcg = $GCG{$codepoint};
178 my $fnc = $fni->{$gcg} or next;
179  
180 defined(my $count = pop @$fnc) or next;
181 my $fnm = $FNM[$count] or die "Cannot find fnm #$count";
182  
183 # printf "%s - 0x%02X 0x%02X - @$fnm\n", $rotation, $section, $codepoint;
184  
185 $sth->bind_param(
186 1,
187 ($section ? pack('n', $section * 256 + $codepoint) : pack('C', $codepoint)),
188 SQL_VARCHAR,
189 );
190 $sth->bind_param(
191 2 + $_,
192 $fnc->[$_],
193 SQL_INTEGER,
194 ) for 0..$#$fnc;
195  
196 if ($rotation) {
197 $sth->bind_param( 9, $FontName, SQL_VARCHAR );
198 $sth->bind_param( 10, $rotation, SQL_INTEGER );
199 }
200 else {
201 $sth->bind_param(
202 9,
203 substr($FNG, pop @$fnm, int(($fnm->[Width] + 7)/8)*$fnm->[Height]),
204 SQL_BLOB,
205 );
206 $sth->bind_param(
207 10 + $_,
208 $fnm->[$_],
209 SQL_INTEGER,
210 ) for 0..$#$fnm;
211 }
212  
213 $sth->execute;
214 }
215 }
216 }
217  
218 my $CPIRepeatingGroupLength;
219 sub CPC {
220 $CPIRepeatingGroupLength = $_[0]->CPIRepeatingGroupLength;
221 }
222  
223 sub CPI {
224 my $data = $_[0]->Data;
225 my $offset = 0;
226 while (my $CPIRepeatingGroup = substr($data, $offset, $CPIRepeatingGroupLength)) {
227 my ($GCGID, $CodePoint) = unpack("a8xC", $CPIRepeatingGroup);
228 $GCG{$CodePoint} = $GCGID;
229 $offset += $CPIRepeatingGroupLength;
230 }
231 }
232  
233 my $FNIRepeatingGroupLength;
234 my $FNMRepeatingGroupLength;
235 sub FNC {
236 $FNIRepeatingGroupLength = $_[0]->FNIRepeatingGroupLength;
237 $FNMRepeatingGroupLength = $_[0]->FNMRepeatingGroupLength;
238 die "UnitXBase other than 00 not handled" unless $_[0]->UnitXBase eq '00';
239 $Resolution = $_[0]->UnitXValue;
240 }
241  
242 sub FNI {
243 my $data = $_[0]->Data;
244 my $offset = 0;
245  
246 while (my $FNIRepeatingGroup = substr($data, $offset, $FNIRepeatingGroupLength)) {
247 my ($GCGID, $CharInc, $AscendHt, $DescendDp, $Reserved, $FNMCnt, $ASpace, $BSpace, $CSpace, $BaseOset) = unpack("a8nnnnnnnnx2n", $FNIRepeatingGroup);
248
249 for ($AscendHt, $DescendDp, $ASpace, $CSpace, $BaseOset) {
250 # cast "unsigned short" to "signed short"
251 $_ -= 65536 if $_ > 32768;
252 }
253  
254 $FNI{$Rotation}{$GCGID} = [
255 $CharInc, $AscendHt, $DescendDp, $ASpace, $BSpace, $CSpace, $BaseOset, $FNMCnt
256 ];
257  
258 $offset += $FNIRepeatingGroupLength;
259 }
260  
261 $Rotation += 90;
262 }
263  
264 sub FNM {
265 my $data = $_[0]->Data;
266 my $offset = 0;
267
268 while (my $FNMRepeatingGroup = substr($data, $offset, $FNMRepeatingGroupLength)) {
269 my ($w, $h, $o) = unpack("nnN", $FNMRepeatingGroup);
270 push @FNM, [ $w+1, $h+1, $o ];
271 $offset += $FNMRepeatingGroupLength;
272 }
273 }
274  
275 sub FNG { $FNG .= $_[0]->Data; }
276  
277 sub FNO {
278 $SpaceIncrement{$_[0]->CharacterRotation}{$FontName}
279 = $_[0]->SpaceCharacterIncrement;
280 }
281  
282 sub init_db {
283 $dbh->do('PRAGMA default_cache_size = 200000; ') or die $dbh->errstr;
284 $dbh->do('PRAGMA default_synchronous = OFF; ') or die $dbh->errstr;
285  
286 $dbh->do(q(
287 CREATE TABLE Fonts (
288 FontName VARCHAR(255) PRIMARY KEY,
289 Resolution INTEGER,
290 FixedWidth INTEGER,
291 FixedHeight INTEGER
292 );
293 ));
294  
295 $dbh->do(q(
296 CREATE TABLE RotationInfo (
297 Character VARCHAR(6),
298  
299 Increment INTEGER,
300 Ascend INTEGER,
301 Descend INTEGER,
302 ASpace INTEGER,
303 BSpace INTEGER,
304 CSpace INTEGER,
305 BaseOffset INTEGER,
306  
307 FontName VARCHAR(255),
308 Rotation INTEGER
309 );
310 ));
311  
312 $dbh->do(q(
313 CREATE INDEX RotationInfo_1 ON RotationInfo(FontName, Character, Rotation);
314 ));
315 }
316  
317 sub init_table {
318 $dbh->do(qq(
319 CREATE TABLE $FontName (
320 Character VARCHAR(6) PRIMARY KEY,
321  
322 Increment INTEGER,
323 Ascend INTEGER,
324 Descend INTEGER,
325 ASpace INTEGER,
326 BSpace INTEGER,
327 CSpace INTEGER,
328 BaseOffset INTEGER,
329  
330 Bitmap LONGBLOB,
331  
332 Width INTEGER,
333 Height INTEGER
334 );
335 ));
336 }
337