-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSAPI.pm
450 lines (320 loc) · 10.4 KB
/
GSAPI.pm
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
package GSAPI;
use 5.008;
use strict;
use warnings;
use Carp;
require Exporter;
use AutoLoader;
use Tie::Handle;
our @ISA = qw(Exporter Tie::Handle);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use GSAPI ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'const' => [ qw(
DISPLAY_555_MASK
DISPLAY_ALPHA_FIRST
DISPLAY_ALPHA_LAST
DISPLAY_ALPHA_MASK
DISPLAY_ALPHA_NONE
DISPLAY_BIGENDIAN
DISPLAY_BOTTOMFIRST
DISPLAY_COLORS_CMYK
DISPLAY_COLORS_GRAY
DISPLAY_COLORS_MASK
DISPLAY_COLORS_NATIVE
DISPLAY_COLORS_RGB
DISPLAY_DEPTH_1
DISPLAY_DEPTH_12
DISPLAY_DEPTH_16
DISPLAY_DEPTH_2
DISPLAY_DEPTH_4
DISPLAY_DEPTH_8
DISPLAY_DEPTH_MASK
DISPLAY_ENDIAN_MASK
DISPLAY_FIRSTROW_MASK
DISPLAY_LITTLEENDIAN
DISPLAY_NATIVE_555
DISPLAY_NATIVE_565
DISPLAY_TOPFIRST
DISPLAY_UNUSED_FIRST
DISPLAY_UNUSED_LAST
DISPLAY_VERSION_MAJOR
DISPLAY_VERSION_MINOR
ERROR_NAMES
GSDLLAPI
GSDLLAPIPTR
GSDLLCALL
GSDLLCALLPTR
GSDLLEXPORT
LEVEL1_ERROR_NAMES
LEVEL2_ERROR_NAMES
e_ExecStackUnderflow
e_Fatal
e_Info
e_InterpreterExit
e_NeedInput
e_NeedStderr
e_NeedStdin
e_NeedStdout
e_Quit
e_RemapColor
e_VMerror
e_VMreclaim
e_configurationerror
e_dictfull
e_dictstackoverflow
e_dictstackunderflow
e_execstackoverflow
e_interrupt
e_invalidaccess
e_invalidcontext
e_invalidexit
e_invalidfileaccess
e_invalidfont
e_invalidid
e_invalidrestore
e_ioerror
e_limitcheck
e_nocurrentpoint
e_rangecheck
e_stackoverflow
e_stackunderflow
e_syntaxerror
e_timeout
e_typecheck
e_undefined
e_undefinedfilename
e_undefinedresource
e_undefinedresult
e_unknownerror
e_unmatchedmark
e_unregistered
gs_error_interrupt
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'const'} } );
our @EXPORT = qw(
);
our $VERSION = '0.6';
sub AUTOLOAD {
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function.
my $constname;
our $AUTOLOAD;
($constname = $AUTOLOAD) =~ s/.*:://;
croak "&GSAPI::constant not defined" if $constname eq 'constant';
my ($error, $val) = constant($constname);
if ($error) { croak $error; }
{
no strict 'refs';
# Fixed between 5.005_53 and 5.005_61
#XXX if ($] >= 5.00561) {
#XXX *$AUTOLOAD = sub () { $val };
#XXX }
#XXX else {
*$AUTOLOAD = sub { $val };
#XXX }
}
goto &$AUTOLOAD;
}
require XSLoader;
XSLoader::load('GSAPI', $VERSION);
# Preloaded methods go here.
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
GSAPI - Perl extension for accessing GNU Ghostscript
=head1 SYNOPSIS
use GSAPI;
my $inst = GSAPI::new_instance();
GSAPI::init_with_args($inst);
GSAPI::run_string($inst, "12345679 9 mul pstack quit\n");
GSAPI::exit($inst);
GSAPI::delete_instance $inst;
=head1 ABSTRACT
GSAPI is an interface to GNU Ghostscript.
It's mainly written to provide a simply interface to ghostscript
that works under Win32 and Unix.
=head1 DESCRIPTION
GSAPI is a very simple interface to the GNU Ghostscript Interpreter API.
This API allows you to use Ghostscript without calling an external program. It
also provides to more control over any output.
Please read the L<current Ghostscript
documentation|http://pages.cs.wisc.edu/~ghost/doc/svn/Readme.htm> for more details.
=head1 FUNCTIONS
=head2 revision
my($prod, $cpy, $rev, $revd) = GSAPI::revision();
Returns Product name, Copyright, Revision and Revision Date
of the ghostscript release.
=head2 new_instance
my $inst = GSAPI::new_instance();
Returns the instance handle.
=head2 delete_instance
GSAPI::delete_instance($inst);
Destroys the instance.
=head2 set_stdio
GSAPI::set_stdio($inst, &stdin, &stdout, &stderr)
Sets the callbacks for ghostscript I/O
C<stdin> gets the maximum number of bytes to return on input and should
return a string up to that length.
C<stdout/stderr> gets the string passed and they should return the number
of bytes written.
Example:
set_stdio $inst,
sub { "\n" },
sub { print STDOUT $_[0]; length $_[0] },
sub { print STDERR $_[0]; lenngth $_[0] };
=head2 init_with_args
$rc = GSAPI::init_with_args($inst, @args)
Initializes the ghostscript library. C<@args> is an array that contains
the same strings you would use on the C<gs> command line.
=head2 exit
$rc = GSPAI::exit($inst)
Calls gsapi_exit
=head2 run_string
$rc = GSAPI::run_string($inst, $string, [$user_errors ])
Calls C<gsapi_run_string_with_length()>, executing the Postscript code
in C<$string>.
=head2 run_file
$rc = GSAPI::run_file($inst, $filename, [$user_errors])
Calls C<gsapi_run_file()>, running the Postscript code in C<$filename>.
=head2 run_string_begin
$rc = GSAPI::run_string_begin($inst, [$user_errors])
Calls C<gsapi_run_string_begin()>, which gets the interpreter ready to run
the Postscript code given via subsequent L</run_string_continue> calls.
=head2 run_string_continue
$rc = GSAPI::run_string_continue($inst, $string, [$user_errors])
Calls C<gsapi_run_string_continue()>, running the Postscript code in
C<$string> in the interpreter which has been prepared with
L</run_string_begin>.
=head2 run_string_end
$rc = GSAPI::run_string_end($inst, [$user_errors])
Calls L<gsapi_run_string_end()>, finishing the execution started in
L</run_string_begin>.
=head2 set_display_callback
$rc = GSAPI::set_display_callback( $inst, &callback );
Sets the callback used for C<-sDEVICE=display>.
Your callback should look like:
sub callback {
my( $func, $handle, $device, @more ) = @_;
...
return $rv;
}
The arguments are:
=over 4
=item $func
Name of the current callback. See below.
=item $handle
Value of C<-dDeviceHandle=> as passed to L<init_with_args>.
=item $device
Opaque pointer to Ghostscript's internal device.
=item @more
Extra parameters. See below.
=back
The callback function is called multiple times. What is happening is decided
by C<$func>.
=over 4
=item display_open
New device has been opened. First call.
=item display_presize
Allows you to accept or reject a size and/or format.
my( $width, $height, $raster, $format ) = @more;
C<$width> width of the page. Note that this is different from the width of
the image, if there is a bounding box defined.
C<$height> is the height of the page.
C<$raster> is the byte count of a row.
C<$format> format of the data.
=item display_size
Called when the page size is fixed.
my( $width, $height, $raster, $format ) = @more;
Note that in the GSAPI, display_size() is called with a pointer (C<pimage>)
to the raw data. However, because of how XS works, this data is only
available to L</display_sync> and L</display_page>.
=item display_sync
my( $pimage ) = @more;
Called when a page is ready to be flushed. Note that C<$pimage> will be a
blank page, if this is called before L</display_page>.
=item display_page
Called when a page is ready to be shown.
my( $copies, $flush, $pimage ) = @more;
C<$pimage> is a string containing the raw image data. It's format is
decided by C<-dDisplayFormat> which you passed to L</init_with_args>.
To get the start of a given row, you use C<$rowN * $raster> where C<$raster>
was provided to L</display_size>.
See C<eg/imager.pl> for an example of how to get the data.
=item display_preclose
Device is about to be closed.
=item display_close
Device has been closed. This is the last call for this device.
=back
Please refer to the Ghostscript documentation and examples for more details.
=head1 TIEHANDLE
TIEHANDLE 'GSAPI', [ stdin => sub { getc STDIN }, ]
[ stdout => &stdout, ]
[ stderr => stderr, ]
[ args => [ arg1, arg2, ...], ]
[ user_errors => 0|1, ]
You may also tie a GSAPI instance to a file handle. This allows you to
print your Postscript code as if to the C<gs> command.
my $output = '';
tie *FH, "GSAPI", stdout => sub { $output .= $_[0]; length $_[0] },
args => [ "gsapi",
"-sDEVICE=pdfwrite",
"-dNOPAUSE",
"-dBATCH",
"-sPAPERSIZE=a4",
"-DSAFER",
"-sOutputFile=/dev/null",
];
$output = '';
print FH "12345679 9 mul pstack quit\n";
close FH;
## $output will contain 111111111.
=cut
sub TIEHANDLE {
my ($class,%args) = @_;
my $inst = new_instance();
$args{stdin} ||= sub { getc STDIN };
$args{stdout} ||= sub { print STDOUT $_[0]; length $_[0] };
$args{stderr} ||= sub { print STDERR $_[0]; length $_[0] };
$args{args} ||= [];
$args{user_errors} ||= 0;
$args{inst} = $inst;
set_stdio($inst, $args{stdin}, $args{stdout}, $args{stderr});
delete @args{qw/stdin stdout stderr/};
init_with_args($inst, @{$args{args}});
run_string_begin($inst, $args{user_errors});
bless \%args, $class;
}
sub WRITE ($$$$) {
my ($ref, $buf, $len, $offs) = @_;
run_string_continue($ref->{inst}, substr($buf, 0, $len), $ref->{user_errors});
}
sub DESTROY ($) {
my $inst = $_[0]->{inst};
run_string_end($inst, $_[0]->{user_errors});
GSAPI::exit($inst);
delete_instance($inst);
}
=begin
=head1 EXPORT
None by default.
=head1 SEE ALSO
L<http://pages.cs.wisc.edu/~ghost/doc/svn/Readme.htm>
=head1 AUTHORS
Stefan Traby, E<lt>[email protected]<gt>
Philip Gwyn, E<lt>gwyn-at-cpan.orgE<gt>
Terry Burton, E<lt>tez-at-terryburton.co.ukE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2008 by Philip Gwyn.
Copyright 2003,2005 by Stefan Traby <[email protected]>.
Copyright (C) 2003,2005 by KW-Computer Ges.m.b.H Graz, Austria.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License 2.0 or any later version.
The main reason why this module is not available under dual license
(Artistic/GPL) is simply the fact that GNU Gostscript is only available
under GPL and not under Artistic License.
=cut
1;
__END__