os/graphics/graphicsdeviceinterface/gdi/sgdi/LinebreakComplex.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.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
sl@0
    17
#include <e32std.h>
sl@0
    18
#include <e32svr.h>
sl@0
    19
#include "LineBreak.h"
sl@0
    20
#include "LineBreakImp.h"
sl@0
    21
#include "GlyphSel.h"
sl@0
    22
sl@0
    23
const TText16 KThaiCodePageStart = 0x0E00;
sl@0
    24
const TText16 KThaiCodePageEnd   = 0x0E5C;
sl@0
    25
const TUint KNumThaiCharacters = KThaiCodePageEnd - KThaiCodePageStart;
sl@0
    26
sl@0
    27
/**
sl@0
    28
 Ecanpsulates rules for when Thai character sequence line breaking.
sl@0
    29
@internalComponent
sl@0
    30
*/
sl@0
    31
class ThaiLinebreakRules
sl@0
    32
	{
sl@0
    33
public:
sl@0
    34
	enum TCharClassification
sl@0
    35
		{
sl@0
    36
		EOutOfRange,
sl@0
    37
sl@0
    38
		EConsOAng,
sl@0
    39
		EConsYoYak,
sl@0
    40
		EConsHoHip,
sl@0
    41
		EConsWoWean,
sl@0
    42
		EConsDigraph,
sl@0
    43
		EConsOther,
sl@0
    44
sl@0
    45
		EPostVowelA,
sl@0
    46
		EPostVowelAA,
sl@0
    47
		EPostVowelOther,
sl@0
    48
		EPreVowel,
sl@0
    49
sl@0
    50
		EDepMaiHanAkat,
sl@0
    51
		EDepSaraI,
sl@0
    52
		EDepOther,
sl@0
    53
sl@0
    54
		// marker for end
sl@0
    55
		EMaxClassification
sl@0
    56
		};
sl@0
    57
	enum
sl@0
    58
		{
sl@0
    59
		KOutOfRangeFlag = 1 << EOutOfRange,
sl@0
    60
		KConsOAngFlag = 1 << EConsOAng,
sl@0
    61
		KConsYoYakFlag = 1 << EConsYoYak,
sl@0
    62
		KConsHoHipFlag = 1 << EConsHoHip,
sl@0
    63
		KConsWoWeanFlag = 1 << EConsWoWean,
sl@0
    64
		KConsDigraphFlag = 1 << EConsDigraph,
sl@0
    65
		KConsOtherFlag = 1 << EConsOther,
sl@0
    66
		KPostVowelAFlag = 1 << EPostVowelA,
sl@0
    67
		KPostVowelAAFlag = 1 << EPostVowelAA,
sl@0
    68
		KPostVowelOtherFlag = 1 << EPostVowelOther,
sl@0
    69
		KPreVowelFlag = 1 << EPreVowel,
sl@0
    70
		KDepMaiHanAkatFlag = 1 << EDepMaiHanAkat,
sl@0
    71
		KDepSaraIFlag = 1 << EDepSaraI,
sl@0
    72
		KDepOtherFlag = 1 << EDepOther,
sl@0
    73
sl@0
    74
		KSpecialDepFlags = KDepMaiHanAkatFlag | KDepSaraIFlag,
sl@0
    75
		KPostVowelFlags = KPostVowelAFlag | KPostVowelAAFlag | KPostVowelOtherFlag,
sl@0
    76
		KConsFlags = KConsOtherFlag | KConsDigraphFlag | KConsWoWeanFlag
sl@0
    77
			| KConsHoHipFlag | KConsYoYakFlag | KConsOAngFlag,
sl@0
    78
		KAllFlags = KOutOfRangeFlag | KConsOAngFlag | KConsYoYakFlag
sl@0
    79
			| KConsHoHipFlag | KConsWoWeanFlag | KConsDigraphFlag
sl@0
    80
			| KConsOtherFlag | KPostVowelAFlag | KPostVowelAAFlag
sl@0
    81
			| KPostVowelOtherFlag | KPreVowelFlag | KDepMaiHanAkatFlag
sl@0
    82
			| KDepSaraIFlag | KDepOtherFlag
sl@0
    83
		};
sl@0
    84
sl@0
    85
	/** Returns the Thai linebreaking class of the character. */
sl@0
    86
	static TCharClassification Class(TInt aChar);
sl@0
    87
	/** Returns true if aChar is a combining character according to aBreaker. */
sl@0
    88
	static TBool IsCombiningChar(TInt aChar, const MLineBreaker& aBreaker);
sl@0
    89
	/** Returns the Thai linebreaking class of the character at (*aChar),
sl@0
    90
	searching backwards for the base character if it is foreign and of type CM.
sl@0
    91
	@param aChar The position of the character within the string.
sl@0
    92
	@param aStart The start of the string.
sl@0
    93
	@param aBreaker The line breaker to query for line breaking class.
sl@0
    94
	*/
sl@0
    95
	static TCharClassification DerivedClass(const TText* aChar,
sl@0
    96
		const TText* aStart, const MLineBreaker& aBreaker);
sl@0
    97
	/**
sl@0
    98
	 Gets the line break rule for the previous and current character pair
sl@0
    99
	@param aPrevClass Class of the previous character.
sl@0
   100
	@param aClass Class of the current character.
sl@0
   101
	@return the rule code corresponding to the input pair.
sl@0
   102
	*/
sl@0
   103
	static TBool BreakAllowedBetween(
sl@0
   104
		TCharClassification aPrevClass, TCharClassification aClass);
sl@0
   105
	/** Returns whether a line break is allowed before the SA character at
sl@0
   106
	(*aChar).
sl@0
   107
	@param aChar The position of the character within the string.
sl@0
   108
	@param aStart The start of the string.
sl@0
   109
	@param aBreaker The line breaker to query for line breaking class.
sl@0
   110
	*/
sl@0
   111
	static TBool BreakAllowedAt(const TText* aChar,
sl@0
   112
		const TText* aStart, const MLineBreaker& aBreaker);
sl@0
   113
sl@0
   114
	/**
sl@0
   115
	 Classification of each character
sl@0
   116
	*/
sl@0
   117
	static const TUint8 KCharClassifications[KNumThaiCharacters];
sl@0
   118
sl@0
   119
	/**
sl@0
   120
	 Rules table of prev to next character
sl@0
   121
	*/
sl@0
   122
	static const TUint32 KRules[EMaxClassification];
sl@0
   123
	};
sl@0
   124
sl@0
   125
const TUint8 ThaiLinebreakRules::KCharClassifications[KNumThaiCharacters] = 
sl@0
   126
	{
sl@0
   127
	EOutOfRange,	// 0x0E00
sl@0
   128
	EConsOther,		// 0x0E01
sl@0
   129
	EConsOther,		// 0x0E02
sl@0
   130
	EConsOther,		// 0x0E03
sl@0
   131
	EConsOther,		// 0x0E04
sl@0
   132
	EConsOther,		// 0x0E05
sl@0
   133
	EConsOther,		// 0x0E06
sl@0
   134
	EConsDigraph,	// 0x0E07
sl@0
   135
	EConsOther,		// 0x0E08
sl@0
   136
	EConsOther,		// 0x0E09
sl@0
   137
	EConsOther,		// 0x0E0A
sl@0
   138
	EConsOther,		// 0x0E0B
sl@0
   139
	EConsOther,		// 0x0E0C
sl@0
   140
	EConsOther,		// 0x0E0D
sl@0
   141
	EConsOther,		// 0x0E0E
sl@0
   142
	EConsOther,		// 0x0E0F
sl@0
   143
sl@0
   144
	EConsOther,		// 0x0E10
sl@0
   145
	EConsOther,		// 0x0E11
sl@0
   146
	EConsOther,		// 0x0E12
sl@0
   147
	EConsOther,		// 0x0E13
sl@0
   148
	EConsOther,		// 0x0E14
sl@0
   149
	EConsOther,		// 0x0E15
sl@0
   150
	EConsOther,		// 0x0E16
sl@0
   151
	EConsOther,		// 0x0E17
sl@0
   152
	EConsOther,		// 0x0E18
sl@0
   153
	EConsDigraph,	// 0x0E19
sl@0
   154
	EConsOther,		// 0x0E1A
sl@0
   155
	EConsOther,		// 0x0E1B
sl@0
   156
	EConsOther,		// 0x0E1C
sl@0
   157
	EConsOther,		// 0x0E1D
sl@0
   158
	EConsOther,		// 0x0E1E
sl@0
   159
	EConsOther,		// 0x0E1F
sl@0
   160
sl@0
   161
	EConsOther,		// 0x0E20
sl@0
   162
	EConsDigraph,	// 0x0E21
sl@0
   163
	EConsYoYak,		// 0x0E22
sl@0
   164
	EConsDigraph,	// 0x0E23
sl@0
   165
	EConsOther,		// 0x0E24
sl@0
   166
	EConsDigraph,	// 0x0E25
sl@0
   167
	EConsOther,		// 0x0E26
sl@0
   168
	EConsWoWean,	// 0x0E27
sl@0
   169
	EConsOther,		// 0x0E28
sl@0
   170
	EConsOther,		// 0x0E29
sl@0
   171
	EConsOther,		// 0x0E2A
sl@0
   172
	EConsHoHip,		// 0x0E2B
sl@0
   173
	EConsOther,		// 0x0E2C
sl@0
   174
	EConsOAng,		// 0x0E2D
sl@0
   175
	EConsOther,		// 0x0E2E
sl@0
   176
	EOutOfRange,	// 0x0E2F
sl@0
   177
sl@0
   178
	EPostVowelA,	// 0x0E30
sl@0
   179
	EDepMaiHanAkat,	// 0x0E31
sl@0
   180
	EPostVowelAA,	// 0x0E32
sl@0
   181
	EPostVowelOther,// 0x0E33
sl@0
   182
sl@0
   183
	EDepSaraI,		// 0x0E34
sl@0
   184
	EDepOther,		// 0x0E35
sl@0
   185
	EDepOther,		// 0x0E36
sl@0
   186
	EDepOther,		// 0x0E37
sl@0
   187
	EDepOther,		// 0x0E38
sl@0
   188
	EDepOther,		// 0x0E39
sl@0
   189
	EDepOther,		// 0x0E3A
sl@0
   190
sl@0
   191
	EOutOfRange,	// 0x0E3B
sl@0
   192
	EOutOfRange,	// 0x0E3C
sl@0
   193
	EOutOfRange,	// 0x0E3D
sl@0
   194
	EOutOfRange,	// 0x0E3E
sl@0
   195
	EOutOfRange,	// 0x0E3F
sl@0
   196
sl@0
   197
	EPreVowel,		// 0x0E40
sl@0
   198
	EPreVowel,		// 0x0E41
sl@0
   199
	EPreVowel,		// 0x0E42
sl@0
   200
	EPreVowel,		// 0x0E43
sl@0
   201
	EPreVowel,		// 0x0E44
sl@0
   202
sl@0
   203
	EPostVowelOther,// 0x0E45
sl@0
   204
	EOutOfRange,	// 0x0E46
sl@0
   205
sl@0
   206
	EDepOther,		// 0x0E47
sl@0
   207
	EDepOther,		// 0x0E48
sl@0
   208
	EDepOther,		// 0x0E49
sl@0
   209
	EDepOther,		// 0x0E4A
sl@0
   210
	EDepOther,		// 0x0E4B
sl@0
   211
	EDepOther,		// 0x0E4C
sl@0
   212
	EDepOther,		// 0x0E4D
sl@0
   213
	EDepOther,		// 0x0E4E
sl@0
   214
	EOutOfRange,	// 0x0E4F
sl@0
   215
sl@0
   216
	EOutOfRange,		// 0x0E50
sl@0
   217
	EOutOfRange,		// 0x0E51
sl@0
   218
	EOutOfRange,		// 0x0E52
sl@0
   219
	EOutOfRange,		// 0x0E53
sl@0
   220
	EOutOfRange,		// 0x0E54
sl@0
   221
	EOutOfRange,		// 0x0E55
sl@0
   222
	EOutOfRange,		// 0x0E56
sl@0
   223
	EOutOfRange,		// 0x0E57
sl@0
   224
	EOutOfRange,		// 0x0E58
sl@0
   225
	EOutOfRange,		// 0x0E59
sl@0
   226
	EOutOfRange,		// 0x0E5A
sl@0
   227
	EOutOfRange			// 0x0E5B
sl@0
   228
	};
sl@0
   229
sl@0
   230
const TUint32 KNormalBreaksBeforeCons =
sl@0
   231
	ThaiLinebreakRules::KPreVowelFlag
sl@0
   232
	| ThaiLinebreakRules::KConsOtherFlag
sl@0
   233
	| ThaiLinebreakRules::KConsDigraphFlag
sl@0
   234
	| ThaiLinebreakRules::KConsHoHipFlag
sl@0
   235
	| ThaiLinebreakRules::KOutOfRangeFlag;
sl@0
   236
const TUint32 KNormalBreaksBeforePostVowel =
sl@0
   237
	ThaiLinebreakRules::KPreVowelFlag
sl@0
   238
	| ThaiLinebreakRules::KPostVowelFlags
sl@0
   239
	| ThaiLinebreakRules::KConsFlags
sl@0
   240
	| ThaiLinebreakRules::KOutOfRangeFlag;
sl@0
   241
const TUint32 ThaiLinebreakRules::KRules[EMaxClassification] =
sl@0
   242
	{
sl@0
   243
	/* Prev Char EOutOfRange */
sl@0
   244
	KAllFlags - KOutOfRangeFlag,
sl@0
   245
	/* Prev Char EConsOAng */
sl@0
   246
	KNormalBreaksBeforeCons,
sl@0
   247
	/* Prev Char EConsYoYak */
sl@0
   248
	KNormalBreaksBeforeCons,
sl@0
   249
	/* Prev Char EConsHoHip */
sl@0
   250
	KNormalBreaksBeforeCons - KConsDigraphFlag,
sl@0
   251
	/* Prev Char EConsWoWean */
sl@0
   252
	KNormalBreaksBeforeCons - KConsDigraphFlag - KConsHoHipFlag,
sl@0
   253
	/* Prev Char EConsDigraph */
sl@0
   254
	KNormalBreaksBeforeCons,
sl@0
   255
	/* Prev Char EConsOther */
sl@0
   256
	KNormalBreaksBeforeCons,
sl@0
   257
	/* Prev Char EPostVowelA */
sl@0
   258
	KNormalBreaksBeforePostVowel,
sl@0
   259
	/* Prev Char EPostVowelAA */
sl@0
   260
	KNormalBreaksBeforePostVowel - KPostVowelAFlag,
sl@0
   261
	/* Prev Char EPostVowelOther */
sl@0
   262
	KNormalBreaksBeforePostVowel,
sl@0
   263
	/* Prev Char EPreVowel */
sl@0
   264
	KPreVowelFlag | KPostVowelFlags | KOutOfRangeFlag,
sl@0
   265
	/* Prev Char EDepMaiHanAkat */
sl@0
   266
	KSpecialDepFlags | KPreVowelFlag | KPostVowelAAFlag
sl@0
   267
		| KPostVowelAFlag | KOutOfRangeFlag,
sl@0
   268
	/* Prev Char EDepSaraI */
sl@0
   269
	KSpecialDepFlags | KPreVowelFlag | KPostVowelAAFlag
sl@0
   270
		| KPostVowelAFlag | KOutOfRangeFlag,
sl@0
   271
	/* Prev Char EDepOther */
sl@0
   272
	KSpecialDepFlags | KPreVowelFlag | KPostVowelAAFlag
sl@0
   273
		| KPostVowelAFlag | KConsOtherFlag | KConsDigraphFlag
sl@0
   274
		| KConsWoWeanFlag | KConsHoHipFlag | KOutOfRangeFlag
sl@0
   275
	};
sl@0
   276
sl@0
   277
ThaiLinebreakRules::TCharClassification ThaiLinebreakRules::Class(TInt a)
sl@0
   278
    {
sl@0
   279
	return static_cast<TCharClassification>(
sl@0
   280
		(KThaiCodePageStart <= a && a < KThaiCodePageEnd) ?
sl@0
   281
			KCharClassifications[a - KThaiCodePageStart] :
sl@0
   282
			EOutOfRange);
sl@0
   283
    }
sl@0
   284
sl@0
   285
TBool ThaiLinebreakRules::IsCombiningChar(TInt aChar,
sl@0
   286
	const MLineBreaker& aBreaker)
sl@0
   287
	{
sl@0
   288
	TUint dummy1, dummy2;
sl@0
   289
	return aBreaker.LineBreakClass(aChar, dummy1, dummy2) == MLineBreaker::ECmLineBreakClass;
sl@0
   290
	}
sl@0
   291
sl@0
   292
ThaiLinebreakRules::TCharClassification ThaiLinebreakRules::DerivedClass(
sl@0
   293
	const TText* aChar, const TText* aStart, const MLineBreaker& aBreaker)
sl@0
   294
	{
sl@0
   295
	ThaiLinebreakRules::TCharClassification c = Class(*aChar);
sl@0
   296
	while (c == EOutOfRange && aChar != aStart
sl@0
   297
		&& IsCombiningChar(*aChar, aBreaker))
sl@0
   298
		{
sl@0
   299
		--aChar;
sl@0
   300
		c = Class(*aChar);
sl@0
   301
		}
sl@0
   302
	return c;
sl@0
   303
	}
sl@0
   304
sl@0
   305
TBool ThaiLinebreakRules::BreakAllowedBetween(
sl@0
   306
	ThaiLinebreakRules::TCharClassification aClass,
sl@0
   307
	ThaiLinebreakRules::TCharClassification aNextClass)
sl@0
   308
	{
sl@0
   309
	return KRules[aClass] & (1 << aNextClass);
sl@0
   310
	}
sl@0
   311
sl@0
   312
TBool ThaiLinebreakRules::BreakAllowedAt(const TText* aChar,
sl@0
   313
	const TText* aStart, const MLineBreaker& aBreaker)
sl@0
   314
	{
sl@0
   315
	__ASSERT_DEBUG(aStart < aChar, User::Invariant());
sl@0
   316
	TCharClassification c = Class(*aChar);
sl@0
   317
	if (c == EOutOfRange && IsCombiningChar(*aChar, aBreaker))
sl@0
   318
		return EFalse;
sl@0
   319
	return BreakAllowedBetween(DerivedClass(aChar - 1, aStart, aBreaker), c);
sl@0
   320
	}
sl@0
   321
sl@0
   322
/**
sl@0
   323
 Returns whether a line break is possible within a run of characters all having
sl@0
   324
 the class ESaLineBreakClass (Complex content). Languages with Unicocde
sl@0
   325
 characters having such a class include: Thai, Lao, Myanmar and Khmer. This
sl@0
   326
 default implementation of the GetLineBreakInContext() method only supports the
sl@0
   327
 Thai script. Breaks are determined in Thai based on a simple understanding of
sl@0
   328
 syllable boundaries. When characters from the other unsupported Sa class
sl@0
   329
 languages are found the method exits with EFalse.
sl@0
   330
@param aText
sl@0
   331
 The text to be searched, which is a contiguous run of characters of class SA
sl@0
   332
 (or CM attatched to SA). The break position may be restricted further by
sl@0
   333
 aMinBreakPos and aMaxBreakPos, but more text is provided for extra context
sl@0
   334
 should it be needed.
sl@0
   335
@param aMinBreakPos
sl@0
   336
 The start of the text to be considered for line breaks.
sl@0
   337
@param aMaxBreakPos
sl@0
   338
 The end of the text to be considered for line breaks.
sl@0
   339
@param aForwards
sl@0
   340
 ETrue if aBreakPos is to be set with the first legal break position,
sl@0
   341
 EFalse if aBreakPos is to be set with the last legal break position.
sl@0
   342
@param aBreakPos
sl@0
   343
 If break position found on exit its value is >= Min and <= Max 
sl@0
   344
 positions supplied.
sl@0
   345
@return TBool
sl@0
   346
 ETrue if and only if a legal break was found, EFalse otherwise.
sl@0
   347
@publishedAll
sl@0
   348
@released
sl@0
   349
*/
sl@0
   350
EXPORT_C TBool MLineBreaker::GetLineBreakInContext(const TDesC16& aText,
sl@0
   351
		TInt aMinBreakPos, TInt aMaxBreakPos, TBool aForwards,
sl@0
   352
		TInt& aBreakPos) const
sl@0
   353
	{
sl@0
   354
	__ASSERT_DEBUG (0 <= aMinBreakPos && aMaxBreakPos <= aText.Length(), 
sl@0
   355
		Panic(ELineBreakPanic_InvalidInputParam));
sl@0
   356
sl@0
   357
	TInt length = aText.Length();
sl@0
   358
sl@0
   359
	if (aMinBreakPos < 1)
sl@0
   360
		aMinBreakPos = 1;
sl@0
   361
	if (length - 1 < aMaxBreakPos)
sl@0
   362
		aMaxBreakPos = length - 1;
sl@0
   363
	if (aMaxBreakPos < aMinBreakPos)
sl@0
   364
		return EFalse;
sl@0
   365
sl@0
   366
	const TText16* text = aText.Ptr();
sl@0
   367
sl@0
   368
	if (*text == KZeroWidthSpace)
sl@0
   369
		{
sl@0
   370
		aBreakPos = aMinBreakPos;
sl@0
   371
		return ETrue;
sl@0
   372
		}
sl@0
   373
	else if (*(text+length-1) == KZeroWidthSpace)
sl@0
   374
		return EFalse;
sl@0
   375
	
sl@0
   376
	TInt start = aForwards? aMinBreakPos : aMaxBreakPos;
sl@0
   377
	TInt end = aForwards? aMaxBreakPos + 1 : aMinBreakPos - 1;
sl@0
   378
	TInt direction = aForwards? 1 : -1;
sl@0
   379
	for (TInt i = start; i != end; i += direction)
sl@0
   380
		{
sl@0
   381
		if (ThaiLinebreakRules::BreakAllowedAt(text + i, text, *this))
sl@0
   382
			{
sl@0
   383
			aBreakPos = i;
sl@0
   384
			return ETrue;
sl@0
   385
			}
sl@0
   386
		}
sl@0
   387
	return EFalse;
sl@0
   388
	}