summaryrefslogtreecommitdiff
path: root/FS/FS/Misc/eps2png.pm
blob: aa8e5729a83ff6f2ca4fa977287f0689fa0a7253 (plain)
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
package FS::Misc::eps2png;

#based on eps2png by Johan Vromans
#Copyright 1994,2008 by Johan Vromans.
#This program is free software; you can redistribute it and/or
#modify it under the terms of the Perl Artistic License or 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.

use strict;
use vars qw( @ISA @EXPORT_OK );
use Exporter;
use File::Temp;
use File::Slurp qw( slurp );
#use FS::UID;

@ISA = qw( Exporter );
@EXPORT_OK = qw( eps2png );

################ Program parameters ################

# Some GhostScript programs can produce GIF directly.
# If not, we need the PBM package for the conversion.
# NOTE: This will be changed upon install.
my $use_pbm = 0;

my $res = 82;			# default resolution
my $scale = 1;			# default scaling
my $mono = 0;			# produce BW images if non-zero
my $format;			# output format
my $gs_format;			# GS output type
my $output;			# output, defaults to STDOUT
my $antialias = 4;              # antialiasing
my $DEF_width;			# desired widht
my $DEF_height;		# desired height
#my $DEF_width = 90;			# desired widht
#my $DEF_height = 36;		# desired height

my ($verbose,$trace,$test,$debug) = (0,0,0,0);
#handle_options ();
set_out_type ('png'); # unless defined $format;
warn "Producing $format ($gs_format) image.\n" if $verbose;

$trace |= $test | $debug;
$verbose |= $trace;

################ Presets ################

################ The Process ################

my $err = 0;

sub eps2png {
    my( $eps, %options ) = @_; #well, no options yet

    my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
    my $eps_file = new File::Temp( TEMPLATE => 'image.XXXXXXXX',
                                   DIR      => $dir,
                                   SUFFIX   => '.eps',
                                   #UNLINK   => 0,
                                 ) or die "can't open temp file: $!\n";
    print $eps_file $eps;
    close $eps_file;

    my @eps = split(/\r?\n/, $eps);

    warn "converting eps (". length($eps). " bytes, ". scalar(@eps). " lines)\n"
      if $verbose;

    my $line = shift @eps; #<EPS>;
    unless ( $eps =~ /^%!PS-Adobe.*EPSF-/ ) {
	warn "not EPS file (no %!PS-Adobe header)\n";
        return; #empty png file?
    }

    my $ps = "";		# PostScript input data
    my $xscale;
    my $yscale;
    my $gotbb;

    # Prevent derived values from propagating.
    my $width = $DEF_width;
    my $height = $DEF_height;

    while ( @eps ) {

        $line = shift(@eps)."\n";

	# Search for BoundingBox.
	if ( $line =~ /^%%BoundingBox:\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/i ) {
	    $gotbb++;
	    warn "$eps_file: x0=$1, y0=$2, w=", $3-$1, ", h=", $4-$2
		if $verbose;

	    if ( defined $width ) {
		$res = 72;
		$xscale = $width / ($3 - $1);
		if ( defined $height ) {
		    $yscale = $height / ($4 - $2);
		}
		else {
		    $yscale = $xscale;
		    $height = ($4 - $2) * $yscale;
		}
	    }
	    elsif ( defined $height ) {
		$res = 72;
		$yscale = $height / ($4 - $2);
		if ( defined $width ) {
		    $xscale = $width / ($3 - $1);
		}
		else {
		    $xscale = $yscale;
		    $width = ($3 - $1) * $xscale;
		}
	    }
	    unless ( defined $xscale ) {
		$xscale = $yscale = $scale;
		# Calculate actual width.
		$width  = $3 - $1;
		$height = $4 - $2;
		# Normal PostScript resolution is 72.
		$width  *= $res/72 * $xscale;
		$height *= $res/72 * $yscale;
		# Round up.
		$width  = int ($width + 0.5) + 1;
		$height = int ($height + 0.5) + 1;
	    }
	    warn ", width=$width, height=$height\n" if $verbose;

	    # Scale.
	    $ps .= "$xscale $yscale scale\n"
	      if $xscale != 1 || $yscale != 1;

	    # Create PostScript code to translate coordinates.
	    $ps .= (0-$1) . " " . (0-$2) . " translate\n"
	      unless $1 == 0 && $2 == 0;

	    # Include the image, show and quit.
	    $ps .= "($eps_file) run\n".
	      "showpage\n".
		"quit\n";

	    last;
	}
	elsif ( $line =~ /^%%EndComments/i ) {
	    last;
	}
    }

    unless ( $gotbb ) {
	warn "No bounding box in $eps_file\n";
	return;
    }

    #it would be better to ask gs to spit out files on stdout, but c'est la vie

    #my $out_file;		# output file
    #my $pbm_file;		# temporary file for PBM conversion

    my $out_file = new File::Temp( TEMPLATE => 'image.XXXXXXXX',
                                   DIR      => $dir,
                                   SUFFIX   => '.png',
                                   #UNLINK   => 0,
                                 ) or die "can't open temp file: $!\n";

    my $pbm_file = new File::Temp( TEMPLATE => 'image.XXXXXXXX',
                                   DIR      => $dir,
                                   SUFFIX   => '.pbm',
                                   #UNLINK   => 0,
                                 ) or die "can't open temp file: $!\n";

    # Note the temporary PBM file is created where the output file is
    # located, since that will guarantee accessibility (and a valid
    # filename).
    warn "Creating $out_file\n" if $verbose;

    my $gs0 = "gs -q -dNOPAUSE -r$res -g${width}x$height";
    my $gs1 = "-";
    $gs0 .= " -dTextAlphaBits=$antialias -dGraphicsAlphaBits=$antialias"
      if $antialias;
    if ( $format eq 'png' ) {
	mysystem ("$gs0 -sDEVICE=". ($mono ? "pngmono" : $gs_format).
		  " -sOutputFile=$out_file $gs1", $ps);
    }
    elsif ( $format eq 'jpg' ) {
	mysystem ("$gs0 -sDEVICE=". ($mono ? "jpeggray" : $gs_format).
		  " -sOutputFile=$out_file $gs1", $ps);
    }
    elsif ( $format eq 'gif' ) {
	if ( $use_pbm ) {
	    # Convert to PPM and use some of the PBM converters.
	    mysystem ("$gs0 -sDEVICE=". ($mono ? "pbm" : "ppm").
		      " -sOutputFile=$pbm_file $gs1", $ps);
	    # mysystem ("pnmcrop $pbm_file | ppmtogif > $out_file");
	    mysystem ("ppmtogif $pbm_file > $out_file");
	    unlink ($pbm_file);
	}
	else {
	    # GhostScript has GIF drivers built-in.
	    mysystem ("$gs0 -sDEVICE=". ($mono ? "gifmono" : "gif8").
		      " -sOutputFile=$out_file $gs1", $ps);
	}
    }
    else {
	warn "ASSERT ERROR: Unhandled output type: $format\n";
	exit (1);
    }

#    unless ( -s $out_file ) {
#	warn "Problem creating $out_file for $eps_file\n";
#	$err++;
#    }

    slurp($out_file);

}

exit 1 if $err;

################ Subroutines ################

sub mysystem {
    my ($cmd, $data) = @_;
    warn "+ $cmd\n" if $trace;
    if ( $data ) {
	if ( $trace ) {
	    my $dp = ">> " . $data;
	    $dp =~ s/\n(.)/\n>> $1/g;
	    warn "$dp";
	}
	open (CMD, "|$cmd") or die ("$cmd: $!\n");
	print CMD $data;
	close CMD or die ("$cmd close: $!\n");
    }
    else {
	system ($cmd);
    }
}

sub set_out_type {
    my ($opt) = lc (shift (@_));
    if ( $opt =~ /^png(mono|gray|16|256|16m|alpha)?$/ ) {
	$format = 'png';
	$gs_format = $format.(defined $1 ? $1 : '16m');
    }
    elsif ( $opt =~ /^gif(mono)?$/ ) {
	$format = 'gif';
	$gs_format = $format.(defined $1 ? $1 : '');
    }
    elsif ( $opt =~ /^(jpg|jpeg)(gray)?$/ ) {
	$format = 'jpg';
	$gs_format = 'jpeg'.(defined $2 ? $2 : '');
    }
    else {
	warn "ASSERT ERROR: Invalid value to set_out_type: $opt\n";
	exit (1);
    }
}

#			     'antialias|aa=i'   => \$antialias,
#			     'noantialias|noaa' => sub { $antialias = 0 },
#			     'scale=f'     => \$scale,
#			     'width=i'	   => \$width,
#			     'height=i'	   => \$height,
#			     'resolution=i' => \$res,

#    die ("Antialias value must be 0, 1, 2, 4, or 8\n")

#    -width XXX		desired with
#    -height XXX		desired height
#    -resolution XXX	resolution (default = $res)
#    -scale XXX		scaling factor
#    -antialias XX	antialias factor (must be 0, 1, 2, 4 or 8; default: 4)
#    -noantialias	no antialiasing (same as -antialias 0)

1;