filters

GlobalParams.cc

00001 //========================================================================
00002 //
00003 // GlobalParams.cc
00004 //
00005 // Copyright 2001-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include <string.h>
00016 #include <ctype.h>
00017 #ifdef HAVE_PAPER_H
00018 #include <paper.h>
00019 #endif
00020 #include "gmem.h"
00021 #include "GString.h"
00022 #include "GList.h"
00023 #include "GHash.h"
00024 #include "gfile.h"
00025 #include "Error.h"
00026 #include "NameToCharCode.h"
00027 #include "CharCodeToUnicode.h"
00028 #include "UnicodeMap.h"
00029 #include "CMap.h"
00030 #include "BuiltinFontTables.h"
00031 #include "FontEncodingTables.h"
00032 #include "GlobalParams.h"
00033 
00034 #include "NameToUnicodeTable.h"
00035 #include "UnicodeMapTables.h"
00036 #include "DisplayFontTable.h"
00037 #include "UTF8.h"
00038 
00039 //------------------------------------------------------------------------
00040 
00041 GlobalParams *globalParams = NULL;
00042 
00043 //------------------------------------------------------------------------
00044 // DisplayFontParam
00045 //------------------------------------------------------------------------
00046 
00047 DisplayFontParam::DisplayFontParam(GString *nameA,
00048                    DisplayFontParamKind kindA) {
00049   name = nameA;
00050   kind = kindA;
00051   switch (kind) {
00052   case displayFontX:
00053     x.xlfd = NULL;
00054     x.encoding = NULL;
00055     break;
00056   case displayFontT1:
00057     t1.fileName = NULL;
00058     break;
00059   case displayFontTT:
00060     tt.fileName = NULL;
00061     break;
00062   }
00063 }
00064 
00065 DisplayFontParam::DisplayFontParam(const char *nameA, const char *xlfdA,
00066                                    const char *encodingA) {
00067   name = new GString(nameA);
00068   kind = displayFontX;
00069   x.xlfd = new GString(xlfdA);
00070   x.encoding = new GString(encodingA);
00071 }
00072 
00073 DisplayFontParam::~DisplayFontParam() {
00074   delete name;
00075   switch (kind) {
00076   case displayFontX:
00077     if (x.xlfd) {
00078       delete x.xlfd;
00079     }
00080     if (x.encoding) {
00081       delete x.encoding;
00082     }
00083     break;
00084   case displayFontT1:
00085     if (t1.fileName) {
00086       delete t1.fileName;
00087     }
00088     break;
00089   case displayFontTT:
00090     if (tt.fileName) {
00091       delete tt.fileName;
00092     }
00093     break;
00094   }
00095 }
00096 
00097 //------------------------------------------------------------------------
00098 // PSFontParam
00099 //------------------------------------------------------------------------
00100 
00101 PSFontParam::PSFontParam(GString *pdfFontNameA, int wModeA,
00102              GString *psFontNameA, GString *encodingA) {
00103   pdfFontName = pdfFontNameA;
00104   wMode = wModeA;
00105   psFontName = psFontNameA;
00106   encoding = encodingA;
00107 }
00108 
00109 PSFontParam::~PSFontParam() {
00110   delete pdfFontName;
00111   delete psFontName;
00112   if (encoding) {
00113     delete encoding;
00114   }
00115 }
00116 
00117 //------------------------------------------------------------------------
00118 // parsing
00119 //------------------------------------------------------------------------
00120 
00121 GlobalParams::GlobalParams(const char *cfgFileName) {
00122   UnicodeMap *map;
00123   DisplayFontParam *dfp;
00124   GString *fileName;
00125   FILE *f;
00126   int i;
00127 
00128   initBuiltinFontTables();
00129 
00130   // scan the encoding in reverse because we want the lowest-numbered
00131   // index for each char name ('space' is encoded twice)
00132   macRomanReverseMap = new NameToCharCode();
00133   for (i = 255; i >= 0; --i) {
00134     if (macRomanEncoding[i]) {
00135       macRomanReverseMap->add(macRomanEncoding[i], (CharCode)i);
00136     }
00137   }
00138 
00139   nameToUnicode = new NameToCharCode();
00140   cidToUnicodes = new GHash(gTrue);
00141   residentUnicodeMaps = new GHash();
00142   unicodeMaps = new GHash(gTrue);
00143   cMapDirs = new GHash(gTrue);
00144   toUnicodeDirs = new GList();
00145   displayFonts = new GHash();
00146   displayCIDFonts = new GHash();
00147   displayNamedCIDFonts = new GHash();
00148 #ifdef HAVE_PAPER_H
00149   char *paperName;
00150   const struct paper *paperType;
00151   paperinit();
00152   if ((paperName = systempapername())) {
00153     paperType = paperinfo(paperName);
00154     psPaperWidth = (int)paperpswidth(paperType);
00155     psPaperHeight = (int)paperpsheight(paperType);
00156   } else {
00157     error(-1, "No paper information available - using defaults");
00158     psPaperWidth = defPaperWidth;
00159     psPaperHeight = defPaperHeight;
00160   }
00161   paperdone();
00162 #else
00163   psPaperWidth = defPaperWidth;
00164   psPaperHeight = defPaperHeight;
00165 #endif
00166   psDuplex = gFalse;
00167   psLevel = psLevel2;
00168   psFile = NULL;
00169   psFonts = new GHash();
00170   psNamedFonts16 = new GList();
00171   psFonts16 = new GList();
00172   psEmbedType1 = gTrue;
00173   psEmbedTrueType = gTrue;
00174   psEmbedCIDPostScript = gTrue;
00175   psEmbedCIDTrueType = gTrue;
00176   psOPI = gFalse;
00177   psASCIIHex = gFalse;
00178   textEncoding = new GString("Latin1");
00179 #if defined(WIN32)
00180   textEOL = eolDOS;
00181 #elif defined(MACOS)
00182   textEOL = eolMac;
00183 #else
00184   textEOL = eolUnix;
00185 #endif
00186   textKeepTinyChars = gFalse;
00187   fontDirs = new GList();
00188   initialZoom = new GString("1");
00189   t1libControl = fontRastAALow;
00190   freetypeControl = fontRastAALow;
00191   urlCommand = NULL;
00192   movieCommand = NULL;
00193   mapNumericCharNames = gTrue;
00194   printCommands = gFalse;
00195   errQuiet = gFalse;
00196 
00197   cidToUnicodeCache = new CIDToUnicodeCache();
00198   unicodeMapCache = new UnicodeMapCache();
00199   cMapCache = new CMapCache();
00200 
00201   // set up the initial nameToUnicode table
00202   for (i = 0; nameToUnicodeTab[i].name; ++i) {
00203     nameToUnicode->add(nameToUnicodeTab[i].name, nameToUnicodeTab[i].u);
00204   }
00205 
00206   // set up the residentUnicodeMaps table
00207   map = new UnicodeMap("Latin1", gFalse,
00208                latin1UnicodeMapRanges, latin1UnicodeMapLen);
00209   residentUnicodeMaps->add(map->getEncodingName(), map);
00210   map = new UnicodeMap("ASCII7", gFalse,
00211                ascii7UnicodeMapRanges, ascii7UnicodeMapLen);
00212   residentUnicodeMaps->add(map->getEncodingName(), map);
00213   map = new UnicodeMap("Symbol", gFalse,
00214                symbolUnicodeMapRanges, symbolUnicodeMapLen);
00215   residentUnicodeMaps->add(map->getEncodingName(), map);
00216   map = new UnicodeMap("ZapfDingbats", gFalse, zapfDingbatsUnicodeMapRanges,
00217                zapfDingbatsUnicodeMapLen);
00218   residentUnicodeMaps->add(map->getEncodingName(), map);
00219   map = new UnicodeMap("UTF-8", gTrue, &mapUTF8);
00220   residentUnicodeMaps->add(map->getEncodingName(), map);
00221   map = new UnicodeMap("UCS-2", gTrue, &mapUCS2);
00222   residentUnicodeMaps->add(map->getEncodingName(), map);
00223 
00224   // default displayFonts table
00225   for (i = 0; displayFontTab[i].name; ++i) {
00226     dfp = new DisplayFontParam(displayFontTab[i].name,
00227                    displayFontTab[i].xlfd,
00228                    displayFontTab[i].encoding);
00229     displayFonts->add(dfp->name, dfp);
00230   }
00231 
00232   // look for a user config file, then a system-wide config file
00233   f = NULL;
00234   fileName = NULL;
00235   if (cfgFileName && cfgFileName[0]) {
00236     fileName = new GString(cfgFileName);
00237     if (!(f = fopen(fileName->getCString(), "r"))) {
00238       delete fileName;
00239     }
00240   }
00241   if (!f) {
00242     fileName = appendToPath(getHomeDir(), xpdfUserConfigFile);
00243     if (!(f = fopen(fileName->getCString(), "r"))) {
00244       delete fileName;
00245     }
00246   }
00247   if (!f) {
00248 #if defined(WIN32) && !defined(__CYGWIN32__)
00249     char buf[512];
00250     i = GetModuleFileName(NULL, buf, sizeof(buf));
00251     if (i <= 0 || i >= sizeof(buf)) {
00252       // error or path too long for buffer - just use the current dir
00253       buf[0] = '\0';
00254     }
00255     fileName = grabPath(buf);
00256     appendToPath(fileName, xpdfSysConfigFile);
00257 #else
00258     fileName = new GString(xpdfSysConfigFile);
00259 #endif
00260     if (!(f = fopen(fileName->getCString(), "r"))) {
00261       delete fileName;
00262     }
00263   }
00264   if (f) {
00265     parseFile(fileName, f);
00266     delete fileName;
00267     fclose(f);
00268   }
00269 }
00270 
00271 void GlobalParams::parseFile(GString *fileName, FILE *f) {
00272   int line;
00273   GList *tokens;
00274   GString *cmd, *incFile;
00275   char *p1, *p2;
00276   char buf[512];
00277   FILE *f2;
00278 
00279   line = 1;
00280   while (fgets(buf, sizeof(buf) - 1, f)) {
00281 
00282     // break the line into tokens
00283     tokens = new GList();
00284     p1 = buf;
00285     while (*p1) {
00286       for (; *p1 && isspace(*p1); ++p1) ;
00287       if (!*p1) {
00288     break;
00289       }
00290       if (*p1 == '"' || *p1 == '\'') {
00291     for (p2 = p1 + 1; *p2 && *p2 != *p1; ++p2) ;
00292     ++p1;
00293       } else {
00294     for (p2 = p1 + 1; *p2 && !isspace(*p2); ++p2) ;
00295       }
00296       tokens->append(new GString(p1, p2 - p1));
00297       p1 = p2 + 1;
00298     }
00299 
00300     if (tokens->getLength() > 0 &&
00301     ((GString *)tokens->get(0))->getChar(0) != '#') {
00302       cmd = (GString *)tokens->get(0);
00303       if (!cmd->cmp("include")) {
00304     if (tokens->getLength() == 2) {
00305       incFile = (GString *)tokens->get(1);
00306       if ((f2 = fopen(incFile->getCString(), "r"))) {
00307         parseFile(incFile, f2);
00308         fclose(f2);
00309       } else {
00310         error(-1, "Couldn't find included config file: '%s' (%s:%d)",
00311           incFile->getCString(), fileName->getCString(), line);
00312       }
00313     } else {
00314       error(-1, "Bad 'include' config file command (%s:%d)",
00315         fileName->getCString(), line);
00316     }
00317       } else if (!cmd->cmp("nameToUnicode")) {
00318     parseNameToUnicode(tokens, fileName, line);
00319       } else if (!cmd->cmp("cidToUnicode")) {
00320     parseCIDToUnicode(tokens, fileName, line);
00321       } else if (!cmd->cmp("unicodeMap")) {
00322     parseUnicodeMap(tokens, fileName, line);
00323       } else if (!cmd->cmp("cMapDir")) {
00324     parseCMapDir(tokens, fileName, line);
00325       } else if (!cmd->cmp("toUnicodeDir")) {
00326     parseToUnicodeDir(tokens, fileName, line);
00327       } else if (!cmd->cmp("displayFontX")) {
00328     parseDisplayFont(tokens, displayFonts, displayFontX, fileName, line);
00329       } else if (!cmd->cmp("displayFontT1")) {
00330     parseDisplayFont(tokens, displayFonts, displayFontT1, fileName, line);
00331       } else if (!cmd->cmp("displayFontTT")) {
00332     parseDisplayFont(tokens, displayFonts, displayFontTT, fileName, line);
00333       } else if (!cmd->cmp("displayCIDFontX")) {
00334     parseDisplayFont(tokens, displayCIDFonts,
00335              displayFontX, fileName, line);
00336       } else if (!cmd->cmp("displayNamedCIDFontX")) {
00337     parseDisplayFont(tokens, displayNamedCIDFonts,
00338              displayFontX, fileName, line);
00339       } else if (!cmd->cmp("psFile")) {
00340     parsePSFile(tokens, fileName, line);
00341       } else if (!cmd->cmp("psFont")) {
00342     parsePSFont(tokens, fileName, line);
00343       } else if (!cmd->cmp("psNamedFont16")) {
00344     parsePSFont16("psNamedFont16", psNamedFonts16,
00345               tokens, fileName, line);
00346       } else if (!cmd->cmp("psFont16")) {
00347     parsePSFont16("psFont16", psFonts16, tokens, fileName, line);
00348       } else if (!cmd->cmp("psPaperSize")) {
00349     parsePSPaperSize(tokens, fileName, line);
00350       } else if (!cmd->cmp("psDuplex")) {
00351     parseYesNo("psDuplex", &psDuplex, tokens, fileName, line);
00352       } else if (!cmd->cmp("psLevel")) {
00353     parsePSLevel(tokens, fileName, line);
00354       } else if (!cmd->cmp("psEmbedType1Fonts")) {
00355     parseYesNo("psEmbedType1", &psEmbedType1, tokens, fileName, line);
00356       } else if (!cmd->cmp("psEmbedTrueTypeFonts")) {
00357     parseYesNo("psEmbedTrueType", &psEmbedTrueType,
00358            tokens, fileName, line);
00359       } else if (!cmd->cmp("psEmbedCIDPostScriptFonts")) {
00360     parseYesNo("psEmbedCIDPostScript", &psEmbedCIDPostScript,
00361            tokens, fileName, line);
00362       } else if (!cmd->cmp("psEmbedCIDTrueTypeFonts")) {
00363     parseYesNo("psEmbedCIDTrueType", &psEmbedCIDTrueType,
00364            tokens, fileName, line);
00365       } else if (!cmd->cmp("psOPI")) {
00366     parseYesNo("psOPI", &psOPI, tokens, fileName, line);
00367       } else if (!cmd->cmp("psASCIIHex")) {
00368     parseYesNo("psASCIIHex", &psASCIIHex, tokens, fileName, line);
00369       } else if (!cmd->cmp("textEncoding")) {
00370     parseTextEncoding(tokens, fileName, line);
00371       } else if (!cmd->cmp("textEOL")) {
00372     parseTextEOL(tokens, fileName, line);
00373       } else if (!cmd->cmp("textKeepTinyChars")) {
00374     parseYesNo("textKeepTinyChars", &textKeepTinyChars,
00375            tokens, fileName, line);
00376       } else if (!cmd->cmp("fontDir")) {
00377     parseFontDir(tokens, fileName, line);
00378       } else if (!cmd->cmp("initialZoom")) {
00379     parseInitialZoom(tokens, fileName, line);
00380       } else if (!cmd->cmp("t1libControl")) {
00381     parseFontRastControl("t1libControl", &t1libControl,
00382                  tokens, fileName, line);
00383       } else if (!cmd->cmp("freetypeControl")) {
00384     parseFontRastControl("freetypeControl", &freetypeControl,
00385                  tokens, fileName, line);
00386       } else if (!cmd->cmp("urlCommand")) {
00387     parseCommand("urlCommand", &urlCommand, tokens, fileName, line);
00388       } else if (!cmd->cmp("movieCommand")) {
00389     parseCommand("movieCommand", &movieCommand, tokens, fileName, line);
00390       } else if (!cmd->cmp("mapNumericCharNames")) {
00391     parseYesNo("mapNumericCharNames", &mapNumericCharNames,
00392            tokens, fileName, line);
00393       } else if (!cmd->cmp("printCommands")) {
00394     parseYesNo("printCommands", &printCommands, tokens, fileName, line);
00395       } else if (!cmd->cmp("errQuiet")) {
00396     parseYesNo("errQuiet", &errQuiet, tokens, fileName, line);
00397       } else if (!cmd->cmp("fontpath") || !cmd->cmp("fontmap")) {
00398     error(-1, "Unknown config file command");
00399     error(-1, "-- the config file format has changed since Xpdf 0.9x");
00400       } else {
00401     error(-1, "Unknown config file command '%s' (%s:%d)",
00402           cmd->getCString(), fileName->getCString(), line);
00403       }
00404     }
00405 
00406     deleteGList(tokens, GString);
00407     ++line;
00408   }
00409 }
00410 
00411 void GlobalParams::parseNameToUnicode(GList *tokens, GString *fileName,
00412                      int line) {
00413   GString *name;
00414   char *tok1, *tok2;
00415   FILE *f;
00416   char buf[256];
00417   int line2;
00418   Unicode u;
00419 
00420   if (tokens->getLength() != 2) {
00421     error(-1, "Bad 'nameToUnicode' config file command (%s:%d)",
00422       fileName->getCString(), line);
00423     return;
00424   }
00425   name = (GString *)tokens->get(1);
00426   if (!(f = fopen(name->getCString(), "r"))) {
00427     error(-1, "Couldn't open 'nameToUnicode' file '%s'",
00428       name->getCString());
00429     return;
00430   }
00431   line2 = 1;
00432   while (fgets(buf, sizeof(buf), f)) {
00433     tok1 = strtok(buf, " \t\r\n");
00434     tok2 = strtok(NULL, " \t\r\n");
00435     if (tok1 && tok2) {
00436       sscanf(tok1, "%x", &u);
00437       nameToUnicode->add(tok2, u);
00438     } else {
00439       error(-1, "Bad line in 'nameToUnicode' file (%s:%d)", name, line2);
00440     }
00441     ++line2;
00442   }
00443   fclose(f);
00444 }
00445 
00446 void GlobalParams::parseCIDToUnicode(GList *tokens, GString *fileName,
00447                      int line) {
00448   GString *collection, *name, *old;
00449 
00450   if (tokens->getLength() != 3) {
00451     error(-1, "Bad 'cidToUnicode' config file command (%s:%d)",
00452       fileName->getCString(), line);
00453     return;
00454   }
00455   collection = (GString *)tokens->get(1);
00456   name = (GString *)tokens->get(2);
00457   if ((old = (GString *)cidToUnicodes->remove(collection))) {
00458     delete old;
00459   }
00460   cidToUnicodes->add(collection->copy(), name->copy());
00461 }
00462 
00463 void GlobalParams::parseUnicodeMap(GList *tokens, GString *fileName,
00464                    int line) {
00465   GString *encodingName, *name, *old;
00466 
00467   if (tokens->getLength() != 3) {
00468     error(-1, "Bad 'unicodeMap' config file command (%s:%d)",
00469       fileName->getCString(), line);
00470     return;
00471   }
00472   encodingName = (GString *)tokens->get(1);
00473   name = (GString *)tokens->get(2);
00474   if ((old = (GString *)unicodeMaps->remove(encodingName))) {
00475     delete old;
00476   }
00477   unicodeMaps->add(encodingName->copy(), name->copy());
00478 }
00479 
00480 void GlobalParams::parseCMapDir(GList *tokens, GString *fileName, int line) {
00481   GString *collection, *dir;
00482   GList *list;
00483 
00484   if (tokens->getLength() != 3) {
00485     error(-1, "Bad 'cMapDir' config file command (%s:%d)",
00486       fileName->getCString(), line);
00487     return;
00488   }
00489   collection = (GString *)tokens->get(1);
00490   dir = (GString *)tokens->get(2);
00491   if (!(list = (GList *)cMapDirs->lookup(collection))) {
00492     list = new GList();
00493     cMapDirs->add(collection->copy(), list);
00494   }
00495   list->append(dir->copy());
00496 }
00497 
00498 void GlobalParams::parseToUnicodeDir(GList *tokens, GString *fileName,
00499                      int line) {
00500   if (tokens->getLength() != 2) {
00501     error(-1, "Bad 'toUnicodeDir' config file command (%s:%d)",
00502       fileName->getCString(), line);
00503     return;
00504   }
00505   toUnicodeDirs->append(((GString *)tokens->get(1))->copy());
00506 }
00507 
00508 void GlobalParams::parseDisplayFont(GList *tokens, GHash *fontHash,
00509                     DisplayFontParamKind kind,
00510                     GString *fileName, int line) {
00511   DisplayFontParam *param, *old;
00512 
00513   if (tokens->getLength() < 2) {
00514     goto err1;
00515   }
00516   param = new DisplayFontParam(((GString *)tokens->get(1))->copy(), kind);
00517 
00518   switch (kind) {
00519   case displayFontX:
00520     if (tokens->getLength() != 4) {
00521       goto err2;
00522     }
00523     param->x.xlfd = ((GString *)tokens->get(2))->copy();
00524     param->x.encoding = ((GString *)tokens->get(3))->copy();
00525     break;
00526   case displayFontT1:
00527     if (tokens->getLength() != 3) {
00528       goto err2;
00529     }
00530     param->t1.fileName = ((GString *)tokens->get(2))->copy();
00531     break;
00532   case displayFontTT:
00533     if (tokens->getLength() != 3) {
00534       goto err2;
00535     }
00536     param->tt.fileName = ((GString *)tokens->get(2))->copy();
00537     break;
00538   }
00539 
00540   if ((old = (DisplayFontParam *)fontHash->remove(param->name))) {
00541     delete old;
00542   }
00543   fontHash->add(param->name, param);
00544   return;
00545 
00546  err2:
00547   delete param;
00548  err1:
00549   error(-1, "Bad 'display*Font*' config file command (%s:%d)",
00550     fileName->getCString(), line);
00551 }
00552 
00553 void GlobalParams::parsePSPaperSize(GList *tokens, GString *fileName,
00554                     int line) {
00555   GString *tok;
00556 
00557   if (tokens->getLength() == 2) {
00558     tok = (GString *)tokens->get(1);
00559     if (!setPSPaperSize(tok->getCString())) {
00560       error(-1, "Bad 'psPaperSize' config file command (%s:%d)",
00561         fileName->getCString(), line);
00562     }
00563   } else if (tokens->getLength() == 3) {
00564     tok = (GString *)tokens->get(1);
00565     psPaperWidth = atoi(tok->getCString());
00566     tok = (GString *)tokens->get(2);
00567     psPaperHeight = atoi(tok->getCString());
00568   } else {
00569     error(-1, "Bad 'psPaperSize' config file command (%s:%d)",
00570       fileName->getCString(), line);
00571   }
00572 }
00573 
00574 void GlobalParams::parsePSLevel(GList *tokens, GString *fileName, int line) {
00575   GString *tok;
00576 
00577   if (tokens->getLength() != 2) {
00578     error(-1, "Bad 'psLevel' config file command (%s:%d)",
00579       fileName->getCString(), line);
00580     return;
00581   }
00582   tok = (GString *)tokens->get(1);
00583   if (!tok->cmp("level1")) {
00584     psLevel = psLevel1;
00585   } else if (!tok->cmp("level1sep")) {
00586     psLevel = psLevel1Sep;
00587   } else if (!tok->cmp("level2")) {
00588     psLevel = psLevel2;
00589   } else if (!tok->cmp("level2sep")) {
00590     psLevel = psLevel2Sep;
00591   } else if (!tok->cmp("level3")) {
00592     psLevel = psLevel3;
00593   } else if (!tok->cmp("level3Sep")) {
00594     psLevel = psLevel3Sep;
00595   } else {
00596     error(-1, "Bad 'psLevel' config file command (%s:%d)",
00597       fileName->getCString(), line);
00598   }
00599 }
00600 
00601 void GlobalParams::parsePSFile(GList *tokens, GString *fileName, int line) {
00602   if (tokens->getLength() != 2) {
00603     error(-1, "Bad 'psFile' config file command (%s:%d)",
00604       fileName->getCString(), line);
00605     return;
00606   }
00607   if (psFile) {
00608     delete psFile;
00609   }
00610   psFile = ((GString *)tokens->get(1))->copy();
00611 }
00612 
00613 void GlobalParams::parsePSFont(GList *tokens, GString *fileName, int line) {
00614   PSFontParam *param;
00615 
00616   if (tokens->getLength() != 3) {
00617     error(-1, "Bad 'psFont' config file command (%s:%d)",
00618       fileName->getCString(), line);
00619     return;
00620   }
00621   param = new PSFontParam(((GString *)tokens->get(1))->copy(), 0,
00622               ((GString *)tokens->get(2))->copy(), NULL);
00623   psFonts->add(param->pdfFontName, param);
00624 }
00625 
00626 void GlobalParams::parsePSFont16(const char *cmdName, GList *fontList,
00627                  GList *tokens, GString *fileName, int line) {
00628   PSFontParam *param;
00629   int wMode;
00630   GString *tok;
00631 
00632   if (tokens->getLength() != 5) {
00633     error(-1, "Bad '%s' config file command (%s:%d)",
00634       cmdName, fileName->getCString(), line);
00635     return;
00636   }
00637   tok = (GString *)tokens->get(2);
00638   if (!tok->cmp("H")) {
00639     wMode = 0;
00640   } else if (!tok->cmp("V")) {
00641     wMode = 1;
00642   } else {
00643     error(-1, "Bad '%s' config file command (%s:%d)",
00644       cmdName, fileName->getCString(), line);
00645     return;
00646   }
00647   param = new PSFontParam(((GString *)tokens->get(1))->copy(),
00648               wMode,
00649               ((GString *)tokens->get(3))->copy(),
00650               ((GString *)tokens->get(4))->copy());
00651   fontList->append(param);
00652 }
00653 
00654 void GlobalParams::parseTextEncoding(GList *tokens, GString *fileName,
00655                      int line) {
00656   if (tokens->getLength() != 2) {
00657     error(-1, "Bad 'textEncoding' config file command (%s:%d)",
00658       fileName->getCString(), line);
00659     return;
00660   }
00661   delete textEncoding;
00662   textEncoding = ((GString *)tokens->get(1))->copy();
00663 }
00664 
00665 void GlobalParams::parseTextEOL(GList *tokens, GString *fileName, int line) {
00666   GString *tok;
00667 
00668   if (tokens->getLength() != 2) {
00669     error(-1, "Bad 'textEOL' config file command (%s:%d)",
00670       fileName->getCString(), line);
00671     return;
00672   }
00673   tok = (GString *)tokens->get(1);
00674   if (!tok->cmp("unix")) {
00675     textEOL = eolUnix;
00676   } else if (!tok->cmp("dos")) {
00677     textEOL = eolDOS;
00678   } else if (!tok->cmp("mac")) {
00679     textEOL = eolMac;
00680   } else {
00681     error(-1, "Bad 'textEOL' config file command (%s:%d)",
00682       fileName->getCString(), line);
00683   }
00684 }
00685 
00686 void GlobalParams::parseFontDir(GList *tokens, GString *fileName, int line) {
00687   if (tokens->getLength() != 2) {
00688     error(-1, "Bad 'fontDir' config file command (%s:%d)",
00689       fileName->getCString(), line);
00690     return;
00691   }
00692   fontDirs->append(((GString *)tokens->get(1))->copy());
00693 }
00694 
00695 void GlobalParams::parseInitialZoom(GList *tokens,
00696                     GString *fileName, int line) {
00697   if (tokens->getLength() != 2) {
00698     error(-1, "Bad 'initialZoom' config file command (%s:%d)",
00699       fileName->getCString(), line);
00700     return;
00701   }
00702   delete initialZoom;
00703   initialZoom = ((GString *)tokens->get(1))->copy();
00704 }
00705 
00706 void GlobalParams::parseFontRastControl(const char *cmdName, FontRastControl *val,
00707                     GList *tokens, GString *fileName,
00708                     int line) {
00709   GString *tok;
00710 
00711   if (tokens->getLength() != 2) {
00712     error(-1, "Bad '%s' config file command (%s:%d)",
00713       cmdName, fileName->getCString(), line);
00714     return;
00715   }
00716   tok = (GString *)tokens->get(1);
00717   if (!setFontRastControl(val, tok->getCString())) {
00718     error(-1, "Bad '%s' config file command (%s:%d)",
00719       cmdName, fileName->getCString(), line);
00720   }
00721 }
00722 
00723 void GlobalParams::parseCommand(const char *cmdName, GString **val,
00724                 GList *tokens, GString *fileName, int line) {
00725   if (tokens->getLength() != 2) {
00726     error(-1, "Bad '%s' config file command (%s:%d)",
00727       cmdName, fileName->getCString(), line);
00728     return;
00729   }
00730   if (*val) {
00731     delete *val;
00732   }
00733   *val = ((GString *)tokens->get(1))->copy();
00734 }
00735 
00736 void GlobalParams::parseYesNo(const char *cmdName, GBool *flag,
00737                   GList *tokens, GString *fileName, int line) {
00738   GString *tok;
00739 
00740   if (tokens->getLength() != 2) {
00741     error(-1, "Bad '%s' config file command (%s:%d)",
00742       cmdName, fileName->getCString(), line);
00743     return;
00744   }
00745   tok = (GString *)tokens->get(1);
00746   if (!tok->cmp("yes")) {
00747     *flag = gTrue;
00748   } else if (!tok->cmp("no")) {
00749     *flag = gFalse;
00750   } else {
00751     error(-1, "Bad '%s' config file command (%s:%d)",
00752       cmdName, fileName->getCString(), line);
00753   }
00754 }
00755 
00756 GlobalParams::~GlobalParams() {
00757   GHashIter *iter;
00758   GString *key;
00759   GList *list;
00760 
00761   freeBuiltinFontTables();
00762 
00763   delete macRomanReverseMap;
00764 
00765   delete nameToUnicode;
00766   deleteGHash(cidToUnicodes, GString);
00767   deleteGHash(residentUnicodeMaps, UnicodeMap);
00768   deleteGHash(unicodeMaps, GString);
00769   deleteGList(toUnicodeDirs, GString);
00770   deleteGHash(displayFonts, DisplayFontParam);
00771   deleteGHash(displayCIDFonts, DisplayFontParam);
00772   deleteGHash(displayNamedCIDFonts, DisplayFontParam);
00773   if (psFile) {
00774     delete psFile;
00775   }
00776   deleteGHash(psFonts, PSFontParam);
00777   deleteGList(psNamedFonts16, PSFontParam);
00778   deleteGList(psFonts16, PSFontParam);
00779   delete textEncoding;
00780   deleteGList(fontDirs, GString);
00781   delete initialZoom;
00782   if (urlCommand) {
00783     delete urlCommand;
00784   }
00785   if (movieCommand) {
00786     delete movieCommand;
00787   }
00788 
00789   cMapDirs->startIter(&iter);
00790   while (cMapDirs->getNext(&iter, &key, (void **)&list)) {
00791     deleteGList(list, GString);
00792   }
00793   delete cMapDirs;
00794 
00795   delete cidToUnicodeCache;
00796   delete unicodeMapCache;
00797   delete cMapCache;
00798 }
00799 
00800 //------------------------------------------------------------------------
00801 // accessors
00802 //------------------------------------------------------------------------
00803 
00804 CharCode GlobalParams::getMacRomanCharCode(const char *charName) {
00805   return macRomanReverseMap->lookup(charName);
00806 }
00807 
00808 Unicode GlobalParams::mapNameToUnicode(const char *charName) {
00809   return nameToUnicode->lookup(charName);
00810 }
00811 
00812 FILE *GlobalParams::getCIDToUnicodeFile(const GString *collection) {
00813   GString *fileName;
00814 
00815   if (!(fileName = (GString *)cidToUnicodes->lookup(collection))) {
00816     return NULL;
00817   }
00818   return fopen(fileName->getCString(), "r");
00819 }
00820 
00821 UnicodeMap *GlobalParams::getResidentUnicodeMap(const GString *encodingName) {
00822   return (UnicodeMap *)residentUnicodeMaps->lookup(encodingName);
00823 }
00824 
00825 FILE *GlobalParams::getUnicodeMapFile(const GString *encodingName) {
00826   GString *fileName;
00827 
00828   if (!(fileName = (GString *)unicodeMaps->lookup(encodingName))) {
00829     return NULL;
00830   }
00831   return fopen(fileName->getCString(), "r");
00832 }
00833 
00834 FILE *GlobalParams::findCMapFile(const GString *collection, const GString *cMapName) {
00835   GList *list;
00836   GString *dir;
00837   GString *fileName;
00838   FILE *f;
00839   int i;
00840 
00841   if (!(list = (GList *)cMapDirs->lookup(collection))) {
00842     return NULL;
00843   }
00844   for (i = 0; i < list->getLength(); ++i) {
00845     dir = (GString *)list->get(i);
00846     fileName = appendToPath(dir->copy(), cMapName->getCString());
00847     f = fopen(fileName->getCString(), "r");
00848     delete fileName;
00849     if (f) {
00850       return f;
00851     }
00852   }
00853   return NULL;
00854 }
00855 
00856 FILE *GlobalParams::findToUnicodeFile(const GString *name) {
00857   GString *dir, *fileName;
00858   FILE *f;
00859   int i;
00860 
00861   for (i = 0; i < toUnicodeDirs->getLength(); ++i) {
00862     dir = (GString *)toUnicodeDirs->get(i);
00863     fileName = appendToPath(dir->copy(), name->getCString());
00864     f = fopen(fileName->getCString(), "r");
00865     delete fileName;
00866     if (f) {
00867       return f;
00868     }
00869   }
00870   return NULL;
00871 }
00872 
00873 DisplayFontParam *GlobalParams::getDisplayFont(const GString *fontName) {
00874   return (DisplayFontParam *)displayFonts->lookup(fontName);
00875 }
00876 
00877 DisplayFontParam *GlobalParams::getDisplayCIDFont(const GString *fontName,
00878                           const GString *collection) {
00879   DisplayFontParam *dfp;
00880 
00881   if (!fontName ||
00882       !(dfp = (DisplayFontParam *)displayNamedCIDFonts->lookup(fontName))) {
00883     dfp = (DisplayFontParam *)displayCIDFonts->lookup(collection);
00884   }
00885   return dfp;
00886 }
00887 
00888 PSFontParam *GlobalParams::getPSFont(GString *fontName) {
00889   return (PSFontParam *)psFonts->lookup(fontName);
00890 }
00891 
00892 PSFontParam *GlobalParams::getPSFont16(GString *fontName,
00893                        GString *collection, int wMode) {
00894   PSFontParam *p;
00895   int i;
00896 
00897   p = NULL;
00898   if (fontName) {
00899     for (i = 0; i < psNamedFonts16->getLength(); ++i) {
00900       p = (PSFontParam *)psNamedFonts16->get(i);
00901       if (!p->pdfFontName->cmp(fontName) &&
00902       p->wMode == wMode) {
00903     break;
00904       }
00905       p = NULL;
00906     }
00907   }
00908   if (!p && collection) {
00909     for (i = 0; i < psFonts16->getLength(); ++i) {
00910       p = (PSFontParam *)psFonts16->get(i);
00911       if (!p->pdfFontName->cmp(collection) &&
00912       p->wMode == wMode) {
00913     break;
00914       }
00915       p = NULL;
00916     }
00917   }
00918   return p;
00919 }
00920 
00921 GString *GlobalParams::findFontFile(const GString *fontName,
00922                     const char *ext1, const char *ext2) {
00923   GString *dir, *fileName;
00924   FILE *f;
00925   int i;
00926 
00927   for (i = 0; i < fontDirs->getLength(); ++i) {
00928     dir = (GString *)fontDirs->get(i);
00929     if (ext1) {
00930       fileName = appendToPath(dir->copy(), fontName->getCString());
00931       fileName->append(ext1);
00932       if ((f = fopen(fileName->getCString(), "r"))) {
00933     fclose(f);
00934     return fileName;
00935       }
00936       delete fileName;
00937     }
00938     if (ext2) {
00939       fileName = appendToPath(dir->copy(), fontName->getCString());
00940       fileName->append(ext2);
00941       if ((f = fopen(fileName->getCString(), "r"))) {
00942     fclose(f);
00943     return fileName;
00944       }
00945       delete fileName;
00946     }
00947   }
00948   return NULL;
00949 }
00950 
00951 CharCodeToUnicode *GlobalParams::getCIDToUnicode(GString *collection) {
00952   return cidToUnicodeCache->getCIDToUnicode(collection);
00953 }
00954 
00955 UnicodeMap *GlobalParams::getUnicodeMap(GString *encodingName) {
00956   UnicodeMap *map;
00957 
00958   if ((map = getResidentUnicodeMap(encodingName))) {
00959     map->incRefCnt();
00960     return map;
00961   }
00962   return unicodeMapCache->getUnicodeMap(encodingName);
00963 }
00964 
00965 CMap *GlobalParams::getCMap(GString *collection, GString *cMapName) {
00966   return cMapCache->getCMap(collection, cMapName);
00967 }
00968 
00969 UnicodeMap *GlobalParams::getTextEncoding() {
00970   return getUnicodeMap(textEncoding);
00971 }
00972 
00973 //------------------------------------------------------------------------
00974 // functions to set parameters
00975 //------------------------------------------------------------------------
00976 
00977 void GlobalParams::addDisplayFont(DisplayFontParam *param) {
00978   DisplayFontParam *old;
00979 
00980   if ((old = (DisplayFontParam *)displayFonts->remove(param->name))) {
00981     delete old;
00982   }
00983   displayFonts->add(param->name, param);
00984 }
00985 
00986 void GlobalParams::setPSFile(char *file) {
00987   if (psFile) {
00988     delete psFile;
00989   }
00990   psFile = new GString(file);
00991 }
00992 
00993 GBool GlobalParams::setPSPaperSize(char *size) {
00994   if (!strcmp(size, "letter")) {
00995     psPaperWidth = 612;
00996     psPaperHeight = 792;
00997   } else if (!strcmp(size, "legal")) {
00998     psPaperWidth = 612;
00999     psPaperHeight = 1008;
01000   } else if (!strcmp(size, "A4")) {
01001     psPaperWidth = 595;
01002     psPaperHeight = 842;
01003   } else if (!strcmp(size, "A3")) {
01004     psPaperWidth = 842;
01005     psPaperHeight = 1190;
01006   } else {
01007     return gFalse;
01008   }
01009   return gTrue;
01010 }
01011 
01012 void GlobalParams::setPSPaperWidth(int width) {
01013   psPaperWidth = width;
01014 }
01015 
01016 void GlobalParams::setPSPaperHeight(int height) {
01017   psPaperHeight = height;
01018 }
01019 
01020 void GlobalParams::setPSDuplex(GBool duplex) {
01021   psDuplex = duplex;
01022 }
01023 
01024 void GlobalParams::setPSLevel(PSLevel level) {
01025   psLevel = level;
01026 }
01027 
01028 void GlobalParams::setPSEmbedType1(GBool embed) {
01029   psEmbedType1 = embed;
01030 }
01031 
01032 void GlobalParams::setPSEmbedTrueType(GBool embed) {
01033   psEmbedTrueType = embed;
01034 }
01035 
01036 void GlobalParams::setPSEmbedCIDPostScript(GBool embed) {
01037   psEmbedCIDPostScript = embed;
01038 }
01039 
01040 void GlobalParams::setPSEmbedCIDTrueType(GBool embed) {
01041   psEmbedCIDTrueType = embed;
01042 }
01043 
01044 void GlobalParams::setPSOPI(GBool opi) {
01045   psOPI = opi;
01046 }
01047 
01048 void GlobalParams::setPSASCIIHex(GBool hex) {
01049   psASCIIHex = hex;
01050 }
01051 
01052 void GlobalParams::setTextEncoding(char *encodingName) {
01053   delete textEncoding;
01054   textEncoding = new GString(encodingName);
01055 }
01056 
01057 GBool GlobalParams::setTextEOL(char *s) {
01058   if (!strcmp(s, "unix")) {
01059     textEOL = eolUnix;
01060   } else if (!strcmp(s, "dos")) {
01061     textEOL = eolDOS;
01062   } else if (!strcmp(s, "mac")) {
01063     textEOL = eolMac;
01064   } else {
01065     return gFalse;
01066   }
01067   return gTrue;
01068 }
01069 
01070 void GlobalParams::setTextKeepTinyChars(GBool keep) {
01071   textKeepTinyChars = keep;
01072 }
01073 
01074 void GlobalParams::setInitialZoom(char *s) {
01075   delete initialZoom;
01076   initialZoom = new GString(s);
01077 }
01078 
01079 GBool GlobalParams::setT1libControl(char *s) {
01080   return setFontRastControl(&t1libControl, s);
01081 }
01082 
01083 GBool GlobalParams::setFreeTypeControl(char *s) {
01084   return setFontRastControl(&freetypeControl, s);
01085 }
01086 
01087 GBool GlobalParams::setFontRastControl(FontRastControl *val, const char *s) {
01088   if (!strcmp(s, "none")) {
01089     *val = fontRastNone;
01090   } else if (!strcmp(s, "plain")) {
01091     *val = fontRastPlain;
01092   } else if (!strcmp(s, "low")) {
01093     *val = fontRastAALow;
01094   } else if (!strcmp(s, "high")) {
01095     *val = fontRastAAHigh;
01096   } else {
01097     return gFalse;
01098   }
01099   return gTrue;
01100 }
01101 
01102 void GlobalParams::setMapNumericCharNames(GBool map) {
01103   mapNumericCharNames = map;
01104 }
01105 
01106 void GlobalParams::setPrintCommands(GBool printCommandsA) {
01107   printCommands = printCommandsA;
01108 }
01109 
01110 void GlobalParams::setErrQuiet(GBool errQuietA) {
01111   errQuiet = errQuietA;
01112 }
KDE Home | KDE Accessibility Home | Description of Access Keys