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 );
113 Length textSize = 0u;
114 const uint8_t* utf8 = NULL;
115 if( markupProcessorEnabled )
117 ProcessMarkupString( text, markupProcessData );
118 textSize = markupProcessData.markupProcessedText.size();
120 // This is a bit horrible but std::string returns a (signed) char*
121 utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
125 textSize = text.size();
127 // This is a bit horrible but std::string returns a (signed) char*
128 utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
131 // 1) Convert to utf32
132 Vector<Character>& utf32Characters = logicalModel->mText;
133 utf32Characters.Resize( textSize );
135 // Transform a text array encoded in utf8 into an array encoded in utf32.
136 // It returns the actual number of characters.
137 Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
138 utf32Characters.Resize( characterCount );
140 // 2) Set the break and paragraph info.
141 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
142 lineBreakInfo.Resize( characterCount );
144 SetLineBreakInfo( utf32Characters,
149 if( 0u == characterCount )
151 // Nothing else to do if the number of characters is zero.
155 // 3) Set the script info.
156 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
158 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
159 multilanguageSupport.SetScripts( utf32Characters,
164 // 4) Set the font info
165 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
166 fontDescriptionRuns = fontDescriptions;
167 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
169 // The default font description.
170 TextAbstraction::FontDescription fontDescription;
172 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
173 fontClient.SetDpi( 96u, 96u );
175 // Validates the fonts. If there is a character with no assigned font it sets a default one.
176 // After this call, fonts are validated.
177 multilanguageSupport.ValidateFonts( utf32Characters,
181 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
186 // 5) Set the bidirectional info per paragraph.
187 Vector<Character> mirroredUtf32Characters;
188 bool textMirrored = false;
190 // Reserve some space for the vector of paragraph's bidirectional info.
191 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
193 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
194 SetBidirectionalInfo( utf32Characters,
201 // Create the paragraph info.
202 logicalModel->CreateParagraphInfo( 0u,
205 // 6) Set character directions.
206 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
207 if( 0u != bidirectionalInfo.Count() )
209 // Only set the character directions if there is right to left characters.
210 GetCharactersDirection( bidirectionalInfo,
214 characterDirections );
217 // This paragraph has right to left text. Some characters may need to be mirrored.
218 textMirrored = GetMirroredText( utf32Characters,
223 mirroredUtf32Characters );
227 // There is no right to left characters. Clear the directions vector.
228 characterDirections.Clear();
231 // 7) Shape the text.
233 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
234 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
235 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
236 Vector<GlyphIndex> newParagraphGlyphs;
238 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
240 ShapeText( textToShape,
248 glyphsToCharactersMap,
250 newParagraphGlyphs );
252 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
253 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
254 visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
256 const Length numberOfGlyphs = glyphs.Count();
258 // 8) Get the glyph metrics
259 metrics = Metrics::New( fontClient );
261 GlyphInfo* glyphsBuffer = glyphs.Begin();
262 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
264 // Update the width and advance of all new paragraph characters.
265 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
266 endIt = newParagraphGlyphs.End();
270 const GlyphIndex index = *it;
271 GlyphInfo& glyph = *( glyphsBuffer + index );
273 glyph.xBearing = 0.f;
278 // 9) Layout the text
279 Layout::Engine layoutEngine;
280 layoutEngine.SetMetrics( metrics );
281 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
283 // Set the layout parameters.
284 textModel->mHorizontalAlignment = Text::HorizontalAlignment::BEGIN;
285 textModel->mLineWrapMode = LineWrap::WORD;
286 textModel->mIgnoreSpacesAfterText = true;
287 textModel->mMatchSystemLanguageDirection = false;
288 Layout::Parameters layoutParameters( textArea,
291 Vector<LineRun>& lines = visualModel->mLines;
293 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
294 glyphPositions.Resize( numberOfGlyphs );
296 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
298 // The initial glyph and the number of glyphs to layout.
299 layoutParameters.startGlyphIndex = 0u;
300 layoutParameters.numberOfGlyphs = numberOfGlyphs;
301 layoutParameters.startLineIndex = 0u;
302 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
304 bool isAutoScroll = false;
305 layoutEngine.LayoutText( layoutParameters,
312 float alignmentOffset = 0.f;
313 layoutEngine.Align( textArea,
316 Text::HorizontalAlignment::BEGIN,
319 Dali::LayoutDirection::LEFT_TO_RIGHT,
324 void ConfigureTextLabel( ControllerPtr controller )
326 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
327 fontClient.SetDpi( 93u, 93u );
329 // Set the text layout as multi-line.
330 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
332 // Set cursor's width to zero.
333 controller->GetLayoutEngine().SetCursorWidth( 0 );
335 InputMethodContext inputMethodContext = InputMethodContext::New();
336 // Disables the text input.
337 controller->EnableTextInput( NULL, inputMethodContext );
339 // Disables the vertical scrolling.
340 controller->SetVerticalScrollEnabled( false );
342 // Disables the horizontal scrolling.
343 controller->SetHorizontalScrollEnabled( false );
345 // Enable the text elide.
346 controller->SetTextElideEnabled( true );
349 void ConfigureTextField( ControllerPtr controller )
351 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
352 fontClient.SetDpi( 93u, 93u );
354 // Creates a decorator.
355 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
358 // Set the text layout as multi-line.
359 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
361 InputMethodContext inputMethodContext = InputMethodContext::New();
362 // Enables the text input.
363 controller->EnableTextInput( decorator, inputMethodContext );
365 // Enables the vertical scrolling after the text input has been enabled.
366 controller->SetVerticalScrollEnabled( false );
368 // Disables the horizontal scrolling.
369 controller->SetHorizontalScrollEnabled( true );
371 // No maximum number of characters.
372 controller->SetMaximumNumberOfCharacters( 50u );
374 // Disable the text elide.
375 controller->SetTextElideEnabled( false );
378 void ConfigureTextEditor( ControllerPtr controller )
380 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
381 fontClient.SetDpi( 93u, 93u );
383 // Creates a decorator.
384 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
387 // Set the text layout as multi-line.
388 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
390 InputMethodContext inputMethodContext = InputMethodContext::New();
391 // Enables the text input.
392 controller->EnableTextInput( decorator, inputMethodContext );
394 // Enables the vertical scrolling after the text input has been enabled.
395 controller->SetVerticalScrollEnabled( true );
397 // Disables the horizontal scrolling.
398 controller->SetHorizontalScrollEnabled( false );
400 // No maximum number of characters.
401 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
403 // Disable the text elide.
404 controller->SetTextElideEnabled( false );
409 } // namespace Toolkit