PairPositioningSubtables.cpp 8.9 KB
Newer Older
D
duke 已提交
1 2 3 4 5
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
6
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
7
 * particular file as subject to the "Classpath" exception as provided
8
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
9 10 11 12 13 14 15 16 17 18 19
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 */

/*
 *
 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
 *
 */

#include "LETypes.h"
#include "LEFontInstance.h"
#include "OpenTypeTables.h"
#include "GlyphPositioningTables.h"
#include "PairPositioningSubtables.h"
#include "ValueRecords.h"
#include "GlyphIterator.h"
#include "OpenTypeUtilities.h"
#include "LESwaps.h"

42 43
U_NAMESPACE_BEGIN

P
prr 已提交
44
le_uint32 PairPositioningSubtable::process(const LEReferenceTo<PairPositioningSubtable> &base, GlyphIterator *glyphIterator, const LEFontInstance *fontInstance, LEErrorCode &success) const
D
duke 已提交
45
{
M
mbalao 已提交
46 47 48 49
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
50 51 52 53 54 55 56
    switch(SWAPW(subtableFormat))
    {
    case 0:
        return 0;

    case 1:
    {
P
prr 已提交
57
      const LEReferenceTo<PairPositioningFormat1Subtable> subtable(base, success, (const PairPositioningFormat1Subtable *) this);
D
duke 已提交
58

P
prr 已提交
59 60 61 62
      if(LE_SUCCESS(success))
      return subtable->process(subtable, glyphIterator, fontInstance, success);
      else
        return 0;
D
duke 已提交
63 64 65 66
    }

    case 2:
    {
P
prr 已提交
67
      const LEReferenceTo<PairPositioningFormat2Subtable> subtable(base, success, (const PairPositioningFormat2Subtable *) this);
D
duke 已提交
68

P
prr 已提交
69 70 71 72
      if(LE_SUCCESS(success))
      return subtable->process(subtable, glyphIterator, fontInstance, success);
      else
        return 0;
D
duke 已提交
73 74
    }
    default:
P
prr 已提交
75
      return 0;
D
duke 已提交
76 77 78
    }
}

P
prr 已提交
79
le_uint32 PairPositioningFormat1Subtable::process(const LEReferenceTo<PairPositioningFormat1Subtable> &base, GlyphIterator *glyphIterator, const LEFontInstance *fontInstance, LEErrorCode &success) const
D
duke 已提交
80
{
M
mbalao 已提交
81 82 83 84
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
85
    LEGlyphID firstGlyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
86
    le_int32 coverageIndex = getGlyphCoverage(base, firstGlyph, success);
D
duke 已提交
87 88
    GlyphIterator tempIterator(*glyphIterator);

P
prr 已提交
89 90 91
    LEReferenceToArrayOf<Offset> pairSetTableOffsetArrayRef(base, success, pairSetTableOffsetArray, SWAPW(pairSetCount));

    if (LE_SUCCESS(success) && coverageIndex >= 0 && glyphIterator->next() && (le_uint32)coverageIndex < pairSetTableOffsetArrayRef.getCount()) {
D
duke 已提交
92
        Offset pairSetTableOffset = SWAPW(pairSetTableOffsetArray[coverageIndex]);
P
prr 已提交
93 94
        LEReferenceTo<PairSetTable> pairSetTable(base, success, pairSetTableOffset);
        if( LE_FAILURE(success) ) return 0;
D
duke 已提交
95
        le_uint16 pairValueCount = SWAPW(pairSetTable->pairValueCount);
P
prr 已提交
96 97
        LEReferenceTo<PairValueRecord> pairValueRecordArray(pairSetTable, success, pairSetTable->pairValueRecordArray);
        if( LE_FAILURE(success) ) return 0;
D
duke 已提交
98 99 100 101
        le_int16 valueRecord1Size = ValueRecord::getSize(SWAPW(valueFormat1));
        le_int16 valueRecord2Size = ValueRecord::getSize(SWAPW(valueFormat2));
        le_int16 recordSize = sizeof(PairValueRecord) - sizeof(ValueRecord) + valueRecord1Size + valueRecord2Size;
        LEGlyphID secondGlyph = glyphIterator->getCurrGlyphID();
102
        LEReferenceTo<PairValueRecord> pairValueRecord;
D
duke 已提交
103 104

        if (pairValueCount != 0) {
P
prr 已提交
105
          pairValueRecord = findPairValueRecord((TTGlyphID) LE_GET_GLYPH(secondGlyph), pairValueRecordArray, pairValueCount, recordSize, success);
D
duke 已提交
106 107
        }

P
prr 已提交
108
        if (pairValueRecord.isEmpty() || LE_FAILURE(success)) {
D
duke 已提交
109 110 111 112
            return 0;
        }

        if (valueFormat1 != 0) {
P
prr 已提交
113
          pairValueRecord->valueRecord1.adjustPosition(SWAPW(valueFormat1), base, tempIterator, fontInstance, success);
D
duke 已提交
114 115 116
        }

        if (valueFormat2 != 0) {
P
prr 已提交
117 118 119 120
          LEReferenceTo<ValueRecord> valueRecord2(base, success, ((char *) &pairValueRecord->valueRecord1 + valueRecord1Size));
          if(LE_SUCCESS(success)) {
            valueRecord2->adjustPosition(SWAPW(valueFormat2), base, *glyphIterator, fontInstance, success);
          }
D
duke 已提交
121 122
        }

S
srl 已提交
123 124 125 126
        // back up glyphIterator so second glyph can be
        // first glyph in the next pair
        glyphIterator->prev();
        return 1;
D
duke 已提交
127 128 129 130 131
    }

    return 0;
}

P
prr 已提交
132
le_uint32 PairPositioningFormat2Subtable::process(const LEReferenceTo<PairPositioningFormat2Subtable> &base, GlyphIterator *glyphIterator, const LEFontInstance *fontInstance, LEErrorCode &success) const
D
duke 已提交
133
{
M
mbalao 已提交
134 135 136 137
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
138
    LEGlyphID firstGlyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
139
    le_int32 coverageIndex = getGlyphCoverage(base, firstGlyph, success);
140 141 142 143 144

    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
145 146 147 148
    GlyphIterator tempIterator(*glyphIterator);

    if (coverageIndex >= 0 && glyphIterator->next()) {
        LEGlyphID secondGlyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
149 150 151 152
        const LEReferenceTo<ClassDefinitionTable> classDef1(base, success, SWAPW(classDef1Offset));
        const LEReferenceTo<ClassDefinitionTable> classDef2(base, success, SWAPW(classDef2Offset));
        le_int32 class1 = classDef1->getGlyphClass(classDef1, firstGlyph, success);
        le_int32 class2 = classDef2->getGlyphClass(classDef2, secondGlyph, success);
D
duke 已提交
153 154 155 156
        le_int16 valueRecord1Size = ValueRecord::getSize(SWAPW(valueFormat1));
        le_int16 valueRecord2Size = ValueRecord::getSize(SWAPW(valueFormat2));
        le_int16 class2RecordSize = valueRecord1Size + valueRecord2Size;
        le_int16 class1RecordSize = class2RecordSize * SWAPW(class2Count);
P
prr 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
        const LEReferenceTo<Class1Record> class1Record(base, success, (const Class1Record *) ((char *) class1RecordArray + (class1RecordSize * class1)));
        const LEReferenceTo<Class2Record> class2Record(base, success, (const Class2Record *) ((char *) class1Record->class2RecordArray + (class2RecordSize * class2)));

        if( LE_SUCCESS(success) ) {
          if (valueFormat1 != 0) {
            class2Record->valueRecord1.adjustPosition(SWAPW(valueFormat1), base, tempIterator, fontInstance, success);
          }
          if (valueFormat2 != 0) {
            const LEReferenceTo<ValueRecord> valueRecord2(base, success, ((char *) &class2Record->valueRecord1) + valueRecord1Size);
            LEReferenceTo<PairPositioningFormat2Subtable> thisRef(base, success, this);
            if(LE_SUCCESS(success)) {
              valueRecord2->adjustPosition(SWAPW(valueFormat2), thisRef, *glyphIterator, fontInstance, success);
            }
          }
D
duke 已提交
171 172
        }

S
srl 已提交
173 174 175 176
        // back up glyphIterator so second glyph can be
        // first glyph in the next pair
        glyphIterator->prev();
        return 1;
D
duke 已提交
177 178 179 180 181
    }

    return 0;
}

P
prr 已提交
182 183 184 185
LEReferenceTo<PairValueRecord>
PairPositioningFormat1Subtable::findPairValueRecord(TTGlyphID glyphID, LEReferenceTo<PairValueRecord>& records,
                                                    le_uint16 recordCount,
                                                    le_uint16 recordSize, LEErrorCode &success) const
D
duke 已提交
186
{
S
srl 已提交
187 188 189 190
#if 1
        // The OpenType spec. says that the ValueRecord table is
        // sorted by secondGlyph. Unfortunately, there are fonts
        // around that have an unsorted ValueRecord table.
P
prr 已提交
191
        LEReferenceTo<PairValueRecord> record(records);
S
srl 已提交
192 193

        for(le_int32 r = 0; r < recordCount; r += 1) {
V
vadim 已提交
194 195 196
          if (r > 0) {
            record.addOffset(recordSize, success);
          }
P
prr 已提交
197 198 199 200
          if(LE_FAILURE(success)) return LEReferenceTo<PairValueRecord>();
          if (SWAPW(record->secondGlyph) == glyphID) {
            return record;
          }
S
srl 已提交
201 202
        }
#else
203
  #error dead code - not updated.
D
duke 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    le_uint8 bit = OpenTypeUtilities::highBit(recordCount);
    le_uint16 power = 1 << bit;
    le_uint16 extra = (recordCount - power) * recordSize;
    le_uint16 probe = power * recordSize;
    const PairValueRecord *record = records;
    const PairValueRecord *trial = (const PairValueRecord *) ((char *) record + extra);

    if (SWAPW(trial->secondGlyph) <= glyphID) {
        record = trial;
    }

    while (probe > recordSize) {
        probe >>= 1;
        trial = (const PairValueRecord *) ((char *) record + probe);

        if (SWAPW(trial->secondGlyph) <= glyphID) {
            record = trial;
        }
    }

    if (SWAPW(record->secondGlyph) == glyphID) {
        return record;
    }
S
srl 已提交
227
#endif
D
duke 已提交
228

P
prr 已提交
229
    return LEReferenceTo<PairValueRecord>();
D
duke 已提交
230
}
231 232

U_NAMESPACE_END