55d87972420e22ddd14abcf1665af63a291d560b
[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 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/math/vector2.h>
25
26 // INTERNAL INCLUDES
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 /**
39  * @brief caches some temporary values of the GetNumberOfLines( glyphIndex, numberOfGlyphs ) operation
40  * as they are going to be used in the GetLinesOfGlyphRange() call.
41  */
42 struct GetLineCache
43 {
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.
48 };
49
50 struct VisualModel::Impl
51 {
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<Length>         mGlyphsPerCharacter; ///< For each character, the number of glyphs that are shaped.
57   Vector<Vector2>        mGlyphPositions;     ///< For each glyph, the position.
58   Vector<LineRun>        mLines;              ///< The laid out lines.
59
60   Size                   mNaturalSize;        ///< Size of the text with no line wrapping.
61   Size                   mActualSize;         ///< Size of the laid-out text considering the layout properties set.
62
63   GetLineCache           mGetLineCache;       ///< Caches the GetNumberOfLines( glyphIndex, numberOfGlyphs ) operation.
64 };
65
66 VisualModelPtr VisualModel::New()
67 {
68   return VisualModelPtr( new VisualModel() );
69 }
70
71 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
72                              const CharacterIndex* characterIndices,
73                              const Length* charactersPerGlyph,
74                              Length numberOfGlyphs )
75 {
76   Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
77   Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
78   Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
79   Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
80   Vector<Length>& modelGlyphsPerCharacter = mImpl->mGlyphsPerCharacter;
81
82   if( 0u == numberOfGlyphs )
83   {
84     modelGlyphs.Clear();
85     modelGlyphsToCharacters.Clear();
86     modelCharactersToGlyph.Clear();
87     modelCharactersPerGlyph.Clear();
88     modelGlyphsPerCharacter.Clear();
89   }
90   else
91   {
92     if( NULL != glyphs )
93     {
94       modelGlyphs.Resize( numberOfGlyphs );
95       memcpy( modelGlyphs.Begin(), glyphs, numberOfGlyphs * sizeof( GlyphInfo ) );
96     }
97
98     if( NULL != characterIndices )
99     {
100       modelGlyphsToCharacters.Resize( numberOfGlyphs );
101       memcpy( modelGlyphsToCharacters.Begin(), characterIndices, numberOfGlyphs * sizeof( CharacterIndex ) );
102     }
103
104     if( NULL != charactersPerGlyph )
105     {
106       modelCharactersPerGlyph.Resize( numberOfGlyphs );
107       memcpy( modelCharactersPerGlyph.Begin(), charactersPerGlyph, numberOfGlyphs * sizeof( Length ) );
108
109       // Build the characters to glyph conversion table.
110
111       // 1) Reserve some space for the characters to avoid reallocations.
112       const Length numberOfCharacters = static_cast<Length> ( static_cast<float>( numberOfGlyphs ) * 1.3f );
113       modelCharactersToGlyph.Reserve( numberOfCharacters );
114       modelGlyphsPerCharacter.Reserve( numberOfCharacters );
115
116       // 2) Traverse the glyphs and set the glyph indices and the glyphs per character.
117
118       // The number of 'characters per glyph' equal to zero.
119       Length zeroCharactersPerGlyph = 0u;
120
121       // Index to the glyph.
122       GlyphIndex glyphIndex = 0u;
123       for( Vector<Length>::ConstIterator it = modelCharactersPerGlyph.Begin(),
124              endIt = modelCharactersPerGlyph.End();
125            it != endIt;
126            ++it, ++glyphIndex )
127       {
128         const Length numberOfCharacters = *it;
129
130         // Set the glyph indices.
131         for( Length index = 0u; index < numberOfCharacters; ++index )
132         {
133           modelCharactersToGlyph.PushBack( glyphIndex );
134         }
135
136         // Set the glyphs per character.
137         if( 0u == numberOfCharacters )
138         {
139           ++zeroCharactersPerGlyph;
140         }
141         else
142         {
143           const Length numberOfZeroGlyphsPerCharacter = ( numberOfCharacters - 1u );
144           for( Length zeroIndex = 0u; zeroIndex < numberOfZeroGlyphsPerCharacter ; ++zeroIndex )
145           {
146             modelGlyphsPerCharacter.PushBack( 0u );
147           }
148
149           modelGlyphsPerCharacter.PushBack( 1u + zeroCharactersPerGlyph );
150
151           zeroCharactersPerGlyph = 0u;
152         }
153       }
154     }
155   }
156 }
157
158 Length VisualModel::GetNumberOfGlyphs() const
159 {
160   return mImpl->mGlyphs.Count();
161 }
162
163 void VisualModel::GetGlyphs( GlyphInfo* glyphs,
164                              GlyphIndex glyphIndex,
165                              Length numberOfGlyphs ) const
166 {
167   const Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
168   memcpy( glyphs, modelGlyphs.Begin() + glyphIndex, numberOfGlyphs * sizeof( GlyphInfo ) );
169 }
170
171 const GlyphInfo& VisualModel::GetGlyphInfo( GlyphIndex glyphIndex ) const
172 {
173   return mImpl->mGlyphs[glyphIndex];
174 }
175
176 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
177 {
178   return mImpl->mGlyphsToCharacters[glyphIndex];
179 }
180
181 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
182 {
183   return mImpl->mCharactersPerGlyph[glyphIndex];
184 }
185
186 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
187 {
188   return mImpl->mCharactersToGlyph[characterIndex];
189 }
190
191 void VisualModel::GetCharacterToGlyphMap( GlyphIndex* characterToGlyphMap,
192                                           CharacterIndex characterIndex,
193                                           Length numberOfCharacters ) const
194 {
195   const Vector<GlyphIndex>& modelCharactersToGlyph = mImpl->mCharactersToGlyph;
196   memcpy( characterToGlyphMap, modelCharactersToGlyph.Begin() + characterIndex, numberOfCharacters * sizeof( GlyphIndex ) );
197 }
198
199 void VisualModel::GetGlyphToCharacterMap( CharacterIndex* glyphToCharacter,
200                                           GlyphIndex glyphIndex,
201                                           Length numberOfGlyphs ) const
202 {
203   const Vector<CharacterIndex>& modelGlyphsToCharacters = mImpl->mGlyphsToCharacters;
204   memcpy( glyphToCharacter, modelGlyphsToCharacters.Begin() + glyphIndex, numberOfGlyphs * sizeof( CharacterIndex ) );
205 }
206
207 void VisualModel::GetCharactersPerGlyphMap( Length* charactersPerGlyph,
208                                             GlyphIndex glyphIndex,
209                                             Length numberOfGlyphs ) const
210 {
211   const Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
212   memcpy( charactersPerGlyph, modelCharactersPerGlyph.Begin() + glyphIndex, numberOfGlyphs * sizeof( Length ) );
213 }
214
215 void VisualModel::GetGlyphsPerCharacterMap( Length* glyphsPerCharacter,
216                                             CharacterIndex characterIndex,
217                                             Length numberOfCharacters ) const
218 {
219   const Vector<Length>& modelGlyphsPerCharacter = mImpl->mGlyphsPerCharacter;
220   memcpy( glyphsPerCharacter, modelGlyphsPerCharacter.Begin() + characterIndex, numberOfCharacters * sizeof( Length ) );
221 }
222
223 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
224                                      Length numberOfGlyphs )
225 {
226   Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
227   if( 0u == numberOfGlyphs )
228   {
229     modelPositions.Clear();
230   }
231   else
232   {
233     modelPositions.Resize( numberOfGlyphs );
234     memcpy( modelPositions.Begin(), glyphPositions, numberOfGlyphs * sizeof( Vector2 ) );
235   }
236 }
237
238 Length VisualModel::GetNumberOfGlyphPositions() const
239 {
240   return mImpl->mGlyphPositions.Count();
241 }
242
243 void VisualModel::GetGlyphPositions( Vector2* glyphPositions,
244                                      GlyphIndex glyphIndex,
245                                      Length numberOfGlyphs ) const
246 {
247   const Vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
248   memcpy( glyphPositions, modelPositions.Begin() + glyphIndex, numberOfGlyphs * sizeof( Vector2 ) );
249 }
250
251 const Vector2& VisualModel::GetGlyphPosition( GlyphIndex glyphIndex ) const
252 {
253   return *( mImpl->mGlyphPositions.Begin() + glyphIndex );
254 }
255
256 void VisualModel::SetLines( const LineRun* const lines,
257                             Length numberOfLines )
258 {
259   Vector<LineRun>& modelLines = mImpl->mLines;
260   GetLineCache& lineCache = mImpl->mGetLineCache;
261
262   if( 0u == numberOfLines )
263   {
264     modelLines.Clear();
265   }
266   else
267   {
268     modelLines.Resize( numberOfLines );
269     memcpy( modelLines.Begin(), lines, numberOfLines * sizeof( LineRun ) );
270   }
271
272   // Clear the get line cache.
273   lineCache.glyphIndex = 0u;
274   lineCache.numberOfGlyphs = 0u;
275   lineCache.firstLine = 0u;
276   lineCache.numberOfLines = 0u;
277 }
278
279 Length VisualModel::GetNumberOfLines() const
280 {
281   return mImpl->mLines.Count();
282 }
283
284 void VisualModel::GetLines( LineRun* lines,
285                             LineIndex lineIndex,
286                             Length numberOfLines ) const
287 {
288   const Vector<LineRun>& modelLines = mImpl->mLines;
289   memcpy( lines, modelLines.Begin() + lineIndex, numberOfLines * sizeof( LineRun ) );
290 }
291
292 Length VisualModel::GetNumberOfLines( GlyphIndex glyphIndex,
293                                       Length numberOfGlyphs ) const
294 {
295   // If is likely the user query consecutively for the number of lines with the same
296   // glyph index and number of glyphs, use the cache could be considered.
297   GetLineCache& lineCache = mImpl->mGetLineCache;
298
299   // Cache the glyph index and number of glyphs to be used in the GetLinesOfGlyphRange().
300   lineCache.glyphIndex = glyphIndex;
301   lineCache.numberOfGlyphs = numberOfGlyphs;
302
303   // Check first if the query is for the total number of glyphs.
304   const Length totalNumberOfGlyphs = mImpl->mGlyphs.Count();
305
306   if( ( 0u == glyphIndex ) &&
307       ( totalNumberOfGlyphs == numberOfGlyphs ) )
308   {
309     lineCache.firstLine = 0u;
310     lineCache.numberOfLines = mImpl->mLines.Count();
311
312     return lineCache.numberOfLines;
313   }
314
315   // Initialize the number of lines and the first line.
316   lineCache.numberOfLines = 0u;
317   lineCache.firstLine = 0u;
318   bool firstLineFound = false;
319
320   const Vector<LineRun>& modelLines = mImpl->mLines;
321   const GlyphIndex lastGlyphIndex = glyphIndex + numberOfGlyphs;
322
323   // Traverse the lines and count those lines within the range of glyphs.
324   for( Vector<LineRun>::ConstIterator it = modelLines.Begin(),
325          endIt = modelLines.End();
326        it != endIt;
327        ++it )
328   {
329     const LineRun& line = *it;
330
331     if( ( line.glyphIndex + line.numberOfGlyphs > glyphIndex ) &&
332         ( lastGlyphIndex > line.glyphIndex ) )
333     {
334       firstLineFound = true;
335       ++lineCache.numberOfLines;
336     }
337     else if( lastGlyphIndex <= line.glyphIndex )
338     {
339       // nothing else to do.
340       break;
341     }
342
343     if( !firstLineFound )
344     {
345       ++lineCache.firstLine;
346     }
347   }
348
349   return lineCache.numberOfLines;
350 }
351
352 void VisualModel::GetLinesOfGlyphRange( LineRun* lines,
353                                         GlyphIndex glyphIndex,
354                                         Length numberOfGlyphs ) const
355 {
356   const Vector<LineRun>& modelLines = mImpl->mLines;
357   GetLineCache& lineCache = mImpl->mGetLineCache;
358
359   if( ( glyphIndex != lineCache.glyphIndex ) ||
360       ( numberOfGlyphs != lineCache.numberOfGlyphs ) )
361   {
362     GetNumberOfLines( glyphIndex,
363                       numberOfGlyphs );
364   }
365
366   memcpy( lines, modelLines.Begin() + lineCache.firstLine, lineCache.numberOfLines * sizeof( LineRun ) );
367 }
368
369 void VisualModel::SetNaturalSize( const Vector2& size  )
370 {
371   mImpl->mNaturalSize = size;
372 }
373
374 const Vector2& VisualModel::GetNaturalSize() const
375 {
376   return mImpl->mNaturalSize;
377 }
378
379 void VisualModel::SetActualSize( const Vector2& size )
380 {
381   mImpl->mActualSize = size;
382 }
383
384 const Vector2& VisualModel::GetActualSize() const
385 {
386   return mImpl->mActualSize;
387 }
388
389 VisualModel::~VisualModel()
390 {
391   delete mImpl;
392 }
393
394 VisualModel::VisualModel()
395 : mImpl( NULL )
396 {
397   mImpl = new VisualModel::Impl();
398 }
399
400 } // namespace Text
401
402 } // namespace Toolkit
403
404 } // namespace Dali