TextController fix.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / visual-model.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/visual-model.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
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>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Text
36 {
37
38 struct VisualModel::Impl
39 {
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.
45
46   Size                   mNaturalSize;
47   Size                   mActualSize;
48 };
49
50 VisualModelPtr VisualModel::New()
51 {
52   return VisualModelPtr( new VisualModel() );
53 }
54
55 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
56                              const CharacterIndex* characterIndices,
57                              const Length* charactersPerGlyph,
58                              Length numberOfGlyphs )
59 {
60   Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
61   modelGlyphs.Resize( numberOfGlyphs );
62   memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
63
64   Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
65   modelGlyphsToCharacters.Resize( numberOfGlyphs );
66   memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
67
68   Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
69   modelCharactersPerGlyph.Resize( numberOfGlyphs );
70   memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
71
72   // Build the characters to glyph conversion table.
73   Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
74
75   // 1) Reserve some space for the characters to avoid reallocations.
76   modelCharactersToGlyph.Reserve( static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f ) );
77
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();
83        it != endIt;
84        ++it, ++glyphIndex )
85   {
86     const Length numberOfCharacters = *it;
87
88     for( Length index = 0u; index < numberOfCharacters; ++index, ++totalNumberOfCharacters )
89     {
90       modelCharactersToGlyph.PushBack( glyphIndex );
91     }
92   }
93 }
94
95 Length VisualModel::GetNumberOfGlyphs() const
96 {
97   return mImpl->mGlyphs.Count();
98 }
99
100 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
101                              GlyphIndex glyphIndex,
102                              Length numberOfGlyphs ) const
103 {
104   Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
105   memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
106 }
107
108 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
109 {
110   return mImpl->mGlyphs[glyphIndex];
111 }
112
113 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
114 {
115   return mImpl->mGlyphsToCharacters[glyphIndex];
116 }
117
118 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
119 {
120   return mImpl->mCharactersPerGlyph[glyphIndex];
121 }
122
123 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
124 {
125   return mImpl->mCharactersToGlyph[characterIndex];
126 }
127
128 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
129                                           CharacterIndex characterIndex,
130                                           Length numberOfCharacters ) const
131 {
132   Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
133   memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
134 }
135
136 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
137                                             GlyphIndex glyphIndex,
138                                             Length numberOfGlyphs ) const
139 {
140   Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
141   memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
142 }
143
144 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
145                                           GlyphIndex glyphIndex,
146                                           Length numberOfGlyphs ) const
147 {
148   Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
149   memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
150 }
151
152 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
153                                      Length numberOfGlyphs )
154 {
155   Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
156   modelPositions.Resize( numberOfGlyphs );
157   memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
158 }
159
160 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
161                                      GlyphIndex glyphIndex,
162                                      Length numberOfGlyphs ) const
163 {
164   Vector<Vector2> modelPositions = mImpl->mGlyphPositions;
165   memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
166 }
167
168 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
169 {
170   return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
171 }
172
173 void VisualModel::SetLines( const LineRun* const lines,
174                             Length numberOfLines )
175 {
176 }
177
178 Length VisualModel::GetNumberOfLines() const
179 {
180   return 0u;
181 }
182
183 void VisualModel::GetLines( LineRun* lines,
184                             LineIndex lineIndex,
185                             Length numberOfLines ) const
186 {
187 }
188
189 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
190                                       Length numberOfGlyphs ) const
191 {
192   return 0u;
193 }
194
195 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
196                                         GlyphIndex glyphIndex,
197                                         Length numberOfGlyphs ) const
198 {
199 }
200
201 void VisualModel::SetNaturalSize( const Vector2& size  )
202 {
203   mImpl->mNaturalSize = size;
204 }
205
206 const Vector2& VisualModel::GetNaturalSize() const
207 {
208   return mImpl->mNaturalSize;
209 }
210
211 void VisualModel::SetActualSize( const Vector2& size )
212 {
213   mImpl->mActualSize = size;
214 }
215
216 const Vector2& VisualModel::GetActualSize() const
217 {
218   return mImpl->mActualSize;
219 }
220
221 VisualModel::~VisualModel()
222 {
223   delete mImpl;
224 }
225
226 VisualModel::VisualModel()
227 : mImpl( NULL )
228 {
229   mImpl = new VisualModel::Impl();
230 }
231
232 } // namespace Text
233
234 } // namespace Toolkit
235
236 } // namespace Dali