os/textandloc/fontservices/textshaperplugin/source/cmaps.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/textshaperplugin/source/cmaps.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,200 @@
     1.4 +/***************************************************************************
     1.5 +*
     1.6 +*   Copyright (C) 1998-2003, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +*
     1.9 +************************************************************************/
    1.10 +
    1.11 +#include "LETypes.h"
    1.12 +#include "LESwaps.h"
    1.13 +
    1.14 +#include "sfnt.h"
    1.15 +#include "cmaps.h"
    1.16 +
    1.17 +#define SWAPU16(code) ((LEUnicode16) SWAPW(code))
    1.18 +#define SWAPU32(code) ((LEUnicode32) SWAPL(code))
    1.19 +
    1.20 +//
    1.21 +// Finds the high bit by binary searching
    1.22 +// through the bits in value.
    1.23 +//
    1.24 +le_int8 highBit(le_uint32 value)
    1.25 +{
    1.26 +    le_uint8 bit = 0;
    1.27 +
    1.28 +    if (value >= 1 << 16) {
    1.29 +        value >>= 16;
    1.30 +        bit += 16;
    1.31 +    }
    1.32 +
    1.33 +    if (value >= 1 << 8) {
    1.34 +        value >>= 8;
    1.35 +        bit += 8;
    1.36 +    }
    1.37 +
    1.38 +    if (value >= 1 << 4) {
    1.39 +        value >>= 4;
    1.40 +        bit += 4;
    1.41 +    }
    1.42 +
    1.43 +    if (value >= 1 << 2) {
    1.44 +        value >>= 2;
    1.45 +        bit += 2;
    1.46 +    }
    1.47 +
    1.48 +    if (value >= 1 << 1) {
    1.49 +        value >>= 1;
    1.50 +        bit += 1;
    1.51 +    }
    1.52 +
    1.53 +    return bit;
    1.54 +}
    1.55 +
    1.56 +CMAPMapper *CMAPMapper::createUnicodeMapper(const CMAPTable *cmap)
    1.57 +{
    1.58 +    le_uint16 i;
    1.59 +    le_uint16 nSubtables = SWAPW(cmap->numberSubtables);
    1.60 +    const CMAPEncodingSubtable *subtable = NULL;
    1.61 +    le_uint32 offset1 = 0, offset10 = 0;
    1.62 +
    1.63 +    for (i = 0; i < nSubtables; i += 1) {
    1.64 +        const CMAPEncodingSubtableHeader *esh = &cmap->encodingSubtableHeaders[i];
    1.65 +
    1.66 +        if (SWAPW(esh->platformID) == 3) {
    1.67 +            switch (SWAPW(esh->platformSpecificID)) {
    1.68 +            case 1:
    1.69 +                offset1 = SWAPL(esh->encodingOffset);
    1.70 +                break;
    1.71 +
    1.72 +            case 10:
    1.73 +                offset10 = SWAPL(esh->encodingOffset);
    1.74 +                break;
    1.75 +            }
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +
    1.80 +    if (offset10 != 0)
    1.81 +    {
    1.82 +        subtable = (const CMAPEncodingSubtable *) ((const char *) cmap + offset10);
    1.83 +    } else if (offset1 != 0) {
    1.84 +        subtable = (const CMAPEncodingSubtable *) ((const char *) cmap + offset1);
    1.85 +    } else {
    1.86 +        return NULL;
    1.87 +    }
    1.88 +
    1.89 +    switch (SWAPW(subtable->format)) {
    1.90 +    case 4:
    1.91 +        return new CMAPFormat4Mapper(cmap, (const CMAPFormat4Encoding *) subtable);
    1.92 +
    1.93 +    case 12:
    1.94 +    {
    1.95 +        const CMAPFormat12Encoding *encoding = (const CMAPFormat12Encoding *) subtable;
    1.96 +
    1.97 +        return new CMAPGroupMapper(cmap, encoding->groups, SWAPL(encoding->nGroups));
    1.98 +    }
    1.99 +
   1.100 +    default:
   1.101 +        break;
   1.102 +    }
   1.103 +
   1.104 +    return NULL;
   1.105 +}
   1.106 +
   1.107 +CMAPFormat4Mapper::CMAPFormat4Mapper(const CMAPTable *cmap, const CMAPFormat4Encoding *header)
   1.108 +    : CMAPMapper(cmap)
   1.109 +{
   1.110 +    le_uint16 segCount = SWAPW(header->segCountX2) / 2;
   1.111 +
   1.112 +    fEntrySelector = SWAPW(header->entrySelector);
   1.113 +    fRangeShift = SWAPW(header->rangeShift) / 2;
   1.114 +    fEndCodes = &header->endCodes[0];
   1.115 +    fStartCodes = &header->endCodes[segCount + 1]; // + 1 for reservedPad...
   1.116 +    fIdDelta = &fStartCodes[segCount];
   1.117 +    fIdRangeOffset = &fIdDelta[segCount];
   1.118 +}
   1.119 +
   1.120 +LEGlyphID CMAPFormat4Mapper::unicodeToGlyph(LEUnicode32 unicode32) const
   1.121 +{
   1.122 +    if (unicode32 >= 0x10000) {
   1.123 +        return 0;
   1.124 +    }
   1.125 +
   1.126 +    LEUnicode16 unicode = (LEUnicode16) unicode32;
   1.127 +    le_uint16 index = 0;
   1.128 +    le_uint16 probe = 1 << fEntrySelector;
   1.129 +    TTGlyphID result = 0;
   1.130 +
   1.131 +    if (SWAPU16(fStartCodes[fRangeShift]) <= unicode) {
   1.132 +        index = fRangeShift;
   1.133 +    }
   1.134 +
   1.135 +    while (probe > (1 << 0)) {
   1.136 +        probe >>= 1;
   1.137 +
   1.138 +        if (SWAPU16(fStartCodes[index + probe]) <= unicode) {
   1.139 +            index += probe;
   1.140 +        }
   1.141 +    }
   1.142 +
   1.143 +    if (unicode >= SWAPU16(fStartCodes[index]) && unicode <= SWAPU16(fEndCodes[index])) {
   1.144 +        if (fIdRangeOffset[index] == 0) {
   1.145 +            result = (TTGlyphID) unicode;
   1.146 +        } else {
   1.147 +            le_uint16 offset = unicode - SWAPU16(fStartCodes[index]);
   1.148 +            le_uint16 rangeOffset = SWAPW(fIdRangeOffset[index]);
   1.149 +            le_uint16 *glyphIndexTable = (le_uint16 *) ((char *) &fIdRangeOffset[index] + rangeOffset);
   1.150 +
   1.151 +            result = SWAPW(glyphIndexTable[offset]);
   1.152 +        }
   1.153 +
   1.154 +        result += SWAPW(fIdDelta[index]);
   1.155 +    } else {
   1.156 +        result = 0;
   1.157 +    }
   1.158 +
   1.159 +    return LE_SET_GLYPH(0, result);
   1.160 +}
   1.161 +
   1.162 +CMAPFormat4Mapper::~CMAPFormat4Mapper()
   1.163 +{
   1.164 +    // parent destructor does it all
   1.165 +}
   1.166 +
   1.167 +CMAPGroupMapper::CMAPGroupMapper(const CMAPTable *cmap, const CMAPGroup *groups, le_uint32 nGroups)
   1.168 +    : CMAPMapper(cmap), fGroups(groups)
   1.169 +{
   1.170 +    le_uint8 bit = highBit(nGroups);
   1.171 +    fPower = 1 << bit;
   1.172 +    fRangeOffset = nGroups - fPower;
   1.173 +}
   1.174 +
   1.175 +LEGlyphID CMAPGroupMapper::unicodeToGlyph(LEUnicode32 unicode32) const
   1.176 +{
   1.177 +    le_int32 probe = fPower;
   1.178 +    le_int32 range = 0;
   1.179 +
   1.180 +    if (SWAPU32(fGroups[fRangeOffset].startCharCode) <= unicode32) {
   1.181 +        range = fRangeOffset;
   1.182 +    }
   1.183 +
   1.184 +    while (probe > (1 << 0)) {
   1.185 +        probe >>= 1;
   1.186 +
   1.187 +        if (SWAPU32(fGroups[range + probe].startCharCode) <= unicode32) {
   1.188 +            range += probe;
   1.189 +        }
   1.190 +    }
   1.191 +
   1.192 +    if (SWAPU32(fGroups[range].startCharCode) <= unicode32 && SWAPU32(fGroups[range].endCharCode) >= unicode32) {
   1.193 +        return (LEGlyphID) (SWAPU32(fGroups[range].startGlyphCode) + unicode32 - SWAPU32(fGroups[range].startCharCode));
   1.194 +    }
   1.195 +
   1.196 +    return 0;
   1.197 +}
   1.198 +
   1.199 +CMAPGroupMapper::~CMAPGroupMapper()
   1.200 +{
   1.201 +    // parent destructor does it all
   1.202 +}
   1.203 +