2 * Copyright (c) 2016 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.
18 #include "toolkit-text-model.h"
21 #include <dali/devel-api/text-abstraction/font-client.h>
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/multi-language-support.h>
29 #include <dali-toolkit/internal/text/segmentation.h>
30 #include <dali-toolkit/internal/text/shaper.h>
31 #include <dali-toolkit/internal/text/text-controller-impl.h>
43 * @brief Frees previously allocated bidirectional resources.
45 * @param[in] bidirectionalLineInfo Bidirectional info per line.
46 * @param[in] index Index to the first line with bidirectional info to be freed.
48 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
51 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
52 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
53 endIt = bidirectionalLineInfo.End();
57 BidirectionalLineInfoRun& bidiLineInfo = *it;
59 free( bidiLineInfo.visualToLogicalMap );
64 * @brief Clear all the model data except for LogicalModel::mText.
66 * @param[in] characterIndex Clear data starting from the index.
68 void ClearModelData( CharacterIndex characterIndex,
69 LogicalModelPtr logicalModel,
70 VisualModelPtr visualModel )
72 // n.b. This does not Clear the mText from mLogicalModel
74 // Frees previously allocated resources.
75 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
77 logicalModel->mScriptRuns.Clear();
78 logicalModel->mFontRuns.Clear();
79 logicalModel->mWordBreakInfo.Clear();
80 logicalModel->mBidirectionalParagraphInfo.Clear();
81 logicalModel->mCharacterDirections.Clear();
82 logicalModel->mBidirectionalLineInfo.Clear();
83 visualModel->mGlyphs.Clear();
84 visualModel->mGlyphsToCharacters.Clear();
85 visualModel->mCharactersToGlyph.Clear();
86 visualModel->mCharactersPerGlyph.Clear();
87 visualModel->mGlyphsPerCharacter.Clear();
88 visualModel->mGlyphPositions.Clear();
89 visualModel->mLines.Clear();
91 visualModel->ClearCaches();
94 void CreateTextModel( const std::string& text,
96 const Vector<FontDescriptionRun>& fontDescriptions,
97 const LayoutOptions& options,
99 LogicalModelPtr& logicalModel,
100 VisualModelPtr& visualModel,
101 MetricsPtr& metrics )
103 logicalModel = LogicalModel::New();
104 visualModel = VisualModel::New();
106 // 1) Convert to utf32
107 Vector<Character>& utf32Characters = logicalModel->mText;
108 utf32Characters.Resize( text.size() );
110 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
112 &utf32Characters[0u] );
113 utf32Characters.Resize( numberOfCharacters );
115 // 2) Set the break and paragraph info.
116 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
117 lineBreakInfo.Resize( numberOfCharacters );
119 SetLineBreakInfo( utf32Characters,
124 if( 0u == numberOfCharacters )
126 // Nothing else to do if the number of characters is zero.
130 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
131 Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
132 wordBreakInfo.Resize( numberOfCharacters );
134 SetWordBreakInfo( utf32Characters,
139 // 3) Set the script info.
140 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
142 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
143 multilanguageSupport.SetScripts( utf32Characters,
148 // 4) Set the font info
149 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
150 fontDescriptionRuns = fontDescriptions;
151 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
153 // The default font description.
154 TextAbstraction::FontDescription fontDescription;
156 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
157 fontClient.SetDpi( 96u, 96u );
159 // Validates the fonts. If there is a character with no assigned font it sets a default one.
160 // After this call, fonts are validated.
161 multilanguageSupport.ValidateFonts( utf32Characters,
165 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
170 // 5) Set the bidirectional info per paragraph.
171 Vector<Character> mirroredUtf32Characters;
172 bool textMirrored = false;
174 // Reserve some space for the vector of paragraph's bidirectional info.
175 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
177 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
178 SetBidirectionalInfo( utf32Characters,
185 // Create the paragraph info.
186 logicalModel->CreateParagraphInfo( 0u,
187 numberOfCharacters );
189 // 6) Set character directions.
190 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
191 if( 0u != bidirectionalInfo.Count() )
193 // Only set the character directions if there is right to left characters.
194 GetCharactersDirection( bidirectionalInfo,
198 characterDirections );
201 // This paragraph has right to left text. Some characters may need to be mirrored.
202 textMirrored = GetMirroredText( utf32Characters,
207 mirroredUtf32Characters );
211 // There is no right to left characters. Clear the directions vector.
212 characterDirections.Clear();
215 // 7) Shape the text.
217 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
218 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
219 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
220 Vector<GlyphIndex> newParagraphGlyphs;
222 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
224 ShapeText( textToShape,
232 glyphsToCharactersMap,
234 newParagraphGlyphs );
236 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
237 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
238 visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
240 const Length numberOfGlyphs = glyphs.Count();
242 // 8) Get the glyph metrics
243 metrics = Metrics::New( fontClient );
245 GlyphInfo* glyphsBuffer = glyphs.Begin();
246 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
248 // Update the width and advance of all new paragraph characters.
249 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
250 endIt = newParagraphGlyphs.End();
254 const GlyphIndex index = *it;
255 GlyphInfo& glyph = *( glyphsBuffer + index );
257 glyph.xBearing = 0.f;
262 // 9) Layout the text
263 LayoutEngine layoutEngine;
264 layoutEngine.SetMetrics( metrics );
265 layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
267 // Set the layout parameters.
268 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
269 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
271 LayoutParameters layoutParameters( textArea,
272 utf32Characters.Begin(),
273 lineBreakInfo.Begin(),
274 wordBreakInfo.Begin(),
275 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
277 glyphsToCharactersMap.Begin(),
278 charactersPerGlyph.Begin(),
279 charactersToGlyph.Begin(),
280 glyphsPerCharacter.Begin(),
283 Vector<LineRun>& lines = visualModel->mLines;
285 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
286 glyphPositions.Resize( numberOfGlyphs );
288 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
290 // The initial glyph and the number of glyphs to layout.
291 layoutParameters.startGlyphIndex = 0u;
292 layoutParameters.numberOfGlyphs = numberOfGlyphs;
293 layoutParameters.startLineIndex = 0u;
294 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
296 layoutEngine.LayoutText( layoutParameters,
301 // 10) Reorder the lines
302 if( 0u != bidirectionalInfo.Count() )
304 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
307 const Length numberOfLines = lines.Count();
309 // Reorder the lines.
310 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
311 ReorderLines( bidirectionalInfo,
315 bidirectionalLineInfo );
317 // Set the bidirectional info per line into the layout parameters.
318 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
319 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
321 if( options.reorder )
323 // Re-layout the text. Reorder those lines with right to left characters.
324 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
333 layoutEngine.Align( textArea,
342 } // namespace Toolkit