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 // 10) Reorder the lines
313 if( 0u != bidirectionalInfo.Count() )
315 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
318 const Length numberOfLines = lines.Count();
320 // Reorder the lines.
321 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
322 ReorderLines( bidirectionalInfo,
326 bidirectionalLineInfo );
328 // Set the bidirectional info per line into the layout parameters.
329 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
330 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
332 if( options.reorder )
334 // Re-layout the text. Reorder those lines with right to left characters.
335 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
344 float alignmentOffset = 0.f;
345 layoutEngine.Align( textArea,
348 Text::HorizontalAlignment::BEGIN,
351 Dali::LayoutDirection::LEFT_TO_RIGHT,
356 void ConfigureTextLabel( ControllerPtr controller )
358 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
359 fontClient.SetDpi( 93u, 93u );
361 // Set the text layout as multi-line.
362 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
364 // Set cursor's width to zero.
365 controller->GetLayoutEngine().SetCursorWidth( 0 );
367 InputMethodContext inputMethodContext = InputMethodContext::New();
368 // Disables the text input.
369 controller->EnableTextInput( NULL, inputMethodContext );
371 // Disables the vertical scrolling.
372 controller->SetVerticalScrollEnabled( false );
374 // Disables the horizontal scrolling.
375 controller->SetHorizontalScrollEnabled( false );
377 // Enable the text elide.
378 controller->SetTextElideEnabled( true );
381 void ConfigureTextField( ControllerPtr controller )
383 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
384 fontClient.SetDpi( 93u, 93u );
386 // Creates a decorator.
387 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
390 // Set the text layout as multi-line.
391 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
393 InputMethodContext inputMethodContext = InputMethodContext::New();
394 // Enables the text input.
395 controller->EnableTextInput( decorator, inputMethodContext );
397 // Enables the vertical scrolling after the text input has been enabled.
398 controller->SetVerticalScrollEnabled( false );
400 // Disables the horizontal scrolling.
401 controller->SetHorizontalScrollEnabled( true );
403 // No maximum number of characters.
404 controller->SetMaximumNumberOfCharacters( 50u );
406 // Disable the text elide.
407 controller->SetTextElideEnabled( false );
410 void ConfigureTextEditor( ControllerPtr controller )
412 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
413 fontClient.SetDpi( 93u, 93u );
415 // Creates a decorator.
416 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
419 // Set the text layout as multi-line.
420 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
422 InputMethodContext inputMethodContext = InputMethodContext::New();
423 // Enables the text input.
424 controller->EnableTextInput( decorator, inputMethodContext );
426 // Enables the vertical scrolling after the text input has been enabled.
427 controller->SetVerticalScrollEnabled( true );
429 // Disables the horizontal scrolling.
430 controller->SetHorizontalScrollEnabled( false );
432 // No maximum number of characters.
433 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
435 // Disable the text elide.
436 controller->SetTextElideEnabled( false );
441 } // namespace Toolkit