-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbanal
1746 lines (1442 loc) · 46.8 KB
/
banal
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -s
#
# Copyright (C) 2007 Geoffrey M. Voelker
#
# banal -- analyze pdf formatting
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Geoffrey M. Voelker ([email protected])
#
# todo:
# -- computer modern roman fonts
# -- embedded java script, remoteapproach.com
use Data::Dumper;
use File::Basename;
sub usage {
print <<EOF
usage: banal [-report | -stats | -judge [specs]] [-zoom=N] files
banal has three modes of operation:
-report print full formatting info for all pages. this mode is
the default if no mode is specified:
% banal paper.pdf
-stats print formatting info condensed into one line with fields
separated by tabs; useful for computing summary stats across
many papers.
fields are 'file', 'paper', 'text region', 'margins', 'font',
'leading', 'columns', 'pages', 'app'. for example:
% banal -stats *.pdf | cut -f 5
extracts font sizes from a set of pdf files.
-judge compare document formatting against a set of formatting
specifications:
-paper=type paper type ('letter' and 'A4' currently supported)
-pages=num max number of pages
-font=num min font size
-leading=num min leading
-cols=num max columns
-width=inches max text region width
-height=inches max text region height
-fudge=inches text region fudge factor (helps with latex
overflow; default is $banal_text_fudge inches)
specifications can consist of any and all elements in any
combination. for example:
% banal -judge -paper=letter -pages=14 -font=10 -leading=12 -width=6.5 -height=9 *.pdf
will check whether a set of pdf files conforms to formatting specs
that require 8.5" x 11" paper, max 14 pages, min 10 point font,
min 12 point leading, and a max text region of 6.5" x 9".
-format=lines|list
lines report format violations on multiple lines (default)
list report format violations on a single line separated by a
comma (e.g., for importing into a spreadsheet).
% banal -judge -format=list [specs] *.pdf
- - - - - - - - - - - - - - - - - -
-zoom=N passed to pdftohtml. -zoom=1 means do not pass a zoom argument,
and use pdftohtml\'s default zoom.
-version report the version of banal
EOF
}
# version
$banal_version = "1.2.2";
# zoom value
usage() if ((defined $zoom) && ($zoom !~ /^[1-9]\d*(\.\d*)?$/));
# mapping from pdftohtml units to inches
#$p2h_per_inch = 72;
$p2h_per_inch;
# scale factor from pdftohtml units to points
#$p2h_to_points = 72 / $p2h_per_inch;
$p2h_to_points;
# minimum amount of text on page for it to be interesting
$banal_min_density = 8000;
# fudge factor when judging text regions (in inches).
$banal_text_fudge = 0.05;
# minimum number of pages that have to fail the text region specs.
# often papers have 1-2 pages where text on a table or figure extends
# into the margin. when judging an entire paper, we'll let those slide...
$banal_judge_min_fail_pages = 3;
# policy to use to estimate leading
$banal_leading_policy;
# pdftohtml executable
if (exists $ENV{"PDFTOHTML"}) {
$pdftohtml = $ENV{"PDFTOHTML"};
} elsif (exists $ENV{"PHP_PDFTOHTML"}) {
$pdftohtml = $ENV{"PHP_PDFTOHTML"};
} elsif (defined $pdftohtml_prog) {
$pdftohtml = $pdftohtml_prog;
} else {
$pdftohtml = "pdftohtml";
}
#print STDERR "using $pdftohtml...\n";
# version of pdftohtml program
$p2h_version = 0;
# full path of file being analyzed
$banal_fullpath = '';
# file name of file being analyzed
$banal_filename = '';
# sketch of perl data structures used
$page = "
$num
$pagedata
$pagespec
";
$pagedata = "
";
$pagespec = "
$paperbb
$regionbb
$bodyfont
$ncols
";
$bbox = "
$top
$left
$width
$height
";
$segdata = "
%widths
%lefts
%rights
%tops
%bots
%leads
";
# return min key in hash
sub minkey ($) {
my ($href) = @_;
return (sort { $a <=> $b } keys %$href)[0];
}
# return max key in hash
sub maxkey ($) {
my ($href) = @_;
return (sort { $a <=> $b } keys %$href)[$#_ - 1];
}
# return key of mode of values in hash
sub modevalkey ($) {
my ($href) = @_;
my ($mode) = (keys %$href)[0];
map { $mode = $_ if ($href->{$_} > $href->{$mode}) } keys %$href;
return $mode;
}
# return max val in hash
sub maxval ($) {
my ($href) = @_;
my ($max) = (keys %$href)[0];
map { $max = $_ if ($href->{$_} > $href->{$max}) } keys %$href;
return $href->{$max};
}
# return 'a' == 'b'
sub bb_equal ($$) {
my ($a, $b) = @_;
return (($a->{top} == $b->{top}) &&
($a->{left} == $b->{left}) &&
($a->{height} == $b->{height}) &&
($a->{width} == $b->{width}));
}
# merge 'a' into 'b'
sub bb_merge ($$) {
my ($a, $b) = @_;
$b->{top} = $a->{top} if ($a->{top} < $b->{top});
$b->{left} = $a->{left} if ($a->{left} < $b->{left});
$b->{height} = $a->{height} if ($a->{height} > $b->{height});
$b->{width} = $a->{width} if ($a->{width} > $b->{width});
}
sub calc_page_body_font ($) {
my ($page) = @_;
my ($mode) = modevalkey ($page->{pagedata}->{segdata}->{byfont});
$page->{pagedata}->{bodyfont} = $page->{doc}->{fonts}->{$mode};
$page->{pagespec}->{bodyfont} = p2h_font_to_font_size ($page->{pagedata}->{bodyfont});
if ($page->{pagespec}->{bodyfont} == 0) {
print "$banal_filename: Error: Zero font on page $page->{num}, font id $mode\n";
}
}
sub utf8ascii_undo ($) {
my ($str) = @_;
return $str unless ($str =~ /^\\376\\377(\\\d\d\d.)*$/);
# string is UTF-8 in ASCII (not binary)
# (PDFCreator seems to like to do this, also freepdfconvert)
print "$banal_filename: ascii UTF-8: $title\n" if ($debug_docapp);
$str =~ s/\\376\\377//;
$str =~ s/\\000//g;
print "$banal_filename: unencoded: $str\n" if ($debug_docapp);
return $str;
}
sub utf8bin_undo ($) {
my ($str) = @_;
return $str unless ($str =~ /^\376\377(\000.)*$/);
# string is UTF-8 in binary
print "$banal_filename: bin UTF-8: $str\n" if ($debug_docapp);
$str =~ s/\376\377//;
$str =~ s/\000//g;
print "$banal_filename: unencoded $str\n" if ($debug_docapp);
return $str;
}
sub utf8revbin_undo ($) {
my ($str) = @_;
# bytes reversed: character then null bytes (ScanSoft on the Mac)
return $str unless ($str =~ /^\377\376(.\000)*$/);
# string is UTF-8 in binary
print "$banal_filename: rev bin UTF-8: $str\n" if ($debug_docapp);
$str =~ s/\377\376//;
$str =~ s/\000//g;
print "$banal_filename: unencoded $str\n" if ($debug_docapp);
return $str;
}
sub utf8hex_undo ($) {
my ($str) = @_;
return $str unless ($str =~ /^FEFF(00..)*$/i);
print "$banal_filename: hex UTF-8: $str\n" if ($debug_docapp);
$str =~ s/^FEFF//i;
$str =~ s/00//g;
print "$banal_filename: hex ascii: $str\n" if ($debug_docapp);
$str = pack ("H*", $str);
print "$banal_filename: packed $str\n" if ($debug_docapp);
return $str;
}
# inferring the document application has two steps:
# 1) extracting the doc metadata
# 2) mapping metadata info to an application
#
# for (1), ideally we could use a module or tool to extract the
# InfoDict from the end of the pdf file. but there are some cases
# where we need to peek outside the InfoDict for additional hints, so
# in the end we still have to scan through the pdf file ourselves.
#
# for (2), the world would be a simpler place if applications followed
# some kind of convention. but given the large combination of apps,
# pdf converters, and OSes, of course the world is not so simple. so,
# as usual, it's back to heuristics gathered from samples...
sub calc_doc_app ($) {
my ($doc) = @_;
my ($fname) = $doc->{fullpath};
my ($creator, $title, $producer, $creatortool, $ptex);
my ($rdftitle, $pdfproducer);
my ($indirect, $quartzpdf, $pdfmachine, $cmrfont, $texfont);
$creator = $title = $producer = $creatortool = $ptex = '';
$rdftitle = $pdfproducer = '';
$indirect = $quartzpdf = $pdfmachine = $cmrfont = $texfont = 0;
my ($app, @allapps);
$app = '';
@allapps = ();
if (!open (PDF, $fname)) {
print STDERR "$banal_filename: Error: Failed to open $fname for inferring doc app.";
$doc->{app} = 'unknown';
return;
}
while (<PDF>) {
if (m|\/Creator\s*\(([^\)]+)\)|) {
$creator = $1;
} elsif (m|\/Creator\s*<([^\)]+)>|) {
# UTF-8 ascii hex
$creator = utf8hex_undo ($1);
} elsif (m|\/Creator \d+ \d+ R|) {
# Indirection:
# << /Producer 313 0 R /Creator 314 0 R ...
$indirect = 1;
}
if (m|\/Title\s*\(([^\)]+)\)|) {
$title = $1;
} elsif (m|\/Title\s*<([^\)]+)>|) {
# UTF-8 ascii hex
$title = utf8hex_undo ($1);
} elsif (m|<dc:title>.+<rdf:li.+>(.+)</rdf:li>.+</dc:title>|) {
$rdftitle = $1;
} elsif (m|<dc:title>|) {
unless (m|</dc:title>|) {
while (<PDF>) {
last if (m|</dc:title>|);
next unless (m|<rdf:li.+>(.+)</rdf:li>|);
$rdftitle = $1;
}
}
}
if (m|\/Producer\s*\(([^\)]+)\)|) {
$producer = $1;
} elsif (m|\/Producer\s*<([^\)]+)>|) {
# UTF-8 ascii hex
$producer = utf8hex_undo ($1);
} elsif (m|<pdf:Producer>(.+)</pdf:Producer>|) {
$pdfproducer = $1;
}
# xap: Adobe Extensible Authoring and Publishing (early name, 5.0)
# xmp: Adobe Extensible Metadata Platform (final name)
if (m|<x[am]p:CreatorTool>(.+)<\/x[am]p:CreatorTool>|) {
$creatortool = $1;
}
if (m|<pdfx:PTEX|) {
# <pdfx:PTEX.Fullbanner>This is pdfTeX...</pdfx:PTEX.Fullbanner>
$ptex = 1;
}
if (m|\(Mac OS.+Quartz PDFContext\)|) {
# (Mac OS X 10.6.2 Quartz PDFContext) [producer indirection]
$quartzpdf = 1;
} elsif (m|\(TeX\)|) {
# (TeX) [creator indirection]
$tex = 1;
} elsif (m|% created by pdfMachine|) {
# tool doesn't bother to create any metadata whatsoever...
$pdfmachine = 1;
}
if (!$cmrfont && m|(\/BaseFont\s*\/\w+\+[Cc][Mm][Rr]\d+)|) {
# /BaseFont/EGYAWT+CMR8
$pdf_tools{'cmr fonts'}++;
$cmrfont = 1;
} elsif (!$texfont && m|/BaseFont\s*/\w+\+([Cc][Mm]\w\w\d+)|) {
$pdf_tools{'tex fonts'}++;
$texfont = $1;
}
}
close (PDF);
# undo any UTF-8 in ascii (literally "\376\377\000P\000r\000o...")
$title = utf8ascii_undo ($title) if ($title);
$creator = utf8ascii_undo ($creator) if ($creator);
$producer = utf8ascii_undo ($producer) if ($producer);
$creatortool = utf8ascii_undo ($creatortool) if ($creatortool);
$rdftitle = utf8ascii_undo ($rdftitle) if ($rdftitle);
$pdfproducer = utf8ascii_undo ($pdfproducer) if ($pdfproducer);
# undo any UTF-8 in binary
$title = utf8bin_undo ($title) if ($title);
$creator = utf8bin_undo ($creator) if ($creator);
$producer = utf8bin_undo ($producer) if ($producer);
$creatortool = utf8bin_undo ($creatortool) if ($creatortool);
$rdftitle = utf8bin_undo ($rdftitle) if ($rdftitle);
$pdfproducer = utf8bin_undo ($pdfproducer) if ($pdfproducer);
# undo any UTF-8 in binary (reversed)
$title = utf8revbin_undo ($title) if ($title);
$creator = utf8revbin_undo ($creator) if ($creator);
$producer = utf8revbin_undo ($producer) if ($producer);
$creatortool = utf8revbin_undo ($creatortool) if ($creatortool);
$rdftitle = utf8revbin_undo ($rdftitle) if ($rdftitle);
$pdfproducer = utf8revbin_undo ($pdfproducer) if ($pdfproducer);
$title = $rdftitle if (!$title && $rdftitle);
# Word
if ($creator =~ /Microsoft.+Word/) {
# Mac OS Quartz PDFContext, doPDF
$pdf_tools{'word in creator'}++;
$app = 'word';
} elsif ($title =~ /Microsoft Word \-/) {
# ps->pdf w/ gs, distiller
# often doc name in title after '-' (but not always)
$pdf_tools{'gs, distiller'}++;
$app = 'word';
} elsif ($title =~ /Proceedings Template \- WORD/i) {
$pdf_tools{'template'}++;
$app = 'word';
} elsif ($creator =~ /easyPDF/) {
# BCL easyPDF
$pdf_tools{'easyPDF'}++;
$app = 'word';
} elsif ($creator =~ /PDFCreator/) {
$pdf_tools{'PDFCreator'}++;
$app = 'word';
} elsif ($creator =~ /PDFMaker.+Word/) {
$pdf_tools{'PDFMaker'}++;
$app = 'word';
} elsif ($creator =~ /Sonic PDF/) {
$pdf_tools{'sonic pdf'}++;
$app = 'word';
} elsif ($creatortool =~ /Word/) {
# Adobe XMP metadata
$pdf_tools{'Acrobat PDFMaker'}++;
$app = 'word';
} elsif ($producer =~ /freepdfconvert|deskPDF|ReportLab|PDF reDirect/) {
$pdf_tools{'misc pdf tools'}++;
$app = 'word';
# } elsif ($creator =~ /\000M\000i\000c\000r\000o\000s\000o\000f\000t.+\000W\000o\000r\000d/i) {
# UTF-8 binary
# $pdf_tools{'Word (UTF-8)'}++;
# $app = 'word';
} elsif ($pdfmachine) {
$pdf_tools{'pdfmachine'}++;
$app = 'word';
} elsif ($title =~ /\.docx?$/i) {
# Amyuni puts the filename in the title
$pdf_tools{'doc(x) extension'}++;
$app = 'word';
}
if ($app) {
push (@allapps, $app);
$app = '';
# never seen this happen, but let's sanity check...
if ($cmrfont) {
print "$banal_filename: Warning: CMR font in Word doc?\n";
$pdf_tools{'** cmrfont in word doc'}++;
}
}
# TeX
if ($creator =~ /TeX/) {
$pdf_tools{'tex in creator'}++;
$app = 'tex';
} elsif ($creatortool =~ /(MiK)?TeX/) {
$pdf_tools{'(mik)tex in creatortool'}++;
$app = 'tex';
} elsif ($creator =~ /dvips/) {
$pdf_tools{'dvips in creator'}++;
$app = 'tex';
} elsif ($producer =~ /dvips/) {
$pdf_tools{'dvips in producer'}++;
$app = 'tex';
} elsif ($producer =~ /PrimoPDF/ && $title =~ /\.dvi$/) {
$pdf_tools{'primopdf'}++;
$app = 'tex';
} elsif (($creator =~ /gnuplot/) && ($producer =~ /Ghostscript|Distiller/)) {
# highly likely a tex document
$pdf_tools{'gnuplot + gs|dist'}++;
$app = 'tex';
} elsif ($producer =~ /Ghostscript|PDFContext|pstopdf|AntennaHouse PDF/ && !$creator && !$title) {
# just a producer tag, no other InfoDict metadata...
# have yet to see a Word doc that didn't like InfoDict metadata
$pdf_tools{'only producer'}++;
$app = 'tex';
} elsif ($indirect && $quartzpdf && $tex) {
if ($creator || $producer) {
print "$banal_filename: Warning: direct and indirect InfoDict entries\n";
}
$pdf_tools{'tex quartzpdf'}++;
$app = 'tex';
} elsif ($creatortool =~ /gnuplot/ && !$creator && !$producer && !$title) {
$pdf_tools{'only gnuplot'}++;
$app = 'tex';
} elsif ($ptex) {
$pdf_tools{'pdftex in pdfx'}++;
$app = 'tex';
} elsif ($producer =~ /Ghostscript/ && $title =~ /\.pdf$/) {
$pdf_tools{'gs ps to pdf'}++;
$app = 'tex';
} elsif ($cmrfont) {
$pdf_tools{'cmrfont'}++;
$app = 'tex';
}
if ($app) {
push (@allapps, $app);
$app = '';
}
# OpenOffice
if ($producer =~ /OpenOffice/) {
$pdf_tools{'open office'}++;
push (@allapps, 'openoffice');
}
if ($creator =~ /Interleaf/) {
$pdf_tools{'interleaf + distiller'}++;
push (@allapps, 'interleaf');
}
# FrameMaker (!)
if ($creator =~ /FrameMaker/) {
$pdf_tools{'frame'}++;
push (@allapps, 'framemaker');
}
# sanity check that we haven't matched more than one application,
# or whether we didn't match anything...
if (scalar (@allapps) > 1) {
print STDERR "$banal_filename: Error: multiple apps inferred: @allapps\n";
$app = 'unknown';
} elsif (scalar (@allapps) < 1) {
print STDERR "$banal_filename: Warning: failed to infer document app, using 'unknown'\n";
# print STDERR "$banal_filename: Creator: $creator\n" if ($creator);
# print STDERR "$banal_filename: Title: $title\n" if ($title);
# print STDERR "$banal_filename: Producer: $producer\n" if ($producer);
# print STDERR "$banal_filename: CreatorTool: $creatortool\n" if ($creatortool);
# print STDERR "$banal_filename: RDFTitle: $rdftitle\n" if ($rdftitle);
# print STDERR "$banal_filename: PDFProducer: $pdfproducer\n" if ($pdfproducer);
# print STDERR "$banal_filename: cmrfont\n" if ($cmrfont);
# print STDERR "$banal_filename: texfont $texfont\n" if ($texfont);
$app = 'unknown';
} else {
$app = $allapps[0];
}
# $pdf_tools{$app}++;
$doc->{app} = $app;
if ($debug_docapp) {
print STDERR "$banal_filename: Creator: $creator\n" if ($creator);
print STDERR "$banal_filename: Title: $title\n" if ($title);
print STDERR "$banal_filename: Producer: $producer\n" if ($producer);
print STDERR "$banal_filename: CreatorTool: $creatortool\n" if ($creatortool);
print STDERR "$banal_filename: RDFTitle: $rdftitle\n" if ($rdftitle);
print STDERR "$banal_filename: PDFProducer: $pdfproducer\n" if ($pdfproducer);
print STDERR "$banal_filename: cmrfont\n" if ($cmrfont);
print STDERR "$banal_filename: texfont $texfont\n" if ($texfont);
foreach $t (keys %pdf_tools) {
print "$t: $pdf_tools{$t}\n";
}
}
return;
}
sub calc_page_leading ($) {
my ($page) = @_;
# my ($mode) = modevalkey ($page->{pagedata}->{segdata}->{leads});
my ($mode, $segs);
$segs = $page->{pagedata}->{segdata_byfont}->{$page->{pagedata}->{bodyfont}->{id}};
$mode = modevalkey ($segs->{leads});
$count = $segs->{leads}->{$mode} +
$segs->{leads}->{$mode - 1} +
$segs->{leads}->{$mode + 1};
if ($count <= 0) {
$page->{pagespec}->{lead} = 0;
return;
}
if ($banal_leading_policy eq 'mode') {
print "using leading policy 'mode'\n" if ($debug_leading);
$lead = $mode * $p2h_to_points;
$lead *= 10;
$lead = int ($lead + 0.5);
$lead /= 10;
print "leading: $lead\n" if ($debug_leading);
$page->{pagespec}->{lead} = $lead;
return;
}
if ($debug_leading) {
# leading histogram
$ll = $segs->{leads};
foreach $k (sort { $a <=> $b } keys %$ll) {
my ($l) = int (($k * $p2h_to_points * 10) + 0.5);
$l /= 10;
print "$l ($segs->{leads}->{$k}) ";
}
print "\n";
}
$wsum = $mode * ($segs->{leads}->{$mode} / $count);
$wsum += ($mode - 1) * ($segs->{leads}->{$mode - 1} / $count);
$wsum += ($mode + 1) * ($segs->{leads}->{$mode + 1} / $count);
$lead = $wsum * $p2h_to_points;
$lead *= 10;
$lead = int ($lead + 0.5);
$lead /= 10;
$page->{pagespec}->{lead} = $lead;
# print Dumper ($segs->{leads});
}
sub calc_page_columns ($) {
my ($page) = @_;
my ($pagew);
my ($segs, $maxw, $colw, $ncols);
# use estimated width of text region as base
$pagew = $page->{pagespec}->{textbb}->{width};
# use the maximum width segment in the body font to estimate
# column width
$segs = $page->{pagedata}->{segdata_byfont}->{$page->{pagedata}->{bodyfont}->{id}};
# $maxw = maxkey ($segs->{widths});
$modew = modevalkey ($segs->{widths});
$colw = $modew / $p2h_per_inch;
if ($colw >= ($pagew / 2.0)) {
$ncols = 1;
} elsif (($colw < ($pagew / 2.0)) && ($colw >= ($pagew / 3.0))) {
$ncols = 2;
} elsif (($colw < ($pagew / 3.0)) && ($colw >= ($pagew / 4.0))) {
$ncols = 3;
} elsif (($colw < ($pagew / 4.0)) && ($colw >= ($pagew / 5.0))) {
$ncols = 4;
} elsif (($colw < ($pagew / 5.0)) && ($colw >= ($pagew / 6.0))) {
$ncols = 5;
} elsif (($colw < ($pagew / 6.0)) && ($colw >= ($pagew / 7.0))) {
$ncols = 6;
} elsif (($colw < ($pagew / 7.0)) && ($colw >= ($pagew / 8.0))) {
$ncols = 7;
} elsif ($page->{pagespec}->{density} < $banal_min_density) {
$ncols = 1;
} else {
my ($num) = $page->{num};
# print Dumper ($segs->{widths});
printf ("$banal_filename: Error (page $num): Unknown number of columns: width of typical text segment %.2fin, page %.2fin.\n", $colw, $pagew);
$ncols = 1;
}
$page->{pagedata}->{ncols} = $ncols;
$page->{pagespec}->{ncols} = $ncols;
}
sub calc_page_text_region ($$) {
my ($page, $segdata) = @_;
my ($minw, $maxw, $minh, $maxh);
my ($segs_minw, $segs_maxw);
$segs_minw = $segdata->{lefts};
$segs_maxw = $segdata->{rights};
# find the minimum left position among segments (must be
# multiple segments with that position to skip outliers)
$minw = 8 * $p2h_per_inch;
foreach $s (keys %$segs_minw) {
$minw = $s if (($s < $minw) && ($segs_minw->{$s} > 3));
}
# all consistency bets are off with low density pages
$minw = minkey ($segs_minw) if ($minw > 4 * $p2h_per_inch);
# find the maximum right position among segments (must be
# multiple segments with that position to skip outliers)
$maxw = 0;
foreach $s (keys %$segs_maxw) {
$maxw = $s if (($s > $maxw) && ($segs_maxw->{$s} >= 2));
}
# print "tmpw $tmpw maxw $maxw\n";
# if ($maxw < 600) {
# print Dumper ($segs_maxw);
# }
# unjustified text may not have multiple segments with the same
# max right position...fall back to just using the max right position
$maxw = maxkey ($segs_maxw) if ($maxw < $minw);
$maxw = $minw + minkey ($segdata->{widths}) if (!defined $maxw);
$maxw = $minw if ($maxw < $minw);
$minh = minkey ($segdata->{tops});
$maxh = maxkey ($segdata->{bots});
$page->{pagedata}->{textbb} = {
top => $minh,
left => $minw,
width => ($maxw - $minw),
height => ($maxh - $minh),
};
# print "$minw $maxw\n";
# print Dumper ($page->{pagedata}->{textbb});
$page->{pagespec}->{textbb} = {
top => $minh / $p2h_per_inch,
left => $minw / $p2h_per_inch,
width => ($maxw - $minw) / $p2h_per_inch,
height => ($maxh - $minh) / $p2h_per_inch,
};
return 1;
}
sub calc_page_density ($) {
my ($page) = @_;
my ($bfont, $density);
$bfont = $page->{pagedata}->{bodyfont}->{id};
$density = maxval ($page->{pagedata}->{segdata_byfont}->{$bfont}->{byfont});
$page->{pagespec}->{density} = $density;
}
sub calc_doc_body_font ($) {
my ($doc) = @_;
my ($fonts) = {};
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
$fonts->{$page->{pagespec}->{bodyfont}}++;
}
$doc->{pagespec}->{bodyfont} = modevalkey ($fonts);
}
sub calc_doc_leading ($) {
my ($doc) = @_;
my ($leads) = {};
my ($lmode, $page);
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
$leads->{$page->{pagespec}->{lead}}++;
}
$lmode = modevalkey ($leads);
# $use_raw_leading = 1;
if (!defined $use_raw_leading) {
# print "mode: $lmode\n";
# print "pages w mode: $leads->{$lmode}\n";
if ($leads->{$lmode} >= $doc->{npages} / 2) {
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
next if ($page->{pagespec}->{lead} == $lmode);
# print "abs diff: ", $lmode - $page->{pagespec}->{lead}, "\n";
if (abs ($lmode - $page->{pagespec}->{lead}) < 0.2) {
# print "setting to ", $lmode, "\n";
$page->{pagespec}->{lead} = $lmode;
}
}
}
}
if ($debug_leading) {
print "entire doc\n";
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
$segs = $page->{pagedata}->{segdata_byfont}->{$page->{pagedata}->{bodyfont}->{id}};
$leads = $segs->{leads};
foreach $k (keys %$leads) {
$doc_leads{$k} += $segs->{leads}->{$k};
$lcount++;
}
}
foreach $k (sort { $a <=> $b } keys %doc_leads) {
my ($l) = int (($k * $p2h_to_points * 10) + 0.5);
$l /= 10;
print "$l ($doc_leads{$k}) ";
}
print "\n";
{
$mode = modevalkey (\%doc_leads);
print "mvk $mode\n";
$count = $doc_leads{$mode} +
$doc_leads{$mode - 1} +
$doc_leads{$mode + 1};
$wsum = $mode * ($doc_leads{$mode} / $count);
print "b: ", $wsum, "\n";
$wsum += ($mode - 1) * ($doc_leads{$mode - 1} / $count);
$wsum += ($mode + 1) * ($doc_leads{$mode + 1} / $count);
$lead = $wsum * $p2h_to_points;
print "c: ", $lead, "\n";
$lead *= 10;
print "d: ", $lead, "\n";
$lead = int ($lead + 0.5);
$lead /= 10;
print "lead: $lead\n";
}
}
$doc->{pagespec}->{lead} = $lmode;
}
sub calc_doc_text_region ($) {
my ($doc) = @_;
my ($page, $maxw, $maxh, $minl, $mint, $rmarg, $bmarg);
$page = $doc->{pages}->{1};
$maxw = $page->{pagespec}->{textbb}->{width};
$maxh = $page->{pagespec}->{textbb}->{height};
$minl = $page->{pagespec}->{textbb}->{left};
$mint = $page->{pagespec}->{textbb}->{top};
for $i (2..$doc->{npages}) {
next if ($page->{density} < $banal_min_density);
$page = $doc->{pages}->{$i};
$maxw = $page->{pagespec}->{textbb}->{width} if
($page->{pagespec}->{textbb}->{width} > $maxw);
$maxh = $page->{pagespec}->{textbb}->{height} if
($page->{pagespec}->{textbb}->{height} > $maxh);
$minl = $page->{pagespec}->{textbb}->{left} if
($page->{pagespec}->{textbb}->{left} < $minl);
$mint = $page->{pagespec}->{textbb}->{top} if
($page->{pagespec}->{textbb}->{top} < $mint);
}
$doc->{textbb}->{width} = $maxw;
$doc->{textbb}->{height} = $maxh;
$doc->{textbb}->{left} = $minl;
$doc->{textbb}->{top} = $mint;
$rmarg = $doc->{pagespec}->{paperbb}->{width} - ($doc->{textbb}->{width} + $doc->{textbb}->{left});
$bmarg = $doc->{pagespec}->{paperbb}->{height} - ($doc->{textbb}->{height} + $doc->{textbb}->{top});
if ($rmarg < 0) {
print "r MARGIN\n";
}
if ($bmarg < 0) {
print "b MARGIN\n";
}
$doc->{textbb}->{rmarg} = $rmarg;
$doc->{textbb}->{bmarg} = $bmarg;
}
sub calc_doc_page_types ($) {
my ($doc) = @_;
my ($page, $font, $type);
$font = $doc->{pagespec}->{bodyfont};
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
$type = 'body';
if ($i == 1 && $page->{pagespec}->{density} < 3000) {
$type = 'cover';
} elsif ($page->{pagespec}->{bodyfont} < $font) {
if (($doc->{npages} - $i) < ($doc->{npages} / 3)) {
$type = 'bib';
}
} elsif ($page->{pagespec}->{density} < $banal_min_density) {
if ($i == $doc->{npages}) {
$type = 'bib';
} else {
$type = 'figure';
}
}
$page->{pagespec}->{type} = $type;
}
}
sub calc_doc_columns ($) {
my ($doc) = @_;
my ($page);
my ($cols) = {};
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
$cols->{$page->{pagespec}->{ncols}}++;
}
# number of columns on greatest number of pages
$doc->{ncols} = modevalkey ($cols);
}
sub p2h_font_to_font_size ($) {
my ($font) = @_;
my ($pt);
if ($font->{family} eq 'Times'
|| $font->{family} eq 'Helvetica'
|| $font->{family} eq 'Courier'
|| $font->{family} eq 'Symbol') {
$pt = ($font->{size} + 3) / $zoom;
} else {
print STDERR "$banal_filename: Error: Unknown font family.\n";
# print Dumper ($font);
$pt = 0;
}
return $pt;
}
sub p2h_font_bug ($) {
my ($doc) = @_;
return 1 if ($doc->{pagespec}->{bodyfont} <= 0);
return 0;
}
sub p2h_serious_font_bug ($) {
my ($doc) = @_;
return 0 if (!p2h_font_bug ($doc));
return 1 if ($doc->{textbb}->{width} == 0 ||
$doc->{textbb}->{height} == 0);
return 0;
}
sub report_verbose ($) {
my ($doc) = @_;
my ($page) = $doc->{pages}->{1};
print $file, "\n";
if (p2h_font_bug ($doc)) {
print STDERR $file, "\n";
print STDERR "$banal_filename: Error: pdftohtml encountered font problems...some info likely bogus.\n";
}
printf ("Paper size: %.2fin x %.2fin\n", $doc->{pagespec}->{paperbb}->{width}, $doc->{pagespec}->{paperbb}->{height});
printf ("Text region: %.2fin x %.2fin\n", $doc->{textbb}->{width},
$doc->{textbb}->{height});
printf ("Margins: %.2fin x %.2fin x %.2fin x %.2fin (l/r/t/b)\n",
$doc->{textbb}->{left},
$doc->{textbb}->{rmarg},
$doc->{textbb}->{top},
$doc->{textbb}->{bmarg});
printf ("Body font size: %.2fpt", $doc->{pagespec}->{bodyfont});
if (p2h_font_bug ($doc)) {
print " (bogus)";
}
print "\n";
printf ("Leading: %.1fpt\n", $doc->{pagespec}->{lead});
print "Columns: ", $page->{pagespec}->{ncols}, "\n";
print "Pages: ", $doc->{npages}, "\n";
print "App: ", $doc->{app}, "\n";
print "\n";
for $i (1..$doc->{npages}) {
$page = $doc->{pages}->{$i};
print "Page $page->{num}:\n";
printf (" text region: %.2fin x %.2fin\n", $page->{pagespec}->{textbb}->{width}, $page->{pagespec}->{textbb}->{height});
$left_i = $page->{pagespec}->{textbb}->{left};
$right_i = $page->{pagespec}->{paperbb}->{width} -