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-impl.h>
23 #include <dali/public-api/math/vector2.h>
34 VisualModelPtr VisualModel::New()
36 return VisualModelPtr( new VisualModel() );
39 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
40 const CharacterIndex* characterIndices,
41 const Length* charactersPerGlyph,
42 Length numberOfGlyphs )
44 if( 0u == numberOfGlyphs )
47 mGlyphsToCharacters.Clear();
48 mCharactersToGlyph.Clear();
49 mCharactersPerGlyph.Clear();
50 mGlyphsPerCharacter.Clear();
56 mGlyphs.Resize( numberOfGlyphs );
57 memcpy( mGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
60 if( NULL != characterIndices )
62 mGlyphsToCharacters.Resize( numberOfGlyphs );
63 memcpy( mGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
66 if( NULL != charactersPerGlyph )
68 mCharactersPerGlyph.Resize( numberOfGlyphs );
69 memcpy( mCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
71 // Build the characters to glyph conversion table.
72 CreateCharacterToGlyphTable();
74 // Build the glyphs per character table.
75 CreateGlyphsPerCharacterTable();
80 void VisualModel::CreateCharacterToGlyphTable( Length numberOfCharacters )
82 // 1) Reserve some space for the characters to avoid reallocations.
83 if( 0u == numberOfCharacters )
85 // If no number of characters is given, just set something sensible to avoid reallocations.
86 numberOfCharacters = static_cast<Length> ( static_cast<float>( mGlyphs.Count() ) * 1.3f );
88 mCharactersToGlyph.Reserve( numberOfCharacters );
90 // 2) Traverse the glyphs and set the glyph indices per character.
92 // Index to the glyph.
93 GlyphIndex glyphIndex = 0u;
94 for( Vector<Length>::ConstIterator it = mCharactersPerGlyph.Begin(),
95 endIt = mCharactersPerGlyph.End();
99 const Length numberOfCharactersPerGlyph = *it;
101 // Set the glyph indices.
102 for( Length index = 0u; index < numberOfCharactersPerGlyph; ++index )
104 mCharactersToGlyph.PushBack( glyphIndex );
109 void VisualModel::CreateGlyphsPerCharacterTable( Length numberOfCharacters )
111 // 1) Reserve some space for the characters to avoid reallocations.
112 if( 0u == numberOfCharacters )
114 // If no number of characters is given, just set something sensible to avoid reallocations.
115 numberOfCharacters = static_cast<Length> ( static_cast<float>( mGlyphs.Count() ) * 1.3f );
117 mCharactersToGlyph.Reserve( numberOfCharacters );
119 // 2) Traverse the glyphs and set the number of glyphs per character.
121 // The number of 'characters per glyph' equal to zero.
122 Length zeroCharactersPerGlyph = 0u;
124 for( Vector<Length>::ConstIterator it = mCharactersPerGlyph.Begin(),
125 endIt = mCharactersPerGlyph.End();
129 const Length numberOfCharactersPerGlyph = *it;
131 // Set the glyphs per character.
132 if( 0u == numberOfCharactersPerGlyph )
134 ++zeroCharactersPerGlyph;
138 const Length numberOfZeroGlyphsPerCharacter = ( numberOfCharactersPerGlyph - 1u );
139 for( Length zeroIndex = 0u; zeroIndex < numberOfZeroGlyphsPerCharacter ; ++zeroIndex )
141 mGlyphsPerCharacter.PushBack( 0u );
144 mGlyphsPerCharacter.PushBack( 1u + zeroCharactersPerGlyph );
146 zeroCharactersPerGlyph = 0u;
151 Length VisualModel::GetNumberOfGlyphs() const
153 return mGlyphs.Count();
156 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
157 GlyphIndex glyphIndex,
158 Length numberOfGlyphs ) const
160 memcpy( glyphs, mGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
163 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
165 return mGlyphs[glyphIndex];
168 void VisualModel::ReplaceGlyphs( GlyphIndex glyphIndex,
169 Length numberOfGlyphsToRemove,
170 const GlyphInfo* const glyphs,
171 const Length* const numberOfCharacters,
172 Length numberOfGlyphsToInsert )
176 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
178 return mGlyphsToCharacters[glyphIndex];
181 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
183 return mCharactersPerGlyph[glyphIndex];
186 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
188 return mCharactersToGlyph[characterIndex];
191 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
192 CharacterIndex characterIndex,
193 Length numberOfCharacters ) const
195 memcpy( characterToGlyphMap, mCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
198 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
199 GlyphIndex glyphIndex,
200 Length numberOfGlyphs ) const
202 memcpy( glyphToCharacter, mGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
205 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
206 GlyphIndex glyphIndex,
207 Length numberOfGlyphs ) const
209 memcpy( charactersPerGlyph, mCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
212 void VisualModel::GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
213 CharacterIndex characterIndex,
214 Length numberOfCharacters ) const
216 memcpy( glyphsPerCharacter, mGlyphsPerCharacter.Begin() + characterIndex, numberOfCharacters * sizeof( Length ) );
219 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
220 Length numberOfGlyphs )
222 if( 0u == numberOfGlyphs )
224 mGlyphPositions.Clear();
228 mGlyphPositions.Resize( numberOfGlyphs );
229 memcpy( mGlyphPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
233 Length VisualModel::GetNumberOfGlyphPositions() const
235 return mGlyphPositions.Count();
238 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
239 GlyphIndex glyphIndex,
240 Length numberOfGlyphs ) const
242 memcpy( glyphPositions, mGlyphPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
245 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
247 return *( mGlyphPositions.Begin() + glyphIndex );
250 void VisualModel::ReplaceGlyphPositions( GlyphIndex glyphIndex,
251 Length numberOfGlyphsToRemove,
252 const Vector2* const positions,
253 Length numberOfGlyphsToInsert )
257 void VisualModel::SetLines( const LineRun* const lines,
258 Length numberOfLines )
260 if( 0u == numberOfLines )
266 mLines.Resize( numberOfLines );
267 memcpy( mLines.Begin(), lines, numberOfLines * sizeof( LineRun ) );
271 Length VisualModel::GetNumberOfLines() const
273 return mLines.Count();
276 void VisualModel::GetLines( LineRun* lines,
278 Length numberOfLines ) const
280 memcpy( lines, mLines.Begin() + lineIndex, numberOfLines * sizeof( LineRun ) );
283 void VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
284 Length numberOfGlyphs,
285 LineIndex& firstLine,
286 Length& numberOfLines ) const
288 // Initialize the number of lines and the first line.
291 bool firstLineFound = false;
293 const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
295 // Traverse the lines and count those lines within the range of glyphs.
296 for( Vector<LineRun>::ConstIterator it = mLines.Begin(),
297 endIt = mLines.End();
301 const LineRun& line = *it;
303 if( ( line.glyphIndex + line.numberOfGlyphs > glyphIndex ) &&
304 ( lastGlyphIndex > line.glyphIndex ) )
306 firstLineFound = true;
309 else if( lastGlyphIndex <= line.glyphIndex )
311 // nothing else to do.
315 if( !firstLineFound )
322 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
323 GlyphIndex glyphIndex,
324 Length numberOfGlyphs ) const
326 LineIndex firstLine = 0u;
327 Length numberOfLines = 0u;
329 GetNumberOfLines( glyphIndex,
334 memcpy( lines, mLines.Begin() + firstLine, numberOfLines * sizeof( LineRun ) );
337 void VisualModel::ReplaceLines( GlyphIndex glyphIndex,
338 Length numberOfGlyphsToRemove,
339 const LineRun* const lines,
340 Length numberOfGlyphsToInsert )
344 void VisualModel::SetNaturalSize( const Vector2& size )
349 const Vector2& VisualModel::GetNaturalSize() const
354 void VisualModel::SetActualSize( const Vector2& size )
359 const Vector2& VisualModel::GetActualSize() const
364 VisualModel::~VisualModel()
368 VisualModel::VisualModel()
374 } // namespace Toolkit