disallow omitting the output filename
[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 "../lib/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 options[] =
93 {{"o","output"},
94  {"V","version"},
95  {"i","ignore"},
96  {"s","shapes"},
97  {"j","jpegquality"},
98  {"p","pages"},
99  {0,0}
100 };
101
102 int args_callback_longoption(char*name,char*val) {
103     return args_long2shortoption(options, name, val);
104 }
105
106 int args_callback_command(char*name, char*val) {
107     if (!filename) 
108         filename = name;
109     else {
110         if(outputname)
111         {
112              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
113                      outputname, name);
114              exit(1);
115         }
116         outputname = name;
117     }
118     return 0;
119 }
120
121 void args_callback_usage(char*name)
122 {
123     printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf [output.swf]\n", name);
124     printf("\n");
125     printf("-p  --pages=range          Convert only pages in range\n");
126     printf("-P  --password=password    Use password for deciphering the pdf\n");
127     printf("-s  --shapes               Don't use SWF Fonts, but store everything as shape\n");
128     printf("-i  --ignore               Ignore draw order (makes the SWF file smaller)\n");
129     printf("-j  --jpegquality=quality  Set quality of embedded jpeg pictures (default:85)\n");
130     printf("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
131     printf("-V  --version              Print program version\n");
132 }
133
134 /* check whether the value t is in a given range.
135   examples: 3 is in range 1-10: true
136             7 is in range 2-4,6,8-10: false
137             9 is in range 1,2,3-12: true
138 */
139 char is_in_range(int t, char*irange)
140 {
141     char*pos = irange;
142     char*digits;
143     int num;
144     char range = 0;
145     int last=0;
146     char tmp;
147
148     if(!irange)  // no range resembles (-OO,OO)
149         return 1;
150
151     while(*pos)
152     {
153         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
154             pos++;
155
156         digits = pos;
157         while(*digits>='0' && *digits<='9')
158             digits++;
159         if(digits == pos) {
160             fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
161             exit(1);
162         }
163         
164         tmp=*digits;*digits=0;
165         num = atoi(pos);
166         *digits=tmp;
167         pos = digits;
168
169         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
170             pos++;
171
172         if(range && last<=t && num>=t)
173             return 1;
174         if(range) {
175             range = 0;
176             if(*pos)
177              pos ++;
178             continue;
179         }
180
181         if(*pos=='-')
182         {
183             if(range) {
184                 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
185                 exit(1);
186             }
187             last = num;
188             range = 1;
189             if(*pos)
190              pos ++;
191             continue;
192         } 
193         else 
194         {
195             /* if it isn't a '-', we assume it is a seperator like
196                ',', ';', ':', whatever. */
197             if(t == num)
198                 return 1;
199             if(*pos)
200              pos ++;
201             continue;
202         }
203     }
204     if(range && last<=t)
205         return 1;
206     return 0;
207 }
208
209 int main(int argn, char *argv[])
210 {
211     srand48(time(0));
212     processargs(argn, argv);
213     initLog(0,-1,0,0,-1,loglevel);
214     if(!outputname)
215     {
216         fprintf(stderr, "Please use -o to specify an output file\n");
217         exit(1);
218     }
219
220     // test if the page range is o.k.
221     is_in_range(0x7fffffff, pagerange);
222
223     if (!filename) {
224         args_callback_usage(argv[0]);
225         exit(0);
226     }
227
228     logf("<verbose> reading data files from %s\n", DATADIR);
229     //TODO: use tempnam here. Check if environment already contains a
230     //T1LIB_CONFIG.
231     putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
232     FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
233     fprintf(fi, "FONTDATABASE=%s/FontDataBase\n", DATADIR);
234     fprintf(fi, "ENCODING=%s:.\n", DATADIR);
235     fprintf(fi, "AFM=%s:.\n", DATADIR);
236     fprintf(fi, "TYPE1=%s:.\n", DATADIR);
237     fclose(fi);
238     /* initialize t1lib */
239     T1_SetBitmapPad( 16);
240     if ((T1_InitLib(NO_LOGFILE)==NULL)){
241         fprintf(stderr, "Initialization of t1lib failed\n");
242         exit(1);
243     }
244     unlink("/tmp/t1lib.config.tmp");
245
246     pdfswf_init(filename, password);
247     pdfswf_setoutputfilename(outputname);
248
249     int pages = pdfswf_numpages();
250     int t = 1;
251     for(t = 1; t <= pages; t++) 
252     {
253         if(is_in_range(t, pagerange))
254         pdfswf_convertpage(t);
255     }
256
257     pdfswf_close();
258     return 0;
259 }
260
261