2 * Copyright (c) 2017 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.
19 #include "toolkit-text-utils.h"
23 #include <dali/devel-api/text-abstraction/font-client.h>
26 #include <dali-toolkit/internal/text/bidirectional-support.h>
27 #include <dali-toolkit/internal/text/character-set-conversion.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
30 #include <dali-toolkit/internal/text/multi-language-support.h>
31 #include <dali-toolkit/internal/text/segmentation.h>
32 #include <dali-toolkit/internal/text/shaper.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
45 * @brief Frees previously allocated bidirectional resources.
47 * @param[in] bidirectionalLineInfo Bidirectional info per line.
48 * @param[in] index Index to the first line with bidirectional info to be freed.
50 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
53 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
54 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
55 endIt = bidirectionalLineInfo.End();
59 BidirectionalLineInfoRun& bidiLineInfo = *it;
61 free( bidiLineInfo.visualToLogicalMap );
66 * @brief Clear all the model data except for LogicalModel::mText.
68 * @param[in] characterIndex Clear data starting from the index.
70 void ClearModelData( CharacterIndex characterIndex,
71 LogicalModelPtr logicalModel,
72 VisualModelPtr visualModel )
74 // n.b. This does not Clear the mText from mLogicalModel
76 // Frees previously allocated resources.
77 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
79 logicalModel->mScriptRuns.Clear();
80 logicalModel->mFontRuns.Clear();
81 logicalModel->mWordBreakInfo.Clear();
82 logicalModel->mBidirectionalParagraphInfo.Clear();
83 logicalModel->mCharacterDirections.Clear();
84 logicalModel->mBidirectionalLineInfo.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,
103 MetricsPtr& metrics )
105 logicalModel = LogicalModel::New();
106 visualModel = VisualModel::New();
108 // 1) Convert to utf32
109 Vector<Character>& utf32Characters = logicalModel->mText;
110 utf32Characters.Resize( text.size() );
112 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
114 &utf32Characters[0u] );
115 utf32Characters.Resize( numberOfCharacters );
117 // 2) Set the break and paragraph info.
118 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
119 lineBreakInfo.Resize( numberOfCharacters );
121 SetLineBreakInfo( utf32Characters,
126 if( 0u == numberOfCharacters )
128 // Nothing else to do if the number of characters is zero.
132 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
133 Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
134 wordBreakInfo.Resize( numberOfCharacters );
136 SetWordBreakInfo( utf32Characters,
141 // 3) Set the script info.
142 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
144 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
145 multilanguageSupport.SetScripts( utf32Characters,
150 // 4) Set the font info
151 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
152 fontDescriptionRuns = fontDescriptions;
153 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
155 // The default font description.
156 TextAbstraction::FontDescription fontDescription;
158 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
159 fontClient.SetDpi( 96u, 96u );
161 // Validates the fonts. If there is a character with no assigned font it sets a default one.
162 // After this call, fonts are validated.
163 multilanguageSupport.ValidateFonts( utf32Characters,
167 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
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 // Create the paragraph info.
188 logicalModel->CreateParagraphInfo( 0u,
189 numberOfCharacters );
191 // 6) Set character directions.
192 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
193 if( 0u != bidirectionalInfo.Count() )
195 // Only set the character directions if there is right to left characters.
196 GetCharactersDirection( bidirectionalInfo,
200 characterDirections );
203 // This paragraph has right to left text. Some characters may need to be mirrored.
204 textMirrored = GetMirroredText( utf32Characters,
209 mirroredUtf32Characters );
213 // There is no right to left characters. Clear the directions vector.
214 characterDirections.Clear();
217 // 7) Shape the text.
219 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
220 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
221 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
222 Vector<GlyphIndex> newParagraphGlyphs;
224 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
226 ShapeText( textToShape,
234 glyphsToCharactersMap,
236 newParagraphGlyphs );
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 );
242 const Length numberOfGlyphs = glyphs.Count();
244 // 8) Get the glyph metrics
245 metrics = Metrics::New( fontClient );
247 GlyphInfo* glyphsBuffer = glyphs.Begin();
248 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
250 // Update the width and advance of all new paragraph characters.
251 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
252 endIt = newParagraphGlyphs.End();
256 const GlyphIndex index = *it;
257 GlyphInfo& glyph = *( glyphsBuffer + index );
259 glyph.xBearing = 0.f;
264 // 9) Layout the text
265 Layout::Engine layoutEngine;
266 layoutEngine.SetMetrics( metrics );
267 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
269 // Set the layout parameters.
270 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
271 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
273 Layout::Parameters layoutParameters( textArea,
274 utf32Characters.Begin(),
275 lineBreakInfo.Begin(),
276 wordBreakInfo.Begin(),
277 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
279 glyphsToCharactersMap.Begin(),
280 charactersPerGlyph.Begin(),
281 charactersToGlyph.Begin(),
282 glyphsPerCharacter.Begin(),
284 Text::HorizontalAlignment::BEGIN,
285 Text::LineWrap::WORD );
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,
306 // 10) Reorder the lines
307 if( 0u != bidirectionalInfo.Count() )
309 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
312 const Length numberOfLines = lines.Count();
314 // Reorder the lines.
315 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
316 ReorderLines( bidirectionalInfo,
320 bidirectionalLineInfo );
322 // Set the bidirectional info per line into the layout parameters.
323 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
324 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
326 if( options.reorder )
328 // Re-layout the text. Reorder those lines with right to left characters.
329 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
338 float alignmentOffset = 0.f;
339 layoutEngine.Align( textArea,
342 Text::HorizontalAlignment::BEGIN,
348 void ConfigureTextLabel( ControllerPtr controller )
350 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
351 fontClient.SetDpi( 93u, 93u );
353 // Set the text layout as multi-line.
354 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
356 // Set cursor's width to zero.
357 controller->GetLayoutEngine().SetCursorWidth( 0 );
359 // Disables the text input.
360 controller->EnableTextInput( NULL );
362 // Disables the vertical scrolling.
363 controller->SetVerticalScrollEnabled( false );
365 // Disables the horizontal scrolling.
366 controller->SetHorizontalScrollEnabled( false );
368 // Enable the text elide.
369 controller->SetTextElideEnabled( true );
372 void ConfigureTextField( ControllerPtr controller )
374 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
375 fontClient.SetDpi( 93u, 93u );
377 // Creates a decorator.
378 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
381 // Set the text layout as multi-line.
382 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
384 // Enables the text input.
385 controller->EnableTextInput( decorator );
387 // Enables the vertical scrolling after the text input has been enabled.
388 controller->SetVerticalScrollEnabled( false );
390 // Disables the horizontal scrolling.
391 controller->SetHorizontalScrollEnabled( true );
393 // No maximum number of characters.
394 controller->SetMaximumNumberOfCharacters( 50u );
396 // Disable the text elide.
397 controller->SetTextElideEnabled( false );
400 void ConfigureTextEditor( ControllerPtr controller )
402 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
403 fontClient.SetDpi( 93u, 93u );
405 // Creates a decorator.
406 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
409 // Set the text layout as multi-line.
410 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
412 // Enables the text input.
413 controller->EnableTextInput( decorator );
415 // Enables the vertical scrolling after the text input has been enabled.
416 controller->SetVerticalScrollEnabled( true );
418 // Disables the horizontal scrolling.
419 controller->SetHorizontalScrollEnabled( false );
421 // No maximum number of characters.
422 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
424 // Disable the text elide.
425 controller->SetTextElideEnabled( false );
430 } // namespace Toolkit