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->mVisualToLogicalMap.Clear();
85 visualModel->mGlyphs.Clear();
86 visualModel->mGlyphsToCharacters.Clear();
87 visualModel->mCharactersToGlyph.Clear();
88 visualModel->mCharactersPerGlyph.Clear();
89 visualModel->mGlyphsPerCharacter.Clear();
90 visualModel->mGlyphPositions.Clear();
91 visualModel->mLines.Clear();
93 visualModel->ClearCaches();
96 void CreateTextModel( const std::string& text,
98 const Vector<FontDescriptionRun>& fontDescriptions,
99 const LayoutOptions& options,
101 LogicalModelPtr logicalModel,
102 VisualModelPtr visualModel )
104 // 1) Convert to utf32
105 Vector<Character>& utf32Characters = logicalModel->mText;
106 utf32Characters.Resize( text.size() );
108 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
110 &utf32Characters[0u] );
111 utf32Characters.Resize( numberOfCharacters );
113 // 2) Set the break and paragraph info.
114 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
115 lineBreakInfo.Resize( numberOfCharacters );
117 SetLineBreakInfo( utf32Characters,
122 if( 0u == numberOfCharacters )
124 // Nothing else to do if the number of characters is zero.
128 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
129 Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
130 wordBreakInfo.Resize( numberOfCharacters );
132 SetWordBreakInfo( utf32Characters,
137 // 3) Set the script info.
138 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
140 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
141 multilanguageSupport.SetScripts( utf32Characters,
146 // 4) Set the font info
147 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
148 fontDescriptionRuns = fontDescriptions;
149 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
151 // The default font id.
152 FontDefaults fontDefaults;
153 fontDefaults.mFontDescription.family = "";
154 fontDefaults.familyDefined = true;
155 fontDefaults.mDefaultPointSize = 12.f;
156 fontDefaults.sizeDefined = true;
158 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
159 fontClient.SetDpi( 96u, 96u );
161 const FontId defaultFontId = fontDefaults.GetFontId( fontClient );
163 // Validates the fonts. If there is a character with no assigned font it sets a default one.
164 // After this call, fonts are validated.
165 multilanguageSupport.ValidateFonts( utf32Characters,
173 // 5) Set the bidirectional info per paragraph.
174 Vector<Character> mirroredUtf32Characters;
175 bool textMirrored = false;
177 // Reserve some space for the vector of paragraph's bidirectional info.
178 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
180 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
181 SetBidirectionalInfo( utf32Characters,
188 // Create the paragraph info.
189 logicalModel->CreateParagraphInfo( 0u,
190 numberOfCharacters );
192 // 6) Set character directions.
193 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
194 if( 0u != bidirectionalInfo.Count() )
196 // Only set the character directions if there is right to left characters.
197 GetCharactersDirection( bidirectionalInfo,
201 characterDirections );
204 // This paragraph has right to left text. Some characters may need to be mirrored.
205 textMirrored = GetMirroredText( utf32Characters,
210 mirroredUtf32Characters );
214 // There is no right to left characters. Clear the directions vector.
215 characterDirections.Clear();
218 // 7) Shape the text.
220 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
221 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
222 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
223 Vector<GlyphIndex> newParagraphGlyphs;
225 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
227 ShapeText( textToShape,
235 glyphsToCharactersMap,
237 newParagraphGlyphs );
239 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
240 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
241 visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
243 const Length numberOfGlyphs = glyphs.Count();
245 // 8) Get the glyph metrics
246 MetricsPtr metrics = Metrics::New( fontClient );
248 GlyphInfo* glyphsBuffer = glyphs.Begin();
249 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
251 // Update the width and advance of all new paragraph characters.
252 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
253 endIt = newParagraphGlyphs.End();
257 const GlyphIndex index = *it;
258 GlyphInfo& glyph = *( glyphsBuffer + index );
260 glyph.xBearing = 0.f;
265 // 9) Layout the text
266 LayoutEngine layoutEngine;
267 layoutEngine.SetMetrics( metrics );
268 layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
270 // Set the layout parameters.
271 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
272 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
274 LayoutParameters layoutParameters( textArea,
275 utf32Characters.Begin(),
276 lineBreakInfo.Begin(),
277 wordBreakInfo.Begin(),
278 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
280 glyphsToCharactersMap.Begin(),
281 charactersPerGlyph.Begin(),
282 charactersToGlyph.Begin(),
283 glyphsPerCharacter.Begin(),
286 Vector<LineRun>& lines = visualModel->mLines;
288 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
289 glyphPositions.Resize( numberOfGlyphs );
291 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
293 // The initial glyph and the number of glyphs to layout.
294 layoutParameters.startGlyphIndex = 0u;
295 layoutParameters.numberOfGlyphs = numberOfGlyphs;
296 layoutParameters.startLineIndex = 0u;
297 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
299 layoutEngine.LayoutText( layoutParameters,
304 // 10) Reorder the lines
305 if( 0u != bidirectionalInfo.Count() )
307 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
310 const Length numberOfLines = lines.Count();
312 // Reorder the lines.
313 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
314 ReorderLines( bidirectionalInfo,
318 bidirectionalLineInfo );
320 // Set the bidirectional info per line into the layout parameters.
321 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
322 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
324 // Set the bidirectional info into the model.
325 logicalModel->SetVisualToLogicalMap( 0u,
326 numberOfCharacters );
328 if( options.reorder )
330 // Re-layout the text. Reorder those lines with right to left characters.
331 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
340 layoutEngine.Align( textArea,
349 } // namespace Toolkit