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>
25 #include <dali/public-api/common/dali-vector.h>
26 #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<Vector2> mGlyphPositions; ///< For each glyph, the position.
57 Vector<LineRun> mLines; ///< The laid out lines.
59 Size mNaturalSize; ///< Size of the text with no line wrapping.
60 Size mActualSize; ///< Size of the laid-out text considering the layout properties set.
62 GetLineCache mGetLineCache; ///< Caches the GetNumberOfLines( glyphIndex, numberOfGlyphs ) operation.
65 VisualModelPtr VisualModel::New()
67 return VisualModelPtr( new VisualModel() );
70 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
71 const CharacterIndex* characterIndices,
72 const Length* charactersPerGlyph,
73 Length numberOfGlyphs )
75 Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
76 Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
77 Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
78 Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
80 if( 0u == numberOfGlyphs )
83 modelGlyphsToCharacters.Clear();
84 modelCharactersToGlyph.Clear();
85 modelCharactersPerGlyph.Clear();
91 modelGlyphs.Resize( numberOfGlyphs );
92 memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
95 if( NULL != characterIndices )
97 modelGlyphsToCharacters.Resize( numberOfGlyphs );
98 memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
101 if( NULL != charactersPerGlyph )
103 modelCharactersPerGlyph.Resize( numberOfGlyphs );
104 memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
106 // Build the characters to glyph conversion table.
108 // 1) Reserve some space for the characters to avoid reallocations.
109 modelCharactersToGlyph.Reserve( static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f ) );
111 // 2) Traverse the glyphs and set the glyph indices.
112 GlyphIndex glyphIndex = 0u;
113 Length totalNumberOfCharacters = 0u;
114 for( Vector<Length>::ConstIterator it = modelCharactersPerGlyph.Begin(),
115 endIt = modelCharactersPerGlyph.End();
119 const Length numberOfCharacters = *it;
121 for( Length index = 0u; index < numberOfCharacters; ++index, ++totalNumberOfCharacters )
123 modelCharactersToGlyph.PushBack( glyphIndex );
130 Length VisualModel::GetNumberOfGlyphs() const
132 return mImpl->mGlyphs.Count();
135 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
136 GlyphIndex glyphIndex,
137 Length numberOfGlyphs ) const
139 const Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
140 memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
143 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
145 return mImpl->mGlyphs[glyphIndex];
148 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
150 return mImpl->mGlyphsToCharacters[glyphIndex];
153 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
155 return mImpl->mCharactersPerGlyph[glyphIndex];
158 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
160 return mImpl->mCharactersToGlyph[characterIndex];
163 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
164 CharacterIndex characterIndex,
165 Length numberOfCharacters ) const
167 const Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
168 memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
171 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
172 GlyphIndex glyphIndex,
173 Length numberOfGlyphs ) const
175 const Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
176 memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
179 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
180 GlyphIndex glyphIndex,
181 Length numberOfGlyphs ) const
183 const Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
184 memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
187 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
188 Length numberOfGlyphs )
190 Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
191 if( 0u == numberOfGlyphs )
193 modelPositions.Clear();
197 modelPositions.Resize( numberOfGlyphs );
198 memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
202 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
203 GlyphIndex glyphIndex,
204 Length numberOfGlyphs ) const
206 const Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
207 memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
210 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
212 return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
215 void VisualModel::SetLines( const LineRun* const lines,
216 Length numberOfLines )
218 Vector<LineRun>& modelLines = mImpl->mLines;
219 GetLineCache& lineCache = mImpl->mGetLineCache;
221 if( 0u == numberOfLines )
227 modelLines.Resize( numberOfLines );
228 memcpy( modelLines.Begin(), lines, numberOfLines * sizeof( LineRun ) );
231 // Clear the get line cache.
232 lineCache.glyphIndex = 0u;
233 lineCache.numberOfGlyphs = 0u;
234 lineCache.firstLine = 0u;
235 lineCache.numberOfLines = 0u;
238 Length VisualModel::GetNumberOfLines() const
240 return mImpl->mLines.Count();
243 void VisualModel::GetLines( LineRun* lines,
245 Length numberOfLines ) const
247 const Vector<LineRun>& modelLines = mImpl->mLines;
248 memcpy( lines, modelLines.Begin() + lineIndex, numberOfLines * sizeof( LineRun ) );
251 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
252 Length numberOfGlyphs ) const
254 // If is likely the user query consecutively for the number of lines with the same
255 // glyph index and number of glyphs, use the cache could be considered.
256 GetLineCache& lineCache = mImpl->mGetLineCache;
258 // Cache the glyph index and number of glyphs to be used in the GetLinesOfGlyphRange().
259 lineCache.glyphIndex = glyphIndex;
260 lineCache.numberOfGlyphs = numberOfGlyphs;
262 // Check first if the query is for the total number of glyphs.
263 const Length totalNumberOfGlyphs = mImpl->mGlyphs.Count();
265 if( ( 0u == glyphIndex ) &&
266 ( totalNumberOfGlyphs == numberOfGlyphs ) )
268 lineCache.firstLine = 0u;
269 lineCache.numberOfLines = mImpl->mLines.Count();
271 return lineCache.numberOfLines;
274 // Initialize the number of lines and the first line.
275 lineCache.numberOfLines = 0u;
276 lineCache.firstLine = 0u;
277 bool firstLineFound = false;
279 const Vector<LineRun>& modelLines = mImpl->mLines;
280 const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
282 // Traverse the lines and cound those lines within the range of glyphs.
283 for( Vector<LineRun>::ConstIterator it = modelLines.Begin(),
284 endIt = modelLines.End();
288 const LineRun& line = *it;
290 if( ( line.glyphIndex + line.numberOfGlyphs > glyphIndex ) &&
291 ( lastGlyphIndex > line.glyphIndex ) )
293 firstLineFound = true;
294 ++lineCache.numberOfLines;
296 else if( lastGlyphIndex <= line.glyphIndex )
298 // nothing else to do.
302 if( !firstLineFound )
304 ++lineCache.firstLine;
308 return lineCache.numberOfLines;
311 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
312 GlyphIndex glyphIndex,
313 Length numberOfGlyphs ) const
315 const Vector<LineRun>& modelLines = mImpl->mLines;
316 GetLineCache& lineCache = mImpl->mGetLineCache;
318 if( ( glyphIndex != lineCache.glyphIndex ) ||
319 ( numberOfGlyphs != lineCache.numberOfGlyphs ) )
321 GetNumberOfLines( glyphIndex,
325 memcpy( lines, modelLines.Begin() + lineCache.firstLine, lineCache.numberOfLines * sizeof( LineRun ) );
328 void VisualModel::SetNaturalSize( const Vector2& size )
330 mImpl->mNaturalSize = size;
333 const Vector2& VisualModel::GetNaturalSize() const
335 return mImpl->mNaturalSize;
338 void VisualModel::SetActualSize( const Vector2& size )
340 mImpl->mActualSize = size;
343 const Vector2& VisualModel::GetActualSize() const
345 return mImpl->mActualSize;
348 VisualModel::~VisualModel()
353 VisualModel::VisualModel()
356 mImpl = new VisualModel::Impl();
361 } // namespace Toolkit