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