os/graphics/graphicsdeviceinterface/gdi/sgdi/BidiCompact.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2002-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
#include "BidiCompact.h"
sl@0
    17
#include "BidiCopy.h"
sl@0
    18
#include <gdi.h>
sl@0
    19
sl@0
    20
static const TInt KZeroWidthJoiner = 0x200D;
sl@0
    21
// This gets round the compiler warning about converting
sl@0
    22
// EFRightToLeft to unsigned long.
sl@0
    23
inline TUint FRightToLeft() { return static_cast<TUint>(TRunInfoCompact::EFRightToLeft); }
sl@0
    24
sl@0
    25
/**
sl@0
    26
Constructs a run description without considering optimisations based
sl@0
    27
on the text itself.
sl@0
    28
@param aStart Index of the start of the run.
sl@0
    29
@param aLength Length of the run.
sl@0
    30
@param aReverse ETrue if the run is right-to-left.
sl@0
    31
@internalTechnology
sl@0
    32
*/
sl@0
    33
TRunInfoCompact::TRunInfoCompact(TInt aStart, TInt aLength,
sl@0
    34
	TBool aReverse)
sl@0
    35
	: iStart(aStart), iLengthAndType(aLength)
sl@0
    36
	{
sl@0
    37
	if (aReverse)
sl@0
    38
		iLengthAndType |= FRightToLeft();
sl@0
    39
	}
sl@0
    40
sl@0
    41
/**
sl@0
    42
Constructs a run description.
sl@0
    43
sl@0
    44
@param aStart Index of the start of the run.
sl@0
    45
@param aLength Length of the run.
sl@0
    46
@param aReverse ETrue if the run is right-to-left.
sl@0
    47
@param aText The text that this run refers to (starting at index 0, not
sl@0
    48
the start of the run). This is required only to determine if optimisations 
sl@0
    49
to the re-ordering are possible.
sl@0
    50
@internalTechnology
sl@0
    51
*/
sl@0
    52
TRunInfoCompact::TRunInfoCompact(TInt aStart, TInt aLength,
sl@0
    53
	TBool aReverse, const TText* aText)
sl@0
    54
	: iStart(aStart), iLengthAndType(aLength)
sl@0
    55
	{
sl@0
    56
	ASSERT(0 <= aLength);
sl@0
    57
	ASSERT(aLength < 0x10000000);
sl@0
    58
	ASSERT(0 <= aStart);
sl@0
    59
	if (!aReverse)
sl@0
    60
		return;
sl@0
    61
	iLengthAndType |= FRightToLeft();
sl@0
    62
	TUint32 flags = EFNoPairsNoCombiners | EFNoMirroredCharacters;
sl@0
    63
	aText += aStart;
sl@0
    64
sl@0
    65
	for (const TText* end = aText + aLength; aText < end && flags; ++aText)
sl@0
    66
		{
sl@0
    67
		TInt code = *aText;
sl@0
    68
		if ((code & 0xF800) == 0xD800)
sl@0
    69
			{
sl@0
    70
			flags &= ~EFNoPairsNoCombiners;
sl@0
    71
			if ((code & 0xFC00) == 0xDC00
sl@0
    72
				&& aText + 1 < end
sl@0
    73
				&& (aText[1] & 0xFC00) == 0xD800)
sl@0
    74
				{
sl@0
    75
				code = (aText[1] << 10) + (code & 0x3FF)
sl@0
    76
					+ (0x10000 - 0xD800*0x400);
sl@0
    77
				++aText;
sl@0
    78
				}
sl@0
    79
			}
sl@0
    80
		TChar c = code;
sl@0
    81
		if (c.GetCombiningClass() != 0)
sl@0
    82
			flags &= ~EFNoPairsNoCombiners;
sl@0
    83
		if (BidiCopy::Mirror(code) != code)
sl@0
    84
			flags &= ~EFNoMirroredCharacters;
sl@0
    85
		}
sl@0
    86
	iLengthAndType |= flags;
sl@0
    87
	}
sl@0
    88
sl@0
    89
/**
sl@0
    90
Attempts to extend a run.
sl@0
    91
sl@0
    92
@param aToBeAdded The run to be merged.
sl@0
    93
@return ETrue if extension succeeded, EFalse if not.
sl@0
    94
@internalTechnology
sl@0
    95
*/
sl@0
    96
TBool TRunInfoCompact::AddRun(const TRunInfoCompact& aToBeAdded)
sl@0
    97
	{
sl@0
    98
	TInt length = Length();
sl@0
    99
	if (length == 0)
sl@0
   100
		{
sl@0
   101
		*this = aToBeAdded;
sl@0
   102
		return ETrue;
sl@0
   103
		}
sl@0
   104
sl@0
   105
	// Are both runs in the same direction?
sl@0
   106
	if ((iLengthAndType ^ aToBeAdded.iLengthAndType) & FRightToLeft())
sl@0
   107
		return EFalse;
sl@0
   108
sl@0
   109
	TBool rightToLeft = TypeFlags() & EFRightToLeft;
sl@0
   110
	TInt end = rightToLeft?
sl@0
   111
		Start() - Length() : Start() + Length();
sl@0
   112
sl@0
   113
	if (end != aToBeAdded.Start())
sl@0
   114
		return EFalse;
sl@0
   115
sl@0
   116
	length += aToBeAdded.Length();
sl@0
   117
sl@0
   118
	iLengthAndType = length | (TypeFlags() & aToBeAdded.TypeFlags());
sl@0
   119
sl@0
   120
	if (rightToLeft)
sl@0
   121
		iStart -= aToBeAdded.Length();
sl@0
   122
sl@0
   123
	return ETrue;
sl@0
   124
	}
sl@0
   125
sl@0
   126
/**
sl@0
   127
Reorders text described by this run according to aContext. Allow 6 extra
sl@0
   128
bytes for a truncation.
sl@0
   129
@param aDestination	Where to write this run of visually-ordered text to.
sl@0
   130
@param aContext The source of the text to be ordered.
sl@0
   131
@return	The first byte not written to: in other words, what aDestination
sl@0
   132
should be updated to.
sl@0
   133
@internalTechnology
sl@0
   134
*/
sl@0
   135
TText* TRunInfoCompact::Reorder(TText* aDestination,
sl@0
   136
	const TRunInfoCompact::TReorderingContext& aContext) const
sl@0
   137
	{
sl@0
   138
	TInt start = Start();
sl@0
   139
	if (aContext.iEnd < start)
sl@0
   140
		// does not overlap
sl@0
   141
		return aDestination;
sl@0
   142
	TInt end = Start() + Length();
sl@0
   143
	if (end <= aContext.iStart)
sl@0
   144
		// does not overlap
sl@0
   145
		return aDestination;
sl@0
   146
	TBool startJoins = EFalse;
sl@0
   147
	if (start <= aContext.iStart)
sl@0
   148
		{
sl@0
   149
		start = aContext.iStart;
sl@0
   150
		startJoins = aContext.iJoinsAtStart;
sl@0
   151
		}
sl@0
   152
	TBool truncated = EFalse;
sl@0
   153
	TBool endJoins = EFalse;
sl@0
   154
	if (aContext.iEnd <= end)
sl@0
   155
		{
sl@0
   156
		if (aContext.iEnd < end
sl@0
   157
			&& aContext.iTruncation != 0xFFFF)
sl@0
   158
			truncated = ETrue;
sl@0
   159
		end = aContext.iEnd;
sl@0
   160
		endJoins = aContext.iJoinsAtEnd;
sl@0
   161
		}
sl@0
   162
	TInt length = end - start;
sl@0
   163
	if (length == 0 && !truncated)
sl@0
   164
		return aDestination;
sl@0
   165
	ASSERT(0 <= length);
sl@0
   166
	const TText* source = aContext.iSource + start;
sl@0
   167
	if (TypeFlags() & FRightToLeft())
sl@0
   168
		{
sl@0
   169
		// Right-to-left
sl@0
   170
		if (truncated)
sl@0
   171
			aDestination = BidiCopy::OutputTChar(aDestination, aContext.iTruncation);
sl@0
   172
		if (endJoins)
sl@0
   173
			*(aDestination++) = KZeroWidthJoiner;
sl@0
   174
		if (TypeFlags() & EFNoPairsNoCombiners)
sl@0
   175
			{
sl@0
   176
			// Simple
sl@0
   177
			aDestination = TypeFlags() & EFNoMirroredCharacters?
sl@0
   178
				BidiCopy::CopyBackwards(aDestination, source, length)
sl@0
   179
				: BidiCopy::CopyBackwardsWithMirroring(aDestination, source, length);
sl@0
   180
			}
sl@0
   181
		else
sl@0
   182
			// Respect groups
sl@0
   183
			aDestination = BidiCopy::CopyGroupsBackwards(aDestination, source, length);
sl@0
   184
		if (startJoins)
sl@0
   185
			*aDestination++ = KZeroWidthJoiner;
sl@0
   186
		return aDestination;
sl@0
   187
		}
sl@0
   188
	// Left-to-right
sl@0
   189
	if (startJoins)
sl@0
   190
		*aDestination++ = KZeroWidthJoiner;
sl@0
   191
	Mem::Copy(aDestination, source, length * sizeof(TText));
sl@0
   192
	aDestination += length;
sl@0
   193
	if (endJoins)
sl@0
   194
		*aDestination++ = KZeroWidthJoiner;
sl@0
   195
	if (truncated)
sl@0
   196
		aDestination = BidiCopy::OutputTChar(aDestination, aContext.iTruncation);
sl@0
   197
	return aDestination;
sl@0
   198
	}
sl@0
   199
sl@0
   200
/**
sl@0
   201
Converts an array of aArraySize TBidirectionalState::TRunInfos into a
sl@0
   202
compact form.
sl@0
   203
sl@0
   204
@param aBuffer Memory to output to, or null just to find out how large the output
sl@0
   205
array will need to be.
sl@0
   206
@param aText The text that aRunArray refers to.
sl@0
   207
@param aRunArray The array to be converted.
sl@0
   208
@param aArraySize The length of aRunArray.
sl@0
   209
@return The length of the output array.
sl@0
   210
@internalTechnology
sl@0
   211
*/
sl@0
   212
TInt TRunInfoCompact::Convert(TRunInfoCompact* aBuffer, const TDesC& aText,
sl@0
   213
	const TBidirectionalState::TRunInfo* aRunArray, TInt aArraySize)
sl@0
   214
	{
sl@0
   215
	const TText* text = aText.Ptr();
sl@0
   216
	TInt outputSize =  0;
sl@0
   217
sl@0
   218
	TRunInfoCompact currentRun;
sl@0
   219
	while (aArraySize)
sl@0
   220
		{
sl@0
   221
		TRunInfoCompact newRun(aRunArray->iStart, aRunArray->iLength,
sl@0
   222
			aRunArray->iDirection, text);
sl@0
   223
		--aArraySize;
sl@0
   224
		if (!currentRun.AddRun(newRun))
sl@0
   225
			{
sl@0
   226
			if (aBuffer)
sl@0
   227
				*aBuffer++ = currentRun;
sl@0
   228
			++outputSize;
sl@0
   229
			currentRun = newRun;
sl@0
   230
			}
sl@0
   231
		++aRunArray; //point to next run
sl@0
   232
		}
sl@0
   233
	if (0 < currentRun.Length())
sl@0
   234
		{
sl@0
   235
		if (aBuffer)
sl@0
   236
			*aBuffer++ = currentRun;
sl@0
   237
		++outputSize;
sl@0
   238
		}
sl@0
   239
sl@0
   240
	return outputSize;
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
Utility tells whether a character will form a join with the previous
sl@0
   245
base character.
sl@0
   246
sl@0
   247
@param aText The text.
sl@0
   248
@param aIndex The index into aText of the character to test.
sl@0
   249
@return ETrue if there is a join before the character.
sl@0
   250
*/
sl@0
   251
TBool TRunInfoCompact::JoinBefore(const TText* aText, TInt aIndex)
sl@0
   252
	{
sl@0
   253
	TInt charUnderTest = aText[aIndex];
sl@0
   254
	if (!CFont::CharactersJoin(charUnderTest, KZeroWidthJoiner))
sl@0
   255
		// Character does not join with anything, so we
sl@0
   256
		// will not do any more work.
sl@0
   257
		return EFalse;
sl@0
   258
	while (aIndex != 0)
sl@0
   259
		{
sl@0
   260
		--aIndex;
sl@0
   261
		TInt c = aText[aIndex];
sl@0
   262
		// If it is an Arabic point, we will skip it.
sl@0
   263
		if (0x64B <= c && c < 0x671
sl@0
   264
			&& !(0x656 <= c && c < 0x670))
sl@0
   265
			continue;
sl@0
   266
		return CFont::CharactersJoin(charUnderTest, c);
sl@0
   267
		}
sl@0
   268
	return EFalse;
sl@0
   269
	}