1 /***************************************************************************
3 * Copyright (C) 1998-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
6 ************************************************************************/
8 #include "layout/LETypes.h"
9 #include "layout/LESwaps.h"
15 #define SWAPU16(code) ((LEUnicode16) SWAPW(code))
16 #define SWAPU32(code) ((LEUnicode32) SWAPL(code))
19 // Finds the high bit by binary searching
20 // through the bits in value.
22 le_int8 highBit(le_uint32 value)
26 if (value >= 1 << 16) {
31 if (value >= 1 << 8) {
36 if (value >= 1 << 4) {
41 if (value >= 1 << 2) {
46 if (value >= 1 << 1) {
54 CMAPMapper *CMAPMapper::createUnicodeMapper(const CMAPTable *cmap)
57 le_uint16 nSubtables = SWAPW(cmap->numberSubtables);
58 const CMAPEncodingSubtable *subtable = NULL;
59 le_bool found = FALSE;
60 le_uint16 foundPlatformID = 0xFFFF;
61 le_uint16 foundPlatformSpecificID = 0xFFFF;
62 le_uint32 foundOffset = 0;
63 le_uint16 foundTable = 0xFFFF;
64 // first pass, look for MS table. (preferred?)
65 for (i = 0; i < nSubtables && !found; i += 1) {
66 const CMAPEncodingSubtableHeader *esh = &cmap->encodingSubtableHeaders[i];
68 le_uint16 platformID = SWAPW(esh->platformID);
69 le_uint16 platformSpecificID = SWAPW(esh->platformSpecificID);
70 if (platformID == 3) { // microsoft
71 switch (platformSpecificID) {
72 case 1: // Unicode BMP (UCS-2)
73 case 10: // Unicode UCS-4
74 foundOffset = SWAPL(esh->encodingOffset);
75 foundPlatformID = platformID;
76 foundPlatformSpecificID = platformSpecificID;
82 // 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);
85 //printf("%s:%d: platform ID %d (wanted 3, microsoft) for subtable %d/%d\n", __FILE__, __LINE__, (SWAPW(esh->platformID)), i, nSubtables);
89 // second pass, allow non MS table
90 // first pass, look for MS table. (preferred?)
91 for (i = 0; i < nSubtables && !found; i += 1) {
92 const CMAPEncodingSubtableHeader *esh = &cmap->encodingSubtableHeaders[i];
93 le_uint16 platformID = SWAPW(esh->platformID);
94 le_uint16 platformSpecificID = SWAPW(esh->platformSpecificID);
95 //printf("%s:%d: table %d/%d has platform:specific %d:%d\n", __FILE__, __LINE__, i, nSubtables, platformID, platformSpecificID);
97 case 0: // Unicode platform
98 switch(platformSpecificID) {
103 foundOffset = SWAPL(esh->encodingOffset);
104 foundPlatformID = platformID;
105 foundPlatformSpecificID = platformSpecificID;
110 default: printf("Error: table %d (psid %d) is unknown. Skipping.\n", i, platformSpecificID); break;
115 //printf("Skipping platform id %d\n", platformID);
122 subtable = (const CMAPEncodingSubtable *) ((const char *) cmap + foundOffset);
123 //printf("%s:%d: using subtable #%d/%d type %d:%d\n", __FILE__, __LINE__, foundTable, nSubtables, foundPlatformID, foundPlatformSpecificID);
125 printf("%s:%d: could not find subtable.\n", __FILE__, __LINE__);
129 le_uint16 tableFormat = SWAPW(subtable->format);
130 //printf("%s:%d: table format %d\n", __FILE__, __LINE__, tableFormat);
132 switch (tableFormat) {
134 return new CMAPFormat4Mapper(cmap, (const CMAPFormat4Encoding *) subtable);
138 const CMAPFormat12Encoding *encoding = (const CMAPFormat12Encoding *) subtable;
140 return new CMAPGroupMapper(cmap, encoding->groups, SWAPL(encoding->nGroups));
147 printf("%s:%d: Unknown format %x.\n", __FILE__, __LINE__, (SWAPW(subtable->format)));
151 CMAPFormat4Mapper::CMAPFormat4Mapper(const CMAPTable *cmap, const CMAPFormat4Encoding *header)
154 le_uint16 segCount = SWAPW(header->segCountX2) / 2;
156 fEntrySelector = SWAPW(header->entrySelector);
157 fRangeShift = SWAPW(header->rangeShift) / 2;
158 fEndCodes = &header->endCodes[0];
159 fStartCodes = &header->endCodes[segCount + 1]; // + 1 for reservedPad...
160 fIdDelta = &fStartCodes[segCount];
161 fIdRangeOffset = &fIdDelta[segCount];
164 LEGlyphID CMAPFormat4Mapper::unicodeToGlyph(LEUnicode32 unicode32) const
166 if (unicode32 >= 0x10000) {
170 LEUnicode16 unicode = (LEUnicode16) unicode32;
172 le_uint16 probe = 1 << fEntrySelector;
173 TTGlyphID result = 0;
175 if (SWAPU16(fStartCodes[fRangeShift]) <= unicode) {
179 while (probe > (1 << 0)) {
182 if (SWAPU16(fStartCodes[index + probe]) <= unicode) {
187 if (unicode >= SWAPU16(fStartCodes[index]) && unicode <= SWAPU16(fEndCodes[index])) {
188 if (fIdRangeOffset[index] == 0) {
189 result = (TTGlyphID) unicode;
191 le_uint16 offset = unicode - SWAPU16(fStartCodes[index]);
192 le_uint16 rangeOffset = SWAPW(fIdRangeOffset[index]);
193 le_uint16 *glyphIndexTable = (le_uint16 *) ((char *) &fIdRangeOffset[index] + rangeOffset);
195 result = SWAPW(glyphIndexTable[offset]);
198 result += SWAPW(fIdDelta[index]);
203 return LE_SET_GLYPH(0, result);
206 CMAPFormat4Mapper::~CMAPFormat4Mapper()
208 // parent destructor does it all
211 CMAPGroupMapper::CMAPGroupMapper(const CMAPTable *cmap, const CMAPGroup *groups, le_uint32 nGroups)
212 : CMAPMapper(cmap), fGroups(groups)
214 le_uint8 bit = highBit(nGroups);
216 fRangeOffset = nGroups - fPower;
219 LEGlyphID CMAPGroupMapper::unicodeToGlyph(LEUnicode32 unicode32) const
221 le_int32 probe = fPower;
224 if (SWAPU32(fGroups[fRangeOffset].startCharCode) <= unicode32) {
225 range = fRangeOffset;
228 while (probe > (1 << 0)) {
231 if (SWAPU32(fGroups[range + probe].startCharCode) <= unicode32) {
236 if (SWAPU32(fGroups[range].startCharCode) <= unicode32 && SWAPU32(fGroups[range].endCharCode) >= unicode32) {
237 return (LEGlyphID) (SWAPU32(fGroups[range].startGlyphCode) + unicode32 - SWAPU32(fGroups[range].startCharCode));
243 CMAPGroupMapper::~CMAPGroupMapper()
245 // parent destructor does it all