2 main routine for pdf2swf(1)
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
27 #include "../../swftools/config.h"
28 #include "../../swftools/lib/args.h"
29 #include "../../swftools/lib/os.h"
30 #include "../../swftools/lib/gfxsource.h"
31 #include "../../swftools/lib/gfxdevice.h"
32 #include "../../swftools/lib/gfxpoly.h"
33 #include "../../swftools/lib/devices/pdf.h"
34 #include "../../swftools/lib/devices/swf.h"
35 #include "../../swftools/lib/devices/text.h"
36 #include "../../swftools/lib/devices/render.h"
37 #include "../../swftools/lib/devices/file.h"
38 #include "../../swftools/lib/devices/bbox.h"
40 #include "../../swftools/lib/devices/lrf.h"
42 #include "../../swftools/lib/devices/rescale.h"
43 #include "../../swftools/lib/devices/record.h"
44 #include "../../swftools/lib/readers/image.h"
45 #include "../../swftools/lib/readers/swf.h"
46 #include "../../swftools/lib/pdf/pdf.h"
47 #include "../../swftools/lib/log.h"
49 static gfxsource_t
*driver
= 0;
51 static char * outputname
= 0;
52 static int loglevel
= 3;
53 static char * pagerange
= 0;
54 static char * filename
= 0;
55 static const char * format
= 0;
57 int args_callback_option(char*name
,char*val
) {
58 if (!strcmp(name
, "o"))
63 else if (!strcmp(name
, "v"))
66 setConsoleLogging(loglevel
);
69 else if (!strcmp(name
, "f"))
74 else if (!strcmp(name
, "q"))
77 setConsoleLogging(loglevel
);
80 else if (name
[0]=='p')
84 } while(*name
== 32 || *name
== 13 || *name
== 10 || *name
== '\t');
93 else if (!strcmp(name
, "s"))
96 fprintf(stderr
, "Specify input file before -s\n");
100 char*c
= strchr(s
, '=');
101 if(c
&& *c
&& c
[1]) {
104 driver
->setparameter(driver
, s
,c
);
106 driver
->setparameter(driver
, s
,"1");
111 else if (!strcmp(name
, "V"))
113 printf("pdf2swf - part of %s %s\n", PACKAGE
, VERSION
);
118 fprintf(stderr
, "Unknown option: -%s\n", name
);
124 struct options_t options
[] =
133 int args_callback_longoption(char*name
,char*val
) {
134 return args_long2shortoption(options
, name
, val
);
137 int args_callback_command(char*name
, char*val
) {
142 if(strstr(filename
, ".pdf") || strstr(filename
, ".PDF")) {
143 msg("<notice> Treating file as PDF");
144 driver
= gfxsource_pdf_create();
145 } else if(strstr(filename
, ".swf") || strstr(filename
, ".SWF")) {
146 msg("<notice> Treating file as SWF");
147 driver
= gfxsource_swf_create();
148 } else if(strstr(filename
, ".jpg") || strstr(filename
, ".JPG") ||
149 strstr(filename
, ".png") || strstr(filename
, ".PNG")) {
150 msg("<notice> Treating file as Image");
151 driver
= gfxsource_image_create();
156 fprintf(stderr
, "Error: Do you want the output to go to %s or to %s?",
165 void args_callback_usage(char*name
)
169 int main(int argn
, char *argv
[])
171 processargs(argn
, argv
);
172 initLog(0,-1,0,0,-1,loglevel
);
175 fprintf(stderr
, "Please specify an input file\n");
182 outputname
= stripFilename(filename
, ".out");
183 msg("<notice> Output filename not given. Writing to %s", outputname
);
188 fprintf(stderr
, "Please use -o to specify an output file\n");
191 is_in_range(0x7fffffff, pagerange
);
193 driver
->setparameter(driver
, "pages", pagerange
);
196 args_callback_usage(argv
[0]);
200 gfxdocument_t
* doc
= driver
->open(driver
, filename
);
202 msg("<error> Couldn't open %s", filename
);
207 char*x
= strrchr(outputname
, '.');
213 gfxresult_t
*result
= 0;
215 if(!strcasecmp(format
, "lrf")) {
217 gfxdevice_lrf_init(&lrf
);
220 gfxdevice_rescale_init(&rescale
, &lrf
, 592, 732, 0);
222 gfxdevice_t
*out
= &rescale
;
223 out
->setparameter(out
, "keepratio", "1");
224 out
->setparameter(out
, "pagepattern", outputname
);
226 gfxdevice_t bbox2
,*bbox
=&bbox2
;
227 gfxdevice_bbox_init(bbox
);
228 bbox
->setparameter(bbox
, "graphics", "0");
232 for(pagenr
= 1; pagenr
<= doc
->num_pages
; pagenr
++)
234 if(is_in_range(pagenr
, pagerange
)) {
235 gfxpage_t
* page
= doc
->getpage(doc
, pagenr
);
236 bbox
->startpage(bbox
,-1,-1);
237 page
->render(page
, bbox
);
238 gfxbbox_t b
= gfxdevice_bbox_getbbox(bbox
);
240 out
->startpage(out
, b
.xmax
-b
.xmin
, b
.ymax
-b
.ymin
);
241 page
->rendersection(page
, out
, -b
.xmin
, -b
.ymin
, 0,0,b
.xmax
-b
.xmin
,b
.ymax
-b
.ymin
);
247 result
= out
->finish(out
);
251 gfxdevice_t _out
,*out
=&_out
;
252 if(!strcasecmp(format
, "swf")) {
253 gfxdevice_swf_init(out
);
254 } else if(!strcasecmp(format
, "img") || !strcasecmp(format
, "png")) {
255 gfxdevice_render_init(out
);
256 out
->setparameter(out
, "antialize", "4");
257 } else if(!strcasecmp(format
, "txt")) {
258 gfxdevice_text_init(out
);
259 } else if(!strcasecmp(format
, "log")) {
260 gfxdevice_file_init(out
, "/tmp/device.log");
261 } else if(!strcasecmp(format
, "pdf")) {
262 gfxdevice_pdf_init(out
);
264 msg("<error> Invalid output format: %s", format
);
269 for(pagenr
= 1; pagenr
<= doc
->num_pages
; pagenr
++)
271 if(is_in_range(pagenr
, pagerange
)) {
272 gfxpage_t
* page
= doc
->getpage(doc
, pagenr
);
273 out
->startpage(out
, page
->width
, page
->height
);
274 page
->render(page
, out
);
279 result
= out
->finish(out
);
283 if(result
->save(result
, outputname
) < 0) {
286 result
->destroy(result
);
291 driver
->destroy(driver
);