2 * Copyright (c) 2018 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>
34 #include <dali-toolkit/internal/text/markup-processor.h>
46 * @brief Frees previously allocated bidirectional resources.
48 * @param[in] bidirectionalLineInfo Bidirectional info per line.
49 * @param[in] index Index to the first line with bidirectional info to be freed.
51 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
54 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
55 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
56 endIt = bidirectionalLineInfo.End();
60 BidirectionalLineInfoRun& bidiLineInfo = *it;
62 free( bidiLineInfo.visualToLogicalMap );
67 * @brief Clear all the model data except for LogicalModel::mText.
69 * @param[in] characterIndex Clear data starting from the index.
71 void ClearModelData( CharacterIndex characterIndex,
72 LogicalModelPtr logicalModel,
73 VisualModelPtr visualModel )
75 // n.b. This does not Clear the mText from mLogicalModel
77 // Frees previously allocated resources.
78 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
80 logicalModel->mScriptRuns.Clear();
81 logicalModel->mFontRuns.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,
103 bool markupProcessorEnabled )
105 textModel = Model::New(); ///< Pointer to the text's model.
106 LogicalModelPtr logicalModel = textModel->mLogicalModel;
107 VisualModelPtr visualModel = textModel->mVisualModel;
109 MarkupProcessData markupProcessData( logicalModel->mColorRuns,
110 logicalModel->mFontDescriptionRuns,
111 logicalModel->mEmbeddedItems,
112 logicalModel->mAnchors,
113 logicalModel->mUnderlinedCharacterRuns);
115 Length textSize = 0u;
116 const uint8_t* utf8 = NULL;
117 if( markupProcessorEnabled )
119 ProcessMarkupString( text, markupProcessData );
120 textSize = markupProcessData.markupProcessedText.size();
122 // This is a bit horrible but std::string returns a (signed) char*
123 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
127 textSize = text.size();
129 // This is a bit horrible but std::string returns a (signed) char*
130 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
133 // 1) Convert to utf32
134 Vector<Character>& utf32Characters = logicalModel->mText;
135 utf32Characters.Resize( textSize );
137 // Transform a text array encoded in utf8 into an array encoded in utf32.
138 // It returns the actual number of characters.
139 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
140 utf32Characters.Resize( characterCount );
142 // 2) Set the break and paragraph info.
143 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
144 lineBreakInfo.Resize( characterCount );
146 SetLineBreakInfo( utf32Characters,
151 if( 0u == characterCount )
153 // Nothing else to do if the number of characters is zero.
157 // 3) Set the script info.
158 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
160 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
161 multilanguageSupport.SetScripts( utf32Characters,
166 // 4) Set the font info
167 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
168 fontDescriptionRuns = fontDescriptions;
169 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
171 // The default font description.
172 TextAbstraction::FontDescription fontDescription;
174 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
175 fontClient.SetDpi( 96u, 96u );
177 // Validates the fonts. If there is a character with no assigned font it sets a default one.
178 // After this call, fonts are validated.
179 multilanguageSupport.ValidateFonts( utf32Characters,
183 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
188 // 5) Set the bidirectional info per paragraph.
189 Vector<Character> mirroredUtf32Characters;
190 bool textMirrored = false;
192 // Reserve some space for the vector of paragraph's bidirectional info.
193 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
195 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
196 SetBidirectionalInfo( utf32Characters,
203 // Create the paragraph info.
204 logicalModel->CreateParagraphInfo( 0u,
207 // 6) Set character directions.
208 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
209 if( 0u != bidirectionalInfo.Count() )
211 // Only set the character directions if there is right to left characters.
212 GetCharactersDirection( bidirectionalInfo,
216 characterDirections );
219 // This paragraph has right to left text. Some characters may need to be mirrored.
220 textMirrored = GetMirroredText( utf32Characters,
225 mirroredUtf32Characters );
229 // There is no right to left characters. Clear the directions vector.
230 characterDirections.Clear();
233 // 7) Shape the text.
235 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
236 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
237 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
238 Vector<GlyphIndex> newParagraphGlyphs;
240 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
242 ShapeText( textToShape,
250 glyphsToCharactersMap,
252 newParagraphGlyphs );
254 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
255 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
256 visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
258 const Length numberOfGlyphs = glyphs.Count();
260 // 8) Get the glyph metrics
261 metrics = Metrics::New( fontClient );
263 GlyphInfo* glyphsBuffer = glyphs.Begin();
264 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
266 // Update the width and advance of all new paragraph characters.
267 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
268 endIt = newParagraphGlyphs.End();
272 const GlyphIndex index = *it;
273 GlyphInfo& glyph = *( glyphsBuffer + index );
275 glyph.xBearing = 0.f;
280 // 9) Layout the text
281 Layout::Engine layoutEngine;
282 layoutEngine.SetMetrics( metrics );
283 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
285 // Set the layout parameters.
286 textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
287 textModel->mLineWrapMode = LineWrap::WORD;
288 textModel->mIgnoreSpacesAfterText = true;
289 textModel->mMatchSystemLanguageDirection = false;
290 Layout::Parameters layoutParameters( textArea,
293 Vector<LineRun>& lines = visualModel->mLines;
295 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
296 glyphPositions.Resize( numberOfGlyphs );
298 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
300 // The initial glyph and the number of glyphs to layout.
301 layoutParameters.startGlyphIndex = 0u;
302 layoutParameters.numberOfGlyphs = numberOfGlyphs;
303 layoutParameters.startLineIndex = 0u;
304 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
306 bool isAutoScroll = false;
307 layoutEngine.LayoutText( layoutParameters,
314 float alignmentOffset = 0.f;
315 layoutEngine.Align( textArea,
318 Text::HorizontalAlignment::BEGIN,
321 Dali::LayoutDirection::LEFT_TO_RIGHT,
326 void ConfigureTextLabel( ControllerPtr controller )
328 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
329 fontClient.SetDpi( 93u, 93u );
331 // Set the text layout as multi-line.
332 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
334 // Set cursor's width to zero.
335 controller->GetLayoutEngine().SetCursorWidth( 0 );
337 InputMethodContext inputMethodContext = InputMethodContext::New();
338 // Disables the text input.
339 controller->EnableTextInput( NULL, inputMethodContext );
341 // Disables the vertical scrolling.
342 controller->SetVerticalScrollEnabled( false );
344 // Disables the horizontal scrolling.
345 controller->SetHorizontalScrollEnabled( false );
347 // Enable the text elide.
348 controller->SetTextElideEnabled( true );
351 void ConfigureTextField( ControllerPtr controller )
353 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
354 fontClient.SetDpi( 93u, 93u );
356 // Creates a decorator.
357 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
360 // Set the text layout as multi-line.
361 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
363 InputMethodContext inputMethodContext = InputMethodContext::New();
364 // Enables the text input.
365 controller->EnableTextInput( decorator, inputMethodContext );
367 // Enables the vertical scrolling after the text input has been enabled.
368 controller->SetVerticalScrollEnabled( false );
370 // Disables the horizontal scrolling.
371 controller->SetHorizontalScrollEnabled( true );
373 // No maximum number of characters.
374 controller->SetMaximumNumberOfCharacters( 50u );
376 // Disable the text elide.
377 controller->SetTextElideEnabled( false );
380 void ConfigureTextEditor( ControllerPtr controller )
382 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
383 fontClient.SetDpi( 93u, 93u );
385 // Creates a decorator.
386 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
389 // Set the text layout as multi-line.
390 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
392 InputMethodContext inputMethodContext = InputMethodContext::New();
393 // Enables the text input.
394 controller->EnableTextInput( decorator, inputMethodContext );
396 // Enables the vertical scrolling after the text input has been enabled.
397 controller->SetVerticalScrollEnabled( true );
399 // Disables the horizontal scrolling.
400 controller->SetHorizontalScrollEnabled( false );
402 // No maximum number of characters.
403 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
405 // Disable the text elide.
406 controller->SetTextElideEnabled( false );
411 } // namespace Toolkit