- CharacterIndex selectionStart = mEventData->mLeftSelectionPosition;
- CharacterIndex selectionEnd = mEventData->mRightSelectionPosition;
-
- if( selectionStart == selectionEnd )
- {
- // Nothing to select if handles are in the same place.
- return;
- }
-
- mEventData->mDecorator->ClearHighlights();
-
- const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
- const Length* const glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
- const GlyphInfo* const glyphsBuffer = mVisualModel->mGlyphs.Begin();
- const Vector2* const positionsBuffer = mVisualModel->mGlyphPositions.Begin();
- const Length* const charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
- const CharacterIndex* const glyphToCharacterBuffer = mVisualModel->mGlyphsToCharacters.Begin();
- const CharacterDirection* const modelCharacterDirectionsBuffer = ( 0u != mLogicalModel->mCharacterDirections.Count() ) ? mLogicalModel->mCharacterDirections.Begin() : NULL;
-
- const bool isLastCharacter = selectionEnd >= mLogicalModel->mText.Count();
- const CharacterDirection startDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + selectionStart ) );
- const CharacterDirection endDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + ( selectionEnd - ( isLastCharacter ? 1u : 0u ) ) ) );
-
- // Swap the indices if the start is greater than the end.
- const bool indicesSwapped = selectionStart > selectionEnd;
-
- // Tell the decorator to flip the selection handles if needed.
- mEventData->mDecorator->SetSelectionHandleFlipState( indicesSwapped, startDirection, endDirection );
-
- if( indicesSwapped )
- {
- std::swap( selectionStart, selectionEnd );
- }
-
- // Get the indices to the first and last selected glyphs.
- const CharacterIndex selectionEndMinusOne = selectionEnd - 1u;
- const GlyphIndex glyphStart = *( charactersToGlyphBuffer + selectionStart );
- const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + selectionEndMinusOne );
- const GlyphIndex glyphEnd = *( charactersToGlyphBuffer + selectionEndMinusOne ) + ( ( numberOfGlyphs > 0 ) ? numberOfGlyphs - 1u : 0u );
-
- // Get the lines where the glyphs are laid-out.
- const LineRun* lineRun = mVisualModel->mLines.Begin();
-
- LineIndex lineIndex = 0u;
- Length numberOfLines = 0u;
- mVisualModel->GetNumberOfLines( glyphStart,
- 1u + glyphEnd - glyphStart,
- lineIndex,
- numberOfLines );
- const LineIndex firstLineIndex = lineIndex;
-
- // Create the structure to store some selection box info.
- Vector<SelectionBoxInfo> selectionBoxLinesInfo;
- selectionBoxLinesInfo.Resize( numberOfLines );
-
- SelectionBoxInfo* selectionBoxInfo = selectionBoxLinesInfo.Begin();
- selectionBoxInfo->minX = MAX_FLOAT;
- selectionBoxInfo->maxX = MIN_FLOAT;
-
- // Keep the min and max 'x' position to calculate the size and position of the highlighed text.
- float minHighlightX = std::numeric_limits<float>::max();
- float maxHighlightX = std::numeric_limits<float>::min();
- Size highLightSize;
- Vector2 highLightPosition; // The highlight position in decorator's coords.
-
- // Retrieve the first line and get the line's vertical offset, the line's height and the index to the last glyph.
-
- // The line's vertical offset of all the lines before the line where the first glyph is laid-out.
- selectionBoxInfo->lineOffset = CalculateLineOffset( mVisualModel->mLines,
- firstLineIndex );
-
- // Transform to decorator's (control) coords.
- selectionBoxInfo->lineOffset += mScrollPosition.y;
-
- lineRun += firstLineIndex;
-
- // The line height is the addition of the line ascender and the line descender.
- // However, the line descender has a negative value, hence the subtraction.
- selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
-
- GlyphIndex lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
-
- // Check if the first glyph is a ligature that must be broken like Latin ff, fi, or Arabic ï»», etc which needs special code.
- const Length numberOfCharactersStart = *( charactersPerGlyphBuffer + glyphStart );
- bool splitStartGlyph = ( numberOfCharactersStart > 1u ) && HasLigatureMustBreak( mLogicalModel->GetScript( selectionStart ) );
-
- // Check if the last glyph is a ligature that must be broken like Latin ff, fi, or Arabic ï»», etc which needs special code.
- const Length numberOfCharactersEnd = *( charactersPerGlyphBuffer + glyphEnd );
- bool splitEndGlyph = ( glyphStart != glyphEnd ) && ( numberOfCharactersEnd > 1u ) && HasLigatureMustBreak( mLogicalModel->GetScript( selectionEndMinusOne ) );
-
- // The number of quads of the selection box.
- const unsigned int numberOfQuads = 1u + ( glyphEnd - glyphStart ) + ( ( numberOfLines > 1u ) ? 2u * numberOfLines : 0u );
- mEventData->mDecorator->ResizeHighlightQuads( numberOfQuads );
-
- // Count the actual number of quads.
- unsigned int actualNumberOfQuads = 0u;
- Vector4 quad;
-
- // Traverse the glyphs.
- for( GlyphIndex index = glyphStart; index <= glyphEnd; ++index )
- {
- const GlyphInfo& glyph = *( glyphsBuffer + index );
- const Vector2& position = *( positionsBuffer + index );
-
- if( splitStartGlyph )
- {
- // If the first glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
-
- const float glyphAdvance = glyph.advance / static_cast<float>( numberOfCharactersStart );
- const CharacterIndex interGlyphIndex = selectionStart - *( glyphToCharacterBuffer + glyphStart );
- // Get the direction of the character.
- CharacterDirection isCurrentRightToLeft = false;
- if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
- {
- isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + selectionStart );
- }
-
- // The end point could be in the middle of the ligature.
- // Calculate the number of characters selected.
- const Length numberOfCharacters = ( glyphStart == glyphEnd ) ? ( selectionEnd - selectionStart ) : ( numberOfCharactersStart - interGlyphIndex );
-
- quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + glyphAdvance * static_cast<float>( isCurrentRightToLeft ? ( numberOfCharactersStart - interGlyphIndex - numberOfCharacters ) : interGlyphIndex );
- quad.y = selectionBoxInfo->lineOffset;
- quad.z = quad.x + static_cast<float>( numberOfCharacters ) * glyphAdvance;
- quad.w = selectionBoxInfo->lineOffset + selectionBoxInfo->lineHeight;
-
- // Store the min and max 'x' for each line.
- selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
- selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
-
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads, quad );
- ++actualNumberOfQuads;
-
- splitStartGlyph = false;
- continue;
- }
-
- if( splitEndGlyph && ( index == glyphEnd ) )
- {
- // Equally, if the last glyph is a ligature that must be broken it may be needed to add only part of the glyph to the highlight box.
-
- const float glyphAdvance = glyph.advance / static_cast<float>( numberOfCharactersEnd );
- const CharacterIndex interGlyphIndex = selectionEnd - *( glyphToCharacterBuffer + glyphEnd );
- // Get the direction of the character.
- CharacterDirection isCurrentRightToLeft = false;
- if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
- {
- isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + selectionEnd );
- }
-
- const Length numberOfCharacters = numberOfCharactersEnd - interGlyphIndex;
-
- quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + ( isCurrentRightToLeft ? ( glyphAdvance * static_cast<float>( numberOfCharacters ) ) : 0.f );
- quad.y = selectionBoxInfo->lineOffset;
- quad.z = quad.x + static_cast<float>( interGlyphIndex ) * glyphAdvance;
- quad.w = quad.y + selectionBoxInfo->lineHeight;
-
- // Store the min and max 'x' for each line.
- selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
- selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
-
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- splitEndGlyph = false;
- continue;
- }
-
- quad.x = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x;
- quad.y = selectionBoxInfo->lineOffset;
- quad.z = quad.x + glyph.advance;
- quad.w = quad.y + selectionBoxInfo->lineHeight;
-
- // Store the min and max 'x' for each line.
- selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, quad.x );
- selectionBoxInfo->maxX = std::max( selectionBoxInfo->maxX, quad.z );
-
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- // Whether to retrieve the next line.
- if( index == lastGlyphOfLine )
- {
- // Retrieve the next line.
- ++lineRun;
-
- // Get the last glyph of the new line.
- lastGlyphOfLine = lineRun->glyphRun.glyphIndex + lineRun->glyphRun.numberOfGlyphs - 1u;
-
- ++lineIndex;
- if( lineIndex < firstLineIndex + numberOfLines )
- {
- // Keep the offset and height of the current selection box.
- const float currentLineOffset = selectionBoxInfo->lineOffset;
- const float currentLineHeight = selectionBoxInfo->lineHeight;
-
- // Get the selection box info for the next line.
- ++selectionBoxInfo;
-
- selectionBoxInfo->minX = MAX_FLOAT;
- selectionBoxInfo->maxX = MIN_FLOAT;
-
- // Update the line's vertical offset.
- selectionBoxInfo->lineOffset = currentLineOffset + currentLineHeight;
-
- // The line height is the addition of the line ascender and the line descender.
- // However, the line descender has a negative value, hence the subtraction.
- selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender;
- }
- }
- }
-
- // Traverses all the lines and updates the min and max 'x' positions and the total height.
- // The final width is calculated after 'boxifying' the selection.
- for( Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin(),
- endIt = selectionBoxLinesInfo.End();
- it != endIt;
- ++it )
- {
- const SelectionBoxInfo& info = *it;
-
- // Update the size of the highlighted text.
- highLightSize.height += info.lineHeight;
- minHighlightX = std::min( minHighlightX, info.minX );
- maxHighlightX = std::max( maxHighlightX, info.maxX );
- }
-
- // Add extra geometry to 'boxify' the selection.
-
- if( 1u < numberOfLines )
- {
- // Boxify the first line.
- lineRun = mVisualModel->mLines.Begin() + firstLineIndex;
- const SelectionBoxInfo& firstSelectionBoxLineInfo = *( selectionBoxLinesInfo.Begin() );
-
- bool boxifyBegin = ( LTR != lineRun->direction ) && ( LTR != startDirection );
- bool boxifyEnd = ( LTR == lineRun->direction ) && ( LTR == startDirection );
-
- if( boxifyBegin )
- {
- quad.x = 0.f;
- quad.y = firstSelectionBoxLineInfo.lineOffset;
- quad.z = firstSelectionBoxLineInfo.minX;
- quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
-
- // Boxify at the beginning of the line.
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- // Update the size of the highlighted text.
- minHighlightX = 0.f;
- }
-
- if( boxifyEnd )
- {
- quad.x = firstSelectionBoxLineInfo.maxX;
- quad.y = firstSelectionBoxLineInfo.lineOffset;
- quad.z = mVisualModel->mControlSize.width;
- quad.w = firstSelectionBoxLineInfo.lineOffset + firstSelectionBoxLineInfo.lineHeight;
-
- // Boxify at the end of the line.
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- // Update the size of the highlighted text.
- maxHighlightX = mVisualModel->mControlSize.width;
- }
-
- // Boxify the central lines.
- if( 2u < numberOfLines )
- {
- for( Vector<SelectionBoxInfo>::ConstIterator it = selectionBoxLinesInfo.Begin() + 1u,
- endIt = selectionBoxLinesInfo.End() - 1u;
- it != endIt;
- ++it )
- {
- const SelectionBoxInfo& info = *it;
-
- quad.x = 0.f;
- quad.y = info.lineOffset;
- quad.z = info.minX;
- quad.w = info.lineOffset + info.lineHeight;
-
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- quad.x = info.maxX;
- quad.y = info.lineOffset;
- quad.z = mVisualModel->mControlSize.width;
- quad.w = info.lineOffset + info.lineHeight;
-
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
- }
-
- // Update the size of the highlighted text.
- minHighlightX = 0.f;
- maxHighlightX = mVisualModel->mControlSize.width;
- }
-
- // Boxify the last line.
- lineRun = mVisualModel->mLines.Begin() + firstLineIndex + numberOfLines - 1u;
- const SelectionBoxInfo& lastSelectionBoxLineInfo = *( selectionBoxLinesInfo.End() - 1u );
-
- boxifyBegin = ( LTR == lineRun->direction ) && ( LTR == endDirection );
- boxifyEnd = ( LTR != lineRun->direction ) && ( LTR != endDirection );
-
- if( boxifyBegin )
- {
- quad.x = 0.f;
- quad.y = lastSelectionBoxLineInfo.lineOffset;
- quad.z = lastSelectionBoxLineInfo.minX;
- quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
-
- // Boxify at the beginning of the line.
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- // Update the size of the highlighted text.
- minHighlightX = 0.f;
- }
-
- if( boxifyEnd )
- {
- quad.x = lastSelectionBoxLineInfo.maxX;
- quad.y = lastSelectionBoxLineInfo.lineOffset;
- quad.z = mVisualModel->mControlSize.width;
- quad.w = lastSelectionBoxLineInfo.lineOffset + lastSelectionBoxLineInfo.lineHeight;
-
- // Boxify at the end of the line.
- mEventData->mDecorator->AddHighlight( actualNumberOfQuads,
- quad );
- ++actualNumberOfQuads;
-
- // Update the size of the highlighted text.
- maxHighlightX = mVisualModel->mControlSize.width;
- }
- }
-
- // Set the actual number of quads.
- mEventData->mDecorator->ResizeHighlightQuads( actualNumberOfQuads );
-
- // Sets the highlight's size and position. In decorator's coords.
- // The highlight's height has been calculated above (before 'boxifying' the highlight).
- highLightSize.width = maxHighlightX - minHighlightX;
-
- highLightPosition.x = minHighlightX;
- const SelectionBoxInfo& firstSelectionBoxLineInfo = *( selectionBoxLinesInfo.Begin() );
- highLightPosition.y = firstSelectionBoxLineInfo.lineOffset;
-
- mEventData->mDecorator->SetHighLightBox( highLightPosition, highLightSize );
-
- if( !mEventData->mDecorator->IsSmoothHandlePanEnabled() )
- {
- CursorInfo primaryCursorInfo;
- GetCursorPosition( mEventData->mLeftSelectionPosition,
- primaryCursorInfo );
-
- const Vector2 primaryPosition = primaryCursorInfo.primaryPosition + mScrollPosition;
-
- mEventData->mDecorator->SetPosition( LEFT_SELECTION_HANDLE,
- primaryPosition.x,
- primaryCursorInfo.lineOffset + mScrollPosition.y,
- primaryCursorInfo.lineHeight );
-
- CursorInfo secondaryCursorInfo;
- GetCursorPosition( mEventData->mRightSelectionPosition,
- secondaryCursorInfo );
-
- const Vector2 secondaryPosition = secondaryCursorInfo.primaryPosition + mScrollPosition;
-
- mEventData->mDecorator->SetPosition( RIGHT_SELECTION_HANDLE,
- secondaryPosition.x,
- secondaryCursorInfo.lineOffset + mScrollPosition.y,
- secondaryCursorInfo.lineHeight );
- }
-
- // Cursor to be positioned at end of selection so if selection interrupted and edit mode restarted the cursor will be at end of selection
- mEventData->mPrimaryCursorPosition = ( indicesSwapped ) ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition;
-
- // Set the flag to update the decorator.
- mEventData->mDecoratorUpdated = true;