1/*
2 *
3 * (C) Copyright IBM Corp. 1998-2006 - All Rights Reserved
4 *
5 */
6
7#include "LETypes.h"
8#include "OpenTypeTables.h"
9#include "OpenTypeUtilities.h"
10#include "CoverageTables.h"
11#include "LESwaps.h"
12
13U_NAMESPACE_BEGIN
14
15le_int32 CoverageTable::getGlyphCoverage(LEGlyphID glyphID) const
16{
17    switch(SWAPW(coverageFormat))
18    {
19    case 0:
20        return -1;
21
22    case 1:
23    {
24        const CoverageFormat1Table *f1Table = (const CoverageFormat1Table *) this;
25
26        return f1Table->getGlyphCoverage(glyphID);
27    }
28
29    case 2:
30    {
31        const CoverageFormat2Table *f2Table = (const CoverageFormat2Table *) this;
32
33        return f2Table->getGlyphCoverage(glyphID);
34    }
35
36    default:
37        return -1;
38    }
39}
40
41le_int32 CoverageFormat1Table::getGlyphCoverage(LEGlyphID glyphID) const
42{
43    TTGlyphID ttGlyphID = (TTGlyphID) LE_GET_GLYPH(glyphID);
44    le_uint16 count = SWAPW(glyphCount);
45    le_uint8 bit = OpenTypeUtilities::highBit(count);
46    le_uint16 power = 1 << bit;
47    le_uint16 extra = count - power;
48    le_uint16 probe = power;
49    le_uint16 index = 0;
50
51	if (count == 0) {
52		return -1;
53	}
54
55    if (SWAPW(glyphArray[extra]) <= ttGlyphID) {
56        index = extra;
57    }
58
59    while (probe > (1 << 0)) {
60        probe >>= 1;
61
62        if (SWAPW(glyphArray[index + probe]) <= ttGlyphID) {
63            index += probe;
64        }
65    }
66
67    if (SWAPW(glyphArray[index]) == ttGlyphID) {
68        return index;
69    }
70
71    return -1;
72}
73
74le_int32 CoverageFormat2Table::getGlyphCoverage(LEGlyphID glyphID) const
75{
76    TTGlyphID ttGlyphID = (TTGlyphID) LE_GET_GLYPH(glyphID);
77    le_uint16 count = SWAPW(rangeCount);
78    le_int32 rangeIndex =
79        OpenTypeUtilities::getGlyphRangeIndex(ttGlyphID, rangeRecordArray, count);
80
81    if (rangeIndex < 0) {
82        return -1;
83    }
84
85    TTGlyphID firstInRange = SWAPW(rangeRecordArray[rangeIndex].firstGlyph);
86    le_uint16  startCoverageIndex = SWAPW(rangeRecordArray[rangeIndex].rangeValue);
87
88    return startCoverageIndex + (ttGlyphID - firstInRange);
89}
90
91U_NAMESPACE_END
92