bugfixes in swfc
[swftools.git] / src / pdf2pdf.c
blob485c6c03693f8f7328012e2ac960bcddd7930712
1 /* pdf2pdf.c
2 main routine for pdf2pdf(1)
4 Part of the swftools package.
6 Copyright (c) 2009 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 */
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"
41 static gfxsource_t*driver = 0;
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";
51 int args_callback_option(char*name,char*val) {
52 if (!strcmp(name, "o"))
54 outputname = val;
55 return 1;
57 else if (!strcmp(name, "v"))
59 loglevel ++;
60 setConsoleLogging(loglevel);
61 return 0;
63 else if (!strcmp(name, "f"))
65 format = val;
66 return 1;
68 else if (!strcmp(name, "q"))
70 loglevel --;
71 setConsoleLogging(loglevel);
72 return 0;
74 else if (name[0]=='p')
76 do {
77 name++;
78 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
80 if(*name) {
81 pagerange = name;
82 return 0;
84 pagerange = val;
85 return 1;
87 else if (!strcmp(name, "s"))
89 if(!driver) {
90 fprintf(stderr, "Specify input file before -s\n");
91 exit(1);
93 char*s = strdup(val);
94 char*c = strchr(s, '=');
95 if(c && *c && c[1]) {
96 *c = 0;
97 c++;
98 driver->setparameter(driver, s,c);
99 } else {
100 driver->setparameter(driver, s,"1");
102 free(s);
103 return 1;
105 else if (!strcmp(name, "X"))
107 maxwidth = atoi(val);
108 return 1;
110 else if (!strcmp(name, "Y"))
112 maxheight = atoi(val);
113 return 1;
115 else if (!strcmp(name, "V"))
117 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
118 exit(0);
120 else
122 fprintf(stderr, "Unknown option: -%s\n", name);
123 exit(1);
125 return 0;
128 static struct options_t options[] = {
129 {"h", "help"},
130 {"v", "verbose"},
131 {"p", "pages"},
132 {"X", "width"},
133 {"Y", "height"},
134 {"s", "set"},
135 {"o", "output"},
136 {"V", "version"},
137 {0,0}
140 int args_callback_longoption(char*name,char*val) {
141 return args_long2shortoption(options, name, val);
144 int args_callback_command(char*name, char*val) {
145 if (!filename) {
147 filename = name;
149 if(strstr(filename, ".pdf") || strstr(filename, ".PDF")) {
150 msg("<verbose> Treating file as PDF");
151 driver = gfxsource_pdf_create();
152 } else if(strstr(filename, ".swf") || strstr(filename, ".SWF")) {
153 msg("<verbose> Treating file as SWF");
154 driver = gfxsource_swf_create();
155 } else if(strstr(filename, ".jpg") || strstr(filename, ".JPG") ||
156 strstr(filename, ".png") || strstr(filename, ".PNG")) {
157 msg("<verbose> Treating file as Image");
158 driver = gfxsource_image_create();
159 } else {
160 driver = gfxsource_pdf_create();
162 } else {
163 if(outputname)
165 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
166 outputname, name);
167 exit(1);
169 outputname = name;
171 return 0;
174 void args_callback_usage(char *name)
176 printf("\n");
177 printf("Usage: %s <pdffile>\n", name);
178 printf("\n");
179 printf("-h , --help Print short help message and exit\n");
180 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
181 printf("-p , --pages <pages> Pages to convert\n");
182 printf("-X , --width <width> Make sure the output pdf is <width> pixels wide\n");
183 printf("-Y , --height <height> Make sure the output pdf is <height> pixels high\n");
184 printf("-s , --set <parameter>=<value> Set <parameter> to <value>\n");
185 printf("-o , --output <filename> Write output to file <filename>.\n");
186 printf("-V , --version Print version info and exit\n");
187 printf("\n");
190 int main(int argn, char *argv[])
192 processargs(argn, argv);
193 initLog(0,-1,0,0,-1,loglevel);
195 if(!filename) {
196 fprintf(stderr, "Please specify an input file\n");
197 exit(1);
200 if(!outputname)
202 if(filename) {
203 outputname = stripFilename(filename, ".print.pdf");
204 msg("<notice> Output filename not given. Writing to %s", outputname);
207 if(!outputname)
209 fprintf(stderr, "Please use -o to specify an output file\n");
210 exit(1);
213 is_in_range(0x7fffffff, pagerange);
214 if(pagerange)
215 driver->setparameter(driver, "pages", pagerange);
217 if(!filename) {
218 args_callback_usage(argv[0]);
219 exit(0);
222 gfxdocument_t* doc = driver->open(driver, filename);
223 //doc->setparameter(doc, "drawonlyshapes", "1");
224 doc->setparameter(doc, "disable_polygon_conversion", "1");
226 if(!doc) {
227 msg("<error> Couldn't open %s", filename);
228 exit(1);
231 gfxdevice_t _out,*out=&_out;
232 gfxdevice_pdf_init(out);
234 /*gfxdevice_t wrap;
235 gfxdevice_removeclippings_init(&wrap, out);
236 out = &wrap;*/
238 gfxdevice_t rescale;
239 if(maxwidth || maxheight) {
240 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
241 out = &rescale;
242 out->setparameter(out, "keepratio", "1");
245 int pagenr;
246 for(pagenr = 1; pagenr <= doc->num_pages; pagenr++)
248 if(is_in_range(pagenr, pagerange)) {
249 gfxpage_t* page = doc->getpage(doc, pagenr);
250 out->startpage(out, page->width, page->height);
251 page->render(page, out);
252 out->endpage(out);
253 page->destroy(page);
256 gfxresult_t*result = out->finish(out);
257 if(result) {
258 if(result->save(result, outputname) < 0) {
259 exit(1);
261 result->destroy(result);
263 doc->destroy(doc);
264 driver->destroy(driver);
265 return 0;