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>
38 struct VisualModel::Impl
40 Vector<GlyphInfo> mGlyphs; ///< For each glyph, the font's id, glyph's index within the font and glyph's metrics.
41 Vector<CharacterIndex> mGlyphsToCharacters; ///< For each glyph, the index of the first character.
42 Vector<GlyphIndex> mCharactersToGlyph; ///< For each character, the index of the first glyph.
43 Vector<Length> mCharactersPerGlyph; ///< For each glyph, the number of characters that form the glyph.
44 Vector<Vector2> mGlyphPositions; ///< For each glyph, the position.
50 VisualModelPtr VisualModel::New()
52 return VisualModelPtr( new VisualModel() );
55 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
56 const CharacterIndex* characterIndices,
57 const Length* charactersPerGlyph,
58 Length numberOfGlyphs )
60 Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
61 modelGlyphs.Resize( numberOfGlyphs );
62 memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
64 Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
65 modelGlyphsToCharacters.Resize( numberOfGlyphs );
66 memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
68 Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
69 modelCharactersPerGlyph.Resize( numberOfGlyphs );
70 memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
72 // Build the characters to glyph conversion table.
73 Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
75 // 1) Reserve some space for the characters to avoid reallocations.
76 modelCharactersToGlyph.Reserve( static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f ) );
78 // 2) Traverse the glyphs and set the glyph indices.
79 GlyphIndex glyphIndex = 0u;
80 Length totalNumberOfCharacters = 0u;
81 for( Vector<Length>::ConstIterator it = modelCharactersPerGlyph.Begin(),
82 endIt = modelCharactersPerGlyph.End();
86 const Length numberOfCharacters = *it;
88 for( Length index = 0u; index < numberOfCharacters; ++index, ++totalNumberOfCharacters )
90 modelCharactersToGlyph.PushBack( glyphIndex );
95 Length VisualModel::GetNumberOfGlyphs() const
97 return mImpl->mGlyphs.Count();
100 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
101 GlyphIndex glyphIndex,
102 Length numberOfGlyphs ) const
104 Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
105 memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
108 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
110 return mImpl->mGlyphs[glyphIndex];
113 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
115 return mImpl->mGlyphsToCharacters[glyphIndex];
118 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
120 return mImpl->mCharactersPerGlyph[glyphIndex];
123 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
125 return mImpl->mCharactersToGlyph[characterIndex];
128 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
129 CharacterIndex characterIndex,
130 Length numberOfCharacters ) const
132 Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
133 memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
136 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
137 GlyphIndex glyphIndex,
138 Length numberOfGlyphs ) const
140 Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
141 memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
144 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
145 GlyphIndex glyphIndex,
146 Length numberOfGlyphs ) const
148 Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
149 memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
152 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
153 Length numberOfGlyphs )
155 Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
156 modelPositions.Resize( numberOfGlyphs );
157 memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
160 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
161 GlyphIndex glyphIndex,
162 Length numberOfGlyphs ) const
164 Vector<Vector2> modelPositions = mImpl->mGlyphPositions;
165 memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
168 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
170 return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
173 void VisualModel::SetLines( const LineRun* const lines,
174 Length numberOfLines )
178 Length VisualModel::GetNumberOfLines() const
183 void VisualModel::GetLines( LineRun* lines,
185 Length numberOfLines ) const
189 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
190 Length numberOfGlyphs ) const
195 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
196 GlyphIndex glyphIndex,
197 Length numberOfGlyphs ) const
201 void VisualModel::SetNaturalSize( const Vector2& size )
203 mImpl->mNaturalSize = size;
206 const Vector2& VisualModel::GetNaturalSize() const
208 return mImpl->mNaturalSize;
211 void VisualModel::SetActualSize( const Vector2& size )
213 mImpl->mActualSize = size;
216 const Vector2& VisualModel::GetActualSize() const
218 return mImpl->mActualSize;
221 VisualModel::~VisualModel()
226 VisualModel::VisualModel()
229 mImpl = new VisualModel::Impl();
234 } // namespace Toolkit