]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/xpdf.cc
Import of xpdf code from gpdf.
[evince.git] / pdf / xpdf / xpdf.cc
1 //========================================================================
2 //
3 // xpdf.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #include <aconf.h>
10 #include "gtypes.h"
11 #include "GString.h"
12 #include "parseargs.h"
13 #include "gfile.h"
14 #include "gmem.h"
15 #include "GlobalParams.h"
16 #include "Object.h"
17 #include "XPDFApp.h"
18 #include "xpdfconfig.h"
19
20 //------------------------------------------------------------------------
21 // command line options
22 //------------------------------------------------------------------------
23
24 static char enableT1libStr[16] = "";
25 static char enableFreeTypeStr[16] = "";
26 static char antialiasStr[16] = "";
27 static char psFileArg[256];
28 static char paperSize[15] = "";
29 static int paperWidth = 0;
30 static int paperHeight = 0;
31 static GBool level1 = gFalse;
32 static char textEncName[128] = "";
33 static char textEOL[16] = "";
34 static char ownerPassword[33] = "\001";
35 static char userPassword[33] = "\001";
36 static GBool fullScreen = gFalse;
37 static char remoteName[100] = "xpdf_";
38 static GBool doRemoteReload = gFalse;
39 static GBool doRemoteRaise = gFalse;
40 static GBool doRemoteQuit = gFalse;
41 static GBool printCommands = gFalse;
42 static GBool quiet = gFalse;
43 static char cfgFileName[256] = "";
44 static GBool printVersion = gFalse;
45 static GBool printHelp = gFalse;
46
47 static ArgDesc argDesc[] = {
48   {"-g",          argStringDummy, NULL,           0,
49    "initial window geometry"},
50   {"-geometry",   argStringDummy, NULL,           0,
51    "initial window geometry"},
52   {"-title",      argStringDummy, NULL,           0,
53    "window title"},
54   {"-cmap",       argFlagDummy,   NULL,           0,
55    "install a private colormap"},
56   {"-rgb",        argIntDummy,    NULL,           0,
57    "biggest RGB cube to allocate (default is 5)"},
58   {"-rv",         argFlagDummy,   NULL,           0,
59    "reverse video"},
60   {"-papercolor", argStringDummy, NULL,           0,
61    "color of paper background"},
62   {"-z",          argStringDummy, NULL,           0,
63    "initial zoom level (percent, 'page', 'width')"},
64 #if HAVE_T1LIB_H
65   {"-t1lib",      argString,      enableT1libStr, sizeof(enableT1libStr),
66    "enable t1lib font rasterizer: yes, no"},
67 #endif
68 #if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
69   {"-freetype",   argString,      enableFreeTypeStr, sizeof(enableFreeTypeStr),
70    "enable FreeType font rasterizer: yes, no"},
71 #endif
72   {"-aa",         argString,      antialiasStr,   sizeof(antialiasStr),
73    "enable font anti-aliasing: yes, no"},
74   {"-ps",         argString,      psFileArg,      sizeof(psFileArg),
75    "default PostScript file name or command"},
76   {"-paper",      argString,      paperSize,      sizeof(paperSize),
77    "paper size (letter, legal, A4, A3, match)"},
78   {"-paperw",     argInt,         &paperWidth,    0,
79    "paper width, in points"},
80   {"-paperh",     argInt,         &paperHeight,   0,
81    "paper height, in points"},
82   {"-level1",     argFlag,        &level1,        0,
83    "generate Level 1 PostScript"},
84   {"-enc",    argString,   textEncName,    sizeof(textEncName),
85    "output text encoding name"},
86   {"-eol",    argString,   textEOL,        sizeof(textEOL),
87    "output end-of-line convention (unix, dos, or mac)"},
88   {"-opw",        argString,      ownerPassword,  sizeof(ownerPassword),
89    "owner password (for encrypted files)"},
90   {"-upw",        argString,      userPassword,   sizeof(userPassword),
91    "user password (for encrypted files)"},
92   {"-fullscreen", argFlag,        &fullScreen,    0,
93    "run in full-screen (presentation) mode"},
94   {"-remote",     argString,      remoteName + 5, sizeof(remoteName) - 5,
95    "start/contact xpdf remote server with specified name"},
96   {"-reload",     argFlag,        &doRemoteReload, 0,
97    "reload xpdf remove server window (with -remote only)"},
98   {"-raise",      argFlag,        &doRemoteRaise, 0,
99    "raise xpdf remote server window (with -remote only)"},
100   {"-quit",       argFlag,        &doRemoteQuit,  0,
101    "kill xpdf remote server (with -remote only)"},
102   {"-cmd",        argFlag,        &printCommands, 0,
103    "print commands as they're executed"},
104   {"-q",          argFlag,        &quiet,         0,
105    "don't print any messages or errors"},
106   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
107    "configuration file to use in place of .xpdfrc"},
108   {"-v",          argFlag,        &printVersion,  0,
109    "print copyright and version info"},
110   {"-h",          argFlag,        &printHelp,     0,
111    "print usage information"},
112   {"-help",       argFlag,        &printHelp,     0,
113    "print usage information"},
114   {"--help",  argFlag,     &printHelp,     0,
115    "print usage information"},
116   {"-?",      argFlag,     &printHelp,     0,
117    "print usage information"},
118   {NULL}
119 };
120
121 //------------------------------------------------------------------------
122
123 int main(int argc, char *argv[]) {
124   XPDFApp *app;
125   GString *fileName;
126   int pg;
127   GString *destName;
128   GString *userPasswordStr, *ownerPasswordStr;
129   GBool ok;
130   int exitCode;
131
132   exitCode = 0;
133   userPasswordStr = ownerPasswordStr = NULL;
134
135   // parse args
136   ok = parseArgs(argDesc, &argc, argv);
137   if (!ok || printVersion || printHelp) {
138     fprintf(stderr, "xpdf version %s\n", xpdfVersion);
139     fprintf(stderr, "%s\n", xpdfCopyright);
140     if (!printVersion) {
141       printUsage("xpdf", "[<PDF-file> [<page> | +<dest>]]", argDesc);
142     }
143     exitCode = 99;
144     goto done0;
145   }
146
147   // read config file
148   globalParams = new GlobalParams(cfgFileName);
149   globalParams->setupBaseFonts(NULL);
150   if (psFileArg[0]) {
151     globalParams->setPSFile(psFileArg);
152   }
153   if (paperSize[0]) {
154     if (!globalParams->setPSPaperSize(paperSize)) {
155       fprintf(stderr, "Invalid paper size\n");
156     }
157   } else {
158     if (paperWidth) {
159       globalParams->setPSPaperWidth(paperWidth);
160     }
161     if (paperHeight) {
162       globalParams->setPSPaperHeight(paperHeight);
163     }
164   }
165   if (level1) {
166     globalParams->setPSLevel(psLevel1);
167   }
168   if (textEncName[0]) {
169     globalParams->setTextEncoding(textEncName);
170   }
171   if (textEOL[0]) {
172     if (!globalParams->setTextEOL(textEOL)) {
173       fprintf(stderr, "Bad '-eol' value on command line\n");
174     }
175   }
176   if (enableT1libStr[0]) {
177     if (!globalParams->setEnableT1lib(enableT1libStr)) {
178       fprintf(stderr, "Bad '-t1lib' value on command line\n");
179     }
180   }
181   if (enableFreeTypeStr[0]) {
182     if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
183       fprintf(stderr, "Bad '-freetype' value on command line\n");
184     }
185   }
186   if (antialiasStr[0]) {
187     if (!globalParams->setAntialias(antialiasStr)) {
188       fprintf(stderr, "Bad '-aa' value on command line\n");
189     }
190   }
191   if (printCommands) {
192     globalParams->setPrintCommands(printCommands);
193   }
194   if (quiet) {
195     globalParams->setErrQuiet(quiet);
196   }
197
198   // create the XPDFApp object
199   app = new XPDFApp(&argc, argv);
200
201   // the initialZoom parameter can be set in either the config file or
202   // as an X resource (or command line arg)
203   if (app->getInitialZoom()) {
204     globalParams->setInitialZoom(app->getInitialZoom()->getCString());
205   }
206
207   // check command line
208   ok = ok && argc >= 1 && argc <= 3;
209   if (doRemoteReload) {
210     ok = ok && remoteName[5] && !doRemoteQuit && argc == 1;
211   }
212   if (doRemoteRaise) {
213     ok = ok && remoteName[5] && !doRemoteQuit;
214   }
215   if (doRemoteQuit) {
216     ok = ok && remoteName[5] && argc == 1;
217   }
218   if (!ok || printVersion || printHelp) {
219     fprintf(stderr, "xpdf version %s\n", xpdfVersion);
220     fprintf(stderr, "%s\n", xpdfCopyright);
221     if (!printVersion) {
222       printUsage("xpdf", "[<PDF-file> [<page> | +<dest>]]", argDesc);
223     }
224     exitCode = 99;
225     goto done1;
226   }
227   if (argc >= 2) {
228     fileName = new GString(argv[1]);
229   } else {
230     fileName = NULL;
231   }
232   pg = 1;
233   destName = NULL;
234   if (argc == 3) {
235     if (argv[2][0] == '+') {
236       destName = new GString(&argv[2][1]);
237     } else {
238       pg = atoi(argv[2]);
239     }
240   }
241
242   // handle remote server stuff
243   if (remoteName[5]) {
244     app->setRemoteName(remoteName);
245     if (app->remoteServerRunning()) {
246       if (fileName) {
247         if (destName) {
248           app->remoteOpenAtDest(fileName, destName, doRemoteRaise);
249         } else {
250           app->remoteOpen(fileName, pg, doRemoteRaise);
251         }
252       } else if (doRemoteReload) {
253         app->remoteReload(doRemoteRaise);
254       } else if (doRemoteRaise) {
255         app->remoteRaise();
256       } else if (doRemoteQuit) {
257         app->remoteQuit();
258       }
259       goto done2;
260     }
261     if (doRemoteQuit) {
262       goto done2;
263     }
264   }
265
266   // set options
267   app->setFullScreen(fullScreen);
268
269   // check for password string(s)
270   ownerPasswordStr = ownerPassword[0] != '\001' ? new GString(ownerPassword)
271                                                 : (GString *)NULL;
272   userPasswordStr = userPassword[0] != '\001' ? new GString(userPassword)
273                                               : (GString *)NULL;
274
275   // open the file and run the main loop
276   if (destName) {
277     if (!app->openAtDest(fileName, destName,
278                          ownerPasswordStr, userPasswordStr)) {
279       exitCode = 1;
280       goto done2;
281     }
282   } else {
283     if (!app->open(fileName, pg, ownerPasswordStr, userPasswordStr)) {
284       exitCode = 1;
285       goto done2;
286     }
287   }
288   app->run();
289
290   exitCode = 0;
291
292   // clean up
293  done2:
294   if (userPasswordStr) {
295     delete userPasswordStr;
296   }
297   if (ownerPasswordStr) {
298     delete ownerPasswordStr;
299   }
300   if (destName) {
301     delete destName;
302   }
303   if (fileName) {
304     delete fileName;
305   }
306  done1:
307   delete app;
308   delete globalParams;
309
310   // check for memory leaks
311  done0:
312   Object::memCheck(stderr);
313   gMemReport(stderr);
314
315   return exitCode;
316 }