]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/ArabicLayoutEngine.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / layout / ArabicLayoutEngine.cpp
1
2 /*
3 * %W% %E%
4 *
5 * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
6 *
7 */
8
9 #include "LETypes.h"
10 #include "LEScripts.h"
11 #include "LEGlyphFilter.h"
12 #include "LayoutEngine.h"
13 #include "OpenTypeLayoutEngine.h"
14 #include "ArabicLayoutEngine.h"
15 #include "ScriptAndLanguageTags.h"
16
17 #include "GlyphSubstitutionTables.h"
18 #include "GlyphDefinitionTables.h"
19 #include "GlyphPositioningTables.h"
20
21 #include "GDEFMarkFilter.h"
22
23 #include "ArabicShaping.h"
24 #include "HebrewShaping.h"
25
26 U_NAMESPACE_BEGIN
27
28 class CharSubstitutionFilter : public UMemory, public LEGlyphFilter
29 {
30 private:
31 const LEFontInstance *fFontInstance;
32
33 CharSubstitutionFilter(const CharSubstitutionFilter &other); // forbid copying of this class
34 CharSubstitutionFilter &operator=(const CharSubstitutionFilter &other); // forbid copying of this class
35
36 public:
37 CharSubstitutionFilter(const LEFontInstance *fontInstance);
38 le_bool accept(LEGlyphID glyph) const;
39 };
40
41 CharSubstitutionFilter::CharSubstitutionFilter(const LEFontInstance *fontInstance)
42 : fFontInstance(fontInstance)
43 {
44 // nothing to do
45 }
46
47 le_bool CharSubstitutionFilter::accept(LEGlyphID glyph) const
48 {
49 return fFontInstance->canDisplay((LEUnicode) glyph);
50 }
51
52 const char ArabicOpenTypeLayoutEngine::fgClassID=0;
53
54 ArabicOpenTypeLayoutEngine::ArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode,
55 const GlyphSubstitutionTableHeader *gsubTable)
56 : OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable)
57 {
58 // fFeatureOrder = ArabicShaping::getFeatureOrder();
59 }
60
61 ArabicOpenTypeLayoutEngine::ArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode)
62 : OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode)
63 {
64 // fFeatureOrder = ArabicShaping::getFeatureOrder();
65 }
66
67 ArabicOpenTypeLayoutEngine::~ArabicOpenTypeLayoutEngine()
68 {
69 // nothing to do
70 }
71
72 // Input: characters
73 // Output: characters, char indices, tags
74 // Returns: output character count
75 le_int32 ArabicOpenTypeLayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
76 LEUnicode *&/*outChars*/, le_int32 *&/*charIndices*/, const LETag **&featureTags, LEErrorCode &success)
77 {
78 if (LE_FAILURE(success)) {
79 return 0;
80 }
81
82 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
83 success = LE_ILLEGAL_ARGUMENT_ERROR;
84 return 0;
85 }
86
87 featureTags = LE_NEW_ARRAY(const LETag *, count);
88
89 if (featureTags == NULL) {
90 success = LE_MEMORY_ALLOCATION_ERROR;
91 return 0;
92 }
93
94 switch (fScriptCode) {
95 case arabScriptCode:
96 {
97 GlyphShaper shaper(featureTags);
98
99 // NOTE: may not need seperate shaper if always use tags...
100 // NOTE: shaper could allocate the feature tags...
101 ArabicShaping::shape(chars, offset, count, max, rightToLeft, shaper);
102 break;
103 }
104
105 case hebrScriptCode:
106 HebrewShaping::shape(chars, offset, count, max, rightToLeft, featureTags);
107 break;
108 }
109
110 return count;
111 }
112
113 void ArabicOpenTypeLayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse,
114 LEGlyphID glyphs[], le_int32 glyphCount, float positions[], LEErrorCode &success)
115 {
116 if (LE_FAILURE(success)) {
117 return;
118 }
119
120 if (chars == NULL || glyphs == NULL || positions == NULL || offset < 0 || count < 0) {
121 success = LE_ILLEGAL_ARGUMENT_ERROR;
122 return;
123 }
124
125 if (fGPOSTable != NULL) {
126 OpenTypeLayoutEngine::adjustGlyphPositions(chars, offset, count, reverse, glyphs, glyphCount, positions, success);
127 } else if (fGDEFTable != NULL) {
128 GDEFMarkFilter filter(fGDEFTable);
129
130 adjustMarkGlyphs(glyphs, glyphCount, false, &filter, positions, success);
131 } else {
132 GlyphDefinitionTableHeader *gdefTable = (GlyphDefinitionTableHeader *) ArabicShaping::glyphDefinitionTable;
133 GDEFMarkFilter filter(gdefTable);
134 LEGlyphID *tempGlyphs;
135
136 // FIXME: we could avoid the memory allocation and copying here by
137 // making a clone of the adjustMarkGlyphs method which took characters
138 // directly...
139 tempGlyphs = LE_NEW_ARRAY(LEGlyphID, count);
140
141 if (tempGlyphs == NULL) {
142 success = LE_MEMORY_ALLOCATION_ERROR;
143 return;
144 }
145
146 for (le_int32 i = 0; i < count; i += 1) {
147 tempGlyphs[i] = (LEGlyphID) chars[offset + i];
148 }
149
150 adjustMarkGlyphs(tempGlyphs, count, reverse, &filter, positions, success);
151
152 LE_DELETE_ARRAY(tempGlyphs);
153 }
154 }
155
156 UnicodeArabicOpenTypeLayoutEngine::UnicodeArabicOpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode)
157 : ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode)
158 {
159 switch (scriptCode) {
160 case arabScriptCode:
161 fGSUBTable = (const GlyphSubstitutionTableHeader *) ArabicShaping::glyphSubstitutionTable;
162 fGDEFTable = (const GlyphDefinitionTableHeader *) ArabicShaping::glyphDefinitionTable;
163 break;
164
165 case hebrScriptCode:
166 fGSUBTable = (const GlyphSubstitutionTableHeader *) HebrewShaping::glyphSubstitutionTable;
167 fGDEFTable = (const GlyphDefinitionTableHeader *) HebrewShaping::glyphDefinitionTable;
168 break;
169 }
170
171
172 fSubstitutionFilter = new CharSubstitutionFilter(fontInstance);
173 }
174
175 UnicodeArabicOpenTypeLayoutEngine::~UnicodeArabicOpenTypeLayoutEngine()
176 {
177 delete fSubstitutionFilter;
178 }
179
180 // "glyphs", "indices" -> glyphs, indices
181 le_int32 UnicodeArabicOpenTypeLayoutEngine::glyphPostProcessing(LEGlyphID tempGlyphs[], le_int32 tempCharIndices[], le_int32 tempGlyphCount,
182 LEGlyphID *&glyphs, le_int32 *&charIndices, LEErrorCode &success)
183 {
184 if (LE_FAILURE(success)) {
185 return 0;
186 }
187
188 if (tempGlyphs == NULL || tempCharIndices == NULL ||tempGlyphCount < 0) {
189 success = LE_ILLEGAL_ARGUMENT_ERROR;
190 return 0;
191 }
192
193 // FIXME: we could avoid the memory allocation and copy if we
194 // made a clone of mapCharsToGlyphs which took the fake glyphs
195 // directly.
196 LEUnicode *tempChars = LE_NEW_ARRAY(LEUnicode, tempGlyphCount);
197
198 if (tempChars == NULL) {
199 success = LE_MEMORY_ALLOCATION_ERROR;
200 return 0;
201 }
202
203 for (le_int32 i = 0; i < tempGlyphCount; i += 1) {
204 tempChars[i] = (LEUnicode) LE_GET_GLYPH(tempGlyphs[i]);
205 }
206
207 charIndices = tempCharIndices;
208
209 ArabicOpenTypeLayoutEngine::mapCharsToGlyphs(tempChars, 0, tempGlyphCount, false, true, glyphs, charIndices, success);
210
211 LE_DELETE_ARRAY(tempChars);
212
213 return tempGlyphCount;
214 }
215
216 void UnicodeArabicOpenTypeLayoutEngine::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, le_bool /*mirror*/, LEGlyphID *&glyphs, le_int32 *&charIndices, LEErrorCode &success)
217 {
218 if (LE_FAILURE(success)) {
219 return;
220 }
221
222 if (chars == NULL || offset < 0 || count < 0) {
223 success = LE_ILLEGAL_ARGUMENT_ERROR;
224 return;
225 }
226
227 le_int32 i, dir, out;
228
229 out = 0;
230 dir = 1;
231
232 if (reverse) {
233 out = count - 1;
234 dir = -1;
235 }
236
237 glyphs = LE_NEW_ARRAY(LEGlyphID, count);
238
239 if (glyphs == NULL) {
240 success = LE_MEMORY_ALLOCATION_ERROR;
241 return;
242 }
243
244 charIndices = LE_NEW_ARRAY(le_int32, count);
245
246 if (charIndices == NULL) {
247 LE_DELETE_ARRAY(glyphs);
248 success = LE_MEMORY_ALLOCATION_ERROR;
249 return;
250 }
251
252 for (i = 0; i < count; i += 1, out += dir) {
253 glyphs[out] = (LEGlyphID) chars[offset + i];
254 charIndices[out] = i;
255 }
256 }
257
258 void UnicodeArabicOpenTypeLayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse,
259 LEGlyphID glyphs[], le_int32 glyphCount, float positions[], LEErrorCode &success)
260 {
261 if (LE_FAILURE(success)) {
262 return;
263 }
264
265 if (chars == NULL || glyphs == NULL || positions == NULL || offset < 0 || count < 0 || glyphCount < 0) {
266 success = LE_ILLEGAL_ARGUMENT_ERROR;
267 return;
268 }
269
270 GDEFMarkFilter filter(fGDEFTable);
271
272 // FIXME: we could avoid the memory allocation and copying here by
273 // making a clone of the adjustMarkGlyphs method which took characters
274 // directly...
275 LEGlyphID *tempGlyphs = LE_NEW_ARRAY(LEGlyphID, count);
276
277 if (tempGlyphs == NULL) {
278 success = LE_MEMORY_ALLOCATION_ERROR;
279 return;
280 }
281
282 for (le_int32 i = 0; i < count; i += 1) {
283 tempGlyphs[i] = (LEGlyphID) chars[offset + i];
284 }
285
286 adjustMarkGlyphs(tempGlyphs, count, reverse, &filter, positions, success);
287
288 LE_DELETE_ARRAY(tempGlyphs);
289 }
290
291 U_NAMESPACE_END
292