summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/CMapManager.cpp
blob: 52b323ebc62ee7e98f29c80ca06ba2f9617fffd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*************************************************************************
** CMapManager.cpp                                                      **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2020 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <array>
#include <sstream>
#include "CMap.hpp"
#include "CMapManager.hpp"
#include "CMapReader.hpp"
#include "FileFinder.hpp"
#include "Font.hpp"
#include "Message.hpp"

using namespace std;


CMapManager& CMapManager::instance () {
	static CMapManager cmm;
	return cmm;
}


/** Loads a cmap and returns the corresponding object. */
CMap* CMapManager::lookup (const string &name) {
	auto it = _cmaps.find(name);
	if (it != _cmaps.end())
		return it->second.get();

	if (_includedCMaps.find(name) != _includedCMaps.end()) {
		_level = 0;
		throw CMapReaderException("circular reference of CMap " + name);
	}

	unique_ptr<CMap> cmap_ptr;
	if (name == "Identity-H")
		cmap_ptr = util::make_unique<IdentityHCMap>();
	else if (name == "Identity-V")
		cmap_ptr = util::make_unique<IdentityVCMap>();
	else if (name == "unicode")
		cmap_ptr = util::make_unique<UnicodeCMap>();
	if (cmap_ptr) {
		CMap *ret = cmap_ptr.get();
		_cmaps[name] = std::move(cmap_ptr);
		return ret;
	}
	// Load cmap data of file <name> and also process all cmaps referenced by operator "usecmap".
	// This can lead to a sequence of further calls of lookup(). In order to prevent infinite loops
	// due to (disallowed) circular cmap inclusions, we keep track of all cmaps processed during
	// a sequence of inclusions.
	_includedCMaps.insert(name);  // save name of current cmap being processed
	_level++;                     // increase nesting level
	CMap *ret=nullptr;
	try {
		CMapReader reader;
		if (!(cmap_ptr = reader.read(name))) {
			_level = 1;
			Message::wstream(true) << "CMap file '" << name << "' not found\n";
		}
		ret = cmap_ptr.get();
		_cmaps[name] = std::move(cmap_ptr);
	}
	catch (const CMapReaderException &e) {
		Message::estream(true) << "CMap file " << name << ": " << e.what() << "\n";
	}
	if (--_level == 0)            // back again at initial nesting level?
		_includedCMaps.clear();    // => names of included cmaps are no longer needed
	return ret;
}


/** Looks for a base font CMap and a compatible encoding table in a given font. The CMap describe
 *  the mapping from CIDs to character codes where the latter are relative to the encoding table
 *  identified by charmapID.
 *  cmap:X->CID, bfmap:CID->Y, enctable:Y->CharCode
 *  @param[in] font look for available encoding tables in this font
 *  @param[in] cmap take the source registry-ordering pair from this CMap
 *  @param[out] charmapID ID of the compatible character map found in the given font
 *  @return base font CMap that maps from CIDs to character codes */
const CMap* CMapManager::findCompatibleBaseFontMap (const PhysicalFont *font, const CMap *cmap, CharMapID &charmapID) {
	if (!font || !cmap)
		return nullptr;

	struct CharMapIDToEncName {
		CharMapID id;
		const char *encname;
	};
	const array<CharMapIDToEncName, 10> encodings {{
		{CharMapID::WIN_UCS4,         "UCS4"},
		{CharMapID::WIN_UCS2,         "UCS2"},
		{CharMapID::WIN_SHIFTJIS,     "90ms-RKSJ"},
		{CharMapID::WIN_PRC,          "GBK-EUC"},
		{CharMapID::WIN_BIG5,         "ETen-B5"},
		{CharMapID::WIN_WANSUNG,      "KSCms-UHC"},
		{CharMapID::MAC_JAPANESE,     "90pv-RKSJ"},
		{CharMapID::MAC_TRADCHINESE,  "B5pc"},
		{CharMapID::MAC_SIMPLCHINESE, "GBpc-EUC"},
		{CharMapID::MAC_KOREAN,       "KSCpc-EUC"}
	}};

	// get IDs of all available charmaps in font
	vector<CharMapID> charmapIDs;
	font->collectCharMapIDs(charmapIDs);


	// try to find a compatible encoding CMap
	const bool is_unicode_map = bool(dynamic_cast<const UnicodeCMap*>(cmap));
	const string ro = cmap->getROString();
	for (const CharMapIDToEncName &enc : encodings) {
		for (const CharMapID &id : charmapIDs) {
			if (enc.id == id) {
				string cmapname = ro+"-"+enc.encname;
				if (is_unicode_map || FileFinder::instance().lookup(cmapname, "cmap", false)) {
					charmapID = enc.id;
					return is_unicode_map ? cmap : lookup(cmapname);
				}
			}
		}
	}
	return nullptr;
}