]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdftops.cc
disable font embedding hack introduced on 2002-12-09 to fix build with
[evince.git] / pdf / xpdf / pdftops.cc
1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stddef.h>
13 #include <string.h>
14 #include "parseargs.h"
15 #include "GString.h"
16 #include "gmem.h"
17 #include "GlobalParams.h"
18 #include "Object.h"
19 #include "Stream.h"
20 #include "Array.h"
21 #include "Dict.h"
22 #include "XRef.h"
23 #include "Catalog.h"
24 #include "Page.h"
25 #include "PDFDoc.h"
26 #include "PSOutputDev.h"
27 #include "Error.h"
28 #include "config.h"
29
30 static int firstPage = 1;
31 static int lastPage = 0;
32 static GBool level1 = gFalse;
33 static GBool level1Sep = gFalse;
34 static GBool level2 = gFalse;
35 static GBool level2Sep = gFalse;
36 static GBool level3 = gFalse;
37 static GBool level3Sep = gFalse;
38 static GBool doEPS = gFalse;
39 static GBool doForm = gFalse;
40 #if OPI_SUPPORT
41 static GBool doOPI = gFalse;
42 #endif
43 static GBool noEmbedT1Fonts = gFalse;
44 static GBool noEmbedTTFonts = gFalse;
45 static GBool noEmbedCIDPSFonts = gFalse;
46 static GBool noEmbedCIDTTFonts = gFalse;
47 static char paperSize[15] = "";
48 static int paperWidth = 0;
49 static int paperHeight = 0;
50 static GBool duplex = gFalse;
51 static char ownerPassword[33] = "";
52 static char userPassword[33] = "";
53 static GBool quiet = gFalse;
54 static char cfgFileName[256] = "";
55 static GBool printVersion = gFalse;
56 static GBool printHelp = gFalse;
57
58 static ArgDesc argDesc[] = {
59   {"-f",      argInt,      &firstPage,      0,
60    "first page to print"},
61   {"-l",      argInt,      &lastPage,       0,
62    "last page to print"},
63   {"-level1", argFlag,     &level1,         0,
64    "generate Level 1 PostScript"},
65   {"-level1sep", argFlag,  &level1Sep,      0,
66    "generate Level 1 separable PostScript"},
67   {"-level2", argFlag,     &level2,         0,
68    "generate Level 2 PostScript"},
69   {"-level2sep", argFlag,  &level2Sep,      0,
70    "generate Level 2 separable PostScript"},
71   {"-level3", argFlag,     &level3,         0,
72    "generate Level 3 PostScript"},
73   {"-level3sep", argFlag,  &level3Sep,      0,
74    "generate Level 3 separable PostScript"},
75   {"-eps",    argFlag,     &doEPS,          0,
76    "generate Encapsulated PostScript (EPS)"},
77   {"-form",   argFlag,     &doForm,         0,
78    "generate a PostScript form"},
79 #if OPI_SUPPORT
80   {"-opi",    argFlag,     &doOPI,          0,
81    "generate OPI comments"},
82 #endif
83   {"-noembt1", argFlag,     &noEmbedT1Fonts, 0,
84    "don't embed Type 1 fonts"},
85   {"-noembtt", argFlag,    &noEmbedTTFonts, 0,
86    "don't embed TrueType fonts"},
87   {"-noembcidps", argFlag, &noEmbedCIDPSFonts, 0,
88    "don't embed CID PostScript fonts"},
89   {"-noembcidtt", argFlag, &noEmbedCIDTTFonts, 0,
90    "don't embed CID TrueType fonts"},
91   {"-paper",  argString,   paperSize,       sizeof(paperSize),
92    "paper size (letter, legal, A4, A3)"},
93   {"-paperw", argInt,      &paperWidth,     0,
94    "paper width, in points"},
95   {"-paperh", argInt,      &paperHeight,    0,
96    "paper height, in points"},
97   {"-duplex", argFlag,     &duplex,         0,
98    "enable duplex printing"},
99   {"-opw",    argString,   ownerPassword,   sizeof(ownerPassword),
100    "owner password (for encrypted files)"},
101   {"-upw",    argString,   userPassword,    sizeof(userPassword),
102    "user password (for encrypted files)"},
103   {"-q",      argFlag,     &quiet,          0,
104    "don't print any messages or errors"},
105   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
106    "configuration file to use in place of .xpdfrc"},
107   {"-v",      argFlag,     &printVersion,   0,
108    "print copyright and version info"},
109   {"-h",      argFlag,     &printHelp,      0,
110    "print usage information"},
111   {"-help",   argFlag,     &printHelp,      0,
112    "print usage information"},
113   {"--help",  argFlag,     &printHelp,      0,
114    "print usage information"},
115   {"-?",      argFlag,     &printHelp,      0,
116    "print usage information"},
117   {NULL}
118 };
119
120 int main(int argc, char *argv[]) {
121   PDFDoc *doc;
122   GString *fileName;
123   GString *psFileName;
124   PSLevel level;
125   PSOutMode mode;
126   GString *ownerPW, *userPW;
127   PSOutputDev *psOut;
128   GBool ok;
129   char *p;
130
131   // parse args
132   ok = parseArgs(argDesc, &argc, argv);
133   if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
134     fprintf(stderr, "pdftops version %s\n", xpdfVersion);
135     fprintf(stderr, "%s\n", xpdfCopyright);
136     if (!printVersion) {
137       printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
138     }
139     exit(1);
140   }
141   if ((level1 ? 1 : 0) +
142       (level1Sep ? 1 : 0) +
143       (level2 ? 1 : 0) +
144       (level2Sep ? 1 : 0) +
145       (level3 ? 1 : 0) +
146       (level3Sep ? 1 : 0) > 1) {
147     fprintf(stderr, "Error: use only one of the 'level' options.\n");
148     exit(1);
149   }
150   if (doEPS && doForm) {
151     fprintf(stderr, "Error: use only one of -eps and -form\n");
152     exit(1);
153   }
154   if (level1) {
155     level = psLevel1;
156   } else if (level1Sep) {
157     level = psLevel1Sep;
158   } else if (level2Sep) {
159     level = psLevel2Sep;
160   } else if (level3) {
161     level = psLevel3;
162   } else if (level3Sep) {
163     level = psLevel3Sep;
164   } else {
165     level = psLevel2;
166   }
167   if (doForm && level < psLevel2) {
168     fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
169     exit(1);
170   }
171   mode = doEPS ? psModeEPS
172                : doForm ? psModeForm
173                         : psModePS;
174   fileName = new GString(argv[1]);
175
176   // read config file
177   globalParams = new GlobalParams(cfgFileName);
178   if (paperSize[0]) {
179     if (!globalParams->setPSPaperSize(paperSize)) {
180       fprintf(stderr, "Invalid paper size\n");
181       exit(1);
182     }
183   } else {
184     if (paperWidth) {
185       globalParams->setPSPaperWidth(paperWidth);
186     }
187     if (paperHeight) {
188       globalParams->setPSPaperHeight(paperHeight);
189     }
190   }
191   if (duplex) {
192     globalParams->setPSDuplex(duplex);
193   }
194   if (level1 || level1Sep || level2 || level2Sep || level3 || level3Sep) {
195     globalParams->setPSLevel(level);
196   }
197   if (noEmbedT1Fonts) {
198     globalParams->setPSEmbedType1(!noEmbedT1Fonts);
199   }
200   if (noEmbedTTFonts) {
201     globalParams->setPSEmbedTrueType(!noEmbedTTFonts);
202   }
203   if (noEmbedCIDPSFonts) {
204     globalParams->setPSEmbedCIDPostScript(!noEmbedCIDPSFonts);
205   }
206   if (noEmbedCIDTTFonts) {
207     globalParams->setPSEmbedCIDTrueType(!noEmbedCIDTTFonts);
208   }
209 #if OPI_SUPPORT
210   if (doOPI) {
211     globalParams->setPSOPI(doOPI);
212   }
213 #endif
214   if (quiet) {
215     globalParams->setErrQuiet(quiet);
216   }
217
218   // open PDF file
219   if (ownerPassword[0]) {
220     ownerPW = new GString(ownerPassword);
221   } else {
222     ownerPW = NULL;
223   }
224   if (userPassword[0]) {
225     userPW = new GString(userPassword);
226   } else {
227     userPW = NULL;
228   }
229   doc = new PDFDoc(fileName, ownerPW, userPW);
230   if (userPW) {
231     delete userPW;
232   }
233   if (ownerPW) {
234     delete ownerPW;
235   }
236   if (!doc->isOk()) {
237     goto err1;
238   }
239
240   // check for print permission
241   if (!doc->okToPrint()) {
242     error(-1, "Printing this document is not allowed.");
243     goto err1;
244   }
245
246   // construct PostScript file name
247   if (argc == 3) {
248     psFileName = new GString(argv[2]);
249   } else {
250     p = fileName->getCString() + fileName->getLength() - 4;
251     if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
252       psFileName = new GString(fileName->getCString(),
253                                fileName->getLength() - 4);
254     } else {
255       psFileName = fileName->copy();
256     }
257     psFileName->append(doEPS ? ".eps" : ".ps");
258   }
259
260   // get page range
261   if (firstPage < 1) {
262     firstPage = 1;
263   }
264   if (lastPage < 1 || lastPage > doc->getNumPages()) {
265     lastPage = doc->getNumPages();
266   }
267
268   // check for multi-page EPS or form
269   if ((doEPS || doForm) && firstPage != lastPage) {
270     error(-1, "EPS and form files can only contain one page.");
271     goto err2;
272   }
273
274   // write PostScript file
275   psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
276                           doc->getCatalog(), firstPage, lastPage, mode);
277   if (psOut->isOk()) {
278     doc->displayPages(psOut, firstPage, lastPage, 72, 0, gFalse);
279   }
280   delete psOut;
281
282   // clean up
283  err2:
284   delete psFileName;
285  err1:
286   delete doc;
287   delete globalParams;
288
289   // check for memory leaks
290   Object::memCheck(stderr);
291   gMemReport(stderr);
292
293   return 0;
294 }