1 /**************************************************************************
3 * Copyright (C) 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
6 ***************************************************************************
7 ***************************************************************************
9 * Copyright (C) 1998-2013, International Business Machines
10 * Corporation and others. All Rights Reserved.
12 ************************************************************************/
14 #include "layout/LETypes.h"
15 #include "layout/LESwaps.h"
21 #define SWAPU16(code) ((LEUnicode16) SWAPW(code))
22 #define SWAPU32(code) ((LEUnicode32) SWAPL(code))
25 // Finds the high bit by binary searching
26 // through the bits in value.
28 le_int8 highBit(le_uint32 value)
32 if (value >= 1 << 16) {
37 if (value >= 1 << 8) {
42 if (value >= 1 << 4) {
47 if (value >= 1 << 2) {
52 if (value >= 1 << 1) {
60 CMAPMapper *CMAPMapper::createUnicodeMapper(const CMAPTable *cmap)
63 le_uint16 nSubtables = SWAPW(cmap->numberSubtables);
64 const CMAPEncodingSubtable *subtable = NULL;
65 le_bool found = FALSE;
66 le_uint16 foundPlatformID = 0xFFFF;
67 le_uint16 foundPlatformSpecificID = 0xFFFF;
68 le_uint32 foundOffset = 0;
69 le_uint16 foundTable = 0xFFFF;
70 // first pass, look for MS table. (preferred?)
71 for (i = 0; i < nSubtables && !found; i += 1) {
72 const CMAPEncodingSubtableHeader *esh = &cmap->encodingSubtableHeaders[i];
74 le_uint16 platformID = SWAPW(esh->platformID);
75 le_uint16 platformSpecificID = SWAPW(esh->platformSpecificID);
76 if (platformID == 3) { // microsoft
77 switch (platformSpecificID) {
78 case 1: // Unicode BMP (UCS-2)
79 case 10: // Unicode UCS-4
80 foundOffset = SWAPL(esh->encodingOffset);
81 foundPlatformID = platformID;
82 foundPlatformSpecificID = platformSpecificID;
88 // printf("%s:%d: microsoft (3) platform specific ID %d (wanted 1 or 10) for subtable %d/%d\n", __FILE__, __LINE__, (SWAPW(esh->platformSpecificID)), i, nSubtables);
91 //printf("%s:%d: platform ID %d (wanted 3, microsoft) for subtable %d/%d\n", __FILE__, __LINE__, (SWAPW(esh->platformID)), i, nSubtables);
95 // second pass, allow non MS table
96 // first pass, look for MS table. (preferred?)
97 for (i = 0; i < nSubtables && !found; i += 1) {
98 const CMAPEncodingSubtableHeader *esh = &cmap->encodingSubtableHeaders[i];
99 le_uint16 platformID = SWAPW(esh->platformID);
100 le_uint16 platformSpecificID = SWAPW(esh->platformSpecificID);
101 //printf("%s:%d: table %d/%d has platform:specific %d:%d\n", __FILE__, __LINE__, i, nSubtables, platformID, platformSpecificID);
103 case 0: // Unicode platform
104 switch(platformSpecificID) {
109 foundOffset = SWAPL(esh->encodingOffset);
110 foundPlatformID = platformID;
111 foundPlatformSpecificID = platformSpecificID;
116 default: printf("Error: table %d (psid %d) is unknown. Skipping.\n", i, platformSpecificID); break;
121 //printf("Skipping platform id %d\n", platformID);
128 subtable = (const CMAPEncodingSubtable *) ((const char *) cmap + foundOffset);
129 //printf("%s:%d: using subtable #%d/%d type %d:%d\n", __FILE__, __LINE__, foundTable, nSubtables, foundPlatformID, foundPlatformSpecificID);
131 printf("%s:%d: could not find subtable.\n", __FILE__, __LINE__);
135 le_uint16 tableFormat = SWAPW(subtable->format);
136 //printf("%s:%d: table format %d\n", __FILE__, __LINE__, tableFormat);
138 switch (tableFormat) {
140 return new CMAPFormat4Mapper(cmap, (const CMAPFormat4Encoding *) subtable);
144 const CMAPFormat12Encoding *encoding = (const CMAPFormat12Encoding *) subtable;
146 return new CMAPGroupMapper(cmap, encoding->groups, SWAPL(encoding->nGroups));
153 printf("%s:%d: Unknown format %x.\n", __FILE__, __LINE__, (SWAPW(subtable->format)));
157 CMAPFormat4Mapper::CMAPFormat4Mapper(const CMAPTable *cmap, const CMAPFormat4Encoding *header)
160 le_uint16 segCount = SWAPW(header->segCountX2) / 2;
162 fEntrySelector = SWAPW(header->entrySelector);
163 fRangeShift = SWAPW(header->rangeShift) / 2;
164 fEndCodes = &header->endCodes[0];
165 fStartCodes = &header->endCodes[segCount + 1]; // + 1 for reservedPad...
166 fIdDelta = &fStartCodes[segCount];
167 fIdRangeOffset = &fIdDelta[segCount];
170 LEGlyphID CMAPFormat4Mapper::unicodeToGlyph(LEUnicode32 unicode32) const
172 if (unicode32 >= 0x10000) {
176 LEUnicode16 unicode = (LEUnicode16) unicode32;
178 le_uint16 probe = 1 << fEntrySelector;
179 TTGlyphID result = 0;
181 if (SWAPU16(fStartCodes[fRangeShift]) <= unicode) {
185 while (probe > (1 << 0)) {
188 if (SWAPU16(fStartCodes[index + probe]) <= unicode) {
193 if (unicode >= SWAPU16(fStartCodes[index]) && unicode <= SWAPU16(fEndCodes[index])) {
194 if (fIdRangeOffset[index] == 0) {
195 result = (TTGlyphID) unicode;
197 le_uint16 offset = unicode - SWAPU16(fStartCodes[index]);
198 le_uint16 rangeOffset = SWAPW(fIdRangeOffset[index]);
199 le_uint16 *glyphIndexTable = (le_uint16 *) ((char *) &fIdRangeOffset[index] + rangeOffset);
201 result = SWAPW(glyphIndexTable[offset]);
204 result += SWAPW(fIdDelta[index]);
209 return LE_SET_GLYPH(0, result);
212 CMAPFormat4Mapper::~CMAPFormat4Mapper()
214 // parent destructor does it all
217 CMAPGroupMapper::CMAPGroupMapper(const CMAPTable *cmap, const CMAPGroup *groups, le_uint32 nGroups)
218 : CMAPMapper(cmap), fGroups(groups)
220 le_uint8 bit = highBit(nGroups);
222 fRangeOffset = nGroups - fPower;
225 LEGlyphID CMAPGroupMapper::unicodeToGlyph(LEUnicode32 unicode32) const
227 le_int32 probe = fPower;
230 if (SWAPU32(fGroups[fRangeOffset].startCharCode) <= unicode32) {
231 range = fRangeOffset;
234 while (probe > (1 << 0)) {
237 if (SWAPU32(fGroups[range + probe].startCharCode) <= unicode32) {
242 if (SWAPU32(fGroups[range].startCharCode) <= unicode32 && SWAPU32(fGroups[range].endCharCode) >= unicode32) {
243 return (LEGlyphID) (SWAPU32(fGroups[range].startGlyphCode) + unicode32 - SWAPU32(fGroups[range].startCharCode));
249 CMAPGroupMapper::~CMAPGroupMapper()
251 // parent destructor does it all