globalparams.cc
来自「将pdf文档转换为高质量的html文档」· CC 代码 · 共 2,012 行 · 第 1/4 页
CC
2,012 行
//========================================================================//// GlobalParams.cc//// Copyright 2001-2003 Glyph & Cog, LLC////========================================================================#include <aconf.h>#ifdef USE_GCC_PRAGMAS#pragma implementation#endif#include <string.h>#include <stdio.h>#include <ctype.h>#ifdef ENABLE_PLUGINS# ifndef WIN32# include <dlfcn.h># endif#endif#ifdef WIN32# include <shlobj.h>#endif#if HAVE_PAPER_H#include <paper.h>#endif#include "gmem.h"#include "GString.h"#include "GList.h"#include "GHash.h"#include "gfile.h"#include "Error.h"#include "NameToCharCode.h"#include "CharCodeToUnicode.h"#include "UnicodeMap.h"#include "CMap.h"#include "BuiltinFontTables.h"#include "FontEncodingTables.h"#ifdef ENABLE_PLUGINS# include "XpdfPluginAPI.h"#endif#include "GlobalParams.h"#if MULTITHREADED# define lockGlobalParams gLockMutex(&mutex)# define lockUnicodeMapCache gLockMutex(&unicodeMapCacheMutex)# define lockCMapCache gLockMutex(&cMapCacheMutex)# define unlockGlobalParams gUnlockMutex(&mutex)# define unlockUnicodeMapCache gUnlockMutex(&unicodeMapCacheMutex)# define unlockCMapCache gUnlockMutex(&cMapCacheMutex)#else# define lockGlobalParams# define lockUnicodeMapCache# define lockCMapCache# define unlockGlobalParams# define unlockUnicodeMapCache# define unlockCMapCache#endif#include "NameToUnicodeTable.h"#include "UnicodeMapTables.h"#include "UTF8.h"#ifdef ENABLE_PLUGINS# ifdef WIN32extern XpdfPluginVecTable xpdfPluginVecTable;# endif#endif//------------------------------------------------------------------------#define cidToUnicodeCacheSize 4#define unicodeToUnicodeCacheSize 4//------------------------------------------------------------------------static struct { char *name; char *t1FileName; char *ttFileName;} displayFontTab[] = { {"Courier", "n022003l.pfb", "cour.ttf"}, {"Courier-Bold", "n022004l.pfb", "courbd.ttf"}, {"Courier-BoldOblique", "n022024l.pfb", "courbi.ttf"}, {"Courier-Oblique", "n022023l.pfb", "couri.ttf"}, {"Helvetica", "n019003l.pfb", "arial.ttf"}, {"Helvetica-Bold", "n019004l.pfb", "arialbd.ttf"}, {"Helvetica-BoldOblique", "n019024l.pfb", "arialbi.ttf"}, {"Helvetica-Oblique", "n019023l.pfb", "ariali.ttf"}, {"Symbol", "s050000l.pfb", NULL}, {"Times-Bold", "n021004l.pfb", "timesbd.ttf"}, {"Times-BoldItalic", "n021024l.pfb", "timesbi.ttf"}, {"Times-Italic", "n021023l.pfb", "timesi.ttf"}, {"Times-Roman", "n021003l.pfb", "times.ttf"}, {"ZapfDingbats", "d050000l.pfb", NULL}, {NULL}};#ifdef WIN32static char *displayFontDirs[] = { "c:/windows/fonts", "c:/winnt/fonts", NULL};#elsestatic char *displayFontDirs[] = { "/usr/share/ghostscript/fonts", "/usr/local/share/ghostscript/fonts", "/usr/share/fonts/default/Type1", "/usr/share/fonts/default/ghostscript", "/usr/share/fonts/type1/gsfonts", NULL};#endif//------------------------------------------------------------------------GlobalParams *globalParams = NULL;//------------------------------------------------------------------------// DisplayFontParam//------------------------------------------------------------------------DisplayFontParam::DisplayFontParam(GString *nameA, DisplayFontParamKind kindA) { name = nameA; kind = kindA; switch (kind) { case displayFontT1: t1.fileName = NULL; break; case displayFontTT: tt.fileName = NULL; break; }}DisplayFontParam::~DisplayFontParam() { delete name; switch (kind) { case displayFontT1: if (t1.fileName) { delete t1.fileName; } break; case displayFontTT: if (tt.fileName) { delete tt.fileName; } break; }}//------------------------------------------------------------------------// PSFontParam//------------------------------------------------------------------------PSFontParam::PSFontParam(GString *pdfFontNameA, int wModeA, GString *psFontNameA, GString *encodingA) { pdfFontName = pdfFontNameA; wMode = wModeA; psFontName = psFontNameA; encoding = encodingA;}PSFontParam::~PSFontParam() { delete pdfFontName; delete psFontName; if (encoding) { delete encoding; }}#ifdef ENABLE_PLUGINS//------------------------------------------------------------------------// Plugin//------------------------------------------------------------------------class Plugin {public: static Plugin *load(char *type, char *name); ~Plugin();private:#ifdef WIN32 Plugin(HMODULE libA); HMODULE lib;#else Plugin(void *dlA); void *dl;#endif};Plugin *Plugin::load(char *type, char *name) { GString *path; Plugin *plugin; XpdfPluginVecTable *vt; XpdfBool (*xpdfInitPlugin)(void);#ifdef WIN32 HMODULE libA;#else void *dlA;#endif path = globalParams->getBaseDir(); appendToPath(path, "plugins"); appendToPath(path, type); appendToPath(path, name);#ifdef WIN32 path->append(".dll"); if (!(libA = LoadLibrary(path->getCString()))) { error(-1, "Failed to load plugin '%s'", path->getCString()); goto err1; } if (!(vt = (XpdfPluginVecTable *) GetProcAddress(libA, "xpdfPluginVecTable"))) { error(-1, "Failed to find xpdfPluginVecTable in plugin '%s'", path->getCString()); goto err2; }#else //~ need to deal with other extensions here path->append(".so"); if (!(dlA = dlopen(path->getCString(), RTLD_NOW))) { error(-1, "Failed to load plugin '%s': %s", path->getCString(), dlerror()); goto err1; } if (!(vt = (XpdfPluginVecTable *)dlsym(dlA, "xpdfPluginVecTable"))) { error(-1, "Failed to find xpdfPluginVecTable in plugin '%s'", path->getCString()); goto err2; }#endif if (vt->version != xpdfPluginVecTable.version) { error(-1, "Plugin '%s' is wrong version", path->getCString()); goto err2; } memcpy(vt, &xpdfPluginVecTable, sizeof(xpdfPluginVecTable));#ifdef WIN32 if (!(xpdfInitPlugin = (XpdfBool (*)(void)) GetProcAddress(libA, "xpdfInitPlugin"))) { error(-1, "Failed to find xpdfInitPlugin in plugin '%s'", path->getCString()); goto err2; }#else if (!(xpdfInitPlugin = (XpdfBool (*)(void))dlsym(dlA, "xpdfInitPlugin"))) { error(-1, "Failed to find xpdfInitPlugin in plugin '%s'", path->getCString()); goto err2; }#endif if (!(*xpdfInitPlugin)()) { error(-1, "Initialization of plugin '%s' failed", path->getCString()); goto err2; }#ifdef WIN32 plugin = new Plugin(libA);#else plugin = new Plugin(dlA);#endif delete path; return plugin; err2:#ifdef WIN32 FreeLibrary(libA);#else dlclose(dlA);#endif err1: delete path; return NULL;}#ifdef WIN32Plugin::Plugin(HMODULE libA) { lib = libA;}#elsePlugin::Plugin(void *dlA) { dl = dlA;}#endifPlugin::~Plugin() { void (*xpdfFreePlugin)(void);#ifdef WIN32 if ((xpdfFreePlugin = (void (*)(void)) GetProcAddress(lib, "xpdfFreePlugin"))) { (*xpdfFreePlugin)(); } FreeLibrary(lib);#else if ((xpdfFreePlugin = (void (*)(void))dlsym(dl, "xpdfFreePlugin"))) { (*xpdfFreePlugin)(); } dlclose(dl);#endif}#endif // ENABLE_PLUGINS//------------------------------------------------------------------------// parsing//------------------------------------------------------------------------GlobalParams::GlobalParams(char *cfgFileName) { UnicodeMap *map; GString *fileName; FILE *f; int i;#if MULTITHREADED gInitMutex(&mutex); gInitMutex(&unicodeMapCacheMutex); gInitMutex(&cMapCacheMutex);#endif initBuiltinFontTables(); // scan the encoding in reverse because we want the lowest-numbered // index for each char name ('space' is encoded twice) macRomanReverseMap = new NameToCharCode(); for (i = 255; i >= 0; --i) { if (macRomanEncoding[i]) { macRomanReverseMap->add(macRomanEncoding[i], (CharCode)i); } }#ifdef WIN32 // baseDir will be set by a call to setBaseDir baseDir = new GString();#else baseDir = appendToPath(getHomeDir(), ".xpdf");#endif nameToUnicode = new NameToCharCode(); cidToUnicodes = new GHash(gTrue); unicodeToUnicodes = new GHash(gTrue); residentUnicodeMaps = new GHash(); unicodeMaps = new GHash(gTrue); cMapDirs = new GHash(gTrue); toUnicodeDirs = new GList(); displayFonts = new GHash(); displayCIDFonts = new GHash(); displayNamedCIDFonts = new GHash();#if HAVE_PAPER_H char *paperName; const struct paper *paperType; paperinit(); if ((paperName = systempapername())) { paperType = paperinfo(paperName); psPaperWidth = (int)paperpswidth(paperType); psPaperHeight = (int)paperpsheight(paperType); } else { error(-1, "No paper information available - using defaults"); psPaperWidth = defPaperWidth; psPaperHeight = defPaperHeight; } paperdone();#else psPaperWidth = defPaperWidth; psPaperHeight = defPaperHeight;#endif psImageableLLX = psImageableLLY = 0; psImageableURX = psPaperWidth; psImageableURY = psPaperHeight; psCrop = gTrue; psExpandSmaller = gFalse; psShrinkLarger = gTrue; psCenter = gTrue; psDuplex = gFalse; psLevel = psLevel2; psFile = NULL; psFonts = new GHash(); psNamedFonts16 = new GList(); psFonts16 = new GList(); psEmbedType1 = gTrue; psEmbedTrueType = gTrue; psEmbedCIDPostScript = gTrue; psEmbedCIDTrueType = gTrue; psOPI = gFalse; psASCIIHex = gFalse; textEncoding = new GString("Latin1");#if defined(WIN32) textEOL = eolDOS;#elif defined(MACOS) textEOL = eolMac;#else textEOL = eolUnix;#endif textPageBreaks = gTrue; textKeepTinyChars = gFalse; fontDirs = new GList(); initialZoom = new GString("125"); continuousView = gFalse; enableT1lib = gTrue; enableFreeType = gTrue; antialias = gTrue; urlCommand = NULL; movieCommand = NULL; mapNumericCharNames = gTrue; printCommands = gFalse; errQuiet = gFalse; cidToUnicodeCache = new CharCodeToUnicodeCache(cidToUnicodeCacheSize); unicodeToUnicodeCache = new CharCodeToUnicodeCache(unicodeToUnicodeCacheSize); unicodeMapCache = new UnicodeMapCache(); cMapCache = new CMapCache();#ifdef ENABLE_PLUGINS plugins = new GList(); securityHandlers = new GList();#endif // set up the initial nameToUnicode table for (i = 0; nameToUnicodeTab[i].name; ++i) { nameToUnicode->add(nameToUnicodeTab[i].name, nameToUnicodeTab[i].u); } // set up the residentUnicodeMaps table map = new UnicodeMap("Latin1", gFalse, latin1UnicodeMapRanges, latin1UnicodeMapLen); residentUnicodeMaps->add(map->getEncodingName(), map); map = new UnicodeMap("ASCII7", gFalse, ascii7UnicodeMapRanges, ascii7UnicodeMapLen); residentUnicodeMaps->add(map->getEncodingName(), map); map = new UnicodeMap("Symbol", gFalse, symbolUnicodeMapRanges, symbolUnicodeMapLen); residentUnicodeMaps->add(map->getEncodingName(), map); map = new UnicodeMap("ZapfDingbats", gFalse, zapfDingbatsUnicodeMapRanges, zapfDingbatsUnicodeMapLen); residentUnicodeMaps->add(map->getEncodingName(), map); map = new UnicodeMap("UTF-8", gTrue, &mapUTF8); residentUnicodeMaps->add(map->getEncodingName(), map); map = new UnicodeMap("UCS-2", gTrue, &mapUCS2); residentUnicodeMaps->add(map->getEncodingName(), map); // look for a user config file, then a system-wide config file f = NULL; fileName = NULL; if (cfgFileName && cfgFileName[0]) { fileName = new GString(cfgFileName); if (!(f = fopen(fileName->getCString(), "r"))) { delete fileName; } } if (!f) { fileName = appendToPath(getHomeDir(), xpdfUserConfigFile); if (!(f = fopen(fileName->getCString(), "r"))) { delete fileName; } } if (!f) {#if defined(WIN32) && !defined(__CYGWIN32__) char buf[512]; i = GetModuleFileName(NULL, buf, sizeof(buf)); if (i <= 0 || i >= sizeof(buf)) { // error or path too long for buffer - just use the current dir buf[0] = '\0'; } fileName = grabPath(buf); appendToPath(fileName, xpdfSysConfigFile);#else fileName = new GString(xpdfSysConfigFile);#endif if (!(f = fopen(fileName->getCString(), "r"))) { delete fileName; } } if (f) { parseFile(fileName, f); delete fileName; fclose(f); }}void GlobalParams::parseFile(GString *fileName, FILE *f) { int line; GList *tokens; GString *cmd, *incFile; char *p1, *p2; char buf[512]; FILE *f2; line = 1; while (getLine(buf, sizeof(buf) - 1, f)) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?