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/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>
44 * @brief Frees previously allocated bidirectional resources.
46 * @param[in] bidirectionalLineInfo Bidirectional info per line.
47 * @param[in] index Index to the first line with bidirectional info to be freed.
49 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
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();
58 BidirectionalLineInfoRun& bidiLineInfo = *it;
60 free( bidiLineInfo.visualToLogicalMap );
65 * @brief Clear all the model data except for LogicalModel::mText.
67 * @param[in] characterIndex Clear data starting from the index.
69 void ClearModelData( CharacterIndex characterIndex,
70 LogicalModelPtr logicalModel,
71 VisualModelPtr visualModel )
73 // n.b. This does not Clear the mText from mLogicalModel
75 // Frees previously allocated resources.
76 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
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();
94 visualModel->ClearCaches();
97 void CreateTextModel( const std::string& text,
99 const Vector<FontDescriptionRun>& fontDescriptions,
100 const LayoutOptions& options,
102 LogicalModelPtr logicalModel,
103 VisualModelPtr visualModel )
105 // 1) Convert to utf32
106 Vector<Character>& utf32Characters = logicalModel->mText;
107 utf32Characters.Resize( text.size() );
109 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
111 &utf32Characters[0u] );
112 utf32Characters.Resize( numberOfCharacters );
114 // 2) Set the break and paragraph info.
115 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
116 lineBreakInfo.Resize( numberOfCharacters );
118 SetLineBreakInfo( utf32Characters,
123 if( 0u == numberOfCharacters )
125 // Nothing else to do if the number of characters is zero.
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 );
133 SetWordBreakInfo( utf32Characters,
138 // 3) Set the script info.
139 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
141 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
142 multilanguageSupport.SetScripts( utf32Characters,
147 // 4) Set the font info
148 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
149 fontDescriptionRuns = fontDescriptions;
150 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
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;
159 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
160 fontClient.SetDpi( 96u, 96u );
162 const FontId defaultFontId = fontDefaults.GetFontId( fontClient );
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,
174 // 5) Set the bidirectional info per paragraph.
175 Vector<Character> mirroredUtf32Characters;
176 bool textMirrored = false;
178 // Reserve some space for the vector of paragraph's bidirectional info.
179 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
181 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
182 SetBidirectionalInfo( utf32Characters,
189 // Create the paragraph info.
190 logicalModel->CreateParagraphInfo( 0u,
191 numberOfCharacters );
193 // 6) Set character directions.
194 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
195 if( 0u != bidirectionalInfo.Count() )
197 // Only set the character directions if there is right to left characters.
198 GetCharactersDirection( bidirectionalInfo,
202 characterDirections );
205 // This paragraph has right to left text. Some characters may need to be mirrored.
206 textMirrored = GetMirroredText( utf32Characters,
211 mirroredUtf32Characters );
215 // There is no right to left characters. Clear the directions vector.
216 characterDirections.Clear();
219 // 7) Shape the text.
221 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
222 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
223 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
224 Vector<GlyphIndex> newParagraphGlyphs;
226 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
228 ShapeText( textToShape,
236 glyphsToCharactersMap,
238 newParagraphGlyphs );
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 );
244 const Length numberOfGlyphs = glyphs.Count();
246 // 8) Get the glyph metrics
247 MetricsPtr metrics = Metrics::New( fontClient );
249 GlyphInfo* glyphsBuffer = glyphs.Begin();
250 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
252 // Update the width and advance of all new paragraph characters.
253 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
254 endIt = newParagraphGlyphs.End();
258 const GlyphIndex index = *it;
259 GlyphInfo& glyph = *( glyphsBuffer + index );
261 glyph.xBearing = 0.f;
266 // 9) Layout the text
267 LayoutEngine layoutEngine;
268 layoutEngine.SetMetrics( metrics );
269 layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
271 // Set the layout parameters.
272 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
273 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
275 LayoutParameters layoutParameters( textArea,
276 utf32Characters.Begin(),
277 lineBreakInfo.Begin(),
278 wordBreakInfo.Begin(),
279 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
281 glyphsToCharactersMap.Begin(),
282 charactersPerGlyph.Begin(),
283 charactersToGlyph.Begin(),
284 glyphsPerCharacter.Begin(),
287 Vector<LineRun>& lines = visualModel->mLines;
289 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
290 glyphPositions.Resize( numberOfGlyphs );
292 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
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();
300 layoutEngine.LayoutText( layoutParameters,
305 // 10) Reorder the lines
306 if( 0u != bidirectionalInfo.Count() )
308 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
311 const Length numberOfLines = lines.Count();
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,
319 bidirectionalLineInfo );
321 // Set the bidirectional info per line into the layout parameters.
322 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
323 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
325 // Set the bidirectional info into the model.
326 logicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
327 layoutParameters.numberOfBidirectionalInfoRuns,
329 numberOfCharacters );
331 if( options.reorder )
333 // Re-layout the text. Reorder those lines with right to left characters.
334 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
343 layoutEngine.Align( textArea,
352 } // namespace Toolkit