filters

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