1 /***************************************************************************
3 * Copyright (C) 1998-2014, 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);
124 (void)foundPlatformID
; // Suppress unused variable compiler warnings.
126 (void)foundPlatformSpecificID
;
128 printf("%s:%d: could not find subtable.\n", __FILE__
, __LINE__
);
132 le_uint16 tableFormat
= SWAPW(subtable
->format
);
133 //printf("%s:%d: table format %d\n", __FILE__, __LINE__, tableFormat);
135 switch (tableFormat
) {
137 return new CMAPFormat4Mapper(cmap
, (const CMAPFormat4Encoding
*) subtable
);
141 const CMAPFormat12Encoding
*encoding
= (const CMAPFormat12Encoding
*) subtable
;
143 return new CMAPGroupMapper(cmap
, encoding
->groups
, SWAPL(encoding
->nGroups
));
150 printf("%s:%d: Unknown format %x.\n", __FILE__
, __LINE__
, (SWAPW(subtable
->format
)));
154 CMAPFormat4Mapper::CMAPFormat4Mapper(const CMAPTable
*cmap
, const CMAPFormat4Encoding
*header
)
157 le_uint16 segCount
= SWAPW(header
->segCountX2
) / 2;
159 fEntrySelector
= SWAPW(header
->entrySelector
);
160 fRangeShift
= SWAPW(header
->rangeShift
) / 2;
161 fEndCodes
= &header
->endCodes
[0];
162 fStartCodes
= &header
->endCodes
[segCount
+ 1]; // + 1 for reservedPad...
163 fIdDelta
= &fStartCodes
[segCount
];
164 fIdRangeOffset
= &fIdDelta
[segCount
];
167 LEGlyphID
CMAPFormat4Mapper::unicodeToGlyph(LEUnicode32 unicode32
) const
169 if (unicode32
>= 0x10000) {
173 LEUnicode16 unicode
= (LEUnicode16
) unicode32
;
175 le_uint16 probe
= 1 << fEntrySelector
;
176 TTGlyphID result
= 0;
178 if (SWAPU16(fStartCodes
[fRangeShift
]) <= unicode
) {
182 while (probe
> (1 << 0)) {
185 if (SWAPU16(fStartCodes
[index
+ probe
]) <= unicode
) {
190 if (unicode
>= SWAPU16(fStartCodes
[index
]) && unicode
<= SWAPU16(fEndCodes
[index
])) {
191 if (fIdRangeOffset
[index
] == 0) {
192 result
= (TTGlyphID
) unicode
;
194 le_uint16 offset
= unicode
- SWAPU16(fStartCodes
[index
]);
195 le_uint16 rangeOffset
= SWAPW(fIdRangeOffset
[index
]);
196 le_uint16
*glyphIndexTable
= (le_uint16
*) ((char *) &fIdRangeOffset
[index
] + rangeOffset
);
198 result
= SWAPW(glyphIndexTable
[offset
]);
201 result
+= SWAPW(fIdDelta
[index
]);
206 return LE_SET_GLYPH(0, result
);
209 CMAPFormat4Mapper::~CMAPFormat4Mapper()
211 // parent destructor does it all
214 CMAPGroupMapper::CMAPGroupMapper(const CMAPTable
*cmap
, const CMAPGroup
*groups
, le_uint32 nGroups
)
215 : CMAPMapper(cmap
), fGroups(groups
)
217 le_uint8 bit
= highBit(nGroups
);
219 fRangeOffset
= nGroups
- fPower
;
222 LEGlyphID
CMAPGroupMapper::unicodeToGlyph(LEUnicode32 unicode32
) const
224 le_int32 probe
= fPower
;
227 if (SWAPU32(fGroups
[fRangeOffset
].startCharCode
) <= unicode32
) {
228 range
= fRangeOffset
;
231 while (probe
> (1 << 0)) {
234 if (SWAPU32(fGroups
[range
+ probe
].startCharCode
) <= unicode32
) {
239 if (SWAPU32(fGroups
[range
].startCharCode
) <= unicode32
&& SWAPU32(fGroups
[range
].endCharCode
) >= unicode32
) {
240 return (LEGlyphID
) (SWAPU32(fGroups
[range
].startGlyphCode
) + unicode32
- SWAPU32(fGroups
[range
].startCharCode
));
246 CMAPGroupMapper::~CMAPGroupMapper()
248 // parent destructor does it all