TextModel - Update the color runs.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / dali-toolkit-test-utils / toolkit-text-model.cpp
1 /*
2  * Copyright (c) 2016 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 #include "toolkit-text-model.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/devel-api/text-abstraction/font-client.h>
22
23 // INTERNAL INCLUDES
24 #include <dali-toolkit/internal/text/bidirectional-support.h>
25 #include <dali-toolkit/internal/text/character-set-conversion.h>
26 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
27 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
28 #include <dali-toolkit/internal/text/metrics.h>
29 #include <dali-toolkit/internal/text/multi-language-support.h>
30 #include <dali-toolkit/internal/text/segmentation.h>
31 #include <dali-toolkit/internal/text/shaper.h>
32 #include <dali-toolkit/internal/text/text-controller-impl.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Text
41 {
42
43 /**
44  * @brief Frees previously allocated bidirectional resources.
45  *
46  * @param[in] bidirectionalLineInfo Bidirectional info per line.
47  * @param[in] index Index to the first line with bidirectional info to be freed.
48  */
49 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
50                                          uint32_t index )
51 {
52   // Free the allocated memory used to store the conversion table in the bidirectional line info run.
53   for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
54          endIt = bidirectionalLineInfo.End();
55        it != endIt;
56        ++it )
57   {
58     BidirectionalLineInfoRun& bidiLineInfo = *it;
59
60     free( bidiLineInfo.visualToLogicalMap );
61   }
62 }
63
64 /**
65  * @brief Clear all the model data except for LogicalModel::mText.
66  *
67  * @param[in] characterIndex Clear data starting from the index.
68  */
69 void ClearModelData( CharacterIndex characterIndex,
70                      LogicalModelPtr logicalModel,
71                      VisualModelPtr visualModel )
72 {
73   // n.b. This does not Clear the mText from mLogicalModel
74
75   // Frees previously allocated resources.
76   FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
77
78   logicalModel->mScriptRuns.Clear();
79   logicalModel->mFontRuns.Clear();
80   logicalModel->mWordBreakInfo.Clear();
81   logicalModel->mBidirectionalParagraphInfo.Clear();
82   logicalModel->mCharacterDirections.Clear();
83   logicalModel->mBidirectionalLineInfo.Clear();
84   logicalModel->mLogicalToVisualMap.Clear();
85   logicalModel->mVisualToLogicalMap.Clear();
86   visualModel->mGlyphs.Clear();
87   visualModel->mGlyphsToCharacters.Clear();
88   visualModel->mCharactersToGlyph.Clear();
89   visualModel->mCharactersPerGlyph.Clear();
90   visualModel->mGlyphsPerCharacter.Clear();
91   visualModel->mGlyphPositions.Clear();
92   visualModel->mLines.Clear();
93
94   visualModel->ClearCaches();
95 }
96
97 void CreateTextModel( const std::string& text,
98                       const Size& textArea,
99                       const Vector<FontDescriptionRun>& fontDescriptions,
100                       const LayoutOptions& options,
101                       Size& layoutSize,
102                       LogicalModelPtr logicalModel,
103                       VisualModelPtr visualModel )
104 {
105   // 1) Convert to utf32
106   Vector<Character>& utf32Characters = logicalModel->mText;
107   utf32Characters.Resize( text.size() );
108
109   const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
110                                                    text.size(),
111                                                    &utf32Characters[0u] );
112   utf32Characters.Resize( numberOfCharacters );
113
114   // 2) Set the break and paragraph info.
115   Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
116   lineBreakInfo.Resize( numberOfCharacters );
117
118   SetLineBreakInfo( utf32Characters,
119                     0u,
120                     numberOfCharacters,
121                     lineBreakInfo );
122
123   if( 0u == numberOfCharacters )
124   {
125     // Nothing else to do if the number of characters is zero.
126     return;
127   }
128
129   // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
130   Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
131   wordBreakInfo.Resize( numberOfCharacters );
132
133   SetWordBreakInfo( utf32Characters,
134                     0u,
135                     numberOfCharacters,
136                     wordBreakInfo );
137
138   // 3) Set the script info.
139   MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
140
141   Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
142   multilanguageSupport.SetScripts( utf32Characters,
143                                    0u,
144                                    numberOfCharacters,
145                                    scripts );
146
147   // 4) Set the font info
148   Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
149   fontDescriptionRuns = fontDescriptions;
150   Vector<FontRun>& validFonts = logicalModel->mFontRuns;
151
152   // The default font id.
153   FontDefaults fontDefaults;
154   fontDefaults.mFontDescription.family = "";
155   fontDefaults.familyDefined = true;
156   fontDefaults.mDefaultPointSize = 12.f;
157   fontDefaults.sizeDefined = true;
158
159   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
160   fontClient.SetDpi( 96u, 96u );
161
162   const FontId defaultFontId = fontDefaults.GetFontId( fontClient );
163
164   // Validates the fonts. If there is a character with no assigned font it sets a default one.
165   // After this call, fonts are validated.
166   multilanguageSupport.ValidateFonts( utf32Characters,
167                                       scripts,
168                                       fontDescriptionRuns,
169                                       defaultFontId,
170                                       0u,
171                                       numberOfCharacters,
172                                       validFonts );
173
174   // 5) Set the bidirectional info per paragraph.
175   Vector<Character> mirroredUtf32Characters;
176   bool textMirrored = false;
177
178   // Reserve some space for the vector of paragraph's bidirectional info.
179   Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
180
181   // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
182   SetBidirectionalInfo( utf32Characters,
183                         scripts,
184                         lineBreakInfo,
185                         0u,
186                         numberOfCharacters,
187                         bidirectionalInfo );
188
189   // Create the paragraph info.
190   logicalModel->CreateParagraphInfo( 0u,
191                                      numberOfCharacters );
192
193   // 6) Set character directions.
194   Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
195   if( 0u != bidirectionalInfo.Count() )
196   {
197     // Only set the character directions if there is right to left characters.
198     GetCharactersDirection( bidirectionalInfo,
199                             numberOfCharacters,
200                             0u,
201                             numberOfCharacters,
202                             characterDirections );
203
204
205     // This paragraph has right to left text. Some characters may need to be mirrored.
206     textMirrored = GetMirroredText( utf32Characters,
207                                     characterDirections,
208                                     bidirectionalInfo,
209                                     0u,
210                                     numberOfCharacters,
211                                     mirroredUtf32Characters );
212   }
213   else
214   {
215     // There is no right to left characters. Clear the directions vector.
216     characterDirections.Clear();
217   }
218
219   // 7) Shape the text.
220
221   Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
222   Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
223   Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
224   Vector<GlyphIndex> newParagraphGlyphs;
225
226   const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
227
228   ShapeText( textToShape,
229              lineBreakInfo,
230              scripts,
231              validFonts,
232              0u,
233              0u,
234              numberOfCharacters,
235              glyphs,
236              glyphsToCharactersMap,
237              charactersPerGlyph,
238              newParagraphGlyphs );
239
240   // Create the 'number of glyphs' per character and the glyph to character conversion tables.
241   visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
242   visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
243
244   const Length numberOfGlyphs = glyphs.Count();
245
246   // 8) Get the glyph metrics
247   MetricsPtr metrics = Metrics::New( fontClient );
248
249   GlyphInfo* glyphsBuffer = glyphs.Begin();
250   metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
251
252   // Update the width and advance of all new paragraph characters.
253   for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
254          endIt = newParagraphGlyphs.End();
255        it != endIt;
256        ++it )
257   {
258     const GlyphIndex index = *it;
259     GlyphInfo& glyph = *( glyphsBuffer + index );
260
261     glyph.xBearing = 0.f;
262     glyph.width = 0.f;
263     glyph.advance = 0.f;
264   }
265
266   // 9) Layout the text
267   LayoutEngine layoutEngine;
268   layoutEngine.SetMetrics( metrics );
269   layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
270
271   // Set the layout parameters.
272   const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
273   const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
274
275   LayoutParameters layoutParameters( textArea,
276                                      utf32Characters.Begin(),
277                                      lineBreakInfo.Begin(),
278                                      wordBreakInfo.Begin(),
279                                      ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
280                                      glyphs.Begin(),
281                                      glyphsToCharactersMap.Begin(),
282                                      charactersPerGlyph.Begin(),
283                                      charactersToGlyph.Begin(),
284                                      glyphsPerCharacter.Begin(),
285                                      numberOfGlyphs );
286
287   Vector<LineRun>& lines = visualModel->mLines;
288
289   Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
290   glyphPositions.Resize( numberOfGlyphs );
291
292   layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
293
294   // The initial glyph and the number of glyphs to layout.
295   layoutParameters.startGlyphIndex = 0u;
296   layoutParameters.numberOfGlyphs = numberOfGlyphs;
297   layoutParameters.startLineIndex = 0u;
298   layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
299
300   layoutEngine.LayoutText( layoutParameters,
301                            glyphPositions,
302                            lines,
303                            layoutSize );
304
305   // 10) Reorder the lines
306   if( 0u != bidirectionalInfo.Count() )
307   {
308     Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
309
310     // Get the lines
311     const Length numberOfLines = lines.Count();
312
313     // Reorder the lines.
314     bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
315     ReorderLines( bidirectionalInfo,
316                   0u,
317                   numberOfCharacters,
318                   lines,
319                   bidirectionalLineInfo );
320
321     // Set the bidirectional info per line into the layout parameters.
322     layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
323     layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
324
325     // Set the bidirectional info into the model.
326     logicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
327                                          layoutParameters.numberOfBidirectionalInfoRuns,
328                                          0u,
329                                          numberOfCharacters );
330
331     if( options.reorder )
332     {
333       // Re-layout the text. Reorder those lines with right to left characters.
334       layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
335                                              0u,
336                                              numberOfCharacters,
337                                              glyphPositions );
338     }
339   }
340
341   if( options.align )
342   {
343     layoutEngine.Align( textArea,
344                         0u,
345                         numberOfCharacters,
346                         lines );
347   }
348 }
349
350 } // namespace Text
351
352 } // namespace Toolkit
353
354 } // namespace Dali