1/*
2 *
3 * (C) Copyright IBM Corp. 1998-2013 - All Rights Reserved
4 *
5 */
6
7#include "LETypes.h"
8#include "OpenTypeTables.h"
9#include "OpenTypeUtilities.h"
10#include "ClassDefinitionTables.h"
11#include "LESwaps.h"
12
13U_NAMESPACE_BEGIN
14
15le_int32 ClassDefinitionTable::getGlyphClass(const LETableReference& base, LEGlyphID glyphID, LEErrorCode &success) const
16{
17  LEReferenceTo<ClassDefinitionTable> thisRef(base, success);
18  if (LE_FAILURE(success)) return 0;
19
20  switch(SWAPW(classFormat)) {
21    case 0:
22        return 0;
23
24    case 1:
25    {
26      const LEReferenceTo<ClassDefFormat1Table> f1Table(thisRef, success);
27      return f1Table->getGlyphClass(f1Table, glyphID, success);
28    }
29
30    case 2:
31    {
32      const LEReferenceTo<ClassDefFormat2Table> f2Table(thisRef, success);
33      return  f2Table->getGlyphClass(f2Table, glyphID, success);
34    }
35
36    default:
37        return 0;
38  }
39}
40
41le_bool ClassDefinitionTable::hasGlyphClass(const LETableReference &base, le_int32 glyphClass, LEErrorCode &success) const
42{
43    LEReferenceTo<ClassDefinitionTable> thisRef(base, success);
44    if (LE_FAILURE(success)) return 0;
45
46    switch(SWAPW(classFormat)) {
47    case 0:
48        return 0;
49
50    case 1:
51    {
52      const LEReferenceTo<ClassDefFormat1Table> f1Table(thisRef, success);
53      return f1Table->hasGlyphClass(f1Table, glyphClass, success);
54    }
55
56    case 2:
57    {
58      const LEReferenceTo<ClassDefFormat2Table> f2Table(thisRef, success);
59      return f2Table->hasGlyphClass(f2Table, glyphClass, success);
60    }
61
62    default:
63        return 0;
64    }
65}
66
67le_int32 ClassDefFormat1Table::getGlyphClass(const LETableReference& base, LEGlyphID glyphID, LEErrorCode &success) const
68{
69    if(LE_FAILURE(success)) return 0;
70
71    le_uint16 count = SWAPW(glyphCount);
72    LEReferenceToArrayOf<le_uint16> classValueArrayRef(base, success, &classValueArray[0], count);
73    TTGlyphID ttGlyphID  = (TTGlyphID) LE_GET_GLYPH(glyphID);
74    TTGlyphID firstGlyph = SWAPW(startGlyph);
75    TTGlyphID lastGlyph  = firstGlyph + count;
76
77    if (LE_SUCCESS(success) && ttGlyphID >= firstGlyph && ttGlyphID < lastGlyph) {
78      return SWAPW( classValueArrayRef(ttGlyphID - firstGlyph, success) );
79    }
80
81    return 0;
82}
83
84le_bool ClassDefFormat1Table::hasGlyphClass(const LETableReference &base, le_int32 glyphClass, LEErrorCode &success) const
85{
86    if(LE_FAILURE(success)) return 0;
87    le_uint16 count = SWAPW(glyphCount);
88    LEReferenceToArrayOf<le_uint16> classValueArrayRef(base, success, &classValueArray[0], count);
89    int i;
90
91    for (i = 0; LE_SUCCESS(success)&& (i < count); i += 1) {
92      if (SWAPW(classValueArrayRef(i,success)) == glyphClass) {
93            return TRUE;
94        }
95    }
96
97    return FALSE;
98}
99
100le_int32 ClassDefFormat2Table::getGlyphClass(const LETableReference& base, LEGlyphID glyphID, LEErrorCode &success) const
101{
102    if(LE_FAILURE(success)) return 0;
103    TTGlyphID ttGlyph    = (TTGlyphID) LE_GET_GLYPH(glyphID);
104    le_uint16 rangeCount = SWAPW(classRangeCount);
105    LEReferenceToArrayOf<GlyphRangeRecord> classRangeRecordArrayRef(base, success, &classRangeRecordArray[0], rangeCount);
106    le_int32  rangeIndex =
107      OpenTypeUtilities::getGlyphRangeIndex(ttGlyph, classRangeRecordArrayRef, success);
108
109    if (rangeIndex < 0 || LE_FAILURE(success)) {
110        return 0;
111    }
112
113    return SWAPW(classRangeRecordArrayRef(rangeIndex, success).rangeValue);
114}
115
116le_bool ClassDefFormat2Table::hasGlyphClass(const LETableReference &base, le_int32 glyphClass, LEErrorCode &success) const
117{
118    if(LE_FAILURE(success)) return 0;
119    le_uint16 rangeCount = SWAPW(classRangeCount);
120    LEReferenceToArrayOf<GlyphRangeRecord> classRangeRecordArrayRef(base, success, &classRangeRecordArray[0], rangeCount);
121    int i;
122
123    for (i = 0; i < rangeCount && LE_SUCCESS(success); i += 1) {
124      if (SWAPW(classRangeRecordArrayRef(i,success).rangeValue) == glyphClass) {
125            return TRUE;
126        }
127    }
128
129    return FALSE;
130}
131
132U_NAMESPACE_END
133