This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstoimg
executable file
·1559 lines (1313 loc) · 42.9 KB
/
pstoimg
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 -w
##############################################################################
# $Id: pstoimg,v 1.4 2005/07/15 13:37:56 cnh Exp $
#
# pstoimg
#
# Accompanies LaTeX2HTML
#
# Script to convert an arbitrary PostScript image to a cropped GIF or PNG
# image suitable for incorporation into HTML documents as inlined images
# to be viewed with WWW browsers.
#
# This software is provided as is without any guarantee.
#
##############################################################################
#
# $Log: pstoimg,v $
# Revision 1.4 2005/07/15 13:37:56 cnh
# Seems to work on faulks as well now - yeah (I hope).
# Only took staying up all night and some 5000+ attempts.
# Not sure why nobody fixed this before :-)!
# For my next trick ---- protex!!!
#
# Revision 1.3 2005/07/15 13:29:25 cnh
# Tidying paths to make it work elsewhere - maybe?
#
# Revision 1.2 2005/07/15 13:20:56 cnh
# Slight change to the fix - plus relative path in .latex2html-init
#
# Revision 1.1 2005/07/15 13:07:23 cnh
# More cunning tricks to get rid of black lines
# pnmcrop is junk!
#
# Revision 1.16 2001/10/25 10:16:12 RRM
# -- make sure that $PNMCROPOPT is declared before -sides
#
# Revision 1.15 2001/08/21 10:48:59 RRM
# -- corrects errors in implementing $PNMCROPOPT
#
# Revision 1.14 2001/08/20 08:51:20 RRM
# -- implement -sides for the arguments to $PNMCROP
# This is needed for v9.12+ of pnmcrop .
# -- when shaving fails, don't try to copy or rename
#
# Revision 1.13 2001/03/27 09:27:38 RRM
# -- inserted missing '$' for $edge .
#
# Revision 1.12 2001/03/25 02:05:14 RRM
# -- implement the temporary hack to pnmcrop, using -black to help detect
# the correct color for cropping-bars; only workd with black bars.
# A complete fix will be available with Netpbm v9.12 .
#
# Revision 1.11 1999/10/25 21:18:22 MRO
#
# -- added more configure options (Jens' suggestions)
# -- fixed bug in regexp range reported by Achim Haertel
# -- fixed old references in documentation (related to mail list/archive)
#
# Revision 1.10 1999/10/06 22:04:13 MRO
#
# -- texexpand: latex2html calls texexpand with the -out option instead of
# output redirection: this is safer on non-UNIX platforms
# -- pstoimg: now there's no default cropping (useful for standalone
# conversions). latex2html was changes appropriately
# -- minor cleanups in latex2html script and documentation
#
# Revision 1.9 1999/09/14 22:02:02 MRO
#
# -- numerous cleanups, no new features
#
# Revision 1.8 1999/07/19 09:51:00 RRM
# -- added -aaliastext switch, for easier way to specify anti-aliased
# font characters, without also anti-aliasing other graphics objects.
#
# Revision 1.7 1999/06/24 07:28:59 MRO
#
#
# -- removed L2HMODULE
# -- fixed processing of -info switch
# -- changed option order for dvips on win32 (thanks JCL)
# -- bumped version to 99.2a8
#
# Revision 1.6 1999/06/06 14:24:50 MRO
#
#
# -- many cleanups wrt. to TeXlive
# -- changed $* to /m as far as possible. $* is deprecated in perl5, all
# occurrences should be removed.
#
# Revision 1.5 1999/06/04 20:14:25 MRO
#
#
# -- Reworked option parsing completely. Should behave much the same as before,
# options with -no_* work just like before.
# -- Changed $NOFORK to $CAN_FORK and inverted the logic.
# -- Small debugging enhancement in pstoimg
#
# Revision 1.4 1999/06/04 15:30:15 MRO
#
#
# -- fixed errors introduced by cleaning up TMP*
# -- made pstoimg -quiet really quiet
# -- pstoimg -debug now saves intermediate result files
# -- several fixes for OS/2
#
# Revision 1.3 1999/06/01 06:55:35 MRO
#
#
# - fixed small bug in L2hos/*
# - added some test_mode related output to latex2html
# - improved documentation
# - fixed small bug in pstoimg wrt. OS2
#
# Revision 1.2 1999/05/17 21:30:59 MRO
#
#
# -- make texexpand warning-free and start making it use strict
# compliant
#
# Revision 1.1 1999/05/11 06:10:00 MRO
#
#
# - merged config stuff, did first tries on Linux. Simple document
# passes! More test required, have to ger rid of Warnings in texexpand
#
# Revision 1.18 1999/05/05 19:47:03 MRO
#
#
# - many cosmetic changes
# - final backup before merge
#
# Revision 1.17 1999/03/15 23:00:53 MRO
#
#
# - moved L2hos modules to top level directory, so that no dir-
# delimiter is necessary in the @INC-statement.
# - changed strategy for "shave": Do not rely on STDERR redirection any
# more (caused problems on at least Win32)
#
# Revision 1.16 1999/02/14 23:44:34 MRO
#
#
# -- first attempt to fix Win32 problems
#
# Revision 1.15 1999/02/11 00:18:29 MRO
#
#
# -- cleaned up warppers, TeXlive stuff and Makefile
#
# Revision 1.14 1999/02/10 01:37:12 MRO
#
#
# -- changed os-dependency structure again - now neat OO modules are
# used: portable, extensible, neat!
# -- some minor cleanups and bugfixes
#
# Revision 1.13 1998/12/07 23:19:58 MRO
#
#
# -- added POD documentation to pstoimg and did a general cleanup
# -- some finetuning of config procedure and modules
#
# Revision 1.12 1998/10/31 14:13:05 MRO
# -- changed OS-dependent module loading strategy: Modules are now located in
# different (OS-specific) directories nut have the same name: Easier to
# maintain and cleaner code
# -- Cleaned up config procedure
# -- Extended makefile functionality
#
# Revision 1.11 1998/08/09 20:45:20 MRO
# -- some cleanup
#
# Revision 1.10 1998/06/14 14:10:38 latex2html
# -- Started to implement TeXlive configuration and better OS specific
# handling (Batch files) (Marek)
#
# Revision 1.9 1998/06/07 22:35:24 latex2html
# -- included things I learned from the Win95 port to config procedure:
# GS_LIB, Win32 module calls, directory separator stuff, ... (Marek)
#
# Revision 1.8 1998/06/01 12:57:56 latex2html
# -- Cleanup and cosmetics.
#
# Revision 1.7 1998/05/14 22:27:37 latex2html
# -- more work on config procedure (Makefile, GS_LIB)
# -- tested pstoimg in 98.1 environment successfully on Linux
#
# Revision 1.6 1998/05/06 22:31:09 latex2html
# -- Enhancements to the config procedure: Added a "generic" target
# in the Makefile for the TeXlive CD (not perfect yet)
# -- included test for kpsewhich / Web2C
# -- included latest stuff from Override.pm into os_*.pm
#
# Revision 1.5 1998/04/28 22:18:11 latex2html
# - The platform specific stuff is now kept in a separate perl module. This
# does not introduce significant overhead and enhances maintainability.
#
# Revision 1.4 1998/03/19 23:38:06 latex2html
# -- made pstoimg plug-in compatible with old one (touchwood!)
# -- cleaned up, added some comments
# -- inserted version information output
# -- incorporated patches to make OS/2 run better (thanks Uli)
# -- updated Makefile: make, make test, make install should work now
#
# Revision 1.3 1998/03/11 23:44:00 latex2html
# -- cleaned up config.pl and reworked dvips checks
# -- got pstoimg.pin up to par with the regular pstoimg
# -- cosmetic changes
# -- runs now under Win95 with Fabrice Popineau's Win32 tools (gs, TeX,...)
#
# Revision 1.2 1998/03/02 23:38:40 latex2html
# Reworked configuration procedure substantially. Fixed some killing bugs.
# Should now run on Win32, too.
# The file prefs.pm contains user-configurable stuff for DOS platforms.
# UNIX users can override the settings with the configure utility (preferred).
#
# Revision 1.1 1998/02/14 19:31:55 latex2html
# Preliminary checkin of configuration procedure
#
# (end CVS log)
###############################################################################
# This file has been automatically generated by build.pl from pstoimg.pin
# Do not edit this file as your changes will be lost when reconfiguring.
# If you want to supply patches, please apply them to pstoimg.pin and send
# the diff relative to the original pstoimg.pin. Thank you.
=head1 NAME
pstoimg - Convert a PostScript file to a bitmap image using
Ghostscript and the Netpbm utilities
=cut
use 5.003;
use strict;
#use diagnostics;
use vars qw(*SAVEERR $LATEX2HTMLDIR $SCRIPT);
# This variable points to the DIRECTORY where the latex2html files
# can be found.
use Getopt::Long;
# see below for a description of the environment
BEGIN {
# print "scanning for l2hdir\n";
if($ENV{LATEX2HTMLDIR}) {
$LATEX2HTMLDIR = $ENV{LATEX2HTMLDIR};
} else {
$ENV{LATEX2HTMLDIR} = $LATEX2HTMLDIR = '/usr/share/latex2html';
}
if(-d $LATEX2HTMLDIR) {
push(@INC,$LATEX2HTMLDIR);
} else {
die qq{Fatal: Directory "$LATEX2HTMLDIR" does not exist.\n};
}
}
use L2hos; # load OS-specific stuff
my $RELEASE = '2002-2-1';
my ($VERSION) = q$Revision: 1.4 $ =~ /:\s*(\S+)/;
$| = 1; # unbuffer STDOUT
my $dd = L2hos->dd; # Directory delimiter
my $prompt;
($prompt = $0) =~ s|^.*[/\\]||;
# Configuration as determined by "configure"
#
# Ghostscript
my $GS = '/usr/bin/gs';
my $GSDEVICE = 'pnmraw';
my $GSALIASDEVICE = 'ppmraw';
# Supported format(s)
my @IMAGE_TYPES = qw(png gif);
# Netpbm
my $PNMCROP = '/usr/bin/pnmcrop -verbose ';
my $PNMCROPOPT = '';
$PNMCROPOPT = '-sides';
my $PPMQUANT = '/usr/bin/ppmquant';
my $PNMFLIP = '/usr/bin/pnmflip';
my $PNMCAT = '/usr/bin/pnmcat';
my $PNMFILE = '/usr/bin/pnmfile';
my $PBMMAKE = '/usr/bin/pbmmake';
# GIF support
my $PPMTOGIF = '/usr/bin/ppmtogif';
# PNG support
my $PNMTOPNG = '/usr/bin/pnmtopng';
# Temporary diskspace
my $def_tmp = '/tmp'; # Space for temporary files
# Some lengths used by dvips
# MRO: Is this true for all runs of dvips?
my $PAGE_HEIGHT = 841.889; # dvips page height, in pts.
my $PAGE_WIDTH = 595.275; # dvips page width, in pts.
my $PAGE_HMARGIN = 72; # dvips margin: 1 inch = 72pt
my $PAGE_VMARGIN = 72; # dvips margin: 1 inch = 72pt
# The color to be made transparent
my $trans_color = '#ffffff';
###############################################################################
# Default settings
# Environment overrides defaults, command line options override everything
unless(@ARGV) {
print_help();
exit 0;
}
=head1 SYNOPSIS
B<pstoimg> B<-help> | B<-version>
B<pstoimg>
S<[ B<-antialias> ]>
S<[ B<-aaliastext> ]>
S<[ B<-center> I<num> ]>
S<[ B<-color> I<num> ]>
S<[ B<-crop> I<code> ]>
S<[ B<-debug> ]>
S<[ B<-density> I<num>]>
S<[ B<-depth> I<num> ]>
S<[ B<-discard> ]>
S<[ B<-flip> I<code> ]>
S<[ B<-geometry> I<X>xI<Y> ]>
S<[ B<-interlaced> ]>
S<[ B<-margins> I<X>,I<Y> ]>
S<[ B<-multipage> ]>
S<[ B<-out> I<file> ]>
S<[ B<-quiet> ]>
S<[ B<-rightjustify> I<num> ]>
S<[ B<-scale> I<num> ]>
S<[ B<-tmp> I<path> ]>
S<[ B<-topjustify> [B<x>]I<num> ]>
S<[ B<-transparent> ]>
S<[ B<-type> I<type> ]>
S<[ B<-shoreup> I<num>[B<d>] ]>
S<[ B<-white> ]>
I<file>
S<[ I<file2> ... ]>
=cut
my %opt = ();
unless(&GetOptions(\%opt, qw(-help -version -debug -discard -antialias -aaliastext
-multipage -type=s -gif -png -out=s -depth=i -color=i -flip=s -density=i
-scale=f -geometry=s -margins=s -crop=s -transparent -interlaced
-rightjustify=i -center=i -topjustify=s -shoreup=s -tmp=s -white -quiet))) {
print_usage("$prompt: Error: Invalid option(s) specified.");
exit 1;
}
=head1 OPTIONS
The command line options may be abbreviated to the shortest unique
prefix.
=over 4
=item B<-help>
Show this help page and exit.
=cut
if($opt{help}) {
print_help();
exit 0;
}
=item B<-version>
Show the release and version of pstoimg and exit.
=cut
if($opt{version}) {
print_version();
exit 0;
}
banner() unless($opt{quiet});
=item B<-antialias>
Use Ghostscript's anti-aliasing feature for rendering "softer" images.
This applies to lines and edges of polygonal and oval or circular shapes.
Only valid if Ghostscipt 4.03 or higher is installed.
=item B<-aaliastext>
Use Ghostscript's anti-aliasing feature for "smoother" font characters,
without the jagged edges. Similar to B<-antialias> for graphic components.
Only valid if Ghostscipt 4.03 or higher is installed.
=item B<-center> I<num>
Add the appropriate amount of whitespace to the left of the image so
that the image appears to be centered in a total width of I<num> pixels.
=cut
my $CENTER = 0; # No centering by default
if($opt{center}) {
$CENTER = $opt{center};
die <<"EOF" unless ($CENTER =~ /^\d+$/ && $CENTER > 0);
$prompt: Error: Illegal width for -center specified: "$CENTER"
Value must be a positive integer.
EOF
}
=item B<-crop> I<code>
Crop the bitmap from the given directions. I<code> may be a string of
several cropping instructions, which are executed strictly in the given
order. Possible values are: B<h> (horizontal, i.e. crop top and
bottom), B<v> (vertical), B<tblr> (top, bottom, left, right) and B<a> (all
directions). A special case is B<s>: "shave" the image at the bottom, but only
if a single line of whitespace exists.
=cut
my $EXTRA_CROP = '';
if($opt{crop}) {
$EXTRA_CROP = lc($opt{crop});
die <<"EOF" unless ( $EXTRA_CROP =~ /^([vhtblras]+)$/i );
$prompt: Error: Illegal crop specified: "$EXTRA_CROP"
Crop must be h, v, t, b, l, r, a, s or combination
EOF
}
=item B<-debug>
Turn on debugging output. This can get rather verbose. Any intermediate
files generated are not removed to help debugging.
=cut
if($ENV{DEBUG}) {
$opt{debug} = 1;
}
=item B<-density> I<num>
The density (resolution) in DPI in which to render the bitmap. The default
is 72.
=cut
my $DENSITY = 72;
if($opt{density}) {
$DENSITY = $opt{density};
}
elsif($ENV{DENSITY}) {
$DENSITY = $ENV{DENSITY};
}
die <<"EOF" unless $DENSITY =~ /^\d+$/;
$prompt: Error: Illegal density specified: "$DENSITY"
Density must be an integer value. Default is 72.
EOF
=item B<-depth> I<num> or B<-color> I<num>
Specify the color depth of the bitmap. Legal values are 1 (black & white),
8 (256 colors) and 24 (true color).
=cut
unless($opt{depth}) {
if($opt{color}) {
$opt{depth} = $opt{color};
}
elsif($ENV{DEPTH}) {
$opt{depth} = $ENV{DEPTH};
}
else {
$opt{depth} = 8;
}
}
die <<"EOF" unless $opt{depth} =~ /^(1|8|24)$/;
$prompt: Error: Illegal color depth specified: "$opt{depth}"
Depth must be either 1, 8 or 24.
EOF
=item B<-discard>
Delete the input postscript file if the conversion was successful. Setting
the environment DISCARD to a true value (as perl sees it) has the same
effect.
=cut
if($ENV{DISCARD}) {
$opt{discard} = 1;
}
=item B<-flip> I<code>
Flip all generated output bitmaps. The following codes are recognized:
lr (flip left-right), tb (flip top-bottom), xy (flip bottom/left-top/right),
r90 and ccw (rotate by 90 degrees counterclockwise), r270 and cw (rotate
90 degrees clockwise) and r180 (rotate 180 degrees).
=cut
if($opt{flip}) {
$opt{flip} = lc($opt{flip});
die <<"EOF" unless $opt{flip} =~ /^(lr|tb|xy|r90|ccw|r270|cw|r180)$/;
$prompt: Error: Illegal flip option specified: "$opt{flip}"
Flip must be one of: lr tb xy r90 ccw r270 cw r180
EOF
}
=item B<-geometry> I<X>xI<Y>
Render only this "window" of the PostScript file. If given, this option
can dramatically reduce memory requirements and speed up conversion. The
geometry is automatically detected in case of EPS files (Encapsulated
PostScript).
=cut
my $GEOMETRY = '';
if($opt{geometry}) {
$GEOMETRY = $opt{geometry};
if($GEOMETRY =~ s/-//o ) {
$EXTRA_CROP .= 'bl';
}
die <<"EOF" unless ($GEOMETRY =~ /^\d+x\d+$/i);
$prompt: Error: Illegal geometry specified: "$GEOMETRY"
Geometry must be <width>x<height>
EOF
}
=item B<-interlaced>
Generate an interlaced bitmap. Interlaced images build up from coarse to
fine as they are loaded. This option may not work on every installation
and/or bitmap type, depending of the capabilities of external programs.
=cut
my $INTERLACE = 0; # Do not make interlaced images by default
if($opt{interlaced}) {
$INTERLACE=1;
}
=item B<-margins> I<X>,I<Y>
The offset of the rectangle in the postscript file that is going to be
rendered from top/left. Can be used together with B<-geometry> to further
reduce the size of the intermediate bitmap file generated by Ghostscript.
=cut
if($opt{margins}) {
die <<"EOF" unless (($opt{margins} =~ /^(\d+),(\d+)$/));
$prompt: Error: Illegal margins specified: "$opt{margins}"
Margins must be <hmargin>,<vmargin>
EOF
$PAGE_HMARGIN = $1;
$PAGE_VMARGIN = $2;
}
=item B<-multipage>
Process a multi-page PostScript file, i.e. create an individual bitmap
for every page. The resulting files are numbered: The decimal number
(starting with 1) is appended to the basename of the PostScript input
file (or the basename of the filename specified with B<-out>), while
keeping the extension.
=item B<-out> I<file>
The file where to write the bitmap. If multiple PostScript files are
supplied on the command line, this option is ignored. The bitmap type
extension is appended automatically if I<file> does not contain a dot.
In connection with B<-multipage> I<file> is extended by the page number
as shown in this example:
-outfile foo.gif --------E<gt> foo1.gif, foo2.gif, ...
=cut
if(!$opt{out} && $ENV{OUTFILE}) {
$opt{out} = $ENV{OUTFILE};
}
=item B<-quiet>
Do not print anything except error messages.
=item B<-rightjustify> I<num>
Add the appropriate amount of whitespace to the left of the image so that
it appears to be aligned to the right in a total width of I<num> pixels.
=cut
my $RIGHT_JUSTIFY = 0; # No right justifying by default
if($opt{rightjustify}) {
$RIGHT_JUSTIFY = $opt{rightjustify};
die <<"EOF" unless ($RIGHT_JUSTIFY =~ /^\d+$/ && $RIGHT_JUSTIFY > 0);
$prompt: Error: Illegal width for -rightjustify specified: "$RIGHT_JUSTIFY"
Value must be a positive integer.
EOF
}
=item B<-scale> I<factor>
Scale the image by I<factor>. Valid choices are any numbers greater than
zero. Useful choices are numbers between 0.1 - 5.
Large numbers may generate very large intermediate files and will take
longer to process. If this option is omitted, the environment SCALE is
considered.
=cut
unless($opt{scale}) {
if($ENV{SCALE}) {
$opt{scale} = $ENV{SCALE};
}
else {
$opt{scale} = 1;
}
}
die <<"EOF" unless ($opt{scale} =~ /^[\d.e]+$/i && $opt{scale} > 0);
$prompt: Error: Illegal scale specified: "$opt{scale}"
Scale must be nonnegative float value.
EOF
=item B<-shoreup> I<num>[B<d>]
Make height and width of the bitmap(s) an exact multiple of I<num>. If
I<num> is followed by a "d", then half the extra vertical space is placed
underneath. This option is useful, if you want to have "blown-up" images
of high quality for print, but downscale them in HTML using
C<E<lt>IMG WIDTH=x HEIGHT=yE<gt>>. If the actual image is is not an
integer multiple of x,y then browsers tend to display distorted images.
=cut
my $SHORE_UP = 0; # No pixel alignment by default
if($opt{shoreup}) {
$SHORE_UP = $opt{shoreup};
die <<"EOF" unless $SHORE_UP =~ /^\d+d?$/i;
$prompt: Error: Illegal shore-up specified: "$SHORE_UP"
Value must be a positive integer, optionally followed by d
EOF
}
=item B<-tmp> I<path>
Use I<path> to store temporary files. Defaults to /tmp on this
installation. This parameter can be set by the environment B<TMP> or
B<TEMP>, too.
=cut
my $TMP = '';
if($opt{tmp}) {
$opt{tmp} =~ s|\Q$dd\E+$||; # remove trailing directory separator(s)
if(-d $opt{tmp} && -r _ && -w _) {
$TMP = $opt{tmp};
} else {
print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
}
}
if(!$TMP && ($ENV{TMP} || $ENV{TEMP})) {
($opt{tmp} = $ENV{TMP} || $ENV{TEMP}) =~ s|\Q$dd\E+$||;
if(-d $opt{tmp} && -r _ && -w _) {
$TMP = $opt{tmp};
} else {
print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
}
}
if(!$TMP && -d $def_tmp && -r _ && -w _) {
$TMP = $def_tmp;
}
print "$prompt: Temporary directory is $TMP\n" if($opt{debug});
=item B<-topjustify> [B<x>]I<num>
Add padding whitespace to the image so that it gets a defined height.
If an integer value is given, it defines the total height. The whitespace
is added at the bottom. If the number is preceded by "x", then this
multiple of the image height is added as whitespace at the bottom.
=cut
my $TOP_JUSTIFY = 0; # No top justifying by default
if($opt{topjustify}) {
$TOP_JUSTIFY = $opt{topjustify};
die <<"EOF" unless $TOP_JUSTIFY =~ /^x?\d+[.]?\d*$/i;
$prompt: Error: Illegal align specified: "$TOP_JUSTIFY"
Value must be positive numeric, optionally preceded by x
EOF
}
=item B<-transparent>
Generate transparent bitmaps, i.e. the background color (white) is
transparent if viewed with certain viewers (e.g. browsers). This option
may not be available due to missing capabilities of external
programs.
=cut
my $TRANSPARENT = 0; # Do not make make images transparent by default
if($opt{transparent}) {
$TRANSPARENT = 1;
}
=item B<-type> I<type>
Instruct pstoimg to render the bitmap in I<type> format. Depending on
the local installation, pstoimg is capable of generating either GIF or
PNG bitmaps. This site features the following types: png gif
If omitted, the first type in this list is taken.
=cut
if($opt{type}) {
$opt{type} = lc($opt{type});
die <<"EOF" unless grep($_ eq $opt{type},@IMAGE_TYPES);
$prompt: Error: This version of pstoimg does not support
"$opt{type}" image format.
EOF
}
else {
($opt{type}) = @IMAGE_TYPES; # default image type
}
# Support -gif and -png for a transition period
if($opt{gif}) {
print qq{$prompt: Warning: The -gif switch is deprecated. Use "-type gif" instead.\n};
if(grep($_ eq 'gif',@IMAGE_TYPES)) {
$opt{type} = 'gif';
}
else {
die <<"EOF";
$prompt: Error: This version of pstoimg does not support "gif" format.
EOF
}
}
if($opt{png}) {
print qq{$prompt: Warning: The -png switch is deprecated. Use "-type png" instead.\n};
if(grep($_ eq 'png',@IMAGE_TYPES)) {
$opt{type} = 'png';
}
else {
die <<"EOF";
$prompt: Error: This version of pstoimg does not support "png" format.
EOF
}
}
=item B<-white>
Remove TeX's page color information from the PostScript file before
converting so that a white background is used.
=back
=cut
# do some consistency checks on the options
die <<"EOF" if($RIGHT_JUSTIFY && $CENTER);
$prompt: Error: Conflicting options -center and -rightjustify.
EOF
# now setup some parameters
# calculate dpi resolution from density and scale
$DENSITY = int($opt{scale} * $DENSITY + .5) if($opt{scale} != 1);
my $reduce_color = '';
if($opt{depth} == 1) {
$reduce_color = "$PPMQUANT 2";
}
elsif ($opt{depth} == 8) {
$reduce_color = "$PPMQUANT 256";
}
my $gs_aalias = '';
if($opt{antialias}) {
$GSDEVICE = $GSALIASDEVICE;
if($opt{depth} == 1) {
$gs_aalias = '-dTextAlphaBits=4 ';
$reduce_color = "$PPMQUANT -floyd 256";
}
else {
$gs_aalias = '-dTextAlphaBits=4 -dGraphicsAlphaBits=4 ';
}
}
elsif ($opt{aaliastext}) {
$GSDEVICE = $GSALIASDEVICE;
$gs_aalias = '-dTextAlphaBits=4 ';
$reduce_color = "$PPMQUANT -floyd 256";
}
my $PAPERSIZE = $ENV{PAPERSIZE} || '';
# This rx matches float values in Bounding Box expressions
my $Brx = '-?\d+(?:\.\d*|)';
##############################################################################
# Main program
=head1 DESCRIPTION
B<pstoimg> iterates over the given input files and runs them through
Ghostscipt. The resulting pnm (portable anymap files) are processed
with different Netpbm tools (cropping, color mapping, aligning, ...)
and finally converted into (currently) either GIF or PNG format. The
bitmaps can now be included e.g. in WWW pages.
The PostScript file is converted as is. If a valid bounding box is
found (EPS format), then only this area is converted. The image is
I<not> cropped by default.
=cut
die "$prompt: Error: No input file(s) specified\n"
unless(@ARGV);
# suppress diagnostics messages if possible
my $NULLFILE = '/dev/null';
open(STDERR, ">$NULLFILE") unless($opt{debug});
my $exit = 0;
$opt{out} = '' if(@ARGV > 1); # disable -out if multiple ps files given
my $psfile;
foreach $psfile (@ARGV) {
unless (-f $psfile) {
print qq{$prompt: Error: Cannot find file "$psfile": $!\n};
exit 1;
}
$exit += (&pstoimg($psfile) ? 0 : 1);
}
=head1 RETURN VALUE
=over 4
=item 0
if everything went all right
=item x
(x != 0) something went wrong. See the message output.
=back
=cut
exit $exit ? 1 : 0;
##############################################################################
# Subroutines
sub pstoimg {
my ($psfile) = @_;
print "$prompt: Processing $psfile\n" unless($opt{quiet});
# remove a trailing suffix the same way a shell would do it
my $base = $psfile;
$base =~ s|[.][^.$dd$dd]*$||;
my $outfile;
if($opt{out}) {
$outfile = $opt{out};
# append the type unless -outfile has a "." in it
$outfile .= ".$opt{type}" unless($outfile =~ /[.]/);
}
else {
$outfile = "$base.$opt{type}";
}
# Invoke Ghostscript
my $pnmdir = $TMP ? "$TMP$dd" : ".$dd";
my $pnmbase = "p$$"; # keep it short for dos
my $pnmfile = $pnmdir .
($opt{multipage} ? "%d_${pnmbase}.pnm" : "$pnmbase.pnm");
ps2pnm($psfile,$pnmfile) || return 0;
my $ok = 1;
if (-f $pnmfile) {
if(crop_scale_etc($pnmfile, $outfile)) {
L2hos->Unlink($pnmfile) unless($opt{debug});
}
else {
return 0;
}
}
elsif($opt{multipage}) {
unless(opendir(DIR,$pnmdir)) {
print qq{$prompt: Error: Could not open directory "$pnmdir": $!\n};
return 0;
}
my @list = grep(/^\d+_\w*\./,readdir(DIR));
closedir(DIR);
if(@list) {
my $i;
foreach $i (@list) {
my ($n) = $i =~ /^(\d+)_/;
my $j = $outfile;
$j =~ s|(\.[^/.]*)$|$n$1|;
if(crop_scale_etc("$pnmdir$i", $j)) {
L2hos->Unlink("$pnmdir$i") unless($opt{debug});
}
else {
$ok = 0;
}
}
}
else {
goto not_found;
}
}
else {
not_found:
print "$prompt: Error: Couldn't find pnm output of $psfile\n";
}
L2hos->Unlink($psfile) if($opt{discard} && !$opt{debug});
$ok;
}
sub ps2pnm {
my ($psfile,$pnmfile) = @_;
my $gs_size = $PAPERSIZE ? "-sPAPERSIZE=$PAPERSIZE" : '';
my $gs_density = ($DENSITY != 72) ? "-r$DENSITY" : '';
my ($bbx, $bby, $bbw, $bbh) = (0,0,0,0);
my $max_lines = 100;
my ($epsf,$have_geometry) = (0,0);
# Parse postscript file for information
unless(open(PS, "<$psfile")) {
print qq{$prompt: Error: Cannot read "$psfile": $!};
return 0;
}
$_ = <PS>; # read one line
if( /^%!.*EPSF/ ) {
# we're in a EPSF file
$epsf = 1;
}
if($GEOMETRY || $epsf) {
while (defined ($_ = <PS>)) {
# Look for bounding box comment
# MRO: add support of precise bounding boxes
if ($epsf && (
/^%+(?:HiRes|Exact)BoundingBox:\s+($Brx)\s+($Brx)\s+($Brx)\s+($Brx)/o ||
/^\%\%BoundingBox:\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/)) {
$bbx = 0 - $1; $bby = 0 - $2;
$bbw = $3 + $bbx; $bbh = $4 + $bby;
if(($bbw > 0) && ($bbh > 0)) { # we have a valid bounding box
print "$prompt: EPSF dimensions are ${bbw}x$bbh\n" if($opt{debug});
# this overrides the -geometry switch
if($DENSITY) { # scale the output
my $scale = $DENSITY / 72.0;
$bbw *= $scale;
$bbh *= $scale;
}
$bbw = int($bbw + 0.99);
$bbh = int($bbh + 0.99);
$GEOMETRY = "${bbw}x${bbh}";
$have_geometry = 1;
last;
}
}
# Look for page size information
elsif($GEOMETRY && /TeXDict\s+begin\s+(\d+)\s+(\d+)\s+/) {
$PAGE_WIDTH = int($1 / 65536 * 72 /72.27 +.5);
$PAGE_HEIGHT = int($2 / 65536 * 72 /72.27 +.5);
print "$prompt: Page dimensions are ${PAGE_WIDTH}x$PAGE_HEIGHT\n"
if($opt{debug});
# we don't have to look further for EPSF stuff at this point
last;
}
elsif(!$GEOMETRY && (/^\%\%EndComments/ || --$max_lines == 0)) {
# abort at a certain point to avoid scanning huge ps files
last;
}
}
}
close PS;
if($GEOMETRY && !$have_geometry) { # RRM: overrides $PAPERSIZE