]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/ArabicLayoutEngine.cpp
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / layout / ArabicLayoutEngine.cpp
1
2 /*
3 *
4 * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
5 *
6 */
7
8 #include "LETypes.h"
9 #include "LEScripts.h"
10 #include "LEGlyphFilter.h"
11 #include "LEGlyphStorage.h"
12 #include "LayoutEngine.h"
13 #include "OpenTypeLayoutEngine.h"
14 #include "ArabicLayoutEngine.h"
15 #include "ScriptAndLanguageTags.h"
16 #include "CharSubstitutionFilter.h"
17
18 #include "GlyphSubstitutionTables.h"
19 #include "GlyphDefinitionTables.h"
20 #include "GlyphPositioningTables.h"
21
22 #include "GDEFMarkFilter.h"
23
24 #include "ArabicShaping.h"
25 #include "CanonShaping.h"
26
27 U_NAMESPACE_BEGIN
28
29 le_bool CharSubstitutionFilter::accept(LEGlyphID glyph) const
30 {
31 return fFontInstance->canDisplay((LEUnicode) glyph);
32 }
33
34 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ArabicOpenTypeLayoutEngine)
35
36 ArabicOpenTypeLayoutEngine::ArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode,
37 const GlyphSubstitutionTableHeader *gsubTable)
38 : OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable)
39 {
40 /**/ fFeatureOrder = ArabicShaping::getFeatureOrder();
41 }
42
43 ArabicOpenTypeLayoutEngine::ArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode)
44 : OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode)
45 {
46 // fFeatureOrder = ArabicShaping::getFeatureOrder();
47 }
48
49 ArabicOpenTypeLayoutEngine::~ArabicOpenTypeLayoutEngine()
50 {
51 // nothing to do
52 }
53
54 // Input: characters
55 // Output: characters, char indices, tags
56 // Returns: output character count
57 le_int32 ArabicOpenTypeLayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
58 LEUnicode *&/*outChars*/, LEGlyphStorage &glyphStorage, LEErrorCode &success)
59 {
60 if (LE_FAILURE(success)) {
61 return 0;
62 }
63
64 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
65 success = LE_ILLEGAL_ARGUMENT_ERROR;
66 return 0;
67 }
68
69 glyphStorage.adoptGlyphCount(count);
70 glyphStorage.allocateAuxData(success);
71
72 if (LE_FAILURE(success)) {
73 success = LE_MEMORY_ALLOCATION_ERROR;
74 return 0;
75 }
76
77 ArabicShaping::shape(chars, offset, count, max, rightToLeft, glyphStorage);
78
79 return count;
80 }
81
82 void ArabicOpenTypeLayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse,
83 LEGlyphStorage &glyphStorage, LEErrorCode &success)
84 {
85 if (LE_FAILURE(success)) {
86 return;
87 }
88
89 if (chars == NULL || offset < 0 || count < 0) {
90 success = LE_ILLEGAL_ARGUMENT_ERROR;
91 return;
92 }
93
94 if (fGPOSTable != NULL) {
95 OpenTypeLayoutEngine::adjustGlyphPositions(chars, offset, count, reverse, glyphStorage, success);
96 } else if (fGDEFTable != NULL) {
97 GDEFMarkFilter filter(fGDEFTable);
98
99 adjustMarkGlyphs(glyphStorage, &filter, success);
100 } else {
101 GlyphDefinitionTableHeader *gdefTable = (GlyphDefinitionTableHeader *) CanonShaping::glyphDefinitionTable;
102 GDEFMarkFilter filter(gdefTable);
103
104 adjustMarkGlyphs(&chars[offset], count, reverse, glyphStorage, &filter, success);
105 }
106 }
107
108 UnicodeArabicOpenTypeLayoutEngine::UnicodeArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode)
109 : ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode)
110 {
111 fGSUBTable = (const GlyphSubstitutionTableHeader *) CanonShaping::glyphSubstitutionTable;
112 fGDEFTable = (const GlyphDefinitionTableHeader *) CanonShaping::glyphDefinitionTable;
113
114 fSubstitutionFilter = new CharSubstitutionFilter(fontInstance);
115 }
116
117 UnicodeArabicOpenTypeLayoutEngine::~UnicodeArabicOpenTypeLayoutEngine()
118 {
119 delete fSubstitutionFilter;
120 }
121
122 // "glyphs", "indices" -> glyphs, indices
123 le_int32 UnicodeArabicOpenTypeLayoutEngine::glyphPostProcessing(LEGlyphStorage &tempGlyphStorage, LEGlyphStorage &glyphStorage, LEErrorCode &success)
124 {
125 if (LE_FAILURE(success)) {
126 return 0;
127 }
128
129 // FIXME: we could avoid the memory allocation and copy if we
130 // made a clone of mapCharsToGlyphs which took the fake glyphs
131 // directly.
132 le_int32 tempGlyphCount = tempGlyphStorage.getGlyphCount();
133 LEUnicode *tempChars = LE_NEW_ARRAY(LEUnicode, tempGlyphCount);
134
135 if (tempChars == NULL) {
136 success = LE_MEMORY_ALLOCATION_ERROR;
137 return 0;
138 }
139
140 for (le_int32 i = 0; i < tempGlyphCount; i += 1) {
141 tempChars[i] = (LEUnicode) LE_GET_GLYPH(tempGlyphStorage[i]);
142 }
143
144 glyphStorage.adoptCharIndicesArray(tempGlyphStorage);
145
146 ArabicOpenTypeLayoutEngine::mapCharsToGlyphs(tempChars, 0, tempGlyphCount, FALSE, TRUE, glyphStorage, success);
147
148 LE_DELETE_ARRAY(tempChars);
149
150 return tempGlyphCount;
151 }
152
153 void UnicodeArabicOpenTypeLayoutEngine::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, le_bool /*mirror*/, LEGlyphStorage &glyphStorage, LEErrorCode &success)
154 {
155 if (LE_FAILURE(success)) {
156 return;
157 }
158
159 if (chars == NULL || offset < 0 || count < 0) {
160 success = LE_ILLEGAL_ARGUMENT_ERROR;
161 return;
162 }
163
164 le_int32 i, dir = 1, out = 0;
165
166 if (reverse) {
167 out = count - 1;
168 dir = -1;
169 }
170
171 glyphStorage.allocateGlyphArray(count, reverse, success);
172
173 for (i = 0; i < count; i += 1, out += dir) {
174 glyphStorage[out] = (LEGlyphID) chars[offset + i];
175 }
176 }
177
178 void UnicodeArabicOpenTypeLayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse,
179 LEGlyphStorage &glyphStorage, LEErrorCode &success)
180 {
181 if (LE_FAILURE(success)) {
182 return;
183 }
184
185 if (chars == NULL || offset < 0 || count < 0) {
186 success = LE_ILLEGAL_ARGUMENT_ERROR;
187 return;
188 }
189
190 GDEFMarkFilter filter(fGDEFTable);
191
192 adjustMarkGlyphs(&chars[offset], count, reverse, glyphStorage, &filter, success);
193 }
194
195 U_NAMESPACE_END
196