Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/libefont/otfcmap.cc

raw
1 // -*- related-file-name: "../include/efont/otfcmap.hh" -*-
2
3 /* otfcmap.{cc,hh} -- OpenType cmap table
4 *
5 * Copyright (c) 2002-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/otfcmap.hh>
20 #include <lcdf/error.hh>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <efont/otfdata.hh> // for ntohl()
25
26 #define USHORT_AT(d) (Data::u16_aligned(d))
27 #define SHORT_AT(d) (Data::s16_aligned(d))
28 #define ULONG_AT(d) (Data::u32_aligned(d))
29 #define ULONG_AT2(d) (Data::u32_aligned16(d))
30
31 namespace Efont { namespace OpenType {
32
33 Cmap::Cmap(const String &s, ErrorHandler *errh)
34 : _str(s)
35 {
36 _str.align(4);
37 _error = parse_header(errh ? errh : ErrorHandler::silent_handler());
38 }
39
40 int
41 Cmap::parse_header(ErrorHandler *errh)
42 {
43 // HEADER FORMAT:
44 // USHORT version
45 // USHORT numTables
46 int len = _str.length();
47 const uint8_t *data = _str.udata();
48 if (HEADER_SIZE > len)
49 return errh->error("OTF cmap too small"), -EFAULT;
50 if (!(data[0] == '\000' && data[1] == '\000'))
51 return errh->error("bad cmap version number"), -ERANGE;
52 _ntables = USHORT_AT(data + 2);
53 if (_ntables == 0)
54 return errh->error("OTF cmap contains no tables"), -EINVAL;
55 if (HEADER_SIZE + ENCODING_SIZE * _ntables > len)
56 return errh->error("cmap directory out of range"), -EFAULT;
57
58 // ENCODING ENTRY FORMAT:
59 // USHORT platformID
60 // USHORT encodingID
61 // ULONG offset
62 int last_platform = -1;
63 int last_encoding = -1;
64 int last_language = -1;
65 _best_unicode_table = -1;
66 int best_unicode_priority = 0;
67 for (int i = 0; i < _ntables; i++) {
68 int loc = HEADER_SIZE + ENCODING_SIZE * i;
69 int platform = USHORT_AT(data + loc);
70 int encoding = USHORT_AT(data + loc + 2);
71 uint32_t offset = ULONG_AT(data + loc + 4);
72 if (offset + 8 > (uint32_t) len) {
73 length_error:
74 return errh->error("encoding data for entry %d out of range", i);
75 }
76 int format = USHORT_AT(data + offset);
77 int language;
78 if (format == F_BYTE || format == F_HIBYTE || format == F_SEGMENTED
79 || format == F_TRIMMED) {
80 if (USHORT_AT(data + offset + 2) < 6)
81 goto length_error;
82 language = USHORT_AT(data + offset + 4);
83 } else if (format == F_HIBYTE32 || format == F_TRIMMED32
84 || format == F_SEGMENTED32) {
85 if (offset + 12 > (uint32_t) len || ULONG_AT2(data + offset + 4) < 12)
86 goto length_error;
87 language = ULONG_AT2(data + offset + 8);
88 } else
89 continue;
90 if (!(platform > last_platform
91 || (platform == last_platform
92 && (encoding > last_encoding
93 || (encoding == last_encoding
94 && language > last_language)))))
95 errh->warning("unsorted cmap encoding records at entry %d (%d,%d,%d follows %d,%d,%d)", i, platform, encoding, language, last_platform, last_encoding, last_language);
96 int unicode_priority = -1;
97 if (platform == 0 && encoding == 4)
98 unicode_priority = 5;
99 else if (platform == 3 && encoding == 10)
100 unicode_priority = 4;
101 else if (platform == 0 && encoding == 6)
102 unicode_priority = 1;
103 else if ((platform == 0 && encoding != 5) || (platform == 3 && encoding == 1))
104 unicode_priority = 3;
105 else
106 unicode_priority = 0;
107 if (unicode_priority > best_unicode_priority) {
108 _best_unicode_table = i;
109 best_unicode_priority = unicode_priority;
110 }
111 last_platform = platform;
112 last_encoding = encoding;
113 last_language = language;
114 }
115
116 _table_error.assign(_ntables, -2);
117 return 0;
118 }
119
120 int
121 Cmap::first_table(int platform, int encoding) const
122 {
123 if (error() < 0)
124 return -1;
125 const uint8_t *data = _str.udata();
126 data += HEADER_SIZE;
127 for (int i = 0; i < _ntables; i++, data += ENCODING_SIZE) {
128 int p = USHORT_AT(data), e = USHORT_AT(data + 2);
129 if (platform == p && (encoding < 0 || encoding == e))
130 return i;
131 }
132 return -1;
133 }
134
135 int
136 Cmap::check_table(int t, ErrorHandler *errh) const
137 {
138 if (!errh)
139 errh = ErrorHandler::silent_handler();
140 if (t == USE_BEST_UNICODE_TABLE && _best_unicode_table == -1) {
141 errh->warning("font appears not to support Unicode");
142 _best_unicode_table = 0;
143 }
144 if (t == USE_BEST_UNICODE_TABLE)
145 t = _best_unicode_table;
146 if (_error < 0 || t < 0 || t >= _ntables)
147 return errh->error("no such table");
148 if (_table_error[t] != -2)
149 return _table_error[t];
150 _table_error[t] = -1;
151
152 const uint8_t *data = table_data(t);
153 uint32_t left = _str.uend() - data;
154 int format = USHORT_AT(data);
155 uint32_t length = 0; // value not used
156
157 switch (format) {
158
159 case F_BYTE:
160 if (left < 4
161 || (length = USHORT_AT(data + 2)) > left
162 || length != 259)
163 return errh->error("bad table %d length (format %d)", t, format);
164 break;
165
166 case F_HIBYTE:
167 if (left < 4
168 || (length = USHORT_AT(data + 2)) > left
169 || length < 524)
170 return errh->error("bad table %d length (format %d)", t, format);
171 for (int hi_byte = 0; hi_byte < 256; hi_byte++)
172 if (uint32_t subh_key = USHORT_AT(data + 6 + 2 * hi_byte)) {
173 if ((subh_key & 7) || HIBYTE_SUBHEADERS + subh_key + 8 > length)
174 return errh->error("bad table %d subheader %d offset (format 2)", t, hi_byte);
175 const uint8_t *subh = data + HIBYTE_SUBHEADERS + subh_key;
176 int firstCode = USHORT_AT(subh);
177 int entryCount = USHORT_AT(subh + 2);
178 int idRangeOffset = USHORT_AT(subh + 6);
179 if (firstCode + entryCount > 256 || entryCount == 0)
180 return errh->error("bad table %d subheader %d contents (format 2)", t, hi_byte);
181 if ((HIBYTE_SUBHEADERS + subh_key + 6) // pos[idRangeOffset]
182 + idRangeOffset + entryCount * 2 > length)
183 return errh->error("bad table %d subheader %d length (format 2)", t, hi_byte);
184 }
185 break;
186
187 case F_SEGMENTED: {
188 if (left < 4
189 || (length = USHORT_AT(data + 2)) > left
190 || length < 16)
191 return errh->error("bad table %d length (format %d)", t, format);
192 int segCountX2 = USHORT_AT(data + 6);
193 int searchRange = USHORT_AT(data + 8);
194 int entrySelector = USHORT_AT(data + 10);
195 int rangeShift = USHORT_AT(data + 12);
196 if ((segCountX2 & 1)
197 || segCountX2 == 0
198 || (searchRange & (searchRange - 1)) /* not a power of 2? */
199 || searchRange <= segCountX2/2
200 || (searchRange>>1) > segCountX2/2
201 || 1 << (entrySelector + 1) != searchRange
202 || rangeShift != segCountX2 - searchRange)
203 return errh->error("bad table %d segment counts (format %d)", format);
204 uint32_t segCount = segCountX2 >> 1;
205 if (length < 16 + 8 * segCount)
206 return errh->error("bad table %d length (format %d, length %u, need %u)", t, format, length, 16 + 8 * segCount);
207 const uint8_t *endCodes = data + 14;
208 const uint8_t *startCodes = endCodes + 2 + segCountX2;
209 const uint8_t *idDeltas = startCodes + segCountX2;
210 const uint8_t *idRangeOffsets = idDeltas + segCountX2;
211 uint32_t idRangeOffsetsPos = idRangeOffsets - data;
212 int last_end = 0;
213 for (int i = 0; i < segCountX2; i += 2) {
214 int endCode = USHORT_AT(endCodes + i);
215 int startCode = USHORT_AT(startCodes + i);
216 /* int idDelta = SHORT_AT(idDeltas + i); // not needed */
217 int idRangeOffset = USHORT_AT(idRangeOffsets + i);
218 if (endCode < startCode || startCode < last_end)
219 return errh->error("bad table %d overlapping range %d (format %d)", t, i/2, format);
220 if (idRangeOffset
221 && idRangeOffset != 65535
222 && idRangeOffsetsPos + i + idRangeOffset + (endCode - startCode)*2 + 2 > length)
223 return errh->error("bad table %d range %d length (format %d, range %d-%d, idRangeOffset %d, length %u)", t, i/2, format, startCode, endCode, idRangeOffset, length);
224 last_end = endCode + 1;
225 }
226 if (USHORT_AT(endCodes + segCountX2 - 2) != 0xFFFF)
227 return errh->error("bad table %d incorrect final endCode (format 4)", t);
228 break;
229 }
230
231 case F_TRIMMED: {
232 if (left < 4
233 || (length = USHORT_AT(data + 2)) > left
234 || length < 10)
235 return errh->error("bad table %d length (format %d)", t, format);
236 uint32_t entryCount = USHORT_AT(data + 8);
237 if (10 + entryCount * 2 > length)
238 return errh->error("bad table %d length (format %d)", t, format);
239 break;
240 }
241
242 case F_SEGMENTED32:
243 case F_MANYTOONE: {
244 if (left < 8
245 || (length = ULONG_AT(data + 4)) > left
246 || length < 16)
247 return errh->error("bad table %d length (format %d)", t, format);
248 uint32_t nGroups = ULONG_AT(data + 16);
249 if ((length - 16) / 12 < nGroups)
250 return errh->error("bad table %d length (format %d)", t, format);
251 uint32_t last_post_end = 0;
252 data += 16;
253 for (uint32_t i = 0; i < nGroups; i++, data += 12) {
254 uint32_t startCharCode = ULONG_AT(data);
255 uint32_t endCharCode = ULONG_AT(data + 4);
256 if (startCharCode < last_post_end || endCharCode < startCharCode)
257 return errh->error("bad table %d overlapping range %d (format %d)", t, i, format);
258 last_post_end = endCharCode + 1;
259 }
260 break;
261 }
262
263 case F_HIBYTE32:
264 case F_TRIMMED32:
265 default:
266 return errh->error("bad table %d unsupported format %d", t, format);
267
268 }
269
270 _table_error[t] = t;
271 return t;
272 }
273
274 Glyph
275 Cmap::map_table(int t, uint32_t uni, ErrorHandler *errh) const
276 {
277 if ((t = check_table(t, errh)) < 0)
278 return 0;
279
280 const uint8_t *data = table_data(t);
281 switch (USHORT_AT(data)) {
282
283 case F_BYTE:
284 if (uni < 256)
285 return data[6 + uni];
286 else
287 return 0;
288
289 case F_HIBYTE: {
290 if (uni >= 65536)
291 return 0;
292 int hi_byte = (uni >> 8) & 255;
293 int subh = USHORT_AT(data + 6 + hi_byte * 2);
294 if (subh == 0 && hi_byte) // XXX?
295 return 0;
296 data += 524 + subh;
297 int firstCode = USHORT_AT(data);
298 int entryCount = USHORT_AT(data + 2);
299 int idDelta = SHORT_AT(data + 4);
300 int idRangeOffset = USHORT_AT(data + 6);
301 int lo_byte = uni & 255;
302 if (lo_byte < firstCode || lo_byte >= firstCode + entryCount)
303 return 0;
304 int answer = USHORT_AT(data + 6 + idRangeOffset + (lo_byte - firstCode) * 2);
305 if (answer == 0)
306 return 0;
307 return (answer + idDelta) & 65535;
308 }
309
310 case F_SEGMENTED: {
311 if (uni >= 65536)
312 return 0;
313 int segCount = USHORT_AT(data + 6) >> 1;
314 const uint8_t *endCounts = data + 14;
315 const uint8_t *startCounts = endCounts + (segCount << 1) + 2;
316 const uint8_t *idDeltas = startCounts + (segCount << 1);
317 const uint8_t *idRangeOffsets = idDeltas + (segCount << 1);
318 int l = 0, r = segCount;
319 while (l < r) {
320 int m = l + (r - l) / 2;
321 uint32_t endCount = USHORT_AT(endCounts + (m << 1));
322 uint32_t startCount = USHORT_AT(startCounts + (m << 1));
323 if (uni < startCount)
324 r = m;
325 else if (uni <= endCount) {
326 int idDelta = SHORT_AT(idDeltas + (m << 1));
327 int idRangeOffset = USHORT_AT(idRangeOffsets + (m << 1));
328 if (idRangeOffset == 0)
329 return (idDelta + uni) & 65535;
330 else if (idRangeOffset == 65535)
331 return 0;
332 int g = USHORT_AT(idRangeOffsets + (m << 1) + idRangeOffset + ((uni - startCount) << 1));
333 if (g == 0)
334 return 0;
335 return (idDelta + g) & 65535;
336 } else
337 l = m + 1;
338 }
339 return 0;
340 }
341
342 case F_TRIMMED: {
343 uint32_t firstCode = USHORT_AT(data + 6);
344 uint32_t entryCount = USHORT_AT(data + 8);
345 if (uni < firstCode || uni >= firstCode + entryCount)
346 return 0;
347 return USHORT_AT(data + 10 + ((uni - firstCode) << 1));
348 }
349
350 case F_SEGMENTED32: {
351 uint32_t nGroups = ULONG_AT2(data + 12);
352 uint32_t l = 0, r = nGroups;
353 const uint8_t *groups = data + 16;
354 while (l < r) {
355 uint32_t m = l + (r - l) / 2;
356 uint32_t startCharCode = ULONG_AT2(groups + m * 12);
357 uint32_t endCharCode = ULONG_AT2(groups + m * 12 + 4);
358 if (uni < startCharCode)
359 r = m;
360 else if (uni <= endCharCode)
361 return ULONG_AT2(groups + m * 12 + 8) + uni - startCharCode;
362 else
363 l = m + 1;
364 }
365 return 0;
366 }
367
368 case F_MANYTOONE: {
369 uint32_t nGroups = ULONG_AT2(data + 12);
370 uint32_t l = 0, r = nGroups;
371 const uint8_t *groups = data + 16;
372 while (l < r) {
373 uint32_t m = l + (r - l) / 2;
374 uint32_t startCharCode = ULONG_AT2(groups + m * 12);
375 uint32_t endCharCode = ULONG_AT2(groups + m * 12 + 4);
376 if (uni < startCharCode)
377 r = m;
378 else if (uni <= endCharCode)
379 return ULONG_AT2(groups + m * 12 + 8);
380 else
381 l = m + 1;
382 }
383 return 0;
384 }
385
386 default:
387 return 0;
388
389 }
390 }
391
392 void
393 Cmap::dump_table(int t, Vector<std::pair<uint32_t, Glyph> > &ugp, ErrorHandler *errh) const
394 {
395 if ((t = check_table(t, errh)) < 0)
396 return;
397
398 const uint8_t *data = table_data(t);
399 switch (USHORT_AT(data)) {
400
401 case F_BYTE:
402 for (uint32_t u = 0; u < 256; ++u)
403 if (int g = data[6 + u])
404 ugp.push_back(std::make_pair(u, g));
405 break;
406
407 case F_HIBYTE:
408 assert(USHORT_AT(data + 6) == 0);
409 for (int hi_byte = 0; hi_byte < 256; hi_byte++) {
410 int subh = USHORT_AT(data + 6 + hi_byte * 4);
411 if (subh == 0 && hi_byte > 0)
412 continue;
413 const uint8_t *tdata = data + 524 + subh;
414 int firstCode = USHORT_AT(tdata);
415 int entryCount = USHORT_AT(tdata + 2);
416 int idDelta = SHORT_AT(tdata + 4);
417 int idRangeOffset = USHORT_AT(tdata + 6);
418 const uint8_t *gdata = tdata + 6 + idRangeOffset;
419 for (int i = 0; i < entryCount; i++)
420 if (Glyph g = USHORT_AT(gdata + (i << 1))) {
421 g = (idDelta + g) & 65535;
422 uint32_t u = (hi_byte << 8) + firstCode + i;
423 ugp.push_back(std::make_pair(u, g));
424 }
425 }
426 break;
427
428 case F_SEGMENTED: {
429 int segCountX2 = USHORT_AT(data + 6);
430 const uint8_t *endCounts = data + 14;
431 const uint8_t *startCounts = endCounts + segCountX2 + 2;
432 const uint8_t *idDeltas = startCounts + segCountX2;
433 const uint8_t *idRangeOffsets = idDeltas + segCountX2;
434 for (int i = 0; i < segCountX2; i += 2) {
435 uint32_t endCount = USHORT_AT(endCounts + i);
436 uint32_t startCount = USHORT_AT(startCounts + i);
437 int idDelta = SHORT_AT(idDeltas + i);
438 int idRangeOffset = USHORT_AT(idRangeOffsets + i);
439 if (idRangeOffset == 0) {
440 for (uint32_t u = startCount; u <= endCount; ++u) {
441 Glyph g = (u + idDelta) & 65535;
442 ugp.push_back(std::make_pair(u, g));
443 }
444 } else {
445 const uint8_t *gdata = idRangeOffsets + i + idRangeOffset;
446 for (uint32_t u = startCount; u <= endCount; ++u, gdata += 2)
447 if (Glyph g = USHORT_AT(gdata)) {
448 g = (g + idDelta) & 65535;
449 ugp.push_back(std::make_pair(u, g));
450 }
451 }
452 }
453 break;
454 }
455
456 case F_TRIMMED: {
457 uint32_t firstCode = USHORT_AT(data + 6);
458 int entryCount = USHORT_AT(data + 8);
459 for (int i = 0; i < entryCount; i++)
460 if (Glyph g = USHORT_AT(data + 10 + (i << 1)))
461 ugp.push_back(std::make_pair(firstCode + i, g));
462 break;
463 }
464
465 case F_SEGMENTED32: {
466 uint32_t nGroups = ULONG_AT2(data + 12);
467 const uint8_t *groups = data + 16;
468 for (uint32_t i = 0; i < nGroups; i++, groups += 12) {
469 uint32_t startCharCode = ULONG_AT2(groups);
470 uint32_t nCharCodes = ULONG_AT2(groups + 4) - startCharCode;
471 Glyph startGlyphID = ULONG_AT2(groups + 8);
472 for (uint32_t i = 0; i <= nCharCodes; i++)
473 ugp.push_back(std::make_pair(startCharCode + i, startGlyphID + i));
474 }
475 break;
476 }
477
478 case F_MANYTOONE: {
479 uint32_t nGroups = ULONG_AT2(data + 12);
480 const uint8_t *groups = data + 16;
481 for (uint32_t i = 0; i < nGroups; i++, groups += 12) {
482 uint32_t startCharCode = ULONG_AT2(groups);
483 uint32_t nCharCodes = ULONG_AT2(groups + 4) - startCharCode;
484 Glyph glyphID = ULONG_AT2(groups + 8);
485 for (uint32_t i = 0; i <= nCharCodes; i++)
486 ugp.push_back(std::make_pair(startCharCode + i, glyphID));
487 }
488 break;
489 }
490
491 default:
492 break;
493
494 }
495 }
496
497 int
498 Cmap::map_uni(const Vector<uint32_t> &vin, Vector<Glyph> &vout) const
499 {
500 int t;
501 if ((t = check_table(USE_BEST_UNICODE_TABLE)) < 0)
502 return -1;
503 vout.resize(vin.size(), 0);
504 for (int i = 0; i < vin.size(); i++)
505 vout[i] = map_table(t, vin[i]);
506 return 0;
507 }
508
509 }}