1
2/*
3 *
4 * (C) Copyright IBM Corp. 1998-2008 - All Rights Reserved
5 *
6 */
7
8#include "LETypes.h"
9#include "LayoutEngine.h"
10#include "ThaiLayoutEngine.h"
11#include "ScriptAndLanguageTags.h"
12#include "LEGlyphStorage.h"
13
14#include "KernTable.h"
15
16#include "ThaiShaping.h"
17
18U_NAMESPACE_BEGIN
19
20UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ThaiLayoutEngine)
21
22ThaiLayoutEngine::ThaiLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags, LEErrorCode &success)
23    : LayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success)
24{
25    fErrorChar = 0x25CC;
26
27    // Figure out which presentation forms the font uses
28    if (! fontInstance->canDisplay(0x0E01)) {
29        // No Thai in font; don't use presentation forms.
30        fGlyphSet = 3;
31    } else if (fontInstance->canDisplay(0x0E64)) {
32        // WorldType uses reserved space in Thai block
33        fGlyphSet = 0;
34    } else if (fontInstance->canDisplay(0xF701)) {
35        // Microsoft corporate zone
36        fGlyphSet = 1;
37
38        if (!fontInstance->canDisplay(fErrorChar)) {
39            fErrorChar = 0xF71B;
40        }
41    } else if (fontInstance->canDisplay(0xF885)) {
42        // Apple corporate zone
43        fGlyphSet = 2;
44    } else {
45        // no presentation forms in the font
46        fGlyphSet = 3;
47    }
48}
49
50ThaiLayoutEngine::~ThaiLayoutEngine()
51{
52    // nothing to do
53}
54
55// Input: characters (0..max provided for context)
56// Output: glyphs, char indices
57// Returns: the glyph count
58// NOTE: this assumes that ThaiShaping::compose will allocate the outChars array...
59le_int32 ThaiLayoutEngine::computeGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool /*rightToLeft*/, LEGlyphStorage &glyphStorage, LEErrorCode &success)
60{
61    if (LE_FAILURE(success)) {
62        return 0;
63    }
64
65    if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
66        success = LE_ILLEGAL_ARGUMENT_ERROR;
67        return 0;
68    }
69
70    LEUnicode *outChars;
71    le_int32 glyphCount;
72
73    // This is enough room for the worst-case expansion
74    // (it says here...)
75    outChars = LE_NEW_ARRAY(LEUnicode, count * 2);
76
77    if (outChars == NULL) {
78        success = LE_MEMORY_ALLOCATION_ERROR;
79        return 0;
80    }
81
82    glyphStorage.allocateGlyphArray(count * 2, FALSE, success);
83
84    if (LE_FAILURE(success)) {
85        LE_DELETE_ARRAY(outChars);
86        success = LE_MEMORY_ALLOCATION_ERROR;
87        return 0;
88    }
89
90    glyphCount = ThaiShaping::compose(chars, offset, count, fGlyphSet, fErrorChar, outChars, glyphStorage);
91    mapCharsToGlyphs(outChars, 0, glyphCount, FALSE, FALSE, glyphStorage, success);
92
93    LE_DELETE_ARRAY(outChars);
94
95    glyphStorage.adoptGlyphCount(glyphCount);
96    return glyphCount;
97}
98
99// This is the same as LayoutEngline::adjustGlyphPositions() except that it doesn't call adjustMarkGlyphs
100void ThaiLayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool  /*reverse*/,
101                                        LEGlyphStorage &glyphStorage, LEErrorCode &success)
102{
103    if (LE_FAILURE(success)) {
104        return;
105    }
106
107    if (chars == NULL || offset < 0 || count < 0) {
108        success = LE_ILLEGAL_ARGUMENT_ERROR;
109        return;
110    }
111
112    if (fTypoFlags & 0x1) { /* kerning enabled */
113      static const le_uint32 kernTableTag = LE_KERN_TABLE_TAG;
114
115      KernTable kt(fFontInstance, getFontTable(kernTableTag));
116      kt.process(glyphStorage);
117    }
118
119    // default is no adjustments
120    return;
121}
122
123U_NAMESPACE_END
124