faa8d1b6dc415c3c3db3bb7237196c70356bd891
[swftools.git] / src / pdf2pdf.c
1 /* pdf2pdf.c
2    main routine for pdf2pdf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2009 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 "../config.h"
28 #include "../lib/args.h"
29 #include "../lib/os.h"
30 #include "../lib/gfxsource.h"
31 #include "../lib/gfxdevice.h"
32 #include "../lib/gfxpoly.h"
33 #include "../lib/devices/rescale.h"
34 #include "../lib/devices/polyops.h"
35 #include "../lib/devices/pdf.h"
36 #include "../lib/readers/image.h"
37 #include "../lib/readers/swf.h"
38 #include "../lib/pdf/pdf.h"
39 #include "../lib/log.h"
40
41 static gfxsource_t*driver = 0;
42
43 static int maxwidth = 0;
44 static int maxheight = 0;
45 static char * outputname = 0;
46 static int loglevel = 3;
47 static char * pagerange = 0;
48 static char * filename = 0;
49 static const char * format = "ocr";
50
51 int args_callback_option(char*name,char*val) {
52     if (!strcmp(name, "o"))
53     {
54         outputname = val;
55         return 1;
56     }
57     else if (!strcmp(name, "v"))
58     {
59         loglevel ++;
60         setConsoleLogging(loglevel);
61         return 0;
62     }
63     else if (!strcmp(name, "f"))
64     {
65         format = val;
66         return 1;
67     }
68     else if (!strcmp(name, "q"))
69     {
70         loglevel --;
71         setConsoleLogging(loglevel);
72         return 0;
73     }
74     else if (name[0]=='p')
75     {
76         do {
77             name++;
78         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
79
80         if(*name) {
81             pagerange = name;
82             return 0;
83         } 
84         pagerange = val;        
85         return 1;
86     }
87     else if (!strcmp(name, "s"))
88     {
89         if(!driver) {
90             fprintf(stderr, "Specify input file before -s\n");
91             exit(1);
92         }
93         char*s = strdup(val);
94         char*c = strchr(s, '=');
95         if(c && *c && c[1])  {
96             *c = 0;
97             c++;
98             driver->set_parameter(driver, s,c);
99         } else {
100             driver->set_parameter(driver, s,"1");
101         }
102         free(s);
103         return 1;
104     }
105     else if (!strcmp(name, "X"))
106     {
107         maxwidth = atoi(val);
108         return 1;
109     }
110     else if (!strcmp(name, "Y"))
111     {
112         maxheight = atoi(val);
113         return 1;
114     }
115     else if (!strcmp(name, "V"))
116     {   
117         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
118         exit(0);
119     }
120     else 
121     {
122         fprintf(stderr, "Unknown option: -%s\n", name);
123         exit(1);
124     }
125     return 0;
126 }
127
128 struct options_t options[] =
129 {{"o","output"},
130  {"q","quiet"},
131  {"V","version"},
132  {"s","set"},
133  {"p","pages"},
134  {0,0}
135 };
136
137 int args_callback_longoption(char*name,char*val) {
138     return args_long2shortoption(options, name, val);
139 }
140
141 int args_callback_command(char*name, char*val) {
142     if (!filename) {
143
144         filename = name;
145
146         if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
147             msg("<verbose> Treating file as PDF");
148             driver = gfxsource_pdf_create();
149         } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
150             msg("<verbose> Treating file as SWF");
151             driver = gfxsource_swf_create();
152         } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
153                   strstr(filename, ".png") || strstr(filename, ".PNG")) {
154             msg("<verbose> Treating file as Image");
155             driver = gfxsource_image_create();
156         } else {
157             driver = gfxsource_pdf_create();
158         }
159     } else {
160         if(outputname)
161         {
162              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
163                      outputname, name);
164              exit(1);
165         }
166         outputname = name;
167     }
168     return 0;
169 }
170
171 void args_callback_usage(char*name)
172 {
173 }
174
175 int main(int argn, char *argv[])
176 {
177     processargs(argn, argv);
178     initLog(0,-1,0,0,-1,loglevel);
179     
180     if(!filename) {
181         fprintf(stderr, "Please specify an input file\n");
182         exit(1);
183     }
184     
185     if(!outputname)
186     {
187         if(filename) {
188             outputname = stripFilename(filename, ".print.pdf");
189             msg("<notice> Output filename not given. Writing to %s", outputname);
190         } 
191     }
192     if(!outputname)
193     {
194         fprintf(stderr, "Please use -o to specify an output file\n");
195         exit(1);
196     }
197
198     is_in_range(0x7fffffff, pagerange);
199     if(pagerange)
200         driver->set_parameter(driver, "pages", pagerange);
201
202     if(!filename) {
203         args_callback_usage(argv[0]);
204         exit(0);
205     }
206
207     gfxdocument_t* doc = driver->open(driver, filename);
208     //doc->set_parameter(doc, "drawonlyshapes", "1");
209     doc->set_parameter(doc, "disable_polygon_conversion", "1");
210
211     if(!doc) {
212         msg("<error> Couldn't open %s", filename);
213         exit(1);
214     }
215
216     gfxdevice_t _out,*out=&_out;
217     gfxdevice_pdf_init(out);
218
219     /*gfxdevice_t wrap;
220     gfxdevice_removeclippings_init(&wrap, out);
221     out = &wrap;*/
222
223     gfxdevice_t rescale;
224     if(maxwidth || maxheight) {
225         gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
226         out = &rescale;
227         out->setparameter(out, "keepratio", "1");
228     }
229
230     int pagenr;
231     for(pagenr = 1; pagenr <= doc->num_pages; pagenr++) 
232     {
233         if(is_in_range(pagenr, pagerange)) {
234             gfxpage_t* page = doc->getpage(doc, pagenr);
235             out->startpage(out, page->width, page->height);
236             page->render(page, out);
237             out->endpage(out);
238             page->destroy(page);
239         }
240     }
241     gfxresult_t*result = out->finish(out);
242     if(result) {
243         if(result->save(result, outputname) < 0) {
244             exit(1);
245         }
246         result->destroy(result);
247     }
248     doc->destroy(doc);
249     driver->destroy(driver);
250     return 0;
251 }
252