os/graphics/graphicsdeviceinterface/gdi/inc/BIDI.H
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 /*
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * Definitions for bidirectional text reordering.
    16 * 
    17 *
    18 */
    19 
    20 
    21 
    22 #ifndef BIDI_H_
    23 #define BIDI_H_
    24 
    25 #include <e32std.h>
    26 
    27 class RWriteStream;
    28 class RReadStream;
    29 
    30 
    31 class TBidirectionalState
    32 /** 
    33 The bidirectional state class. 
    34 This class contains functions that implement the Unicode Bidirectional Algorithm, 
    35 which changes text from logical order to display order for the correct display 
    36 of right-to-left scripts, like Arabic and Hebrew. A TBidirectionalState object 
    37 carries the embedding level and stack from one line to the next. On construction 
    38 it is in its 'start of paragraph' state. 
    39 @publishedAll
    40 @released
    41 */
    42 	{
    43 	friend class CTBiDi;
    44 	
    45 public:
    46 	
    47 	class TRunInfo
    48 	/** 
    49 	Information about a run of characters with the same bidirectional 
    50 	category. 
    51 
    52 	An array of these representing a line is passed into, and reordered by, 
    53 	ReorderLine(). 
    54 	@publishedAll
    55 	@released
    56 	*/
    57 		{
    58 		public:
    59 		/** Category of a run of text: passed to ReorderLine(); these are 
    60 		passed in as TChar::TBdCategory values but are modified for internal 
    61 		use. */
    62 		TUint iCategory;		
    63 		/** Embedding level of this run: used internally by ReorderLine(). */
    64 		TUint8 iEmbeddingLevel;	
    65 		/** Resolved direction of this run: 0 for left to right, 1 for right 
    66 		to left. */
    67 		TUint8 iDirection;		
    68 		/** Index of this run after reordering. */
    69 		TInt iIndex;			
    70 		/** Start of text; returned by ReorderText(). */
    71 		TInt iStart;			
    72 		/** Length of text; returned by ReorderText(). */
    73 		TInt iLength;			
    74 		};
    75 
    76 	IMPORT_C TBidirectionalState();
    77 	IMPORT_C void ReorderLine(TRunInfo* aRunInfo,TInt aRuns,TBool aParStart,TBool aParEnd,TBool aParRightToLeft,
    78 					 TChar::TBdCategory aNextCategory,TChar::TBdCategory aNextStrongCategory,
    79 					 TBool& aVisualEndIsAmbiguous);
    80 	IMPORT_C void ReorderLine(TRunInfo* aRunInfo,TInt aRuns,TBool aParStart,TBool aParEnd,TBool aParRightToLeft,
    81 					 TChar::TBdCategory aNextCategory,TChar::TBdCategory aNextStrongCategory);
    82 	IMPORT_C static TInt ReorderText(const TText* aText,TInt aLength,TBool aParRightToLeft,TText*& aNewText);
    83 	IMPORT_C static void ReverseGroups(TText* aStart,TInt aLength);
    84 	IMPORT_C void Reset();
    85 	IMPORT_C TBool IsDefault() const;
    86 	IMPORT_C TBool operator==(const TBidirectionalState& aState) const;
    87 	IMPORT_C void ExternalizeL(RWriteStream& aDest);
    88 	IMPORT_C void InternalizeL(RReadStream& aSource);
    89 	TBool ParRightToLeft() const { return iStack[0].iEmbeddingLevel & 1; }
    90 
    91 private:
    92 	// Bidirectional categories coded as bit flags
    93 	enum TCategory
    94 		{
    95 		ELeftToRight = 1 << TChar::ELeftToRight, 
    96 		ELeftToRightEmbedding = 1 << TChar::ELeftToRightEmbedding,
    97 		ELeftToRightOverride = 1 << TChar::ELeftToRightOverride,
    98 		ERightToLeft = 1 << TChar::ERightToLeft,
    99 		ERightToLeftArabic = 1 << TChar::ERightToLeftArabic,
   100 		ERightToLeftEmbedding = 1 << TChar::ERightToLeftEmbedding, 
   101 		ERightToLeftOverride = 1 << TChar::ERightToLeftOverride,
   102 		EPopDirectionalFormat = 1 << TChar::EPopDirectionalFormat, 
   103 		EEuropeanNumber = 1 << TChar::EEuropeanNumber, 
   104 		EEuropeanNumberSeparator = 1 << TChar::EEuropeanNumberSeparator, 
   105 		EEuropeanNumberTerminator = 1 << TChar::EEuropeanNumberTerminator, 
   106 		EArabicNumber = 1 << TChar::EArabicNumber,
   107 		ECommonNumberSeparator = 1 << TChar::ECommonNumberSeparator, 
   108 		ENonSpacingMark = 1 << TChar::ENonSpacingMark, 
   109 		EBoundaryNeutral = 1 << TChar::EBoundaryNeutral, 
   110 		EParagraphSeparator = 1 << TChar::EParagraphSeparator, 
   111 		ESegmentSeparator = 1 << TChar::ESegmentSeparator, 
   112 		EWhitespace = 1 << TChar::EWhitespace, 
   113 		EOtherNeutral = 1 << TChar::EOtherNeutral,
   114 
   115 		// useful groups
   116 		EBdControlsGroup = ELeftToRightEmbedding | ERightToLeftEmbedding |
   117 						   ELeftToRightOverride | ERightToLeftOverride | EPopDirectionalFormat,
   118 		ELeftToRightGroup = ELeftToRight | EEuropeanNumber | ELeftToRightOverride | ELeftToRightEmbedding,
   119 		ERightToLeftGroup = ERightToLeft | EArabicNumber | ERightToLeftArabic | ERightToLeftOverride |
   120 							ERightToLeftEmbedding,
   121 		EStrongGroup = ELeftToRightEmbedding | ERightToLeftEmbedding
   122 			| ELeftToRightOverride | ERightToLeftOverride
   123 			| ELeftToRight | ERightToLeft | ERightToLeftArabic,
   124 
   125 		ENoCategory = 0
   126 		};
   127 
   128 	enum
   129 		{
   130 		EMaxExplicitLevel = 61,
   131 		EMaxLevel = 63,
   132 		EMaxStackLevels = 62
   133 		};
   134 
   135 	enum TOverrideState
   136 		{
   137 		ENoOverrideState = 0,
   138 		ELeftToRightOverrideState = ELeftToRightOverride,
   139 		ERightToLeftOverrideState = ERightToLeftOverride
   140 		};
   141 
   142 	class TStackItem
   143 		{
   144 	public:
   145 		TUint8 iEmbeddingLevel;			// embedding level
   146 		TOverrideState iOverrideState;	// directional override state
   147 		TCategory iStartCategory;		// category that started this level; EOtherNeutral if none
   148 		};
   149 
   150 private:
   151 	/**	The information needed during line reordering.
   152 	WARNING: Class for internal use ONLY.  Compatibility is not guaranteed in future releases.	
   153 	 */
   154 	class TReorderContext
   155 		{
   156 	public:
   157 		void SetNextCategory(TChar::TBdCategory aCat);
   158 		void SetNextStrongCategory(TChar::TBdCategory aCat);
   159 	public:
   160 		/** Category at start of next line, or ON if at the end of the
   161 		paragraph. */
   162 		TCategory iNextCategory;
   163 		/** Which of L, R or AL appears first in the remainder of the
   164 		paragraph, or ON if none. */
   165 		TCategory iNextStrongCategory;
   166 		/** The run array for this line. */
   167 		TRunInfo* iRunInfo;
   168 		/** The length of iRunInfo. */
   169 		TInt iRuns;
   170 		/** Bitmap of categories currently present in iRunInfo. */
   171 		TUint32 iCategories;
   172 		/** Found by the algorithm to set iPreviousStrongCategory. */
   173 		TCategory iLastStrongCategory;
   174 		};
   175 
   176 public:
   177 	static TInt GenerateBdRunArray(const TText* aText, TInt aLength,
   178 		TBidirectionalState::TRunInfo* aRun, TInt aMaxRuns);
   179 
   180 private:
   181 	const TStackItem& State() const { return iStack[iStackLevel]; }
   182 	TCategory Push(TCategory aStartCategory);
   183 	TCategory Pop();
   184 
   185 public:
   186 	TBidirectionalState(TChar::TBdCategory aPrevCat,
   187 		TChar::TBdCategory aPrevStrongCat, TBool aParRightToLeft);
   188 	void HandleBdControls(TReorderContext& aContext); 
   189 	void ResolveWeakTypesW1W2W3(TReorderContext& aContext);
   190 	void ResolveWeakTypesW4W5W6(TReorderContext& aContext); 
   191 	void ResolveWeakTypesW7(TReorderContext& aContext); 
   192 	void ResolveNeutralTypes(TReorderContext& aContext); 
   193 	void ResolveImplicitLevels(TReorderContext& aContext); 
   194 	void PrepareForNextLine(const TReorderContext& aContext);
   195 	void ReorderRuns(TReorderContext& aContext);
   196 	static TInt CatToNumber(TInt aCat);
   197 	static TCategory CharToBdCat(TChar::TBdCategory aCat);
   198 	static TCategory UintToBdCat(TUint aCat);
   199 	static void DeneutralizeRuns(TRunInfo* aStart, TRunInfo* aEnd,
   200 		TCategory aStartCategory, TCategory aEndCategory);
   201 private:
   202 	TCategory iPreviousCategory;			// category at end of last line, or EStartOfParagraph if at start of par
   203 	TCategory iPreviousStrongCategory;		// L or R; derived from embedding level if at start of par
   204 	TInt16 iStackLevel;						// current stack level
   205 	TInt8 iPushesBeyond60;	// number of times Push called with iStackLevel == 60 and Left-To-Right category
   206 	TInt8 iPushesBeyond61;	// number of times Push called with iStackLevel == 61
   207 	TStackItem iStack[EMaxStackLevels];		// the stack of embedding levels
   208 	};
   209 
   210 #endif // BIDI_H_