os/graphics/graphicsdeviceinterface/gdi/sgdi/FontArabic.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 /**
    17  @file
    18  @internalComponent
    19 */
    20 
    21 
    22 #include <gdi.h>
    23 #include "FontArabic.h"
    24 #include "GDIPANIC.h"
    25 
    26 
    27 //
    28 // Arabic Presentation Forms.
    29 //
    30 
    31 
    32 /**
    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
    37 */
    38 LOCAL_D const TUint16 TheArabicShapeTable[] =
    39 	{
    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
    83 
    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
    89 
    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
   106 
   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
   123 
   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
   140 
   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
   157 
   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
   174 
   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
   191 
   192 	0xfbe5, 0xfbe6, 0xfbe7,			// 6d0
   193 	0x06d1, 0x06d1, 0x06d1,			// placeholder
   194 	0xfbaf, 0x06d2, 0x06d2,			// 6d2
   195 	0xfbb1, 0x06d3, 0x06d3			// 6d3
   196 	};
   197 
   198 
   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);
   205 
   206 
   207 static inline TInt ArabicShapeTableIndex(TInt aCode)
   208 	{
   209 	if (aCode >= 0x621 && aCode <= 0x64a)
   210 		return (aCode - KStandardShapeTableOffset) * 3;
   211 	if (aCode >= 0x671 && aCode <= 0x6d3)
   212 		return (aCode - KExtendedShapeTableOffset) * 3;
   213 	return KErrNotFound;
   214 	}
   215 
   216 
   217 static inline TBool IsArabicPoint(TInt aCode)
   218 	{
   219 	return (aCode >= 0x64B && aCode <= 0x655) || aCode == 0x670;
   220 	}
   221 
   222 TInt ConsumeCharsAndDeterminePresentationForm(const TUtf32Iterator& aText,
   223 	TUint aFlags, TInt& aLigaturePortionsRemaining)
   224 /**
   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 
   227  reversed.
   228 @internalComponent
   229 */
   230 	{
   231 	TUint c = aText.Get();
   232 	TInt index = ArabicShapeTableIndex(c);
   233 	if (index != -1)
   234 		{
   235 		if (aLigaturePortionsRemaining != 0)
   236 			{
   237 			--aLigaturePortionsRemaining;
   238 			GDI_ASSERT_DEBUG(!aText.AtEnd(), EGdiPanic_Invariant);
   239 			return 0xFFFF;
   240 			}
   241 
   242 		int position = -1;
   243 
   244 		// Find previous and next character, skipping Arabic points.
   245 		TUtf32Iterator tmpText(aText);		
   246 		tmpText.Prev();
   247         TUint prev = 0xFFFF;
   248         while (!tmpText.BeforeStart() && (prev == 0xFFFF || IsArabicPoint(prev)))
   249             prev = tmpText.GetThenPrev();		
   250 
   251 		tmpText = aText;
   252 		tmpText.Next();
   253         TUint next = 0xFFFF;
   254         while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
   255             next = tmpText.GetThenNext();		
   256 		
   257 		TInt possible_lam = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
   258 			c : next;
   259 		TInt possible_alef = (aFlags & CFont::TPositionParam::EFLogicalOrder)?
   260 			next : c;
   261 
   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))
   265 			{
   266 			switch(possible_alef)
   267 				{
   268 				case 0x622:
   269 					{
   270 					c = 0xfef5;
   271 					index = KLamAlefShapeTableStart;
   272 					break;
   273 					}
   274 				case 0x623:
   275 					{
   276 					c = 0xfef7;
   277 					index = KLamAlefShapeTableStart + 1;
   278 					break;
   279 					}
   280 				case 0x625:
   281 					{
   282 					c = 0xfef9;
   283 					index = KLamAlefShapeTableStart + 2;
   284 					break;
   285 					}
   286 				default:
   287 					{
   288 					c = 0xfefb;
   289 					index = KLamAlefShapeTableStart + 3;
   290 					}
   291 				}
   292 			index *= 3;
   293 			aLigaturePortionsRemaining = 2;
   294 
   295 			next = 0xFFFF;
   296 			while (!tmpText.AtEnd() && (next == 0xFFFF || IsArabicPoint(next)))
   297 				{
   298 				next = tmpText.Get();
   299 				tmpText.Next();
   300 				}
   301 			}
   302 
   303 		TInt left = (aFlags & CFont::TPositionParam::EFLogicalOrder)? prev : next;
   304 		TInt right = (aFlags & CFont::TPositionParam::EFLogicalOrder)? next : prev;
   305 
   306 		TBool prefix = FALSE;
   307 		if (left == 0x640 || left == 0x200D) // kashida or Zero-Width Joiner
   308 			prefix = TRUE;
   309 		else
   310 			{
   311 			TInt left_index = ArabicShapeTableIndex(left);
   312 			if (left_index != -1)
   313 				{
   314 				if (static_cast<TInt>(TheArabicShapeTable[left_index + KInitial]) != left ||
   315 					static_cast<TInt>(TheArabicShapeTable[left_index + KMedial]) != left)
   316 					prefix = TRUE;
   317 				}
   318 			}
   319 		TBool suffix = FALSE;
   320 		if (right == 0x640 || right == 0x200D) // kashida or Zero-Width Joiner
   321 			suffix = TRUE;
   322 		else 
   323 			{
   324 			TInt right_index = ArabicShapeTableIndex(right);
   325 			if (right_index != -1)
   326 				{
   327 				if (static_cast<TInt>(TheArabicShapeTable[right_index + KFinal]) != right ||
   328 					static_cast<TInt>(TheArabicShapeTable[right_index + KMedial]) != right)
   329 					suffix = TRUE;
   330 				}
   331 			}
   332 		TBool medial = static_cast<TUint>(TheArabicShapeTable[index + KMedial]) != c;
   333 		if (medial)
   334 			{
   335 			if (prefix && suffix)
   336 				position = KMedial;
   337 			else if (prefix)
   338 				position = KFinal;
   339 			else if (suffix)
   340 				position = KInitial;
   341 			}
   342 		else
   343 			{
   344 			if (prefix)
   345 				position = KFinal;
   346 			else if (suffix)
   347 				position = KInitial;
   348 			}
   349 		if (position >= 0)
   350 			c = TheArabicShapeTable[index + position];
   351 		}
   352 
   353 	return c;
   354 	}
   355 
   356 
   357 // 
   358 //
   359 // GlyphSelector_Arabic Class definition
   360 //
   361 //
   362 
   363 TBool GlyphSelector_Arabic::Process(TGlyphSelectionState& aGss, RShapeInfo&) 
   364 	{
   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);
   370 
   371 	aGss.iText.Next();
   372 	aGss.iClusterState = !aGss.iText.AtEnd() &&
   373 		(aGss.iText.Get().GetCategory() & 0xF0) == TChar::EMarkGroup?
   374 		TGlyphSelectionState::EGClusterNotComplete
   375 		: TGlyphSelectionState::EGClusterComplete;
   376 
   377 	// If we are beginning a new portion of a ligature, we must set the state.
   378 	if (ligaturePortions != aGss.iLigaturePortionsRemaining)
   379 		{
   380 		if (ligaturePortions != 1)
   381 			aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
   382 		aGss.iLigaturePortionFirstMark = aGss.iParam.iOutputGlyphs;
   383 		if (ligaturePortions < aGss.iLigaturePortionsRemaining)
   384 			{
   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);
   389 			return ETrue;
   390 			}
   391 		aGss.iLigaturePortionsRemaining = ligaturePortions;
   392 		}
   393 
   394 	// In this method we always output the glyph code calculate previously.
   395 	if (!aGss.AppendGlyphToCluster(code))
   396 		return EFalse;
   397 
   398 	if (!aGss.IsCombiningClass())
   399 		{
   400 		aGss.iPen = TGlyphSelectionState::EPenAdvance_Yes;
   401 		return ETrue;
   402 		}
   403 
   404 	// Adjust glyph's bounds further to position this character.
   405 	aGss.iPen = TGlyphSelectionState::EPenAdvance_No;
   406 	TRect baseBounds = aGss.iParam.iOutput[0].iBounds;
   407 	TInt firstMark = 1;
   408 	if (aGss.iLigaturePortionsRemaining != 0)
   409 		{
   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;
   414 		if (rightHandSide)
   415 			baseBounds.iTl.iX = centre;
   416 		else
   417 			baseBounds.iBr.iX = centre;
   418 		if (firstHalf)
   419 			aGss.iClusterState = TGlyphSelectionState::EGClusterNotComplete;
   420 		firstMark = aGss.iLigaturePortionFirstMark;
   421 		}
   422 	aGss.CombineLastGlyphToBase(baseBounds, firstMark);
   423 
   424 	return ETrue;
   425 	}
   426 
   427 
   428 TBool GlyphSelector_Arabic::CharactersJoin(TInt aLeftCharacter, TInt aRightCharacter)
   429 	{
   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)
   434 		{
   435 		TInt leftIndex = ArabicShapeTableIndex(aLeftCharacter);
   436 		if (leftIndex == -1)
   437 			return EFalse;
   438 		if (static_cast<TInt>(TheArabicShapeTable[leftIndex + KFinal])
   439 			== aLeftCharacter)
   440 			return EFalse;
   441 		}
   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])
   447 			!= aRightCharacter;
   448 	}
   449