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();
93 visualModel->mColorRuns.Clear();
95 visualModel->ClearCaches();
98 void CreateTextModel( const std::string& text,
100 const Vector<FontDescriptionRun>& fontDescriptions,
101 const LayoutOptions& options,
103 LogicalModelPtr logicalModel,
104 VisualModelPtr visualModel )
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, lineBreakInfo );
121 if( 0u == numberOfCharacters )
123 // Nothing else to do if the number of characters is zero.
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 );
131 SetWordBreakInfo( utf32Characters,
136 // 3) Set the script info.
137 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
139 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
140 multilanguageSupport.SetScripts( utf32Characters,
145 // 4) Set the font info
146 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
147 fontDescriptionRuns = fontDescriptions;
148 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
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;
157 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
158 fontClient.SetDpi( 96u, 96u );
160 const FontId defaultFontId = fontDefaults.GetFontId( fontClient );
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,
172 // 5) Set the bidirectional info per paragraph.
173 Vector<Character> mirroredUtf32Characters;
174 bool textMirrored = false;
176 // Reserve some space for the vector of paragraph's bidirectional info.
177 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
179 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
180 SetBidirectionalInfo( utf32Characters,
187 // 6) Set character directions.
188 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
189 if( 0u != bidirectionalInfo.Count() )
191 // Only set the character directions if there is right to left characters.
192 GetCharactersDirection( bidirectionalInfo,
196 characterDirections );
199 // This paragraph has right to left text. Some characters may need to be mirrored.
200 textMirrored = GetMirroredText( utf32Characters,
205 mirroredUtf32Characters );
209 // There is no right to left characters. Clear the directions vector.
210 characterDirections.Clear();
213 // 7) Shape the text.
215 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
216 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
217 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
218 Vector<GlyphIndex> newParagraphGlyphs;
220 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
222 ShapeText( textToShape,
230 glyphsToCharactersMap,
232 newParagraphGlyphs );
234 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
235 visualModel->CreateGlyphsPerCharacterTable( 0u, numberOfCharacters );
236 visualModel->CreateCharacterToGlyphTable( 0u, numberOfCharacters );
238 const Length numberOfGlyphs = glyphs.Count();
240 // 8) Get the glyph metrics
241 MetricsPtr metrics = Metrics::New( fontClient );
243 GlyphInfo* glyphsBuffer = glyphs.Begin();
244 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
246 // Update the width and advance of all new paragraph characters.
247 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
248 endIt = newParagraphGlyphs.End();
252 const GlyphIndex index = *it;
253 GlyphInfo& glyph = *( glyphsBuffer + index );
255 glyph.xBearing = 0.f;
260 // 9) Layout the text
261 LayoutEngine layoutEngine;
262 layoutEngine.SetMetrics( metrics );
263 layoutEngine.SetLayout( LayoutEngine::MULTI_LINE_BOX );
265 // Set the layout parameters.
266 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
267 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
269 LayoutParameters layoutParameters( textArea,
270 utf32Characters.Begin(),
271 lineBreakInfo.Begin(),
272 wordBreakInfo.Begin(),
273 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
275 glyphsToCharactersMap.Begin(),
276 charactersPerGlyph.Begin(),
277 charactersToGlyph.Begin(),
278 glyphsPerCharacter.Begin(),
281 Vector<LineRun>& lines = visualModel->mLines;
283 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
284 glyphPositions.Resize( numberOfGlyphs );
286 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
288 // The initial glyph and the number of glyphs to layout.
289 layoutParameters.startGlyphIndex = 0u;
290 layoutParameters.numberOfGlyphs = numberOfGlyphs;
292 layoutEngine.LayoutText( layoutParameters,
297 // 10) Reorder the lines
298 if( 0u != bidirectionalInfo.Count() )
300 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
303 const Length numberOfLines = lines.Count();
305 // Reorder the lines.
306 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
307 ReorderLines( bidirectionalInfo,
311 bidirectionalLineInfo );
313 // Set the bidirectional info per line into the layout parameters.
314 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
315 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
317 // Set the bidirectional info into the model.
318 logicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
319 layoutParameters.numberOfBidirectionalInfoRuns,
321 numberOfCharacters );
323 if( options.reorder )
325 // Re-layout the text. Reorder those lines with right to left characters.
326 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
335 layoutEngine.Align( textArea,
344 } // namespace Toolkit