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