added more command-line options
[swftools.git] / pdf2swf / pdf2swf.cc
1 /* pdf2swf.cc
2    main routine for pdf2swf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include "../config.h"
15 #include "args.h"
16 #include "pdfswf.h"
17 #include "t1lib.h"
18 extern "C" {
19 #include "log.h"
20 }
21
22 static char * outputname = 0;
23 static int loglevel = 3;
24 static char * pagerange = 0;
25 static char * filename = 0;
26 static char * password = 0;
27
28 int args_callback_option(char*name,char*val) {
29     if (!strcmp(name, "o"))
30     {
31         outputname = val;
32         return 1;
33     }
34     else if (!strcmp(name, "v"))
35     {
36         loglevel ++;
37         return 0;
38     }
39     else if (name[0]=='p')
40     {
41         /* check whether the page range follows the p directly, like 
42            in -p1,2 */
43         do {
44             name++;
45         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
46
47         if(*name) {
48             pagerange = name;
49             return 0;
50         } 
51         pagerange = val;        
52         return 1;
53     }
54     else if (!strcmp(name, "P"))
55     {
56         password = val;
57         return 1;
58     }
59     else if (!strcmp(name, "s"))
60     {
61         pdfswf_drawonlyshapes();
62         return 0;
63     }
64     else if (!strcmp(name, "i"))
65     {
66         pdfswf_ignoredraworder();
67         return 0;
68     }
69     else if (name[0]=='j')
70     {
71         if(name[1]) {
72             pdfswf_jpegquality(atoi(&name[1]));
73             return 0;
74         } else {
75             pdfswf_jpegquality(atoi(val));
76             return 1;
77         }
78     }
79     else if (!strcmp(name, "V"))
80     {   
81         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
82         exit(0);
83     }
84     else 
85     {
86         fprintf(stderr, "Unknown option: -%s\n", name);
87         exit(1);
88     }
89     return 0;
90 }
91
92 struct options_t
93 {
94     char shortoption;
95     char*longoption;
96 } options[] =
97 {{'o',"output"},
98  {'V',"version"},
99  {'i',"ignore"},
100  {'s',"shapes"},
101  {'j',"jpegquality"},
102  {'p',"pages"}
103 };
104
105 int args_callback_longoption(char*name,char*val) {
106     int t;
107     char*equal = strchr(name,'=');
108     if (equal) {
109         *equal = 0;
110         equal++;
111     }
112     for(t=0;t<sizeof(options)/sizeof(struct options_t);t++) {
113         if(!strcmp(options[t].longoption, name)) {
114                 char*tmp = (char*)malloc(strlen(name)+(equal?strlen(equal)+2:2));
115                 tmp[0] = options[t].shortoption;
116                 tmp[1] = 0;
117                 if(equal) {
118                     strcpy(&tmp[1], equal);
119                 }
120                 return args_callback_option(tmp,val);
121         }
122     }
123     fprintf(stderr, "Unknown option: --%s\n", name);
124     exit(1);
125 }
126
127 int args_callback_command(char*name, char*val) {
128     if (!filename) 
129         filename = name;
130     else {
131         if(outputname)
132         {
133              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
134                      outputname, name);
135              exit(1);
136         }
137         outputname = name;
138     }
139     return 0;
140 }
141
142 void args_callback_usage(char*name)
143 {
144     printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf [output.swf]\n", name);
145     printf("\n");
146     printf("-p  --pages=range          Convert only pages in range\n");
147     printf("-P  --password=password    Use password for deciphering the pdf\n");
148     printf("-s  --shapes               Don't use SWF Fonts, but store everything as shape\n");
149     printf("-i  --ignore               Ignore draw order (makes the SWF file smaller)\n");
150     printf("-j  --jpegquality=quality  Set quality of embedded jpeg pictures (default:85)\n");
151     printf("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
152     printf("-V  --version              Print program version\n");
153 }
154
155 /* check whether the value t is in a given range.
156   examples: 3 is in range 1-10: true
157             7 is in range 2-4,6,8-10: false
158             9 is in range 1,2,3-12: true
159 */
160 char is_in_range(int t, char*irange)
161 {
162     char*pos = irange;
163     char*digits;
164     int num;
165     char range = 0;
166     int last=0;
167     char tmp;
168
169     if(!irange)  // no range resembles (-OO,OO)
170         return 1;
171
172     while(*pos)
173     {
174         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
175             pos++;
176
177         digits = pos;
178         while(*digits>='0' && *digits<='9')
179             digits++;
180         if(digits == pos) {
181             fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
182             exit(1);
183         }
184         
185         tmp=*digits;*digits=0;
186         num = atoi(pos);
187         *digits=tmp;
188         pos = digits;
189
190         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
191             pos++;
192
193         if(range && last<=t && num>=t)
194             return 1;
195         if(range) {
196             range = 0;
197             if(*pos)
198              pos ++;
199             continue;
200         }
201
202         if(*pos=='-')
203         {
204             if(range) {
205                 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
206                 exit(1);
207             }
208             last = num;
209             range = 1;
210             if(*pos)
211              pos ++;
212             continue;
213         } 
214         else 
215         {
216             /* if it isn't a '-', we assume it is a seperator like
217                ',', ';', ':', whatever. */
218             if(t == num)
219                 return 1;
220             if(*pos)
221              pos ++;
222             continue;
223         }
224     }
225     if(range && last<=t)
226         return 1;
227     return 0;
228 }
229
230 int main(int argn, char *argv[])
231 {
232     srand48(time(0));
233     processargs(argn, argv);
234     initLog(0,-1,0,0,-1,loglevel);
235
236     // test if the page range is o.k.
237     is_in_range(0x7fffffff, pagerange);
238
239     if (!filename) {
240         args_callback_usage(argv[0]);
241         exit(0);
242     }
243
244     logf("<verbose> reading data files from %s\n", DATADIR);
245     //TODO: use tempnam here. Check if environment already contains a
246     //T1LIB_CONFIG.
247     putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
248     FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
249     fprintf(fi, "FONTDATABASE=%s/FontDataBase\n", DATADIR);
250     fprintf(fi, "ENCODING=%s:.\n", DATADIR);
251     fprintf(fi, "AFM=%s:.\n", DATADIR);
252     fprintf(fi, "TYPE1=%s:.\n", DATADIR);
253     fclose(fi);
254     /* initialize t1lib */
255     T1_SetBitmapPad( 16);
256     if ((T1_InitLib(NO_LOGFILE)==NULL)){
257         fprintf(stderr, "Initialization of t1lib failed\n");
258         exit(1);
259     }
260     unlink("/tmp/t1lib.config.tmp");
261
262     pdfswf_init(filename, password);
263     pdfswf_setoutputfilename(outputname);
264
265     int pages = pdfswf_numpages();
266     int t = 1;
267     for(t = 1; t <= pages; t++) 
268     {
269         if(is_in_range(t, pagerange))
270         pdfswf_convertpage(t);
271     }
272
273     pdfswf_close();
274     return 0;
275 }
276
277