2 * Copyright (c) 2021 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>
35 #include <dali-toolkit/internal/text/hyphenator.h>
47 * @brief Frees previously allocated bidirectional resources.
49 * @param[in] bidirectionalLineInfo Bidirectional info per line.
50 * @param[in] index Index to the first line with bidirectional info to be freed.
52 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
55 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
56 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
57 endIt = bidirectionalLineInfo.End();
61 BidirectionalLineInfoRun& bidiLineInfo = *it;
63 free( bidiLineInfo.visualToLogicalMap );
68 * @brief Clear all the model data except for LogicalModel::mText.
70 * @param[in] characterIndex Clear data starting from the index.
72 void ClearModelData( CharacterIndex characterIndex,
73 LogicalModelPtr logicalModel,
74 VisualModelPtr visualModel )
76 // n.b. This does not Clear the mText from mLogicalModel
78 // Frees previously allocated resources.
79 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
81 logicalModel->mScriptRuns.Clear();
82 logicalModel->mFontRuns.Clear();
83 logicalModel->mBidirectionalParagraphInfo.Clear();
84 logicalModel->mCharacterDirections.Clear();
85 logicalModel->mBidirectionalLineInfo.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,
104 bool markupProcessorEnabled,
105 LineWrap::Mode wrapMode )
107 textModel = Model::New(); ///< Pointer to the text's model.
108 LogicalModelPtr logicalModel = textModel->mLogicalModel;
109 VisualModelPtr visualModel = textModel->mVisualModel;
111 MarkupProcessData markupProcessData( logicalModel->mColorRuns,
112 logicalModel->mFontDescriptionRuns,
113 logicalModel->mEmbeddedItems,
114 logicalModel->mAnchors,
115 logicalModel->mUnderlinedCharacterRuns,
116 logicalModel->mBackgroundColorRuns);
118 Length textSize = 0u;
119 const uint8_t* utf8 = NULL;
120 if( markupProcessorEnabled )
122 ProcessMarkupString( text, markupProcessData );
123 textSize = markupProcessData.markupProcessedText.size();
125 // This is a bit horrible but std::string returns a (signed) char*
126 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
130 textSize = text.size();
132 // This is a bit horrible but std::string returns a (signed) char*
133 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
136 // 1) Convert to utf32
137 Vector<Character>& utf32Characters = logicalModel->mText;
138 utf32Characters.Resize( textSize );
140 // Transform a text array encoded in utf8 into an array encoded in utf32.
141 // It returns the actual number of characters.
142 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
143 utf32Characters.Resize( characterCount );
145 // 2) Set the break and paragraph info.
146 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
147 lineBreakInfo.Resize( characterCount );
149 SetLineBreakInfo( utf32Characters,
154 if( 0u == characterCount )
156 // Nothing else to do if the number of characters is zero.
160 textModel->mLineWrapMode = wrapMode;
162 if(textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::HYPHENATION) ||
163 textModel->mLineWrapMode == ((Text::LineWrap::Mode)DevelText::LineWrap::MIXED))
165 CharacterIndex end = characterCount;
166 LineBreakInfo* lineBreakInfoBuffer = lineBreakInfo.Begin();
168 for(CharacterIndex index = 0; index < end; index++)
170 CharacterIndex wordEnd = index;
171 while((*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_ALLOW_BREAK) && (*(lineBreakInfoBuffer + wordEnd) != TextAbstraction::LINE_MUST_BREAK))
176 if((wordEnd + 1) == end) // add last char
181 Vector<bool> hyphens = GetWordHyphens(utf32Characters.Begin() + index, wordEnd - index, nullptr);
183 for(CharacterIndex i = 0; i < (wordEnd - index); i++)
187 *(lineBreakInfoBuffer + index + i) = TextAbstraction::LINE_HYPHENATION_BREAK;
195 // 3) Set the script info.
196 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
198 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
199 multilanguageSupport.SetScripts( utf32Characters,
204 // 4) Set the font info
205 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
206 fontDescriptionRuns = fontDescriptions;
207 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
209 // The default font description.
210 TextAbstraction::FontDescription fontDescription;
212 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
213 fontClient.SetDpi( 96u, 96u );
215 // Validates the fonts. If there is a character with no assigned font it sets a default one.
216 // After this call, fonts are validated.
217 multilanguageSupport.ValidateFonts( utf32Characters,
221 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
226 // 5) Set the bidirectional info per paragraph.
227 Vector<Character> mirroredUtf32Characters;
228 bool textMirrored = false;
230 // Reserve some space for the vector of paragraph's bidirectional info.
231 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
233 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
234 SetBidirectionalInfo( utf32Characters,
241 // Create the paragraph info.
242 logicalModel->CreateParagraphInfo( 0u,
245 // 6) Set character directions.
246 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
247 if( 0u != bidirectionalInfo.Count() )
249 // Only set the character directions if there is right to left characters.
250 GetCharactersDirection( bidirectionalInfo,
254 characterDirections );
257 // This paragraph has right to left text. Some characters may need to be mirrored.
258 textMirrored = GetMirroredText( utf32Characters,
263 mirroredUtf32Characters );
267 // There is no right to left characters. Clear the directions vector.
268 characterDirections.Clear();
271 // 7) Shape the text.
273 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
274 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
275 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
276 Vector<GlyphIndex> newParagraphGlyphs;
278 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
280 ShapeText( textToShape,
288 glyphsToCharactersMap,
290 newParagraphGlyphs );
292 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
293 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
294 visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
296 const Length numberOfGlyphs = glyphs.Count();
298 // 8) Get the glyph metrics
299 metrics = Metrics::New( fontClient );
301 GlyphInfo* glyphsBuffer = glyphs.Begin();
302 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
304 // Update the width and advance of all new paragraph characters.
305 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
306 endIt = newParagraphGlyphs.End();
310 const GlyphIndex index = *it;
311 GlyphInfo& glyph = *( glyphsBuffer + index );
313 glyph.xBearing = 0.f;
318 // 9) Layout the text
319 Layout::Engine layoutEngine;
320 layoutEngine.SetMetrics( metrics );
321 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
323 // Set the layout parameters.
324 textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
325 textModel->mIgnoreSpacesAfterText = true;
326 textModel->mMatchSystemLanguageDirection = false;
327 Layout::Parameters layoutParameters( textArea,
330 Vector<LineRun>& lines = visualModel->mLines;
332 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
333 glyphPositions.Resize( numberOfGlyphs );
335 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
337 // The initial glyph and the number of glyphs to layout.
338 layoutParameters.startGlyphIndex = 0u;
339 layoutParameters.numberOfGlyphs = numberOfGlyphs;
340 layoutParameters.startLineIndex = 0u;
341 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
343 bool isAutoScroll = false;
344 layoutEngine.LayoutText( layoutParameters,
351 float alignmentOffset = 0.f;
352 layoutEngine.Align( textArea,
355 Text::HorizontalAlignment::BEGIN,
358 Dali::LayoutDirection::LEFT_TO_RIGHT,
363 void ConfigureTextLabel( ControllerPtr controller )
365 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
366 fontClient.SetDpi( 93u, 93u );
368 // Set the text layout as multi-line.
369 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
371 // Set cursor's width to zero.
372 controller->GetLayoutEngine().SetCursorWidth( 0 );
374 InputMethodContext inputMethodContext = InputMethodContext::New();
375 // Disables the text input.
376 controller->EnableTextInput( NULL, inputMethodContext );
378 // Disables the vertical scrolling.
379 controller->SetVerticalScrollEnabled( false );
381 // Disables the horizontal scrolling.
382 controller->SetHorizontalScrollEnabled( false );
384 // Enable the text elide.
385 controller->SetTextElideEnabled( true );
387 // Disable match system language direction
388 controller->SetMatchSystemLanguageDirection(false);
391 void ConfigureTextField( ControllerPtr controller )
393 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
394 fontClient.SetDpi( 93u, 93u );
396 // Creates a decorator.
397 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
400 // Set the text layout as multi-line.
401 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
403 InputMethodContext inputMethodContext = InputMethodContext::New();
404 // Enables the text input.
405 controller->EnableTextInput( decorator, inputMethodContext );
407 // Enables the vertical scrolling after the text input has been enabled.
408 controller->SetVerticalScrollEnabled( false );
410 // Disables the horizontal scrolling.
411 controller->SetHorizontalScrollEnabled( true );
413 // No maximum number of characters.
414 controller->SetMaximumNumberOfCharacters( 50u );
416 // Disable the text elide.
417 controller->SetTextElideEnabled( false );
419 // Disable match system language direction
420 controller->SetMatchSystemLanguageDirection(false);
423 void ConfigureTextEditor( ControllerPtr controller )
425 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
426 fontClient.SetDpi( 93u, 93u );
428 // Creates a decorator.
429 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
432 // Set the text layout as multi-line.
433 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
435 InputMethodContext inputMethodContext = InputMethodContext::New();
436 // Enables the text input.
437 controller->EnableTextInput( decorator, inputMethodContext );
439 // Enables the vertical scrolling after the text input has been enabled.
440 controller->SetVerticalScrollEnabled( true );
442 // Disables the horizontal scrolling.
443 controller->SetHorizontalScrollEnabled( false );
445 // No maximum number of characters.
446 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
448 // Disable the text elide.
449 controller->SetTextElideEnabled( false );
451 // Disable match system language direction
452 controller->SetMatchSystemLanguageDirection(false);
457 } // namespace Toolkit