2 * Copyright (c) 2015 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/visual-model.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/math/vector2.h>
27 #include <dali-toolkit/internal/text/line-run.h>
39 * @brief caches some temporary values of the GetNumberOfLines( glyphIndex, numberOfGlyphs ) operation
40 * as they are going to be used in the GetLinesOfGlyphRange() call.
44 GlyphIndex glyphIndex; ///< The glyph index.
45 Length numberOfGlyphs; ///< The number of glyphs.
46 Length firstLine; ///< Index to the first line.
47 Length numberOfLines; ///< The number of lines.
50 struct VisualModel::Impl
52 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
53 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
54 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
55 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
56 Vector<Length> mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
57 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
58 Vector<LineRun> mLines; ///< The laid out lines.
60 Size mNaturalSize; ///< Size of the text with no line wrapping.
61 Size mActualSize; ///< Size of the laid-out text considering the layout properties set.
63 GetLineCache mGetLineCache; ///< Caches the GetNumberOfLines( glyphIndex, numberOfGlyphs ) operation.
66 VisualModelPtr VisualModel::New()
68 return VisualModelPtr( new VisualModel() );
71 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
72 const CharacterIndex* characterIndices,
73 const Length* charactersPerGlyph,
74 Length numberOfGlyphs )
76 Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
77 Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
78 Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
79 Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
80 Vector<Length>& modelGlyphsPerCharacter = mImpl->mGlyphsPerCharacter;
82 if( 0u == numberOfGlyphs )
85 modelGlyphsToCharacters.Clear();
86 modelCharactersToGlyph.Clear();
87 modelCharactersPerGlyph.Clear();
88 modelGlyphsPerCharacter.Clear();
94 modelGlyphs.Resize( numberOfGlyphs );
95 memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
98 if( NULL != characterIndices )
100 modelGlyphsToCharacters.Resize( numberOfGlyphs );
101 memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
104 if( NULL != charactersPerGlyph )
106 modelCharactersPerGlyph.Resize( numberOfGlyphs );
107 memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
109 // Build the characters to glyph conversion table.
111 // 1) Reserve some space for the characters to avoid reallocations.
112 const Length numberOfCharacters = static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f );
113 modelCharactersToGlyph.Reserve( numberOfCharacters );
114 modelGlyphsPerCharacter.Reserve( numberOfCharacters );
116 // 2) Traverse the glyphs and set the glyph indices and the glyphs per character.
118 // The number of 'characters per glyph' equal to zero.
119 Length zeroCharactersPerGlyph = 0u;
121 // Index to the glyph.
122 GlyphIndex glyphIndex = 0u;
123 for( Vector<Length>::ConstIterator it = modelCharactersPerGlyph.Begin(),
124 endIt = modelCharactersPerGlyph.End();
128 const Length numberOfCharacters = *it;
130 // Set the glyph indices.
131 for( Length index = 0u; index < numberOfCharacters; ++index )
133 modelCharactersToGlyph.PushBack( glyphIndex );
136 // Set the glyphs per character.
137 if( 0u == numberOfCharacters )
139 ++zeroCharactersPerGlyph;
143 const Length numberOfZeroGlyphsPerCharacter = ( numberOfCharacters - 1u );
144 for( Length zeroIndex = 0u; zeroIndex < numberOfZeroGlyphsPerCharacter ; ++zeroIndex )
146 modelGlyphsPerCharacter.PushBack( 0u );
149 modelGlyphsPerCharacter.PushBack( 1u + zeroCharactersPerGlyph );
151 zeroCharactersPerGlyph = 0u;
158 Length VisualModel::GetNumberOfGlyphs() const
160 return mImpl->mGlyphs.Count();
163 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
164 GlyphIndex glyphIndex,
165 Length numberOfGlyphs ) const
167 const Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
168 memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
171 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
173 return mImpl->mGlyphs[glyphIndex];
176 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
178 return mImpl->mGlyphsToCharacters[glyphIndex];
181 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
183 return mImpl->mCharactersPerGlyph[glyphIndex];
186 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
188 return mImpl->mCharactersToGlyph[characterIndex];
191 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
192 CharacterIndex characterIndex,
193 Length numberOfCharacters ) const
195 const Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
196 memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
199 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
200 GlyphIndex glyphIndex,
201 Length numberOfGlyphs ) const
203 const Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
204 memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
207 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
208 GlyphIndex glyphIndex,
209 Length numberOfGlyphs ) const
211 const Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
212 memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
215 void VisualModel::GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
216 CharacterIndex characterIndex,
217 Length numberOfCharacters ) const
219 const Vector<Length>& modelGlyphsPerCharacter = mImpl->mGlyphsPerCharacter;
220 memcpy( glyphsPerCharacter, modelGlyphsPerCharacter.Begin() + characterIndex, numberOfCharacters * sizeof( Length ) );
223 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
224 Length numberOfGlyphs )
226 Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
227 if( 0u == numberOfGlyphs )
229 modelPositions.Clear();
233 modelPositions.Resize( numberOfGlyphs );
234 memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
238 Length VisualModel::GetNumberOfGlyphPositions() const
240 return mImpl->mGlyphPositions.Count();
243 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
244 GlyphIndex glyphIndex,
245 Length numberOfGlyphs ) const
247 const Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
248 memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
251 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
253 return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
256 void VisualModel::SetLines( const LineRun* const lines,
257 Length numberOfLines )
259 Vector<LineRun>& modelLines = mImpl->mLines;
260 GetLineCache& lineCache = mImpl->mGetLineCache;
262 if( 0u == numberOfLines )
268 modelLines.Resize( numberOfLines );
269 memcpy( modelLines.Begin(), lines, numberOfLines * sizeof( LineRun ) );
272 // Clear the get line cache.
273 lineCache.glyphIndex = 0u;
274 lineCache.numberOfGlyphs = 0u;
275 lineCache.firstLine = 0u;
276 lineCache.numberOfLines = 0u;
279 Length VisualModel::GetNumberOfLines() const
281 return mImpl->mLines.Count();
284 void VisualModel::GetLines( LineRun* lines,
286 Length numberOfLines ) const
288 const Vector<LineRun>& modelLines = mImpl->mLines;
289 memcpy( lines, modelLines.Begin() + lineIndex, numberOfLines * sizeof( LineRun ) );
292 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
293 Length numberOfGlyphs ) const
295 // If is likely the user query consecutively for the number of lines with the same
296 // glyph index and number of glyphs, use the cache could be considered.
297 GetLineCache& lineCache = mImpl->mGetLineCache;
299 // Cache the glyph index and number of glyphs to be used in the GetLinesOfGlyphRange().
300 lineCache.glyphIndex = glyphIndex;
301 lineCache.numberOfGlyphs = numberOfGlyphs;
303 // Check first if the query is for the total number of glyphs.
304 const Length totalNumberOfGlyphs = mImpl->mGlyphs.Count();
306 if( ( 0u == glyphIndex ) &&
307 ( totalNumberOfGlyphs == numberOfGlyphs ) )
309 lineCache.firstLine = 0u;
310 lineCache.numberOfLines = mImpl->mLines.Count();
312 return lineCache.numberOfLines;
315 // Initialize the number of lines and the first line.
316 lineCache.numberOfLines = 0u;
317 lineCache.firstLine = 0u;
318 bool firstLineFound = false;
320 const Vector<LineRun>& modelLines = mImpl->mLines;
321 const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
323 // Traverse the lines and count those lines within the range of glyphs.
324 for( Vector<LineRun>::ConstIterator it = modelLines.Begin(),
325 endIt = modelLines.End();
329 const LineRun& line = *it;
331 if( ( line.glyphIndex + line.numberOfGlyphs > glyphIndex ) &&
332 ( lastGlyphIndex > line.glyphIndex ) )
334 firstLineFound = true;
335 ++lineCache.numberOfLines;
337 else if( lastGlyphIndex <= line.glyphIndex )
339 // nothing else to do.
343 if( !firstLineFound )
345 ++lineCache.firstLine;
349 return lineCache.numberOfLines;
352 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
353 GlyphIndex glyphIndex,
354 Length numberOfGlyphs ) const
356 const Vector<LineRun>& modelLines = mImpl->mLines;
357 GetLineCache& lineCache = mImpl->mGetLineCache;
359 if( ( glyphIndex != lineCache.glyphIndex ) ||
360 ( numberOfGlyphs != lineCache.numberOfGlyphs ) )
362 GetNumberOfLines( glyphIndex,
366 memcpy( lines, modelLines.Begin() + lineCache.firstLine, lineCache.numberOfLines * sizeof( LineRun ) );
369 void VisualModel::SetNaturalSize( const Vector2& size )
371 mImpl->mNaturalSize = size;
374 const Vector2& VisualModel::GetNaturalSize() const
376 return mImpl->mNaturalSize;
379 void VisualModel::SetActualSize( const Vector2& size )
381 mImpl->mActualSize = size;
384 const Vector2& VisualModel::GetActualSize() const
386 return mImpl->mActualSize;
389 VisualModel::~VisualModel()
394 VisualModel::VisualModel()
397 mImpl = new VisualModel::Impl();
402 } // namespace Toolkit