improved polygon conversion in pdf2pdf
[swftools.git] / src / gfx2gfx.c
1 /* pdf2swf.c
2    main routine for pdf2swf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org> 
7  
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.
12
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.
17
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 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
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/bbox.h"
38 #ifdef HAVE_LRF
39 #include "../../swftools/lib/devices/lrf.h"
40 #endif
41 #include "../../swftools/lib/devices/ocr.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"
48
49 static gfxsource_t*driver = 0;
50
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;
56
57 int args_callback_option(char*name,char*val) {
58     if (!strcmp(name, "o"))
59     {
60         outputname = val;
61         return 1;
62     }
63     else if (!strcmp(name, "v"))
64     {
65         loglevel ++;
66         setConsoleLogging(loglevel);
67         return 0;
68     }
69     else if (!strcmp(name, "f"))
70     {
71         format = val;
72         return 1;
73     }
74     else if (!strcmp(name, "q"))
75     {
76         loglevel --;
77         setConsoleLogging(loglevel);
78         return 0;
79     }
80     else if (name[0]=='p')
81     {
82         do {
83             name++;
84         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
85
86         if(*name) {
87             pagerange = name;
88             return 0;
89         } 
90         pagerange = val;        
91         return 1;
92     }
93     else if (!strcmp(name, "s"))
94     {
95         if(!driver) {
96             fprintf(stderr, "Specify input file before -s\n");
97             exit(1);
98         }
99         char*s = strdup(val);
100         char*c = strchr(s, '=');
101         if(c && *c && c[1])  {
102             *c = 0;
103             c++;
104             driver->set_parameter(driver, s,c);
105         } else {
106             driver->set_parameter(driver, s,"1");
107         }
108         free(s);
109         return 1;
110     }
111     else if (!strcmp(name, "V"))
112     {   
113         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
114         exit(0);
115     }
116     else 
117     {
118         fprintf(stderr, "Unknown option: -%s\n", name);
119         exit(1);
120     }
121     return 0;
122 }
123
124 struct options_t options[] =
125 {{"o","output"},
126  {"q","quiet"},
127  {"V","version"},
128  {"s","set"},
129  {"p","pages"},
130  {0,0}
131 };
132
133 int args_callback_longoption(char*name,char*val) {
134     return args_long2shortoption(options, name, val);
135 }
136
137 int args_callback_command(char*name, char*val) {
138     if (!filename) {
139
140         filename = name;
141
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();
152         }
153     } else {
154         if(outputname)
155         {
156              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
157                      outputname, name);
158              exit(1);
159         }
160         outputname = name;
161     }
162     return 0;
163 }
164
165 void args_callback_usage(char*name)
166 {
167 }
168
169 int main(int argn, char *argv[])
170 {
171     processargs(argn, argv);
172     initLog(0,-1,0,0,-1,loglevel);
173     
174     if(!filename) {
175         fprintf(stderr, "Please specify an input file\n");
176         exit(1);
177     }
178     
179     if(!outputname)
180     {
181         if(filename) {
182             outputname = stripFilename(filename, ".out");
183             msg("<notice> Output filename not given. Writing to %s", outputname);
184         } 
185     }
186     if(!outputname)
187     {
188         fprintf(stderr, "Please use -o to specify an output file\n");
189         exit(1);
190     }
191     is_in_range(0x7fffffff, pagerange);
192     if(pagerange)
193         driver->set_parameter(driver, "pages", pagerange);
194
195     if(!filename) {
196         args_callback_usage(argv[0]);
197         exit(0);
198     }
199
200     gfxdocument_t* doc = driver->open(driver, filename);
201     if(!doc) {
202         msg("<error> Couldn't open %s", filename);
203         exit(1);
204     }
205
206     if(!format) {
207         char*x = strrchr(outputname, '.');
208         if(x) 
209             format = x+1;
210     }
211
212
213     gfxresult_t*result = 0;
214 #ifdef HAVE_LRF
215     if(!strcasecmp(format, "lrf")) {
216         gfxdevice_t lrf;
217         gfxdevice_lrf_init(&lrf);
218
219         gfxdevice_t rescale;
220         gfxdevice_rescale_init(&rescale, &lrf, 592, 732, 0);
221
222         gfxdevice_t*out = &rescale;
223         out->setparameter(out, "keepratio", "1");
224         out->setparameter(out, "pagepattern", outputname);
225
226         gfxdevice_t bbox2,*bbox=&bbox2;
227         gfxdevice_bbox_init(bbox);
228         bbox->setparameter(bbox, "graphics", "0");
229
230         int pagenr;
231
232         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
233         {
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);
239
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);
242                 out->endpage(out);
243
244                 page->destroy(page);
245             }
246         }
247         result = out->finish(out);
248     } else 
249 #endif
250     {
251         gfxdevice_t _out,*out=&_out;
252         if(!strcasecmp(format, "ocr")) {
253             gfxdevice_ocr_init(out);
254         } else if(!strcasecmp(format, "swf")) {
255             gfxdevice_swf_init(out);
256         } else if(!strcasecmp(format, "img") || !strcasecmp(format, "png")) {
257             gfxdevice_render_init(out);
258             out->setparameter(out, "antialize", "4");
259         } else if(!strcasecmp(format, "txt")) {
260             gfxdevice_text_init(out);
261         } else if(!strcasecmp(format, "pdf")) {
262             gfxdevice_pdf_init(out);
263         } else {
264             msg("<error> Invalid output format: %s", format);
265             exit(1);
266         }
267
268         int pagenr;
269         for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
270         {
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);
275                 out->endpage(out);
276                 page->destroy(page);
277             }
278         }
279         result = out->finish(out);
280     }
281
282     if(result) {
283         if(result->save(result, outputname) < 0) {
284             exit(1);
285         }
286         result->destroy(result);
287     }
288
289     doc->destroy(doc);
290
291     driver->destroy(driver);
292     return 0;
293 }
294