1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicsdeviceinterface/gdi/sgdi/FontArabic.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,449 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +/**
1.20 + @file
1.21 + @internalComponent
1.22 +*/
1.23 +
1.24 +
1.25 +#include <gdi.h>
1.26 +#include "FontArabic.h"
1.27 +#include "GDIPANIC.h"
1.28 +
1.29 +
1.30 +//
1.31 +// Arabic Presentation Forms.
1.32 +//
1.33 +
1.34 +
1.35 +/**
1.36 + The contextual forms of each character in the range 0621 - 064A, in
1.37 + the order final, initial, medial; followed by the forms of
1.38 + the lam-alef ligatures (lam with 0622, 0623, 0625 and 0627);
1.39 + followed by the forms of the characters in the range 0671 - 06D3
1.40 +*/
1.41 +LOCAL_D const TUint16 TheArabicShapeTable[] =
1.42 + {
1.43 + // Contextual forms for standard Arabic letters
1.44 + 0x621, 0x621, 0x621, // 621
1.45 + 0xfe82, 0x622, 0x622, // 622
1.46 + 0xfe84, 0x623, 0x623, // 623
1.47 + 0xfe86, 0x624, 0x624, // 624
1.48 + 0xfe88, 0x625, 0x625, // 625
1.49 + 0xfe8a, 0xfe8b, 0xfe8c, // 626
1.50 + 0xfe8e, 0x627, 0x627, // 627
1.51 + 0xfe90, 0xfe91, 0xfe92, // 628
1.52 + 0xfe94, 0x629, 0x629, // 629
1.53 + 0xfe96, 0xfe97, 0xfe98, // 62a
1.54 + 0xfe9a, 0xfe9b, 0xfe9c, // 62b
1.55 + 0xfe9e, 0xfe9f, 0xfea0, // 62c
1.56 + 0xfea2, 0xfea3, 0xfea4, // 62d
1.57 + 0xfea6, 0xfea7, 0xfea8, // 62e
1.58 + 0xfeaa, 0x62f, 0x62f, // 62f
1.59 + 0xfeac, 0x630, 0x630, // 630
1.60 + 0xfeae, 0x631, 0x631, // 631
1.61 + 0xfeb0, 0x632, 0x632, // 632
1.62 + 0xfeb2, 0xfeb3, 0xfeb4, // 633
1.63 + 0xfeb6, 0xfeb7, 0xfeb8, // 634
1.64 + 0xfeba, 0xfebb, 0xfebc, // 635
1.65 + 0xfebe, 0xfebf, 0xfec0, // 636
1.66 + 0xfec2, 0xfec3, 0xfec4, // 637
1.67 + 0xfec6, 0xfec7, 0xfec8, // 638
1.68 + 0xfeca, 0xfecb, 0xfecc, // 639
1.69 + 0xfece, 0xfecf, 0xfed0, // 63a
1.70 + 0x63b, 0x63b, 0x63b, // placeholder
1.71 + 0x63c, 0x63c, 0x63c, // placeholder
1.72 + 0x63d, 0x63d, 0x63d, // placeholder
1.73 + 0x63e, 0x63e, 0x63e, // placeholder
1.74 + 0x63f, 0x63f, 0x63f, // placeholder
1.75 + 0x640, 0x640, 0x640, // 640
1.76 + 0xfed2, 0xfed3, 0xfed4, // 641
1.77 + 0xfed6, 0xfed7, 0xfed8, // 642
1.78 + 0xfeda, 0xfedb, 0xfedc, // 643
1.79 + 0xfede, 0xfedf, 0xfee0, // 644
1.80 + 0xfee2, 0xfee3, 0xfee4, // 645
1.81 + 0xfee6, 0xfee7, 0xfee8, // 646
1.82 + 0xfeea, 0xfeeb, 0xfeec, // 647
1.83 + 0xfeee, 0x648, 0x648, // 648
1.84 + 0xfef0, 0xfbe8, 0xfbe9, // 649
1.85 + 0xfef2, 0xfef3, 0xfef4, // 64A
1.86 +
1.87 + // Contextual forms for lam-alef ligatures
1.88 + 0xfef6, 0xfef5, 0xfef5, // 644 + 622
1.89 + 0xfef8, 0xfef7, 0xfef7, // 644 + 623
1.90 + 0xfefa, 0xfef9, 0xfef9, // 644 + 625
1.91 + 0xfefc, 0xfefb, 0xfefb, // 644 + 627
1.92 +
1.93 + // Contextual forms for extended Arabic letters for Persian, Urdu, Sindhi, etc.
1.94 + 0xfb51, 0x0671, 0x0671, // 671
1.95 + 0x0672, 0x0672, 0x0672, // placeholder
1.96 + 0x0673, 0x0673, 0x0673, // placeholder
1.97 + 0x0674, 0x0674, 0x0674, // placeholder
1.98 + 0x0675, 0x0675, 0x0675, // placeholder
1.99 + 0x0676, 0x0676, 0x0676, // placeholder
1.100 + 0x0677, 0x0677, 0x0677, // placeholder
1.101 + 0x0678, 0x0678, 0x0678, // placeholder
1.102 + 0xfb67, 0xfb68, 0xfb69, // 679
1.103 + 0xfb5f, 0xfb60, 0xfb61, // 67a
1.104 + 0xfb53, 0xfb54, 0xfb55, // 67b
1.105 + 0x067c, 0x067c, 0x067c, // placeholder
1.106 + 0x067d, 0x067d, 0x067d, // placeholder
1.107 + 0xfb57, 0xfb58, 0xfb59, // 67e
1.108 + 0xfb63, 0xfb64, 0xfb65, // 67f
1.109 +
1.110 + 0xfb5b, 0xfb5c, 0xfb5d, // 680
1.111 + 0x0681, 0x0681, 0x0681, // placeholder
1.112 + 0x0682, 0x0682, 0x0682, // placeholder
1.113 + 0xfb77, 0xfb78, 0xfb79, // 683
1.114 + 0xfb73, 0xfb74, 0xfb75, // 684
1.115 + 0x0685, 0x0685, 0x0685, // placeholder
1.116 + 0xfb7b, 0xfb7c, 0xfb7d, // 686
1.117 + 0xfb7f, 0xfb80, 0xfb81, // 687
1.118 + 0xfb89, 0x0688, 0x0688, // 688
1.119 + 0x0689, 0x0689, 0x0689, // placeholder
1.120 + 0x068a, 0x068a, 0x068a, // placeholder
1.121 + 0x068b, 0x068b, 0x068b, // placeholder
1.122 + 0xfb85, 0x068c, 0x068c, // 68c
1.123 + 0xfb83, 0x068d, 0x068d, // 68d
1.124 + 0xfb87, 0x068e, 0x068e, // 68e
1.125 + 0x068f, 0x068f, 0x068f, // placeholder
1.126 +
1.127 + 0x0690, 0x0690, 0x0690, // placeholder
1.128 + 0xfb8d, 0x0691, 0x0691, // 691
1.129 + 0x0692, 0x0692, 0x0692, // placeholder
1.130 + 0x0693, 0x0693, 0x0693, // placeholder
1.131 + 0x0694, 0x0694, 0x0694, // placeholder
1.132 + 0x0695, 0x0695, 0x0695, // placeholder
1.133 + 0x0696, 0x0696, 0x0696, // placeholder
1.134 + 0x0697, 0x0697, 0x0697, // placeholder
1.135 + 0xfb8b, 0x0698, 0x0698, // 698
1.136 + 0x0699, 0x0699, 0x0699, // placeholder
1.137 + 0x069a, 0x069a, 0x069a, // placeholder
1.138 + 0x069b, 0x069b, 0x069b, // placeholder
1.139 + 0x069c, 0x069c, 0x069c, // placeholder
1.140 + 0x069d, 0x069d, 0x069d, // placeholder
1.141 + 0x069e, 0x069e, 0x069e, // placeholder
1.142 + 0x069f, 0x069f, 0x069f, // placeholder
1.143 +
1.144 + 0x06a0, 0x06a0, 0x06a0, // placeholder
1.145 + 0x06a1, 0x06a1, 0x06a1, // placeholder
1.146 + 0x06a2, 0x06a2, 0x06a2, // placeholder
1.147 + 0x06a3, 0x06a3, 0x06a3, // placeholder
1.148 + 0xfb6b, 0xfb6c, 0xfb6d, // 6a4
1.149 + 0x06a5, 0x06a5, 0x06a5, // placeholder
1.150 + 0xfb6f, 0xfb70, 0xfb71, // 6a6
1.151 + 0x06a7, 0x06a7, 0x06a7, // placeholder
1.152 + 0x06a8, 0x06a8, 0x06a8, // placeholder
1.153 + 0xfb8f, 0xfb90, 0xfb91, // 6a9
1.154 + 0x06aa, 0x06aa, 0x06aa, // 6aa
1.155 + 0x06ab, 0x06ab, 0x06ab, // 6ab
1.156 + 0x06ac, 0x06ac, 0x06ac, // 6ac
1.157 + 0xfbd4, 0xfbd5, 0xfbd6, // 6ad
1.158 + 0x06ae, 0x06ae, 0x06ae, // 6ae
1.159 + 0xfb93, 0xfb94, 0xfb95, // 6af
1.160 +
1.161 + 0x06b0, 0x06b0, 0x06b0, // placeholder
1.162 + 0xfb9b, 0xfb9c, 0xfb9d, // 6b1
1.163 + 0x06b2, 0x06b2, 0x06b2, // 6b2
1.164 + 0xfb97, 0xfb98, 0xfb99, // 6b3
1.165 + 0x06b4, 0x06b4, 0x06b4, // placeholder
1.166 + 0x06b5, 0x06b5, 0x06b5, // placeholder
1.167 + 0x06b6, 0x06b6, 0x06b6, // placeholder
1.168 + 0x06b7, 0x06b7, 0x06b7, // placeholder
1.169 + 0x06b8, 0x06b8, 0x06b8, // placeholder
1.170 + 0x06b9, 0x06b9, 0x06b9, // placeholder
1.171 + 0xfb9f, 0x06ba, 0x06ba, // 6ba
1.172 + 0xfba1, 0xfba2, 0xfba3, // 6bb
1.173 + 0x06bc, 0x06bc, 0x06bc, // placeholder
1.174 + 0x06bd, 0x06bd, 0x06bd, // placeholder
1.175 + 0xfbab, 0xfbac, 0xfbad, // 6be
1.176 + 0x06bf, 0x06bf, 0x06bf, // placeholder
1.177 +
1.178 + 0xfba5, 0x06c0, 0x06c0, // 6c0
1.179 + 0xfba7, 0xfba8, 0xfba9, // 6c1
1.180 + 0x06c2, 0x06c2, 0x06c2, // placeholder
1.181 + 0x06c3, 0x06c3, 0x06c3, // placeholder
1.182 + 0x06c4, 0x06c4, 0x06c4, // placeholder
1.183 + 0xfbe1, 0x06c5, 0x06c5, // 6c5
1.184 + 0xfbda, 0x06c6, 0x06c6, // 6c6
1.185 + 0xfbd8, 0x06c7, 0x06c7, // 6c7
1.186 + 0xfbdc, 0x06c8, 0x06c8, // 6c8
1.187 + 0xfbe3, 0x06c9, 0x06c9, // 6c9
1.188 + 0x06ca, 0x06ca, 0x06ca, // placeholder
1.189 + 0xfbdf, 0x06cb, 0x06cb, // 6cb
1.190 + 0xfbfd, 0xfbfe, 0xfbff, // 6cc
1.191 + 0x06cd, 0x06cd, 0x06cd, // placeholder
1.192 + 0x06ce, 0x06ce, 0x06ce, // placeholder
1.193 + 0x06cf, 0x06cf, 0x06cf, // placeholder
1.194 +
1.195 + 0xfbe5, 0xfbe6, 0xfbe7, // 6d0
1.196 + 0x06d1, 0x06d1, 0x06d1, // placeholder
1.197 + 0xfbaf, 0x06d2, 0x06d2, // 6d2
1.198 + 0xfbb1, 0x06d3, 0x06d3 // 6d3
1.199 + };
1.200 +
1.201 +
1.202 +static const int KFinal = 0;
1.203 +static const int KInitial = 1;
1.204 +static const int KMedial = 2;
1.205 +static const int KStandardShapeTableOffset = 0x621;
1.206 +static const int KLamAlefShapeTableStart = 0x64B - 0x621;
1.207 +static const int KExtendedShapeTableOffset = 0x671 - (0x64f - 0x621);
1.208 +
1.209 +
1.210 +static inline TInt ArabicShapeTableIndex(TInt aCode)
1.211 + {
1.212 + if (aCode >= 0x621 && aCode <= 0x64a)
1.213 + return (aCode - KStandardShapeTableOffset) * 3;
1.214 + if (aCode >= 0x671 && aCode <= 0x6d3)
1.215 + return (aCode - KExtendedShapeTableOffset) * 3;
1.216 + return KErrNotFound;
1.217 + }
1.218 +
1.219 +
1.220 +static inline TBool IsArabicPoint(TInt aCode)
1.221 + {
1.222 + return (aCode >= 0x64B && aCode <= 0x655) || aCode == 0x670;
1.223 + }
1.224 +
1.225 +TInt ConsumeCharsAndDeterminePresentationForm(const TUtf32Iterator& aText,
1.226 + TUint aFlags, TInt& aLigaturePortionsRemaining)
1.227 +/**
1.228 + Get the Arabic or other presentation form, consuming as many characters
1.229 + as necessary. This function can cope with text that has already been
1.230 + reversed.
1.231 +@internalComponent
1.232 +*/
1.233 + {
1.234 + TUint c = aText.Get();
1.235 + TInt index = ArabicShapeTableIndex(c);
1.236 + if (index != -1)
1.237 + {
1.238 + if (aLigaturePortionsRemaining != 0)
1.239 + {
1.240 + --aLigaturePortionsRemaining;
1.241 + GDI_ASSERT_DEBUG(!aText.AtEnd(), EGdiPanic_Invariant);
1.242 + return 0xFFFF;
1.243 + }
1.244 +
1.245 + int position = -1;
1.246 +
1.247 + // Find previous and next character, skipping Arabic points.
1.248 + TUtf32Iterator tmpText(aText);
1.249 + tmpText.Prev();
1.250 + TUint prev = 0xFFFF;
1.251 + while (!tmpText.BeforeStart() && (prev == 0xFFFF || IsArabicPoint(prev)))
1.252 + prev = tmpText.GetThenPrev();
1.253 +
1.254 + tmpText = aText;
1.255 + tmpText.Next();
1.256 + TUint next = 0xFFFF;
1.257 + while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
1.258 + next = tmpText.GetThenNext();
1.259 +
1.260 + TInt possible_lam = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
1.261 + c : next;
1.262 + TInt possible_alef = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
1.263 + next : c;
1.264 +
1.265 + // Perform lam-alef ligation if necessary.
1.266 + if (possible_lam == 0x644 &&
1.267 + (possible_alef == 0x622 || possible_alef == 0x623 || possible_alef == 0x625 || possible_alef == 0x627))
1.268 + {
1.269 + switch(possible_alef)
1.270 + {
1.271 + case 0x622:
1.272 + {
1.273 + c = 0xfef5;
1.274 + index = KLamAlefShapeTableStart;
1.275 + break;
1.276 + }
1.277 + case 0x623:
1.278 + {
1.279 + c = 0xfef7;
1.280 + index = KLamAlefShapeTableStart + 1;
1.281 + break;
1.282 + }
1.283 + case 0x625:
1.284 + {
1.285 + c = 0xfef9;
1.286 + index = KLamAlefShapeTableStart + 2;
1.287 + break;
1.288 + }
1.289 + default:
1.290 + {
1.291 + c = 0xfefb;
1.292 + index = KLamAlefShapeTableStart + 3;
1.293 + }
1.294 + }
1.295 + index *= 3;
1.296 + aLigaturePortionsRemaining = 2;
1.297 +
1.298 + next = 0xFFFF;
1.299 + while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
1.300 + {
1.301 + next = tmpText.Get();
1.302 + tmpText.Next();
1.303 + }
1.304 + }
1.305 +
1.306 + TInt left = (aFlags & CFont::TPositionParam::EFLogicalOrder)? prev : next;
1.307 + TInt right = (aFlags & CFont::TPositionParam::EFLogicalOrder)? next : prev;
1.308 +
1.309 + TBool prefix = FALSE;
1.310 + if (left == 0x640 || left == 0x200D) // kashida or Zero-Width Joiner
1.311 + prefix = TRUE;
1.312 + else
1.313 + {
1.314 + TInt left_index = ArabicShapeTableIndex(left);
1.315 + if (left_index != -1)
1.316 + {
1.317 + if (static_cast<TInt>(TheArabicShapeTable[left_index + KInitial]) != left ||
1.318 + static_cast<TInt>(TheArabicShapeTable[left_index + KMedial]) != left)
1.319 + prefix = TRUE;
1.320 + }
1.321 + }
1.322 + TBool suffix = FALSE;
1.323 + if (right == 0x640 || right == 0x200D) // kashida or Zero-Width Joiner
1.324 + suffix = TRUE;
1.325 + else
1.326 + {
1.327 + TInt right_index = ArabicShapeTableIndex(right);
1.328 + if (right_index != -1)
1.329 + {
1.330 + if (static_cast<TInt>(TheArabicShapeTable[right_index + KFinal]) != right ||
1.331 + static_cast<TInt>(TheArabicShapeTable[right_index + KMedial]) != right)
1.332 + suffix = TRUE;
1.333 + }
1.334 + }
1.335 + TBool medial = static_cast<TUint>(TheArabicShapeTable[index + KMedial]) != c;
1.336 + if (medial)
1.337 + {
1.338 + if (prefix && suffix)
1.339 + position = KMedial;
1.340 + else if (prefix)
1.341 + position = KFinal;
1.342 + else if (suffix)
1.343 + position = KInitial;
1.344 + }
1.345 + else
1.346 + {
1.347 + if (prefix)
1.348 + position = KFinal;
1.349 + else if (suffix)
1.350 + position = KInitial;
1.351 + }
1.352 + if (position >= 0)
1.353 + c = TheArabicShapeTable[index + position];
1.354 + }
1.355 +
1.356 + return c;
1.357 + }
1.358 +
1.359 +
1.360 +//
1.361 +//
1.362 +// GlyphSelector_Arabic Class definition
1.363 +//
1.364 +//
1.365 +
1.366 +TBool GlyphSelector_Arabic::Process(TGlyphSelectionState& aGss, RShapeInfo&)
1.367 + {
1.368 + // Select glyph - use presentation form if applicable.
1.369 + TInt ligaturePortions = aGss.iLigaturePortionsRemaining;
1.370 + TUint flags = aGss.iParam.iFlags;
1.371 + TUint code = ConsumeCharsAndDeterminePresentationForm(aGss.iText,
1.372 + flags, ligaturePortions);
1.373 +
1.374 + aGss.iText.Next();
1.375 + aGss.iClusterState = !aGss.iText.AtEnd() &&
1.376 + (aGss.iText.Get().GetCategory() & 0xF0) == TChar::EMarkGroup?
1.377 + TGlyphSelectionState::EGClusterNotComplete
1.378 + : TGlyphSelectionState::EGClusterComplete;
1.379 +
1.380 + // If we are beginning a new portion of a ligature, we must set the state.
1.381 + if (ligaturePortions != aGss.iLigaturePortionsRemaining)
1.382 + {
1.383 + if (ligaturePortions != 1)
1.384 + aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
1.385 + aGss.iLigaturePortionFirstMark = aGss.iParam.iOutputGlyphs;
1.386 + if (ligaturePortions < aGss.iLigaturePortionsRemaining)
1.387 + {
1.388 + aGss.iLigaturePortionsRemaining = ligaturePortions;
1.389 + // We have just swallowed a non-initial part of the ligature, which
1.390 + // must not be drawn.
1.391 + GDI_ASSERT_DEBUG(code == 0xFFFF, EGdiPanic_Invariant);
1.392 + return ETrue;
1.393 + }
1.394 + aGss.iLigaturePortionsRemaining = ligaturePortions;
1.395 + }
1.396 +
1.397 + // In this method we always output the glyph code calculate previously.
1.398 + if (!aGss.AppendGlyphToCluster(code))
1.399 + return EFalse;
1.400 +
1.401 + if (!aGss.IsCombiningClass())
1.402 + {
1.403 + aGss.iPen = TGlyphSelectionState::EPenAdvance_Yes;
1.404 + return ETrue;
1.405 + }
1.406 +
1.407 + // Adjust glyph's bounds further to position this character.
1.408 + aGss.iPen = TGlyphSelectionState::EPenAdvance_No;
1.409 + TRect baseBounds = aGss.iParam.iOutput[0].iBounds;
1.410 + TInt firstMark = 1;
1.411 + if (aGss.iLigaturePortionsRemaining != 0)
1.412 + {
1.413 + TInt centre = (baseBounds.iBr.iX + baseBounds.iTl.iX) >> 1;
1.414 + TBool firstHalf = aGss.iLigaturePortionsRemaining == 2;
1.415 + TBool rightHandSide = flags & CFont::TPositionParam::EFLogicalOrder?
1.416 + firstHalf : !firstHalf;
1.417 + if (rightHandSide)
1.418 + baseBounds.iTl.iX = centre;
1.419 + else
1.420 + baseBounds.iBr.iX = centre;
1.421 + if (firstHalf)
1.422 + aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
1.423 + firstMark = aGss.iLigaturePortionFirstMark;
1.424 + }
1.425 + aGss.CombineLastGlyphToBase(baseBounds, firstMark);
1.426 +
1.427 + return ETrue;
1.428 + }
1.429 +
1.430 +
1.431 +TBool GlyphSelector_Arabic::CharactersJoin(TInt aLeftCharacter, TInt aRightCharacter)
1.432 + {
1.433 + // All the characters join to the right, so we don't need to look them up.
1.434 + // as long as they are there at all, it is OK.
1.435 + TBool leftIsFixedJoiner = aLeftCharacter == 0x640 || aLeftCharacter == 0x200D;
1.436 + if (!leftIsFixedJoiner)
1.437 + {
1.438 + TInt leftIndex = ArabicShapeTableIndex(aLeftCharacter);
1.439 + if (leftIndex == -1)
1.440 + return EFalse;
1.441 + if (static_cast<TInt>(TheArabicShapeTable[leftIndex + KFinal])
1.442 + == aLeftCharacter)
1.443 + return EFalse;
1.444 + }
1.445 + if (aRightCharacter == 0x640 || aRightCharacter == 0x200D)
1.446 + return !leftIsFixedJoiner;
1.447 + TInt rightIndex = ArabicShapeTableIndex(aRightCharacter);
1.448 + return rightIndex != -1
1.449 + && static_cast<TInt>(TheArabicShapeTable[rightIndex + KInitial])
1.450 + != aRightCharacter;
1.451 + }
1.452 +