83fa45c0d6f496707152893c6a57bc6125cd9980
[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   visualModel->mGlyphs.Clear();
85   visualModel->mGlyphsToCharacters.Clear();
86   visualModel->mCharactersToGlyph.Clear();
87   visualModel->mCharactersPerGlyph.Clear();
88   visualModel->mGlyphsPerCharacter.Clear();
89   visualModel->mGlyphPositions.Clear();
90   visualModel->mLines.Clear();
91
92   visualModel->ClearCaches();
93 }
94
95 void CreateTextModel( const std::string& text,
96                       const Size& textArea,
97                       const Vector<FontDescriptionRun>& fontDescriptions,
98                       const LayoutOptions& options,
99                       Size& layoutSize,
100                       LogicalModelPtr logicalModel,
101                       VisualModelPtr visualModel )
102 {
103   // 1) Convert to utf32
104   Vector<Character>& utf32Characters = logicalModel->mText;
105   utf32Characters.Resize( text.size() );
106
107   const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
108                                                    text.size(),
109                                                    &utf32Characters[0u] );
110   utf32Characters.Resize( numberOfCharacters );
111
112   // 2) Set the break and paragraph info.
113   Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
114   lineBreakInfo.Resize( numberOfCharacters );
115
116   SetLineBreakInfo( utf32Characters,
117                     0u,
118                     numberOfCharacters,
119                     lineBreakInfo );
120
121   if( 0u == numberOfCharacters )
122   {
123     // Nothing else to do if the number of characters is zero.
124     return;
125   }
126
127   // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
128   Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
129   wordBreakInfo.Resize( numberOfCharacters );
130
131   SetWordBreakInfo( utf32Characters,
132                     0u,
133                     numberOfCharacters,
134                     wordBreakInfo );
135
136   // 3) Set the script info.
137   MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
138
139   Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
140   multilanguageSupport.SetScripts( utf32Characters,
141                                    0u,
142                                    numberOfCharacters,
143                                    scripts );
144
145   // 4) Set the font info
146   Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
147   fontDescriptionRuns = fontDescriptions;
148   Vector<FontRun>& validFonts = logicalModel->mFontRuns;
149
150   // The default font id.
151   FontDefaults fontDefaults;
152   fontDefaults.mFontDescription.family = "";
153   fontDefaults.familyDefined = true;
154   fontDefaults.mDefaultPointSize = 12.f;
155   fontDefaults.sizeDefined = true;
156
157   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
158   fontClient.SetDpi( 96u, 96u );
159
160   const FontId defaultFontId = fontDefaults.GetFontId( fontClient );
161
162   // Validates the fonts. If there is a character with no assigned font it sets a default one.
163   // After this call, fonts are validated.
164   multilanguageSupport.ValidateFonts( utf32Characters,
165                                       scripts,
166                                       fontDescriptionRuns,
167                                       defaultFontId,
168                                       0u,
169                                       numberOfCharacters,
170                                       validFonts );
171
172   // 5) Set the bidirectional info per paragraph.
173   Vector<Character> mirroredUtf32Characters;
174   bool textMirrored = false;
175
176   // Reserve some space for the vector of paragraph's bidirectional info.
177   Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
178
179   // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
180   SetBidirectionalInfo( utf32Characters,
181                         scripts,
182                         lineBreakInfo,
183                         0u,
184                         numberOfCharacters,
185                         bidirectionalInfo );
186
187   // Create the paragraph info.
188   logicalModel->CreateParagraphInfo( 0u,
189                                      numberOfCharacters );
190
191   // 6) Set character directions.
192   Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
193   if( 0u != bidirectionalInfo.Count() )
194   {
195     // Only set the character directions if there is right to left characters.
196     GetCharactersDirection( bidirectionalInfo,
197                             numberOfCharacters,
198                             0u,
199                             numberOfCharacters,
200                             characterDirections );
201
202
203     // This paragraph has right to left text. Some characters may need to be mirrored.
204     textMirrored = GetMirroredText( utf32Characters,
205                                     characterDirections,
206                                     bidirectionalInfo,
207                                     0u,
208                                     numberOfCharacters,
209                                     mirroredUtf32Characters );
210   }
211   else
212   {
213     // There is no right to left characters. Clear the directions vector.
214     characterDirections.Clear();
215   }
216
217   // 7) Shape the text.
218
219   Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
220   Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
221   Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
222   Vector<GlyphIndex> newParagraphGlyphs;
223
224   const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
225
226   ShapeText( textToShape,
227              lineBreakInfo,
228              scripts,
229              validFonts,
230              0u,
231              0u,
232              numberOfCharacters,
233              glyphs,
234              glyphsToCharactersMap,
235              charactersPerGlyph,
236              newParagraphGlyphs );
237
238   // Create the 'number of glyphs' per character and the glyph to character conversion tables.
239   visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
240   visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
241
242   const Length numberOfGlyphs = glyphs.Count();
243
244   // 8) Get the glyph metrics
245   MetricsPtr metrics = Metrics::New( fontClient );
246
247   GlyphInfo* glyphsBuffer = glyphs.Begin();
248   metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
249
250   // Update the width and advance of all new paragraph characters.
251   for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
252          endIt = newParagraphGlyphs.End();
253        it != endIt;
254        ++it )
255   {
256     const GlyphIndex index = *it;
257     GlyphInfo& glyph = *( glyphsBuffer + index );
258
259     glyph.xBearing = 0.f;
260     glyph.width = 0.f;
261     glyph.advance = 0.f;
262   }
263
264   // 9) Layout the text
265   LayoutEngine layoutEngine;
266   layoutEngine.SetMetrics( metrics );
267   layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
268
269   // Set the layout parameters.
270   const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
271   const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
272
273   LayoutParameters layoutParameters( textArea,
274                                      utf32Characters.Begin(),
275                                      lineBreakInfo.Begin(),
276                                      wordBreakInfo.Begin(),
277                                      ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
278                                      glyphs.Begin(),
279                                      glyphsToCharactersMap.Begin(),
280                                      charactersPerGlyph.Begin(),
281                                      charactersToGlyph.Begin(),
282                                      glyphsPerCharacter.Begin(),
283                                      numberOfGlyphs );
284
285   Vector<LineRun>& lines = visualModel->mLines;
286
287   Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
288   glyphPositions.Resize( numberOfGlyphs );
289
290   layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
291
292   // The initial glyph and the number of glyphs to layout.
293   layoutParameters.startGlyphIndex = 0u;
294   layoutParameters.numberOfGlyphs = numberOfGlyphs;
295   layoutParameters.startLineIndex = 0u;
296   layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
297
298   layoutEngine.LayoutText( layoutParameters,
299                            glyphPositions,
300                            lines,
301                            layoutSize );
302
303   // 10) Reorder the lines
304   if( 0u != bidirectionalInfo.Count() )
305   {
306     Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
307
308     // Get the lines
309     const Length numberOfLines = lines.Count();
310
311     // Reorder the lines.
312     bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
313     ReorderLines( bidirectionalInfo,
314                   0u,
315                   numberOfCharacters,
316                   lines,
317                   bidirectionalLineInfo );
318
319     // Set the bidirectional info per line into the layout parameters.
320     layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
321     layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
322
323     if( options.reorder )
324     {
325       // Re-layout the text. Reorder those lines with right to left characters.
326       layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
327                                              0u,
328                                              numberOfCharacters,
329                                              glyphPositions );
330     }
331   }
332
333   if( options.align )
334   {
335     layoutEngine.Align( textArea,
336                         0u,
337                         numberOfCharacters,
338                         lines );
339   }
340 }
341
342 } // namespace Text
343
344 } // namespace Toolkit
345
346 } // namespace Dali