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