-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUnicodeMap.cc
296 lines (262 loc) · 6.12 KB
/
UnicodeMap.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//========================================================================
//
// UnicodeMap.cc
//
// Copyright 2001-2003 Glyph & Cog, LLC
//
//========================================================================
#include <aconf.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <stdio.h>
#include <string.h>
#include "gmem.h"
#include "gfile.h"
#include "GString.h"
#include "GList.h"
#include "Error.h"
#include "GlobalParams.h"
#include "UnicodeMap.h"
//------------------------------------------------------------------------
#define maxExtCode 16
struct UnicodeMapExt {
Unicode u; // Unicode char
char code[maxExtCode];
Guint nBytes;
};
//------------------------------------------------------------------------
UnicodeMap *UnicodeMap::parse(GString *encodingNameA) {
FILE *f;
UnicodeMap *map;
UnicodeMapRange *range;
UnicodeMapExt *eMap;
int size, eMapsSize;
char buf[256];
int line, nBytes, i, x;
char *tok1, *tok2, *tok3;
if (!(f = globalParams->getUnicodeMapFile(encodingNameA))) {
error(errSyntaxError, -1,
"Couldn't find unicodeMap file for the '{0:t}' encoding",
encodingNameA);
return NULL;
}
map = new UnicodeMap(encodingNameA->copy());
size = 8;
map->ranges = (UnicodeMapRange *)gmallocn(size, sizeof(UnicodeMapRange));
eMapsSize = 0;
line = 1;
while (getLine(buf, sizeof(buf), f)) {
if ((tok1 = strtok(buf, " \t\r\n")) &&
(tok2 = strtok(NULL, " \t\r\n"))) {
if (!(tok3 = strtok(NULL, " \t\r\n"))) {
tok3 = tok2;
tok2 = tok1;
}
nBytes = (int)strlen(tok3) / 2;
if (nBytes <= 4) {
if (map->len == size) {
size *= 2;
map->ranges = (UnicodeMapRange *)
greallocn(map->ranges, size, sizeof(UnicodeMapRange));
}
range = &map->ranges[map->len];
sscanf(tok1, "%x", &range->start);
sscanf(tok2, "%x", &range->end);
sscanf(tok3, "%x", &range->code);
range->nBytes = nBytes;
++map->len;
} else if (tok2 == tok1) {
if (map->eMapsLen == eMapsSize) {
eMapsSize += 16;
map->eMaps = (UnicodeMapExt *)
greallocn(map->eMaps, eMapsSize, sizeof(UnicodeMapExt));
}
eMap = &map->eMaps[map->eMapsLen];
sscanf(tok1, "%x", &eMap->u);
for (i = 0; i < nBytes; ++i) {
sscanf(tok3 + i*2, "%2x", &x);
eMap->code[i] = (char)x;
}
eMap->nBytes = nBytes;
++map->eMapsLen;
} else {
error(errSyntaxError, -1,
"Bad line ({0:d}) in unicodeMap file for the '{1:t}' encoding",
line, encodingNameA);
}
} else {
error(errSyntaxError, -1,
"Bad line ({0:d}) in unicodeMap file for the '{1:t}' encoding",
line, encodingNameA);
}
++line;
}
fclose(f);
return map;
}
UnicodeMap::UnicodeMap(GString *encodingNameA) {
encodingName = encodingNameA;
unicodeOut = gFalse;
kind = unicodeMapUser;
ranges = NULL;
len = 0;
eMaps = NULL;
eMapsLen = 0;
refCnt = 1;
#if MULTITHREADED
gInitMutex(&mutex);
#endif
}
UnicodeMap::UnicodeMap(const char *encodingNameA, GBool unicodeOutA,
UnicodeMapRange *rangesA, int lenA) {
encodingName = new GString(encodingNameA);
unicodeOut = unicodeOutA;
kind = unicodeMapResident;
ranges = rangesA;
len = lenA;
eMaps = NULL;
eMapsLen = 0;
refCnt = 1;
#if MULTITHREADED
gInitMutex(&mutex);
#endif
}
UnicodeMap::UnicodeMap(const char *encodingNameA, GBool unicodeOutA,
UnicodeMapFunc funcA) {
encodingName = new GString(encodingNameA);
unicodeOut = unicodeOutA;
kind = unicodeMapFunc;
func = funcA;
eMaps = NULL;
eMapsLen = 0;
refCnt = 1;
#if MULTITHREADED
gInitMutex(&mutex);
#endif
}
UnicodeMap::~UnicodeMap() {
delete encodingName;
if (kind == unicodeMapUser && ranges) {
gfree(ranges);
}
if (eMaps) {
gfree(eMaps);
}
#if MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
void UnicodeMap::incRefCnt() {
#if MULTITHREADED
gLockMutex(&mutex);
#endif
++refCnt;
#if MULTITHREADED
gUnlockMutex(&mutex);
#endif
}
void UnicodeMap::decRefCnt() {
GBool done;
#if MULTITHREADED
gLockMutex(&mutex);
#endif
done = --refCnt == 0;
#if MULTITHREADED
gUnlockMutex(&mutex);
#endif
if (done) {
delete this;
}
}
GBool UnicodeMap::match(GString *encodingNameA) {
return !encodingName->cmp(encodingNameA);
}
int UnicodeMap::mapUnicode(Unicode u, char *buf, int bufSize) {
int a, b, m, n, i, j;
Guint code;
if (kind == unicodeMapFunc) {
return (*func)(u, buf, bufSize);
}
a = 0;
b = len;
if (u >= ranges[a].start) {
// invariant: ranges[a].start <= u < ranges[b].start
while (b - a > 1) {
m = (a + b) / 2;
if (u >= ranges[m].start) {
a = m;
} else if (u < ranges[m].start) {
b = m;
}
}
if (u <= ranges[a].end) {
n = ranges[a].nBytes;
if (n > bufSize) {
return 0;
}
code = ranges[a].code + (u - ranges[a].start);
for (i = n - 1; i >= 0; --i) {
buf[i] = (char)(code & 0xff);
code >>= 8;
}
return n;
}
}
for (i = 0; i < eMapsLen; ++i) {
if (eMaps[i].u == u) {
n = eMaps[i].nBytes;
for (j = 0; j < n; ++j) {
buf[j] = eMaps[i].code[j];
}
return n;
}
}
return 0;
}
//------------------------------------------------------------------------
UnicodeMapCache::UnicodeMapCache() {
int i;
for (i = 0; i < unicodeMapCacheSize; ++i) {
cache[i] = NULL;
}
}
UnicodeMapCache::~UnicodeMapCache() {
int i;
for (i = 0; i < unicodeMapCacheSize; ++i) {
if (cache[i]) {
cache[i]->decRefCnt();
}
}
}
UnicodeMap *UnicodeMapCache::getUnicodeMap(GString *encodingName) {
UnicodeMap *map;
int i, j;
if (cache[0] && cache[0]->match(encodingName)) {
cache[0]->incRefCnt();
return cache[0];
}
for (i = 1; i < unicodeMapCacheSize; ++i) {
if (cache[i] && cache[i]->match(encodingName)) {
map = cache[i];
for (j = i; j >= 1; --j) {
cache[j] = cache[j - 1];
}
cache[0] = map;
map->incRefCnt();
return map;
}
}
if ((map = UnicodeMap::parse(encodingName))) {
if (cache[unicodeMapCacheSize - 1]) {
cache[unicodeMapCacheSize - 1]->decRefCnt();
}
for (j = unicodeMapCacheSize - 1; j >= 1; --j) {
cache[j] = cache[j - 1];
}
cache[0] = map;
map->incRefCnt();
return map;
}
return NULL;
}