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 modelGlyphs.Resize( numberOfGlyphs );
77 memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
79 Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
80 modelGlyphsToCharacters.Resize( numberOfGlyphs );
81 memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
83 Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
84 modelCharactersPerGlyph.Resize( numberOfGlyphs );
85 memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
87 // Build the characters to glyph conversion table.
88 Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
90 // 1) Reserve some space for the characters to avoid reallocations.
91 modelCharactersToGlyph.Reserve( static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f ) );
93 // 2) Traverse the glyphs and set the glyph indices.
94 GlyphIndex glyphIndex = 0u;
95 Length totalNumberOfCharacters = 0u;
96 for( Vector<Length>::ConstIterator it = modelCharactersPerGlyph.Begin(),
97 endIt = modelCharactersPerGlyph.End();
101 const Length numberOfCharacters = *it;
103 for( Length index = 0u; index < numberOfCharacters; ++index, ++totalNumberOfCharacters )
105 modelCharactersToGlyph.PushBack( glyphIndex );
110 Length VisualModel::GetNumberOfGlyphs() const
112 return mImpl->mGlyphs.Count();
115 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
116 GlyphIndex glyphIndex,
117 Length numberOfGlyphs ) const
119 const Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
120 memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
123 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
125 return mImpl->mGlyphs[glyphIndex];
128 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
130 return mImpl->mGlyphsToCharacters[glyphIndex];
133 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
135 return mImpl->mCharactersPerGlyph[glyphIndex];
138 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
140 return mImpl->mCharactersToGlyph[characterIndex];
143 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
144 CharacterIndex characterIndex,
145 Length numberOfCharacters ) const
147 const Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
148 memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
151 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
152 GlyphIndex glyphIndex,
153 Length numberOfGlyphs ) const
155 const Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
156 memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
159 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
160 GlyphIndex glyphIndex,
161 Length numberOfGlyphs ) const
163 const Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
164 memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
167 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
168 Length numberOfGlyphs )
170 Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
171 modelPositions.Resize( numberOfGlyphs );
172 memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
175 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
176 GlyphIndex glyphIndex,
177 Length numberOfGlyphs ) const
179 const Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
180 memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
183 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
185 return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
188 void VisualModel::SetLines( const LineRun* const lines,
189 Length numberOfLines )
191 Vector<LineRun>& modelLines = mImpl->mLines;
192 GetLineCache& lineCache = mImpl->mGetLineCache;
194 modelLines.Resize( numberOfLines );
195 memcpy( modelLines.Begin(), lines, numberOfLines * sizeof( LineRun ) );
197 // Clear the get line cache.
198 lineCache.glyphIndex = 0u;
199 lineCache.numberOfGlyphs = 0u;
200 lineCache.firstLine = 0u;
201 lineCache.numberOfLines = 0u;
204 Length VisualModel::GetNumberOfLines() const
206 return mImpl->mLines.Count();
209 void VisualModel::GetLines( LineRun* lines,
211 Length numberOfLines ) const
213 const Vector<LineRun>& modelLines = mImpl->mLines;
214 memcpy( lines, modelLines.Begin() + lineIndex, numberOfLines * sizeof( LineRun ) );
217 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
218 Length numberOfGlyphs ) const
220 // If is likely the user query consecutively for the number of lines with the same
221 // glyph index and number of glyphs, use the cache could be considered.
222 GetLineCache& lineCache = mImpl->mGetLineCache;
224 // Cache the glyph index and number of glyphs to be used in the GetLinesOfGlyphRange().
225 lineCache.glyphIndex = glyphIndex;
226 lineCache.numberOfGlyphs = numberOfGlyphs;
228 // Check first if the query is for the total number of glyphs.
229 const Length totalNumberOfGlyphs = mImpl->mGlyphs.Count();
231 if( ( 0u == glyphIndex ) &&
232 ( totalNumberOfGlyphs == numberOfGlyphs ) )
234 lineCache.firstLine = 0u;
235 lineCache.numberOfLines = mImpl->mLines.Count();
237 return lineCache.numberOfLines;
240 // Initialize the number of lines and the first line.
241 lineCache.numberOfLines = 0u;
242 lineCache.firstLine = 0u;
243 bool firstLineFound = false;
245 const Vector<LineRun>& modelLines = mImpl->mLines;
246 const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
248 // Traverse the lines and cound those lines within the range of glyphs.
249 for( Vector<LineRun>::ConstIterator it = modelLines.Begin(),
250 endIt = modelLines.End();
254 const LineRun& line = *it;
256 if( ( line.glyphIndex + line.numberOfGlyphs > glyphIndex ) &&
257 ( lastGlyphIndex > line.glyphIndex ) )
259 firstLineFound = true;
260 ++lineCache.numberOfLines;
262 else if( lastGlyphIndex <= line.glyphIndex )
264 // nothing else to do.
268 if( !firstLineFound )
270 ++lineCache.firstLine;
274 return lineCache.numberOfLines;
277 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
278 GlyphIndex glyphIndex,
279 Length numberOfGlyphs ) const
281 const Vector<LineRun>& modelLines = mImpl->mLines;
282 GetLineCache& lineCache = mImpl->mGetLineCache;
284 if( ( glyphIndex != lineCache.glyphIndex ) ||
285 ( numberOfGlyphs != lineCache.numberOfGlyphs ) )
287 GetNumberOfLines( glyphIndex,
291 memcpy( lines, modelLines.Begin() + lineCache.firstLine, lineCache.numberOfLines * sizeof( LineRun ) );
294 void VisualModel::SetNaturalSize( const Vector2& size )
296 mImpl->mNaturalSize = size;
299 const Vector2& VisualModel::GetNaturalSize() const
301 return mImpl->mNaturalSize;
304 void VisualModel::SetActualSize( const Vector2& size )
306 mImpl->mActualSize = size;
309 const Vector2& VisualModel::GetActualSize() const
311 return mImpl->mActualSize;
314 VisualModel::~VisualModel()
319 VisualModel::VisualModel()
322 mImpl = new VisualModel::Impl();
327 } // namespace Toolkit