2 ****************************************************************************** *
4 * Copyright (C) 1999-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ****************************************************************************** *
10 * created on: ??/??/2001
11 * created by: Eric R. Mader
14 #include "layout/LETypes.h"
15 #include "layout/LESwaps.h"
20 #define SWAPU16(code) ((LEUnicode16) SWAPW(code))
21 #define SWAPU32(code) ((LEUnicode32) SWAPL(code))
24 // Finds the high bit by binary searching
25 // through the bits in value.
27 le_uint8
highBit(le_uint32 value
)
31 if (value
>= 1 << 16) {
36 if (value
>= 1 << 8) {
41 if (value
>= 1 << 4) {
46 if (value
>= 1 << 2) {
51 if (value
>= 1 << 1) {
59 CMAPMapper
*CMAPMapper::createUnicodeMapper(const CMAPTable
*cmap
)
62 le_uint16 nSubtables
= SWAPW(cmap
->numberSubtables
);
63 const CMAPEncodingSubtable
*subtable
= NULL
;
64 le_uint32 offset1
= 0, offset10
= 0;
66 for (i
= 0; i
< nSubtables
; i
+= 1) {
67 const CMAPEncodingSubtableHeader
*esh
= &cmap
->encodingSubtableHeaders
[i
];
69 if (SWAPW(esh
->platformID
) == 3) {
70 switch (SWAPW(esh
->platformSpecificID
)) {
72 offset1
= SWAPL(esh
->encodingOffset
);
76 offset10
= SWAPL(esh
->encodingOffset
);
85 subtable
= (const CMAPEncodingSubtable
*) ((const char *) cmap
+ offset10
);
86 } else if (offset1
!= 0) {
87 subtable
= (const CMAPEncodingSubtable
*) ((const char *) cmap
+ offset1
);
92 switch (SWAPW(subtable
->format
)) {
94 return new CMAPFormat4Mapper(cmap
, (const CMAPFormat4Encoding
*) subtable
);
98 const CMAPFormat12Encoding
*encoding
= (const CMAPFormat12Encoding
*) subtable
;
100 return new CMAPGroupMapper(cmap
, encoding
->groups
, SWAPL(encoding
->nGroups
));
110 CMAPFormat4Mapper::CMAPFormat4Mapper(const CMAPTable
*cmap
, const CMAPFormat4Encoding
*header
)
113 le_uint16 segCount
= SWAPW(header
->segCountX2
) / 2;
115 fEntrySelector
= SWAPW(header
->entrySelector
);
116 fRangeShift
= SWAPW(header
->rangeShift
) / 2;
117 fEndCodes
= &header
->endCodes
[0];
118 fStartCodes
= &header
->endCodes
[segCount
+ 1]; // + 1 for reservedPad...
119 fIdDelta
= &fStartCodes
[segCount
];
120 fIdRangeOffset
= &fIdDelta
[segCount
];
123 LEGlyphID
CMAPFormat4Mapper::unicodeToGlyph(LEUnicode32 unicode32
) const
125 if (unicode32
>= 0x10000) {
129 LEUnicode16 unicode
= (LEUnicode16
) unicode32
;
131 le_uint16 probe
= 1 << fEntrySelector
;
132 TTGlyphID result
= 0;
134 if (SWAPU16(fStartCodes
[fRangeShift
]) <= unicode
) {
138 while (probe
> (1 << 0)) {
141 if (SWAPU16(fStartCodes
[index
+ probe
]) <= unicode
) {
146 if (unicode
>= SWAPU16(fStartCodes
[index
]) && unicode
<= SWAPU16(fEndCodes
[index
])) {
147 if (fIdRangeOffset
[index
] == 0) {
148 result
= (TTGlyphID
) unicode
;
150 le_uint16 offset
= unicode
- SWAPU16(fStartCodes
[index
]);
151 le_uint16 rangeOffset
= SWAPW(fIdRangeOffset
[index
]);
152 le_uint16
*glyphIndexTable
= (le_uint16
*) ((char *) &fIdRangeOffset
[index
] + rangeOffset
);
154 result
= SWAPW(glyphIndexTable
[offset
]);
157 result
+= SWAPW(fIdDelta
[index
]);
162 return LE_SET_GLYPH(0, result
);
165 CMAPFormat4Mapper::~CMAPFormat4Mapper()
167 // parent destructor does it all
170 CMAPGroupMapper::CMAPGroupMapper(const CMAPTable
*cmap
, const CMAPGroup
*groups
, le_uint32 nGroups
)
171 : CMAPMapper(cmap
), fGroups(groups
)
173 le_uint8 bit
= highBit(nGroups
);
175 fRangeOffset
= nGroups
- fPower
;
178 LEGlyphID
CMAPGroupMapper::unicodeToGlyph(LEUnicode32 unicode32
) const
180 le_int32 probe
= fPower
;
183 if (SWAPU32(fGroups
[fRangeOffset
].startCharCode
) <= unicode32
) {
184 range
= fRangeOffset
;
187 while (probe
> (1 << 0)) {
190 if (SWAPU32(fGroups
[range
+ probe
].startCharCode
) <= unicode32
) {
195 if (SWAPU32(fGroups
[range
].startCharCode
) <= unicode32
&& SWAPU32(fGroups
[range
].endCharCode
) >= unicode32
) {
196 return (LEGlyphID
) (SWAPU32(fGroups
[range
].startGlyphCode
) + unicode32
- SWAPU32(fGroups
[range
].startCharCode
));
202 CMAPGroupMapper::~CMAPGroupMapper()
204 // parent destructor does it all