- CharacterDirection isCurrentRightToLeft = false;
- CharacterDirection isNextRightToLeft = false;
- if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
- {
- isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + characterIndex );
- isNextRightToLeft = *( modelCharacterDirectionsBuffer + nextCharacterIndex );
- }
-
- // Get the line where the character is laid-out.
- const LineRun* const modelLines = mVisualModel->mLines.Begin();
-
- const LineIndex lineIndex = mVisualModel->GetLineOfCharacter( characterIndex );
- const LineRun& line = *( modelLines + lineIndex );
-
- // Get the paragraph's direction.
- const CharacterDirection isRightToLeftParagraph = line.direction;
-
- // Check whether there is an alternative position:
-
- cursorInfo.isSecondaryCursor = ( !isLastPosition && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
- ( isLastPosition && ( isRightToLeftParagraph != isCurrentRightToLeft ) );
-
- // Set the line offset and height.
- cursorInfo.lineOffset = 0.f;
- cursorInfo.lineHeight = line.ascender + -line.descender;
-
- // Calculate the primary cursor.
-
- CharacterIndex index = characterIndex;
- if( cursorInfo.isSecondaryCursor )
- {
- // If there is a secondary position, the primary cursor may be in a different place than the logical index.
-
- if( isLastPosition )
- {
- // The position of the cursor after the last character needs special
- // care depending on its direction and the direction of the paragraph.
-
- // Need to find the first character after the last character with the paragraph's direction.
- // i.e l0 l1 l2 r0 r1 should find r0.
-
- // TODO: check for more than one line!
- index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
- index = mLogicalModel->GetLogicalCharacterIndex( index );
- }
- else
- {
- index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
- }
- }
-
- const GlyphIndex* const charactersToGlyphBuffer = mVisualModel->mCharactersToGlyph.Begin();
- const Length* const glyphsPerCharacterBuffer = mVisualModel->mGlyphsPerCharacter.Begin();
- const Length* const charactersPerGlyphBuffer = mVisualModel->mCharactersPerGlyph.Begin();
- const CharacterIndex* const glyphsToCharactersBuffer = mVisualModel->mGlyphsToCharacters.Begin();
- const Vector2* const glyphPositionsBuffer = mVisualModel->mGlyphPositions.Begin();
- const GlyphInfo* const glyphInfoBuffer = mVisualModel->mGlyphs.Begin();
-
- // Convert the cursor position into the glyph position.
- const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
- const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
- const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
-
- // Get the metrics for the group of glyphs.
- GlyphMetrics glyphMetrics;
- GetGlyphsMetrics( primaryGlyphIndex,
- primaryNumberOfGlyphs,
- glyphMetrics,
- glyphInfoBuffer,
- mMetrics );
-
- // Whether to add the glyph's advance to the cursor position.
- // i.e if the paragraph is left to right and the logical cursor is zero, the position is the position of the first glyph and the advance is not added,
- // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
- // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
- //
- // FLCP A
- // ------
- // 0000 1
- // 0001 1
- // 0010 0
- // 0011 0
- // 0100 1
- // 0101 0
- // 0110 1
- // 0111 0
- // 1000 0
- // 1001 x
- // 1010 x
- // 1011 1
- // 1100 x
- // 1101 x
- // 1110 x
- // 1111 x
- //
- // Where F -> isFirstPosition
- // L -> isLastPosition
- // C -> isCurrentRightToLeft
- // P -> isRightToLeftParagraph
- // A -> Whether to add the glyph's advance.
-
- const bool addGlyphAdvance = ( ( isLastPosition && !isRightToLeftParagraph ) ||
- ( isFirstPosition && isRightToLeftParagraph ) ||
- ( !isFirstPosition && !isLastPosition && !isCurrentRightToLeft ) );
-
- float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
-
- if( !isLastPosition &&
- ( primaryNumberOfCharacters > 1u ) )
- {
- const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
-
- bool isCurrentRightToLeft = false;
- if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
- {
- isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + index );
- }
-
- Length numberOfGlyphAdvance = ( isFirstPosition ? 0u : 1u ) + characterIndex - firstIndex;
- if( isCurrentRightToLeft )
- {
- numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
- }
-
- glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
- }
-
- // Get the glyph position and x bearing.
- const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
-
- // Set the primary cursor's height.
- cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
-
- // Set the primary cursor's position.
- cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
- cursorInfo.primaryPosition.y = line.ascender - glyphMetrics.ascender;
-
- // Calculate the secondary cursor.
-
- if( cursorInfo.isSecondaryCursor )
- {
- // Set the secondary cursor's height.
- cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
-
- CharacterIndex index = characterIndex;
- if( !isLastPosition )
- {
- index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
- }
-
- const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
- const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
-
- const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
-
- GetGlyphsMetrics( secondaryGlyphIndex,
- secondaryNumberOfGlyphs,
- glyphMetrics,
- glyphInfoBuffer,
- mMetrics );
-
- // Set the secondary cursor's position.
- cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( isCurrentRightToLeft ? 0.f : glyphMetrics.advance );
- cursorInfo.secondaryPosition.y = cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight - line.descender - ( glyphMetrics.fontHeight - glyphMetrics.ascender );
- }
-
- if( LayoutEngine::MULTI_LINE_BOX == mLayoutEngine.GetLayout() )