filters

GlobalParams.cpp
1//========================================================================
2//
3// GlobalParams.cpp
4//
5// Copyright 2001-2002 Glyph & Cog, LLC
6//
7//========================================================================
8
9#include <aconf.h>
10
11#ifdef USE_GCC_PRAGMAS
12#pragma implementation
13#endif
14
15#include <string.h>
16#include <ctype.h>
17#ifdef HAVE_PAPER_H
18#include <paper.h>
19#endif
20#include "gmem.h"
21#include "GString.h"
22#include "GList.h"
23#include "GHash.h"
24#include "gfile.h"
25#include "Error.h"
26#include "NameToCharCode.h"
27#include "CharCodeToUnicode.h"
28#include "UnicodeMap.h"
29#include "CMap.h"
30#include "BuiltinFontTables.h"
31#include "FontEncodingTables.h"
32#include "GlobalParams.h"
33
34#include "NameToUnicodeTable.h"
35#include "UnicodeMapTables.h"
36#include "DisplayFontTable.h"
37#include "UTF8.h"
38
39//------------------------------------------------------------------------
40
41GlobalParams *globalParams = NULL;
42
43//------------------------------------------------------------------------
44// DisplayFontParam
45//------------------------------------------------------------------------
46
47DisplayFontParam::DisplayFontParam(GString *nameA,
48 DisplayFontParamKind kindA) {
49 name = nameA;
50 kind = kindA;
51 switch (kind) {
52 case displayFontX:
53 x.xlfd = NULL;
54 x.encoding = NULL;
55 break;
56 case displayFontT1:
57 t1.fileName = NULL;
58 break;
59 case displayFontTT:
60 tt.fileName = NULL;
61 break;
62 }
63}
64
65DisplayFontParam::DisplayFontParam(const char *nameA, const char *xlfdA,
66 const char *encodingA) {
67 name = new GString(nameA);
68 kind = displayFontX;
69 x.xlfd = new GString(xlfdA);
70 x.encoding = new GString(encodingA);
71}
72
73DisplayFontParam::~DisplayFontParam() {
74 delete name;
75 switch (kind) {
76 case displayFontX:
77 if (x.xlfd) {
78 delete x.xlfd;
79 }
80 if (x.encoding) {
81 delete x.encoding;
82 }
83 break;
84 case displayFontT1:
85 if (t1.fileName) {
86 delete t1.fileName;
87 }
88 break;
89 case displayFontTT:
90 if (tt.fileName) {
91 delete tt.fileName;
92 }
93 break;
94 }
95}
96
97//------------------------------------------------------------------------
98// PSFontParam
99//------------------------------------------------------------------------
100
101PSFontParam::PSFontParam(GString *pdfFontNameA, int wModeA,
102 GString *psFontNameA, GString *encodingA) {
103 pdfFontName = pdfFontNameA;
104 wMode = wModeA;
105 psFontName = psFontNameA;
106 encoding = encodingA;
107}
108
109PSFontParam::~PSFontParam() {
110 delete pdfFontName;
111 delete psFontName;
112 if (encoding) {
113 delete encoding;
114 }
115}
116
117//------------------------------------------------------------------------
118// parsing
119//------------------------------------------------------------------------
120
121GlobalParams::GlobalParams(const char *cfgFileName) {
122 UnicodeMap *map;
123 DisplayFontParam *dfp;
124 GString *fileName;
125 FILE *f;
126 int i;
127
128 initBuiltinFontTables();
129
130 // scan the encoding in reverse because we want the lowest-numbered
131 // index for each char name ('space' is encoded twice)
132 macRomanReverseMap = new NameToCharCode();
133 for (i = 255; i >= 0; --i) {
134 if (macRomanEncoding[i]) {
135 macRomanReverseMap->add(macRomanEncoding[i], (CharCode)i);
136 }
137 }
138
139 nameToUnicode = new NameToCharCode();
140 cidToUnicodes = new GHash(gTrue);
141 residentUnicodeMaps = new GHash();
142 unicodeMaps = new GHash(gTrue);
143 cMapDirs = new GHash(gTrue);
144 toUnicodeDirs = new GList();
145 displayFonts = new GHash();
146 displayCIDFonts = new GHash();
147 displayNamedCIDFonts = new GHash();
148#ifdef HAVE_PAPER_H
149 const char *paperName;
150 const struct paper *paperType;
151 paperinit();
152 if ((paperName = systempapername())) {
153 paperType = paperinfo(paperName);
154 psPaperWidth = (int)paperpswidth(paperType);
155 psPaperHeight = (int)paperpsheight(paperType);
156 } else {
157 error(-1, "No paper information available - using defaults");
158 psPaperWidth = defPaperWidth;
159 psPaperHeight = defPaperHeight;
160 }
161 paperdone();
162#else
163 psPaperWidth = defPaperWidth;
164 psPaperHeight = defPaperHeight;
165#endif
166 psDuplex = gFalse;
167 psLevel = psLevel2;
168 psFile = NULL;
169 psFonts = new GHash();
170 psNamedFonts16 = new GList();
171 psFonts16 = new GList();
172 psEmbedType1 = gTrue;
173 psEmbedTrueType = gTrue;
174 psEmbedCIDPostScript = gTrue;
175 psEmbedCIDTrueType = gTrue;
176 psOPI = gFalse;
177 psASCIIHex = gFalse;
178 textEncoding = new GString("Latin1");
179#if defined(WIN32)
180 textEOL = eolDOS;
181#elif defined(MACOS)
182 textEOL = eolMac;
183#else
184 textEOL = eolUnix;
185#endif
186 textKeepTinyChars = gFalse;
187 fontDirs = new GList();
188 initialZoom = new GString("1");
189 t1libControl = fontRastAALow;
190 freetypeControl = fontRastAALow;
191 urlCommand = NULL;
192 movieCommand = NULL;
193 mapNumericCharNames = gTrue;
194 printCommands = gFalse;
195 errQuiet = gFalse;
196
197 cidToUnicodeCache = new CIDToUnicodeCache();
198 unicodeMapCache = new UnicodeMapCache();
199 cMapCache = new CMapCache();
200
201 // set up the initial nameToUnicode table
202 for (i = 0; nameToUnicodeTab[i].name; ++i) {
203 nameToUnicode->add(nameToUnicodeTab[i].name, nameToUnicodeTab[i].u);
204 }
205
206 // set up the residentUnicodeMaps table
207 map = new UnicodeMap("Latin1", gFalse,
208 latin1UnicodeMapRanges, latin1UnicodeMapLen);
209 residentUnicodeMaps->add(map->getEncodingName(), map);
210 map = new UnicodeMap("ASCII7", gFalse,
211 ascii7UnicodeMapRanges, ascii7UnicodeMapLen);
212 residentUnicodeMaps->add(map->getEncodingName(), map);
213 map = new UnicodeMap("Symbol", gFalse,
214 symbolUnicodeMapRanges, symbolUnicodeMapLen);
215 residentUnicodeMaps->add(map->getEncodingName(), map);
216 map = new UnicodeMap("ZapfDingbats", gFalse, zapfDingbatsUnicodeMapRanges,
217 zapfDingbatsUnicodeMapLen);
218 residentUnicodeMaps->add(map->getEncodingName(), map);
219 map = new UnicodeMap("UTF-8", gTrue, &mapUTF8);
220 residentUnicodeMaps->add(map->getEncodingName(), map);
221 map = new UnicodeMap("UCS-2", gTrue, &mapUCS2);
222 residentUnicodeMaps->add(map->getEncodingName(), map);
223
224 // default displayFonts table
225 for (i = 0; displayFontTab[i].name; ++i) {
226 dfp = new DisplayFontParam(displayFontTab[i].name,
227 displayFontTab[i].xlfd,
228 displayFontTab[i].encoding);
229 displayFonts->add(dfp->name, dfp);
230 }
231
232 // look for a user config file, then a system-wide config file
233 f = NULL;
234 fileName = NULL;
235 if (cfgFileName && cfgFileName[0]) {
236 fileName = new GString(cfgFileName);
237 if (!(f = fopen(fileName->getCString(), "r"))) {
238 delete fileName;
239 }
240 }
241 if (!f) {
242 fileName = appendToPath(getHomeDir(), xpdfUserConfigFile);
243 if (!(f = fopen(fileName->getCString(), "r"))) {
244 delete fileName;
245 }
246 }
247 if (!f) {
248#if defined(WIN32) && !defined(__CYGWIN32__)
249 char buf[512];
250 i = GetModuleFileName(NULL, buf, sizeof(buf));
251 if (i <= 0 || i >= sizeof(buf)) {
252 // error or path too long for buffer - just use the current dir
253 buf[0] = '\0';
254 }
255 fileName = grabPath(buf);
256 appendToPath(fileName, xpdfSysConfigFile);
257#else
258 fileName = new GString(xpdfSysConfigFile);
259#endif
260 if (!(f = fopen(fileName->getCString(), "r"))) {
261 delete fileName;
262 }
263 }
264 if (f) {
265 parseFile(fileName, f);
266 delete fileName;
267 fclose(f);
268 }
269}
270
271void GlobalParams::parseFile(GString *fileName, FILE *f) {
272 int line;
273 GList *tokens;
274 GString *cmd, *incFile;
275 char *p1, *p2;
276 char buf[512];
277 FILE *f2;
278
279 line = 1;
280 while (fgets(buf, sizeof(buf) - 1, f)) {
281
282 // break the line into tokens
283 tokens = new GList();
284 p1 = buf;
285 while (*p1) {
286 for (; *p1 && isspace(*p1); ++p1) ;
287 if (!*p1) {
288 break;
289 }
290 if (*p1 == '"' || *p1 == '\'') {
291 for (p2 = p1 + 1; *p2 && *p2 != *p1; ++p2) ;
292 ++p1;
293 } else {
294 for (p2 = p1 + 1; *p2 && !isspace(*p2); ++p2) ;
295 }
296 tokens->append(new GString(p1, p2 - p1));
297 p1 = p2 + 1;
298 }
299
300 if (tokens->getLength() > 0 &&
301 ((GString *)tokens->get(0))->getChar(0) != '#') {
302 cmd = (GString *)tokens->get(0);
303 if (!cmd->cmp("include")) {
304 if (tokens->getLength() == 2) {
305 incFile = (GString *)tokens->get(1);
306 if ((f2 = fopen(incFile->getCString(), "r"))) {
307 parseFile(incFile, f2);
308 fclose(f2);
309 } else {
310 error(-1, "Couldn't find included config file: '%s' (%s:%d)",
311 incFile->getCString(), fileName->getCString(), line);
312 }
313 } else {
314 error(-1, "Bad 'include' config file command (%s:%d)",
315 fileName->getCString(), line);
316 }
317 } else if (!cmd->cmp("nameToUnicode")) {
318 parseNameToUnicode(tokens, fileName, line);
319 } else if (!cmd->cmp("cidToUnicode")) {
320 parseCIDToUnicode(tokens, fileName, line);
321 } else if (!cmd->cmp("unicodeMap")) {
322 parseUnicodeMap(tokens, fileName, line);
323 } else if (!cmd->cmp("cMapDir")) {
324 parseCMapDir(tokens, fileName, line);
325 } else if (!cmd->cmp("toUnicodeDir")) {
326 parseToUnicodeDir(tokens, fileName, line);
327 } else if (!cmd->cmp("displayFontX")) {
328 parseDisplayFont(tokens, displayFonts, displayFontX, fileName, line);
329 } else if (!cmd->cmp("displayFontT1")) {
330 parseDisplayFont(tokens, displayFonts, displayFontT1, fileName, line);
331 } else if (!cmd->cmp("displayFontTT")) {
332 parseDisplayFont(tokens, displayFonts, displayFontTT, fileName, line);
333 } else if (!cmd->cmp("displayCIDFontX")) {
334 parseDisplayFont(tokens, displayCIDFonts,
335 displayFontX, fileName, line);
336 } else if (!cmd->cmp("displayNamedCIDFontX")) {
337 parseDisplayFont(tokens, displayNamedCIDFonts,
338 displayFontX, fileName, line);
339 } else if (!cmd->cmp("psFile")) {
340 parsePSFile(tokens, fileName, line);
341 } else if (!cmd->cmp("psFont")) {
342 parsePSFont(tokens, fileName, line);
343 } else if (!cmd->cmp("psNamedFont16")) {
344 parsePSFont16("psNamedFont16", psNamedFonts16,
345 tokens, fileName, line);
346 } else if (!cmd->cmp("psFont16")) {
347 parsePSFont16("psFont16", psFonts16, tokens, fileName, line);
348 } else if (!cmd->cmp("psPaperSize")) {
349 parsePSPaperSize(tokens, fileName, line);
350 } else if (!cmd->cmp("psDuplex")) {
351 parseYesNo("psDuplex", &psDuplex, tokens, fileName, line);
352 } else if (!cmd->cmp("psLevel")) {
353 parsePSLevel(tokens, fileName, line);
354 } else if (!cmd->cmp("psEmbedType1Fonts")) {
355 parseYesNo("psEmbedType1", &psEmbedType1, tokens, fileName, line);
356 } else if (!cmd->cmp("psEmbedTrueTypeFonts")) {
357 parseYesNo("psEmbedTrueType", &psEmbedTrueType,
358 tokens, fileName, line);
359 } else if (!cmd->cmp("psEmbedCIDPostScriptFonts")) {
360 parseYesNo("psEmbedCIDPostScript", &psEmbedCIDPostScript,
361 tokens, fileName, line);
362 } else if (!cmd->cmp("psEmbedCIDTrueTypeFonts")) {
363 parseYesNo("psEmbedCIDTrueType", &psEmbedCIDTrueType,
364 tokens, fileName, line);
365 } else if (!cmd->cmp("psOPI")) {
366 parseYesNo("psOPI", &psOPI, tokens, fileName, line);
367 } else if (!cmd->cmp("psASCIIHex")) {
368 parseYesNo("psASCIIHex", &psASCIIHex, tokens, fileName, line);
369 } else if (!cmd->cmp("textEncoding")) {
370 parseTextEncoding(tokens, fileName, line);
371 } else if (!cmd->cmp("textEOL")) {
372 parseTextEOL(tokens, fileName, line);
373 } else if (!cmd->cmp("textKeepTinyChars")) {
374 parseYesNo("textKeepTinyChars", &textKeepTinyChars,
375 tokens, fileName, line);
376 } else if (!cmd->cmp("fontDir")) {
377 parseFontDir(tokens, fileName, line);
378 } else if (!cmd->cmp("initialZoom")) {
379 parseInitialZoom(tokens, fileName, line);
380 } else if (!cmd->cmp("t1libControl")) {
381 parseFontRastControl("t1libControl", &t1libControl,
382 tokens, fileName, line);
383 } else if (!cmd->cmp("freetypeControl")) {
384 parseFontRastControl("freetypeControl", &freetypeControl,
385 tokens, fileName, line);
386 } else if (!cmd->cmp("urlCommand")) {
387 parseCommand("urlCommand", &urlCommand, tokens, fileName, line);
388 } else if (!cmd->cmp("movieCommand")) {
389 parseCommand("movieCommand", &movieCommand, tokens, fileName, line);
390 } else if (!cmd->cmp("mapNumericCharNames")) {
391 parseYesNo("mapNumericCharNames", &mapNumericCharNames,
392 tokens, fileName, line);
393 } else if (!cmd->cmp("printCommands")) {
394 parseYesNo("printCommands", &printCommands, tokens, fileName, line);
395 } else if (!cmd->cmp("errQuiet")) {
396 parseYesNo("errQuiet", &errQuiet, tokens, fileName, line);
397 } else if (!cmd->cmp("fontpath") || !cmd->cmp("fontmap")) {
398 error(-1, "Unknown config file command");
399 error(-1, "-- the config file format has changed since Xpdf 0.9x");
400 } else {
401 error(-1, "Unknown config file command '%s' (%s:%d)",
402 cmd->getCString(), fileName->getCString(), line);
403 }
404 }
405
406 deleteGList(tokens, GString);
407 ++line;
408 }
409}
410
411void GlobalParams::parseNameToUnicode(GList *tokens, GString *fileName,
412 int line) {
413 GString *name;
414 char *tok1, *tok2;
415 FILE *f;
416 char buf[256];
417 int line2;
418 Unicode u;
419
420 if (tokens->getLength() != 2) {
421 error(-1, "Bad 'nameToUnicode' config file command (%s:%d)",
422 fileName->getCString(), line);
423 return;
424 }
425 name = (GString *)tokens->get(1);
426 if (!(f = fopen(name->getCString(), "r"))) {
427 error(-1, "Couldn't open 'nameToUnicode' file '%s'",
428 name->getCString());
429 return;
430 }
431 line2 = 1;
432 while (fgets(buf, sizeof(buf), f)) {
433 tok1 = strtok(buf, " \t\r\n");
434 tok2 = strtok(NULL, " \t\r\n");
435 if (tok1 && tok2) {
436 sscanf(tok1, "%x", &u);
437 nameToUnicode->add(tok2, u);
438 } else {
439 error(-1, "Bad line in 'nameToUnicode' file (%s:%d)", name, line2);
440 }
441 ++line2;
442 }
443 fclose(f);
444}
445
446void GlobalParams::parseCIDToUnicode(GList *tokens, GString *fileName,
447 int line) {
448 GString *collection, *name, *old;
449
450 if (tokens->getLength() != 3) {
451 error(-1, "Bad 'cidToUnicode' config file command (%s:%d)",
452 fileName->getCString(), line);
453 return;
454 }
455 collection = (GString *)tokens->get(1);
456 name = (GString *)tokens->get(2);
457 if ((old = (GString *)cidToUnicodes->remove(collection))) {
458 delete old;
459 }
460 cidToUnicodes->add(collection->copy(), name->copy());
461}
462
463void GlobalParams::parseUnicodeMap(GList *tokens, GString *fileName,
464 int line) {
465 GString *encodingName, *name, *old;
466
467 if (tokens->getLength() != 3) {
468 error(-1, "Bad 'unicodeMap' config file command (%s:%d)",
469 fileName->getCString(), line);
470 return;
471 }
472 encodingName = (GString *)tokens->get(1);
473 name = (GString *)tokens->get(2);
474 if ((old = (GString *)unicodeMaps->remove(encodingName))) {
475 delete old;
476 }
477 unicodeMaps->add(encodingName->copy(), name->copy());
478}
479
480void GlobalParams::parseCMapDir(GList *tokens, GString *fileName, int line) {
481 GString *collection, *dir;
482 GList *list;
483
484 if (tokens->getLength() != 3) {
485 error(-1, "Bad 'cMapDir' config file command (%s:%d)",
486 fileName->getCString(), line);
487 return;
488 }
489 collection = (GString *)tokens->get(1);
490 dir = (GString *)tokens->get(2);
491 if (!(list = (GList *)cMapDirs->lookup(collection))) {
492 list = new GList();
493 cMapDirs->add(collection->copy(), list);
494 }
495 list->append(dir->copy());
496}
497
498void GlobalParams::parseToUnicodeDir(GList *tokens, GString *fileName,
499 int line) {
500 if (tokens->getLength() != 2) {
501 error(-1, "Bad 'toUnicodeDir' config file command (%s:%d)",
502 fileName->getCString(), line);
503 return;
504 }
505 toUnicodeDirs->append(((GString *)tokens->get(1))->copy());
506}
507
508void GlobalParams::parseDisplayFont(GList *tokens, GHash *fontHash,
509 DisplayFontParamKind kind,
510 GString *fileName, int line) {
511 DisplayFontParam *param, *old;
512
513 if (tokens->getLength() < 2) {
514 goto err1;
515 }
516 param = new DisplayFontParam(((GString *)tokens->get(1))->copy(), kind);
517
518 switch (kind) {
519 case displayFontX:
520 if (tokens->getLength() != 4) {
521 goto err2;
522 }
523 param->x.xlfd = ((GString *)tokens->get(2))->copy();
524 param->x.encoding = ((GString *)tokens->get(3))->copy();
525 break;
526 case displayFontT1:
527 if (tokens->getLength() != 3) {
528 goto err2;
529 }
530 param->t1.fileName = ((GString *)tokens->get(2))->copy();
531 break;
532 case displayFontTT:
533 if (tokens->getLength() != 3) {
534 goto err2;
535 }
536 param->tt.fileName = ((GString *)tokens->get(2))->copy();
537 break;
538 }
539
540 if ((old = (DisplayFontParam *)fontHash->remove(param->name))) {
541 delete old;
542 }
543 fontHash->add(param->name, param);
544 return;
545
546 err2:
547 delete param;
548 err1:
549 error(-1, "Bad 'display*Font*' config file command (%s:%d)",
550 fileName->getCString(), line);
551}
552
553void GlobalParams::parsePSPaperSize(GList *tokens, GString *fileName,
554 int line) {
555 GString *tok;
556
557 if (tokens->getLength() == 2) {
558 tok = (GString *)tokens->get(1);
559 if (!setPSPaperSize(tok->getCString())) {
560 error(-1, "Bad 'psPaperSize' config file command (%s:%d)",
561 fileName->getCString(), line);
562 }
563 } else if (tokens->getLength() == 3) {
564 tok = (GString *)tokens->get(1);
565 psPaperWidth = atoi(tok->getCString());
566 tok = (GString *)tokens->get(2);
567 psPaperHeight = atoi(tok->getCString());
568 } else {
569 error(-1, "Bad 'psPaperSize' config file command (%s:%d)",
570 fileName->getCString(), line);
571 }
572}
573
574void GlobalParams::parsePSLevel(GList *tokens, GString *fileName, int line) {
575 GString *tok;
576
577 if (tokens->getLength() != 2) {
578 error(-1, "Bad 'psLevel' config file command (%s:%d)",
579 fileName->getCString(), line);
580 return;
581 }
582 tok = (GString *)tokens->get(1);
583 if (!tok->cmp("level1")) {
584 psLevel = psLevel1;
585 } else if (!tok->cmp("level1sep")) {
586 psLevel = psLevel1Sep;
587 } else if (!tok->cmp("level2")) {
588 psLevel = psLevel2;
589 } else if (!tok->cmp("level2sep")) {
590 psLevel = psLevel2Sep;
591 } else if (!tok->cmp("level3")) {
592 psLevel = psLevel3;
593 } else if (!tok->cmp("level3Sep")) {
594 psLevel = psLevel3Sep;
595 } else {
596 error(-1, "Bad 'psLevel' config file command (%s:%d)",
597 fileName->getCString(), line);
598 }
599}
600
601void GlobalParams::parsePSFile(GList *tokens, GString *fileName, int line) {
602 if (tokens->getLength() != 2) {
603 error(-1, "Bad 'psFile' config file command (%s:%d)",
604 fileName->getCString(), line);
605 return;
606 }
607 if (psFile) {
608 delete psFile;
609 }
610 psFile = ((GString *)tokens->get(1))->copy();
611}
612
613void GlobalParams::parsePSFont(GList *tokens, GString *fileName, int line) {
614 PSFontParam *param;
615
616 if (tokens->getLength() != 3) {
617 error(-1, "Bad 'psFont' config file command (%s:%d)",
618 fileName->getCString(), line);
619 return;
620 }
621 param = new PSFontParam(((GString *)tokens->get(1))->copy(), 0,
622 ((GString *)tokens->get(2))->copy(), NULL);
623 psFonts->add(param->pdfFontName, param);
624}
625
626void GlobalParams::parsePSFont16(const char *cmdName, GList *fontList,
627 GList *tokens, GString *fileName, int line) {
628 PSFontParam *param;
629 int wMode;
630 GString *tok;
631
632 if (tokens->getLength() != 5) {
633 error(-1, "Bad '%s' config file command (%s:%d)",
634 cmdName, fileName->getCString(), line);
635 return;
636 }
637 tok = (GString *)tokens->get(2);
638 if (!tok->cmp("H")) {
639 wMode = 0;
640 } else if (!tok->cmp("V")) {
641 wMode = 1;
642 } else {
643 error(-1, "Bad '%s' config file command (%s:%d)",
644 cmdName, fileName->getCString(), line);
645 return;
646 }
647 param = new PSFontParam(((GString *)tokens->get(1))->copy(),
648 wMode,
649 ((GString *)tokens->get(3))->copy(),
650 ((GString *)tokens->get(4))->copy());
651 fontList->append(param);
652}
653
654void GlobalParams::parseTextEncoding(GList *tokens, GString *fileName,
655 int line) {
656 if (tokens->getLength() != 2) {
657 error(-1, "Bad 'textEncoding' config file command (%s:%d)",
658 fileName->getCString(), line);
659 return;
660 }
661 delete textEncoding;
662 textEncoding = ((GString *)tokens->get(1))->copy();
663}
664
665void GlobalParams::parseTextEOL(GList *tokens, GString *fileName, int line) {
666 GString *tok;
667
668 if (tokens->getLength() != 2) {
669 error(-1, "Bad 'textEOL' config file command (%s:%d)",
670 fileName->getCString(), line);
671 return;
672 }
673 tok = (GString *)tokens->get(1);
674 if (!tok->cmp("unix")) {
675 textEOL = eolUnix;
676 } else if (!tok->cmp("dos")) {
677 textEOL = eolDOS;
678 } else if (!tok->cmp("mac")) {
679 textEOL = eolMac;
680 } else {
681 error(-1, "Bad 'textEOL' config file command (%s:%d)",
682 fileName->getCString(), line);
683 }
684}
685
686void GlobalParams::parseFontDir(GList *tokens, GString *fileName, int line) {
687 if (tokens->getLength() != 2) {
688 error(-1, "Bad 'fontDir' config file command (%s:%d)",
689 fileName->getCString(), line);
690 return;
691 }
692 fontDirs->append(((GString *)tokens->get(1))->copy());
693}
694
695void GlobalParams::parseInitialZoom(GList *tokens,
696 GString *fileName, int line) {
697 if (tokens->getLength() != 2) {
698 error(-1, "Bad 'initialZoom' config file command (%s:%d)",
699 fileName->getCString(), line);
700 return;
701 }
702 delete initialZoom;
703 initialZoom = ((GString *)tokens->get(1))->copy();
704}
705
706void GlobalParams::parseFontRastControl(const char *cmdName, FontRastControl *val,
707 GList *tokens, GString *fileName,
708 int line) {
709 GString *tok;
710
711 if (tokens->getLength() != 2) {
712 error(-1, "Bad '%s' config file command (%s:%d)",
713 cmdName, fileName->getCString(), line);
714 return;
715 }
716 tok = (GString *)tokens->get(1);
717 if (!setFontRastControl(val, tok->getCString())) {
718 error(-1, "Bad '%s' config file command (%s:%d)",
719 cmdName, fileName->getCString(), line);
720 }
721}
722
723void GlobalParams::parseCommand(const char *cmdName, GString **val,
724 GList *tokens, GString *fileName, int line) {
725 if (tokens->getLength() != 2) {
726 error(-1, "Bad '%s' config file command (%s:%d)",
727 cmdName, fileName->getCString(), line);
728 return;
729 }
730 if (*val) {
731 delete *val;
732 }
733 *val = ((GString *)tokens->get(1))->copy();
734}
735
736void GlobalParams::parseYesNo(const char *cmdName, GBool *flag,
737 GList *tokens, GString *fileName, int line) {
738 GString *tok;
739
740 if (tokens->getLength() != 2) {
741 error(-1, "Bad '%s' config file command (%s:%d)",
742 cmdName, fileName->getCString(), line);
743 return;
744 }
745 tok = (GString *)tokens->get(1);
746 if (!tok->cmp("yes")) {
747 *flag = gTrue;
748 } else if (!tok->cmp("no")) {
749 *flag = gFalse;
750 } else {
751 error(-1, "Bad '%s' config file command (%s:%d)",
752 cmdName, fileName->getCString(), line);
753 }
754}
755
756GlobalParams::~GlobalParams() {
757 GHashIter *iter;
758 GString *key;
759 GList *list;
760
761 freeBuiltinFontTables();
762
763 delete macRomanReverseMap;
764
765 delete nameToUnicode;
766 deleteGHash(cidToUnicodes, GString);
767 deleteGHash(residentUnicodeMaps, UnicodeMap);
768 deleteGHash(unicodeMaps, GString);
769 deleteGList(toUnicodeDirs, GString);
770 deleteGHash(displayFonts, DisplayFontParam);
771 deleteGHash(displayCIDFonts, DisplayFontParam);
772 deleteGHash(displayNamedCIDFonts, DisplayFontParam);
773 if (psFile) {
774 delete psFile;
775 }
776 deleteGHash(psFonts, PSFontParam);
777 deleteGList(psNamedFonts16, PSFontParam);
778 deleteGList(psFonts16, PSFontParam);
779 delete textEncoding;
780 deleteGList(fontDirs, GString);
781 delete initialZoom;
782 if (urlCommand) {
783 delete urlCommand;
784 }
785 if (movieCommand) {
786 delete movieCommand;
787 }
788
789 cMapDirs->startIter(&iter);
790 while (cMapDirs->getNext(&iter, &key, (void **)&list)) {
791 deleteGList(list, GString);
792 }
793 delete cMapDirs;
794
795 delete cidToUnicodeCache;
796 delete unicodeMapCache;
797 delete cMapCache;
798}
799
800//------------------------------------------------------------------------
801// accessors
802//------------------------------------------------------------------------
803
804CharCode GlobalParams::getMacRomanCharCode(const char *charName) {
805 return macRomanReverseMap->lookup(charName);
806}
807
808Unicode GlobalParams::mapNameToUnicode(const char *charName) {
809 return nameToUnicode->lookup(charName);
810}
811
812FILE *GlobalParams::getCIDToUnicodeFile(const GString *collection) {
813 GString *fileName;
814
815 if (!(fileName = (GString *)cidToUnicodes->lookup(collection))) {
816 return NULL;
817 }
818 return fopen(fileName->getCString(), "r");
819}
820
821UnicodeMap *GlobalParams::getResidentUnicodeMap(const GString *encodingName) {
822 return (UnicodeMap *)residentUnicodeMaps->lookup(encodingName);
823}
824
825FILE *GlobalParams::getUnicodeMapFile(const GString *encodingName) {
826 GString *fileName;
827
828 if (!(fileName = (GString *)unicodeMaps->lookup(encodingName))) {
829 return NULL;
830 }
831 return fopen(fileName->getCString(), "r");
832}
833
834FILE *GlobalParams::findCMapFile(const GString *collection, const GString *cMapName) {
835 GList *list;
836 GString *dir;
837 GString *fileName;
838 FILE *f;
839 int i;
840
841 if (!(list = (GList *)cMapDirs->lookup(collection))) {
842 return NULL;
843 }
844 for (i = 0; i < list->getLength(); ++i) {
845 dir = (GString *)list->get(i);
846 fileName = appendToPath(dir->copy(), cMapName->getCString());
847 f = fopen(fileName->getCString(), "r");
848 delete fileName;
849 if (f) {
850 return f;
851 }
852 }
853 return NULL;
854}
855
856FILE *GlobalParams::findToUnicodeFile(const GString *name) {
857 GString *dir, *fileName;
858 FILE *f;
859 int i;
860
861 for (i = 0; i < toUnicodeDirs->getLength(); ++i) {
862 dir = (GString *)toUnicodeDirs->get(i);
863 fileName = appendToPath(dir->copy(), name->getCString());
864 f = fopen(fileName->getCString(), "r");
865 delete fileName;
866 if (f) {
867 return f;
868 }
869 }
870 return NULL;
871}
872
873DisplayFontParam *GlobalParams::getDisplayFont(const GString *fontName) {
874 return (DisplayFontParam *)displayFonts->lookup(fontName);
875}
876
877DisplayFontParam *GlobalParams::getDisplayCIDFont(const GString *fontName,
878 const GString *collection) {
879 DisplayFontParam *dfp;
880
881 if (!fontName ||
882 !(dfp = (DisplayFontParam *)displayNamedCIDFonts->lookup(fontName))) {
883 dfp = (DisplayFontParam *)displayCIDFonts->lookup(collection);
884 }
885 return dfp;
886}
887
888PSFontParam *GlobalParams::getPSFont(GString *fontName) {
889 return (PSFontParam *)psFonts->lookup(fontName);
890}
891
892PSFontParam *GlobalParams::getPSFont16(GString *fontName,
893 GString *collection, int wMode) {
894 PSFontParam *p;
895 int i;
896
897 p = NULL;
898 if (fontName) {
899 for (i = 0; i < psNamedFonts16->getLength(); ++i) {
900 p = (PSFontParam *)psNamedFonts16->get(i);
901 if (!p->pdfFontName->cmp(fontName) &&
902 p->wMode == wMode) {
903 break;
904 }
905 p = NULL;
906 }
907 }
908 if (!p && collection) {
909 for (i = 0; i < psFonts16->getLength(); ++i) {
910 p = (PSFontParam *)psFonts16->get(i);
911 if (!p->pdfFontName->cmp(collection) &&
912 p->wMode == wMode) {
913 break;
914 }
915 p = NULL;
916 }
917 }
918 return p;
919}
920
921GString *GlobalParams::findFontFile(const GString *fontName,
922 const char *ext1, const char *ext2) {
923 GString *dir, *fileName;
924 FILE *f;
925 int i;
926
927 for (i = 0; i < fontDirs->getLength(); ++i) {
928 dir = (GString *)fontDirs->get(i);
929 if (ext1) {
930 fileName = appendToPath(dir->copy(), fontName->getCString());
931 fileName->append(ext1);
932 if ((f = fopen(fileName->getCString(), "r"))) {
933 fclose(f);
934 return fileName;
935 }
936 delete fileName;
937 }
938 if (ext2) {
939 fileName = appendToPath(dir->copy(), fontName->getCString());
940 fileName->append(ext2);
941 if ((f = fopen(fileName->getCString(), "r"))) {
942 fclose(f);
943 return fileName;
944 }
945 delete fileName;
946 }
947 }
948 return NULL;
949}
950
951CharCodeToUnicode *GlobalParams::getCIDToUnicode(GString *collection) {
952 return cidToUnicodeCache->getCIDToUnicode(collection);
953}
954
955UnicodeMap *GlobalParams::getUnicodeMap(GString *encodingName) {
956 UnicodeMap *map;
957
958 if ((map = getResidentUnicodeMap(encodingName))) {
959 map->incRefCnt();
960 return map;
961 }
962 return unicodeMapCache->getUnicodeMap(encodingName);
963}
964
965CMap *GlobalParams::getCMap(GString *collection, GString *cMapName) {
966 return cMapCache->getCMap(collection, cMapName);
967}
968
969UnicodeMap *GlobalParams::getTextEncoding() {
970 return getUnicodeMap(textEncoding);
971}
972
973//------------------------------------------------------------------------
974// functions to set parameters
975//------------------------------------------------------------------------
976
977void GlobalParams::addDisplayFont(DisplayFontParam *param) {
978 DisplayFontParam *old;
979
980 if ((old = (DisplayFontParam *)displayFonts->remove(param->name))) {
981 delete old;
982 }
983 displayFonts->add(param->name, param);
984}
985
986void GlobalParams::setPSFile(char *file) {
987 if (psFile) {
988 delete psFile;
989 }
990 psFile = new GString(file);
991}
992
993GBool GlobalParams::setPSPaperSize(char *size) {
994 if (!strcmp(size, "letter")) {
995 psPaperWidth = 612;
996 psPaperHeight = 792;
997 } else if (!strcmp(size, "legal")) {
998 psPaperWidth = 612;
999 psPaperHeight = 1008;
1000 } else if (!strcmp(size, "A4")) {
1001 psPaperWidth = 595;
1002 psPaperHeight = 842;
1003 } else if (!strcmp(size, "A3")) {
1004 psPaperWidth = 842;
1005 psPaperHeight = 1190;
1006 } else {
1007 return gFalse;
1008 }
1009 return gTrue;
1010}
1011
1012void GlobalParams::setPSPaperWidth(int width) {
1013 psPaperWidth = width;
1014}
1015
1016void GlobalParams::setPSPaperHeight(int height) {
1017 psPaperHeight = height;
1018}
1019
1020void GlobalParams::setPSDuplex(GBool duplex) {
1021 psDuplex = duplex;
1022}
1023
1024void GlobalParams::setPSLevel(PSLevel level) {
1025 psLevel = level;
1026}
1027
1028void GlobalParams::setPSEmbedType1(GBool embed) {
1029 psEmbedType1 = embed;
1030}
1031
1032void GlobalParams::setPSEmbedTrueType(GBool embed) {
1033 psEmbedTrueType = embed;
1034}
1035
1036void GlobalParams::setPSEmbedCIDPostScript(GBool embed) {
1037 psEmbedCIDPostScript = embed;
1038}
1039
1040void GlobalParams::setPSEmbedCIDTrueType(GBool embed) {
1041 psEmbedCIDTrueType = embed;
1042}
1043
1044void GlobalParams::setPSOPI(GBool opi) {
1045 psOPI = opi;
1046}
1047
1048void GlobalParams::setPSASCIIHex(GBool hex) {
1049 psASCIIHex = hex;
1050}
1051
1052void GlobalParams::setTextEncoding(char *encodingName) {
1053 delete textEncoding;
1054 textEncoding = new GString(encodingName);
1055}
1056
1057GBool GlobalParams::setTextEOL(char *s) {
1058 if (!strcmp(s, "unix")) {
1059 textEOL = eolUnix;
1060 } else if (!strcmp(s, "dos")) {
1061 textEOL = eolDOS;
1062 } else if (!strcmp(s, "mac")) {
1063 textEOL = eolMac;
1064 } else {
1065 return gFalse;
1066 }
1067 return gTrue;
1068}
1069
1070void GlobalParams::setTextKeepTinyChars(GBool keep) {
1071 textKeepTinyChars = keep;
1072}
1073
1074void GlobalParams::setInitialZoom(char *s) {
1075 delete initialZoom;
1076 initialZoom = new GString(s);
1077}
1078
1079GBool GlobalParams::setT1libControl(char *s) {
1080 return setFontRastControl(&t1libControl, s);
1081}
1082
1083GBool GlobalParams::setFreeTypeControl(char *s) {
1084 return setFontRastControl(&freetypeControl, s);
1085}
1086
1087GBool GlobalParams::setFontRastControl(FontRastControl *val, const char *s) {
1088 if (!strcmp(s, "none")) {
1089 *val = fontRastNone;
1090 } else if (!strcmp(s, "plain")) {
1091 *val = fontRastPlain;
1092 } else if (!strcmp(s, "low")) {
1093 *val = fontRastAALow;
1094 } else if (!strcmp(s, "high")) {
1095 *val = fontRastAAHigh;
1096 } else {
1097 return gFalse;
1098 }
1099 return gTrue;
1100}
1101
1102void GlobalParams::setMapNumericCharNames(GBool map) {
1103 mapNumericCharNames = map;
1104}
1105
1106void GlobalParams::setPrintCommands(GBool printCommandsA) {
1107 printCommands = printCommandsA;
1108}
1109
1110void GlobalParams::setErrQuiet(GBool errQuietA) {
1111 errQuiet = errQuietA;
1112}