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