First public contribution.
1 // Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 //#include <textbase.h>
24 #include "FontArabic.h"
25 #include "TextBasePanic.h"
29 // Arabic Presentation Forms.
34 The contextual forms of each character in the range 0621 - 064A, in
35 the order final, initial, medial; followed by the forms of
36 the lam-alef ligatures (lam with 0622, 0623, 0625 and 0627);
37 followed by the forms of the characters in the range 0671 - 06D3
39 LOCAL_D const TUint16 TheArabicShapeTable[] =
41 // Contextual forms for standard Arabic letters
42 0x621, 0x621, 0x621, // 621
43 0xfe82, 0x622, 0x622, // 622
44 0xfe84, 0x623, 0x623, // 623
45 0xfe86, 0x624, 0x624, // 624
46 0xfe88, 0x625, 0x625, // 625
47 0xfe8a, 0xfe8b, 0xfe8c, // 626
48 0xfe8e, 0x627, 0x627, // 627
49 0xfe90, 0xfe91, 0xfe92, // 628
50 0xfe94, 0x629, 0x629, // 629
51 0xfe96, 0xfe97, 0xfe98, // 62a
52 0xfe9a, 0xfe9b, 0xfe9c, // 62b
53 0xfe9e, 0xfe9f, 0xfea0, // 62c
54 0xfea2, 0xfea3, 0xfea4, // 62d
55 0xfea6, 0xfea7, 0xfea8, // 62e
56 0xfeaa, 0x62f, 0x62f, // 62f
57 0xfeac, 0x630, 0x630, // 630
58 0xfeae, 0x631, 0x631, // 631
59 0xfeb0, 0x632, 0x632, // 632
60 0xfeb2, 0xfeb3, 0xfeb4, // 633
61 0xfeb6, 0xfeb7, 0xfeb8, // 634
62 0xfeba, 0xfebb, 0xfebc, // 635
63 0xfebe, 0xfebf, 0xfec0, // 636
64 0xfec2, 0xfec3, 0xfec4, // 637
65 0xfec6, 0xfec7, 0xfec8, // 638
66 0xfeca, 0xfecb, 0xfecc, // 639
67 0xfece, 0xfecf, 0xfed0, // 63a
68 0x63b, 0x63b, 0x63b, // placeholder
69 0x63c, 0x63c, 0x63c, // placeholder
70 0x63d, 0x63d, 0x63d, // placeholder
71 0x63e, 0x63e, 0x63e, // placeholder
72 0x63f, 0x63f, 0x63f, // placeholder
73 0x640, 0x640, 0x640, // 640
74 0xfed2, 0xfed3, 0xfed4, // 641
75 0xfed6, 0xfed7, 0xfed8, // 642
76 0xfeda, 0xfedb, 0xfedc, // 643
77 0xfede, 0xfedf, 0xfee0, // 644
78 0xfee2, 0xfee3, 0xfee4, // 645
79 0xfee6, 0xfee7, 0xfee8, // 646
80 0xfeea, 0xfeeb, 0xfeec, // 647
81 0xfeee, 0x648, 0x648, // 648
82 0xfef0, 0xfbe8, 0xfbe9, // 649
83 0xfef2, 0xfef3, 0xfef4, // 64A
85 // Contextual forms for lam-alef ligatures
86 0xfef6, 0xfef5, 0xfef5, // 644 + 622
87 0xfef8, 0xfef7, 0xfef7, // 644 + 623
88 0xfefa, 0xfef9, 0xfef9, // 644 + 625
89 0xfefc, 0xfefb, 0xfefb, // 644 + 627
91 // Contextual forms for extended Arabic letters for Persian, Urdu, Sindhi, etc.
92 0xfb51, 0x0671, 0x0671, // 671
93 0x0672, 0x0672, 0x0672, // placeholder
94 0x0673, 0x0673, 0x0673, // placeholder
95 0x0674, 0x0674, 0x0674, // placeholder
96 0x0675, 0x0675, 0x0675, // placeholder
97 0x0676, 0x0676, 0x0676, // placeholder
98 0x0677, 0x0677, 0x0677, // placeholder
99 0x0678, 0x0678, 0x0678, // placeholder
100 0xfb67, 0xfb68, 0xfb69, // 679
101 0xfb5f, 0xfb60, 0xfb61, // 67a
102 0xfb53, 0xfb54, 0xfb55, // 67b
103 0x067c, 0x067c, 0x067c, // placeholder
104 0x067d, 0x067d, 0x067d, // placeholder
105 0xfb57, 0xfb58, 0xfb59, // 67e
106 0xfb63, 0xfb64, 0xfb65, // 67f
108 0xfb5b, 0xfb5c, 0xfb5d, // 680
109 0x0681, 0x0681, 0x0681, // placeholder
110 0x0682, 0x0682, 0x0682, // placeholder
111 0xfb77, 0xfb78, 0xfb79, // 683
112 0xfb73, 0xfb74, 0xfb75, // 684
113 0x0685, 0x0685, 0x0685, // placeholder
114 0xfb7b, 0xfb7c, 0xfb7d, // 686
115 0xfb7f, 0xfb80, 0xfb81, // 687
116 0xfb89, 0x0688, 0x0688, // 688
117 0x0689, 0x0689, 0x0689, // placeholder
118 0x068a, 0x068a, 0x068a, // placeholder
119 0x068b, 0x068b, 0x068b, // placeholder
120 0xfb85, 0x068c, 0x068c, // 68c
121 0xfb83, 0x068d, 0x068d, // 68d
122 0xfb87, 0x068e, 0x068e, // 68e
123 0x068f, 0x068f, 0x068f, // placeholder
125 0x0690, 0x0690, 0x0690, // placeholder
126 0xfb8d, 0x0691, 0x0691, // 691
127 0x0692, 0x0692, 0x0692, // placeholder
128 0x0693, 0x0693, 0x0693, // placeholder
129 0x0694, 0x0694, 0x0694, // placeholder
130 0x0695, 0x0695, 0x0695, // placeholder
131 0x0696, 0x0696, 0x0696, // placeholder
132 0x0697, 0x0697, 0x0697, // placeholder
133 0xfb8b, 0x0698, 0x0698, // 698
134 0x0699, 0x0699, 0x0699, // placeholder
135 0x069a, 0x069a, 0x069a, // placeholder
136 0x069b, 0x069b, 0x069b, // placeholder
137 0x069c, 0x069c, 0x069c, // placeholder
138 0x069d, 0x069d, 0x069d, // placeholder
139 0x069e, 0x069e, 0x069e, // placeholder
140 0x069f, 0x069f, 0x069f, // placeholder
142 0x06a0, 0x06a0, 0x06a0, // placeholder
143 0x06a1, 0x06a1, 0x06a1, // placeholder
144 0x06a2, 0x06a2, 0x06a2, // placeholder
145 0x06a3, 0x06a3, 0x06a3, // placeholder
146 0xfb6b, 0xfb6c, 0xfb6d, // 6a4
147 0x06a5, 0x06a5, 0x06a5, // placeholder
148 0xfb6f, 0xfb70, 0xfb71, // 6a6
149 0x06a7, 0x06a7, 0x06a7, // placeholder
150 0x06a8, 0x06a8, 0x06a8, // placeholder
151 0xfb8f, 0xfb90, 0xfb91, // 6a9
152 0x06aa, 0x06aa, 0x06aa, // 6aa
153 0x06ab, 0x06ab, 0x06ab, // 6ab
154 0x06ac, 0x06ac, 0x06ac, // 6ac
155 0xfbd4, 0xfbd5, 0xfbd6, // 6ad
156 0x06ae, 0x06ae, 0x06ae, // 6ae
157 0xfb93, 0xfb94, 0xfb95, // 6af
159 0x06b0, 0x06b0, 0x06b0, // placeholder
160 0xfb9b, 0xfb9c, 0xfb9d, // 6b1
161 0x06b2, 0x06b2, 0x06b2, // 6b2
162 0xfb97, 0xfb98, 0xfb99, // 6b3
163 0x06b4, 0x06b4, 0x06b4, // placeholder
164 0x06b5, 0x06b5, 0x06b5, // placeholder
165 0x06b6, 0x06b6, 0x06b6, // placeholder
166 0x06b7, 0x06b7, 0x06b7, // placeholder
167 0x06b8, 0x06b8, 0x06b8, // placeholder
168 0x06b9, 0x06b9, 0x06b9, // placeholder
169 0xfb9f, 0x06ba, 0x06ba, // 6ba
170 0xfba1, 0xfba2, 0xfba3, // 6bb
171 0x06bc, 0x06bc, 0x06bc, // placeholder
172 0x06bd, 0x06bd, 0x06bd, // placeholder
173 0xfbab, 0xfbac, 0xfbad, // 6be
174 0x06bf, 0x06bf, 0x06bf, // placeholder
176 0xfba5, 0x06c0, 0x06c0, // 6c0
177 0xfba7, 0xfba8, 0xfba9, // 6c1
178 0x06c2, 0x06c2, 0x06c2, // placeholder
179 0x06c3, 0x06c3, 0x06c3, // placeholder
180 0x06c4, 0x06c4, 0x06c4, // placeholder
181 0xfbe1, 0x06c5, 0x06c5, // 6c5
182 0xfbda, 0x06c6, 0x06c6, // 6c6
183 0xfbd8, 0x06c7, 0x06c7, // 6c7
184 0xfbdc, 0x06c8, 0x06c8, // 6c8
185 0xfbe3, 0x06c9, 0x06c9, // 6c9
186 0x06ca, 0x06ca, 0x06ca, // placeholder
187 0xfbdf, 0x06cb, 0x06cb, // 6cb
188 0xfbfd, 0xfbfe, 0xfbff, // 6cc
189 0x06cd, 0x06cd, 0x06cd, // placeholder
190 0x06ce, 0x06ce, 0x06ce, // placeholder
191 0x06cf, 0x06cf, 0x06cf, // placeholder
193 0xfbe5, 0xfbe6, 0xfbe7, // 6d0
194 0x06d1, 0x06d1, 0x06d1, // placeholder
195 0xfbaf, 0x06d2, 0x06d2, // 6d2
196 0xfbb1, 0x06d3, 0x06d3 // 6d3
200 static const int KFinal = 0;
201 static const int KInitial = 1;
202 static const int KMedial = 2;
203 static const int KStandardShapeTableOffset = 0x621;
204 static const int KLamAlefShapeTableStart = 0x64B - 0x621;
205 static const int KExtendedShapeTableOffset = 0x671 - (0x64f - 0x621);
208 static inline TInt ArabicShapeTableIndex(TInt aCode)
210 if (aCode >= 0x621 && aCode <= 0x64a)
211 return (aCode - KStandardShapeTableOffset) * 3;
212 if (aCode >= 0x671 && aCode <= 0x6d3)
213 return (aCode - KExtendedShapeTableOffset) * 3;
218 static inline TBool IsArabicPoint(TInt aCode)
220 return (aCode >= 0x64B && aCode <= 0x655) || aCode == 0x670;
223 TInt ConsumeCharsAndDeterminePresentationForm(const TUtf32Iterator& aText,
224 TUint aFlags, TInt& aLigaturePortionsRemaining)
226 Get the Arabic or other presentation form, consuming as many characters
227 as necessary. This function can cope with text that has already been
232 TUint c = aText.Get();
233 TInt index = ArabicShapeTableIndex(c);
236 if (aLigaturePortionsRemaining != 0)
238 --aLigaturePortionsRemaining;
239 TEXTBASE_ASSERT_DEBUG(!aText.AtEnd(), ETextBasePanic_Invariant);
245 // Find previous and next character, skipping Arabic points.
246 TUtf32Iterator tmpText(aText);
249 while (!tmpText.BeforeStart() && (prev == 0xFFFF || IsArabicPoint(prev)))
250 prev = tmpText.GetThenPrev();
255 while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
256 next = tmpText.GetThenNext();
258 TInt possible_lam = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
260 TInt possible_alef = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
263 // Perform lam-alef ligation if necessary.
264 if (possible_lam == 0x644 &&
265 (possible_alef == 0x622 || possible_alef == 0x623 || possible_alef == 0x625 || possible_alef == 0x627))
267 switch(possible_alef)
272 index = KLamAlefShapeTableStart;
278 index = KLamAlefShapeTableStart + 1;
284 index = KLamAlefShapeTableStart + 2;
290 index = KLamAlefShapeTableStart + 3;
294 aLigaturePortionsRemaining = 2;
297 while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
299 next = tmpText.Get();
304 TInt left = (aFlags & CFont::TPositionParam::EFLogicalOrder)? prev : next;
305 TInt right = (aFlags & CFont::TPositionParam::EFLogicalOrder)? next : prev;
307 TBool prefix = FALSE;
308 if (left == 0x640 || left == 0x200D) // kashida or Zero-Width Joiner
312 TInt left_index = ArabicShapeTableIndex(left);
313 if (left_index != -1)
315 if (static_cast<TInt>(TheArabicShapeTable[left_index + KInitial]) != left ||
316 static_cast<TInt>(TheArabicShapeTable[left_index + KMedial]) != left)
320 TBool suffix = FALSE;
321 if (right == 0x640 || right == 0x200D) // kashida or Zero-Width Joiner
325 TInt right_index = ArabicShapeTableIndex(right);
326 if (right_index != -1)
328 if (static_cast<TInt>(TheArabicShapeTable[right_index + KFinal]) != right ||
329 static_cast<TInt>(TheArabicShapeTable[right_index + KMedial]) != right)
333 TBool medial = static_cast<TUint>(TheArabicShapeTable[index + KMedial]) != c;
336 if (prefix && suffix)
351 c = TheArabicShapeTable[index + position];
360 // GlyphSelector_Arabic Class definition
364 TBool GlyphSelector_Arabic::Process(TGlyphSelectionState& aGss, RShapeInfo&)
366 // Select glyph - use presentation form if applicable.
367 TInt ligaturePortions = aGss.iLigaturePortionsRemaining;
368 TUint flags = aGss.iParam.iFlags;
369 TUint code = ConsumeCharsAndDeterminePresentationForm(aGss.iText,
370 flags, ligaturePortions);
373 aGss.iClusterState = !aGss.iText.AtEnd() &&
374 (aGss.iText.Get().GetCategory() & 0xF0) == TChar::EMarkGroup?
375 TGlyphSelectionState::EGClusterNotComplete
376 : TGlyphSelectionState::EGClusterComplete;
378 // If we are beginning a new portion of a ligature, we must set the state.
379 if (ligaturePortions != aGss.iLigaturePortionsRemaining)
381 if (ligaturePortions != 1)
382 aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
383 aGss.iLigaturePortionFirstMark = aGss.iParam.iOutputGlyphs;
384 if (ligaturePortions < aGss.iLigaturePortionsRemaining)
386 aGss.iLigaturePortionsRemaining = ligaturePortions;
387 // We have just swallowed a non-initial part of the ligature, which
388 // must not be drawn.
389 TEXTBASE_ASSERT_DEBUG(code == 0xFFFF, ETextBasePanic_Invariant);
392 aGss.iLigaturePortionsRemaining = ligaturePortions;
395 // In this method we always output the glyph code calculate previously.
396 if (!aGss.AppendGlyphToCluster(code))
399 if (!aGss.IsCombiningClass())
401 aGss.iPen = TGlyphSelectionState::EPenAdvance_Yes;
405 // Adjust glyph's bounds further to position this character.
406 aGss.iPen = TGlyphSelectionState::EPenAdvance_No;
407 TRect baseBounds = aGss.iParam.iOutput[0].iBounds;
409 if (aGss.iLigaturePortionsRemaining != 0)
411 TInt centre = (baseBounds.iBr.iX + baseBounds.iTl.iX) >> 1;
412 TBool firstHalf = aGss.iLigaturePortionsRemaining == 2;
413 TBool rightHandSide = flags & CFont::TPositionParam::EFLogicalOrder?
414 firstHalf : !firstHalf;
416 baseBounds.iTl.iX = centre;
418 baseBounds.iBr.iX = centre;
420 aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
421 firstMark = aGss.iLigaturePortionFirstMark;
423 aGss.CombineLastGlyphToBase(baseBounds, firstMark);
429 TBool GlyphSelector_Arabic::CharactersJoin(TInt aLeftCharacter, TInt aRightCharacter)
431 // All the characters join to the right, so we don't need to look them up.
432 // as long as they are there at all, it is OK.
433 TBool leftIsFixedJoiner = aLeftCharacter == 0x640 || aLeftCharacter == 0x200D;
434 if (!leftIsFixedJoiner)
436 TInt leftIndex = ArabicShapeTableIndex(aLeftCharacter);
439 if (static_cast<TInt>(TheArabicShapeTable[leftIndex + KFinal])
443 if (aRightCharacter == 0x640 || aRightCharacter == 0x200D)
444 return !leftIsFixedJoiner;
445 TInt rightIndex = ArabicShapeTableIndex(aRightCharacter);
446 return rightIndex != -1
447 && static_cast<TInt>(TheArabicShapeTable[rightIndex + KInitial])