2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/cursor-helper-functions.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/text/glyph-metrics-helper.h>
30 #if defined(DEBUG_ENABLED)
31 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
45 LineIndex GetClosestLine( VisualModelPtr visualModel,
48 float totalHeight = 0.f;
49 LineIndex lineIndex = 0u;
51 const Vector<LineRun>& lines = visualModel->mLines;
52 for( LineIndex endLine = lines.Count();
56 const LineRun& lineRun = lines[lineIndex];
57 totalHeight += lineRun.ascender + -lineRun.descender;
58 if( visualY < totalHeight )
72 CharacterIndex GetClosestCursorIndex( VisualModelPtr visualModel,
73 LogicalModelPtr logicalModel,
78 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "GetClosestCursorIndex, closest visualX %f visualY %f\n", visualX, visualY );
80 CharacterIndex logicalIndex = 0u;
82 const Length numberOfGlyphs = visualModel->mGlyphs.Count();
83 const Length numberOfLines = visualModel->mLines.Count();
84 if( ( 0 == numberOfGlyphs ) ||
85 ( 0 == numberOfLines ) )
90 // Find which line is closest.
91 const LineIndex lineIndex = Text::GetClosestLine( visualModel,
93 const LineRun& line = visualModel->mLines[lineIndex];
95 // Get the positions of the glyphs.
96 const Vector<Vector2>& positions = visualModel->mGlyphPositions;
97 const Vector2* const positionsBuffer = positions.Begin();
99 // Get the character to glyph conversion table.
100 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
102 // Get the glyphs per character table.
103 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
105 // Get the glyph's info buffer.
106 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
108 const CharacterIndex startCharacter = line.characterRun.characterIndex;
109 const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters;
110 DALI_ASSERT_DEBUG( endCharacter <= logicalModel->mText.Count() && "Invalid line info" );
112 // Whether this line is a bidirectional line.
113 const bool bidiLineFetched = logicalModel->FetchBidirectionalLineInfo( startCharacter );
115 // Whether there is a hit on a glyph.
116 bool matched = false;
118 // Traverses glyphs in visual order. To do that use the visual to logical conversion table.
119 CharacterIndex visualIndex = startCharacter;
120 Length numberOfCharacters = 0u;
121 for( ; !matched && ( visualIndex < endCharacter ); ++visualIndex )
123 // The character in logical order.
124 const CharacterIndex characterLogicalOrderIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( visualIndex ) : visualIndex );
126 // Get the script of the character.
127 const Script script = logicalModel->GetScript( characterLogicalOrderIndex );
129 // The number of glyphs for that character
130 const Length numberOfGlyphs = *( glyphsPerCharacterBuffer + characterLogicalOrderIndex );
131 ++numberOfCharacters;
134 if( 0u != numberOfGlyphs )
136 // Get the first character/glyph of the group of glyphs.
137 const CharacterIndex firstVisualCharacterIndex = 1u + visualIndex - numberOfCharacters;
138 const CharacterIndex firstLogicalCharacterIndex = ( bidiLineFetched ? logicalModel->GetLogicalCharacterIndex( firstVisualCharacterIndex ) : firstVisualCharacterIndex );
139 const GlyphIndex firstLogicalGlyphIndex = *( charactersToGlyphBuffer + firstLogicalCharacterIndex );
141 // Get the metrics for the group of glyphs.
142 GlyphMetrics glyphMetrics;
143 GetGlyphsMetrics( firstLogicalGlyphIndex,
149 // Get the position of the first glyph.
150 const Vector2& position = *( positionsBuffer + firstLogicalGlyphIndex );
152 // Whether the glyph can be split, like Latin ligatures fi, ff or Arabic ﻻ.
153 const bool isInterglyphIndex = ( numberOfCharacters > numberOfGlyphs ) && HasLigatureMustBreak( script );
154 const Length numberOfBlocks = isInterglyphIndex ? numberOfCharacters : 1u;
155 const float glyphAdvance = glyphMetrics.advance / static_cast<float>( numberOfBlocks );
157 GlyphIndex index = 0u;
158 for( ; !matched && ( index < numberOfBlocks ); ++index )
160 // Find the mid-point of the area containing the glyph
161 const float glyphCenter = -glyphMetrics.xBearing + position.x + ( static_cast<float>( index ) + 0.5f ) * glyphAdvance;
163 if( visualX < glyphCenter )
172 visualIndex = firstVisualCharacterIndex + index;
176 numberOfCharacters = 0u;
181 // Return the logical position of the cursor in characters.
185 visualIndex = endCharacter;
188 logicalIndex = ( bidiLineFetched ? logicalModel->GetLogicalCursorIndex( visualIndex ) : visualIndex );
189 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "closest visualIndex %d logicalIndex %d\n", visualIndex, logicalIndex );
191 DALI_ASSERT_DEBUG( ( logicalIndex <= logicalModel->mText.Count() && logicalIndex >= 0 ) && "GetClosestCursorIndex - Out of bounds index" );
197 void GetCursorPosition( VisualModelPtr visualModel,
198 LogicalModelPtr logicalModel,
200 CharacterIndex logical,
201 CursorInfo& cursorInfo )
203 // TODO: Check for multiline with \n, etc...
205 const Length numberOfCharacters = logicalModel->mText.Count();
207 // Check if the logical position is the first or the last one of the text.
208 const bool isFirstPosition = 0u == logical;
209 const bool isLastPosition = numberOfCharacters == logical;
211 // 'logical' is the logical 'cursor' index.
212 // Get the next and current logical 'character' index.
213 const CharacterIndex nextCharacterIndex = logical;
214 const CharacterIndex characterIndex = isFirstPosition ? logical : logical - 1u;
216 // Get the direction of the character and the next one.
217 const CharacterDirection* const modelCharacterDirectionsBuffer = ( 0u != logicalModel->mCharacterDirections.Count() ) ? logicalModel->mCharacterDirections.Begin() : NULL;
219 CharacterDirection isCurrentRightToLeft = false;
220 CharacterDirection isNextRightToLeft = false;
221 if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
223 isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + characterIndex );
224 isNextRightToLeft = *( modelCharacterDirectionsBuffer + nextCharacterIndex );
227 // Get the line where the character is laid-out.
228 const LineRun* const modelLines = visualModel->mLines.Begin();
230 const LineIndex lineIndex = visualModel->GetLineOfCharacter( characterIndex );
231 const LineRun& line = *( modelLines + lineIndex );
233 // Get the paragraph's direction.
234 const CharacterDirection isRightToLeftParagraph = line.direction;
236 // Check whether there is an alternative position:
238 cursorInfo.isSecondaryCursor = ( !isLastPosition && ( isCurrentRightToLeft != isNextRightToLeft ) ) ||
239 ( isLastPosition && ( isRightToLeftParagraph != isCurrentRightToLeft ) );
241 // Set the line offset and height.
242 cursorInfo.lineOffset = 0.f;
243 cursorInfo.lineHeight = line.ascender + -line.descender;
245 // Calculate the primary cursor.
247 CharacterIndex index = characterIndex;
248 if( cursorInfo.isSecondaryCursor )
250 // If there is a secondary position, the primary cursor may be in a different place than the logical index.
254 // The position of the cursor after the last character needs special
255 // care depending on its direction and the direction of the paragraph.
257 // Need to find the first character after the last character with the paragraph's direction.
258 // i.e l0 l1 l2 r0 r1 should find r0.
260 // TODO: check for more than one line!
261 index = isRightToLeftParagraph ? line.characterRun.characterIndex : line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1u;
262 index = logicalModel->GetLogicalCharacterIndex( index );
266 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? characterIndex : nextCharacterIndex;
270 const GlyphIndex* const charactersToGlyphBuffer = visualModel->mCharactersToGlyph.Begin();
271 const Length* const glyphsPerCharacterBuffer = visualModel->mGlyphsPerCharacter.Begin();
272 const Length* const charactersPerGlyphBuffer = visualModel->mCharactersPerGlyph.Begin();
273 const CharacterIndex* const glyphsToCharactersBuffer = visualModel->mGlyphsToCharacters.Begin();
274 const Vector2* const glyphPositionsBuffer = visualModel->mGlyphPositions.Begin();
275 const GlyphInfo* const glyphInfoBuffer = visualModel->mGlyphs.Begin();
277 // Convert the cursor position into the glyph position.
278 const GlyphIndex primaryGlyphIndex = *( charactersToGlyphBuffer + index );
279 const Length primaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
280 const Length primaryNumberOfCharacters = *( charactersPerGlyphBuffer + primaryGlyphIndex );
282 // Get the metrics for the group of glyphs.
283 GlyphMetrics glyphMetrics;
284 GetGlyphsMetrics( primaryGlyphIndex,
285 primaryNumberOfGlyphs,
290 // Whether to add the glyph's advance to the cursor position.
291 // 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,
292 // if the logical cursor is one, the position is the position of the first glyph and the advance is added.
293 // A 'truth table' was build and an online Karnaugh map tool was used to simplify the logic.
314 // Where F -> isFirstPosition
315 // L -> isLastPosition
316 // C -> isCurrentRightToLeft
317 // P -> isRightToLeftParagraph
318 // A -> Whether to add the glyph's advance.
320 const bool addGlyphAdvance = ( ( isLastPosition && !isRightToLeftParagraph ) ||
321 ( isFirstPosition && isRightToLeftParagraph ) ||
322 ( !isFirstPosition && !isLastPosition && !isCurrentRightToLeft ) );
324 float glyphAdvance = addGlyphAdvance ? glyphMetrics.advance : 0.f;
326 if( !isLastPosition &&
327 ( primaryNumberOfCharacters > 1u ) )
329 const CharacterIndex firstIndex = *( glyphsToCharactersBuffer + primaryGlyphIndex );
331 bool isCurrentRightToLeft = false;
332 if( NULL != modelCharacterDirectionsBuffer ) // If modelCharacterDirectionsBuffer is NULL, it means the whole text is left to right.
334 isCurrentRightToLeft = *( modelCharacterDirectionsBuffer + index );
337 Length numberOfGlyphAdvance = ( isFirstPosition ? 0u : 1u ) + characterIndex - firstIndex;
338 if( isCurrentRightToLeft )
340 numberOfGlyphAdvance = primaryNumberOfCharacters - numberOfGlyphAdvance;
343 glyphAdvance = static_cast<float>( numberOfGlyphAdvance ) * glyphMetrics.advance / static_cast<float>( primaryNumberOfCharacters );
346 // Get the glyph position and x bearing.
347 const Vector2& primaryPosition = *( glyphPositionsBuffer + primaryGlyphIndex );
349 // Set the primary cursor's height.
350 cursorInfo.primaryCursorHeight = cursorInfo.isSecondaryCursor ? 0.5f * glyphMetrics.fontHeight : glyphMetrics.fontHeight;
352 // Set the primary cursor's position.
353 cursorInfo.primaryPosition.x = -glyphMetrics.xBearing + primaryPosition.x + glyphAdvance;
354 cursorInfo.primaryPosition.y = line.ascender - glyphMetrics.ascender;
356 // Calculate the secondary cursor.
358 if( cursorInfo.isSecondaryCursor )
360 // Set the secondary cursor's height.
361 cursorInfo.secondaryCursorHeight = 0.5f * glyphMetrics.fontHeight;
363 CharacterIndex index = characterIndex;
364 if( !isLastPosition )
366 index = ( isRightToLeftParagraph == isCurrentRightToLeft ) ? nextCharacterIndex : characterIndex;
369 const GlyphIndex secondaryGlyphIndex = *( charactersToGlyphBuffer + index );
370 const Length secondaryNumberOfGlyphs = *( glyphsPerCharacterBuffer + index );
372 const Vector2& secondaryPosition = *( glyphPositionsBuffer + secondaryGlyphIndex );
374 GetGlyphsMetrics( secondaryGlyphIndex,
375 secondaryNumberOfGlyphs,
380 // Set the secondary cursor's position.
381 cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( isCurrentRightToLeft ? 0.f : glyphMetrics.advance );
382 cursorInfo.secondaryPosition.y = cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight - line.descender - ( glyphMetrics.fontHeight - glyphMetrics.ascender );
386 void FindSelectionIndices( VisualModelPtr visualModel,
387 LogicalModelPtr logicalModel,
391 CharacterIndex& startIndex,
392 CharacterIndex& endIndex )
394 CharacterIndex hitCharacter = Text::GetClosestCursorIndex( visualModel,
399 DALI_ASSERT_DEBUG( hitCharacter <= logicalModel->mText.Count() && "GetClosestCursorIndex returned out of bounds index" );
401 if( logicalModel->mText.Count() == 0 )
403 return; // if model empty
406 if( hitCharacter >= logicalModel->mText.Count() )
408 // Closest hit character is the last character.
409 if( hitCharacter == logicalModel->mText.Count() )
411 hitCharacter--; //Hit character index set to last character in logical model
415 // hitCharacter is out of bounds
420 startIndex = hitCharacter;
421 endIndex = hitCharacter;
422 bool isHitCharacterWhitespace = TextAbstraction::IsWhiteSpace( logicalModel->mText[hitCharacter] );
424 // Find the start and end of the text
425 for( startIndex = hitCharacter; startIndex > 0; --startIndex )
427 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ startIndex-1 ] ) )
432 const CharacterIndex pastTheEnd = logicalModel->mText.Count();
433 for( endIndex = hitCharacter + 1u; endIndex < pastTheEnd; ++endIndex )
435 if( isHitCharacterWhitespace != TextAbstraction::IsWhiteSpace( logicalModel->mText[ endIndex ] ) )
444 } // namespace Toolkit