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->mWordBreakInfo.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,
102 LogicalModelPtr& logicalModel,
103 VisualModelPtr& visualModel,
105 bool markupProcessorEnabled )
107 logicalModel = LogicalModel::New();
108 visualModel = VisualModel::New();
110 MarkupProcessData markupProcessData( logicalModel->mColorRuns,
111 logicalModel->mFontDescriptionRuns );
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 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
156 Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
157 wordBreakInfo.Resize( characterCount );
159 SetWordBreakInfo( utf32Characters,
164 // 3) Set the script info.
165 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
167 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
168 multilanguageSupport.SetScripts( utf32Characters,
173 // 4) Set the font info
174 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
175 fontDescriptionRuns = fontDescriptions;
176 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
178 // The default font description.
179 TextAbstraction::FontDescription fontDescription;
181 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
182 fontClient.SetDpi( 96u, 96u );
184 // Validates the fonts. If there is a character with no assigned font it sets a default one.
185 // After this call, fonts are validated.
186 multilanguageSupport.ValidateFonts( utf32Characters,
190 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
195 // 5) Set the bidirectional info per paragraph.
196 Vector<Character> mirroredUtf32Characters;
197 bool textMirrored = false;
199 // Reserve some space for the vector of paragraph's bidirectional info.
200 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
202 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
203 SetBidirectionalInfo( utf32Characters,
210 // Create the paragraph info.
211 logicalModel->CreateParagraphInfo( 0u,
214 // 6) Set character directions.
215 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
216 if( 0u != bidirectionalInfo.Count() )
218 // Only set the character directions if there is right to left characters.
219 GetCharactersDirection( bidirectionalInfo,
223 characterDirections );
226 // This paragraph has right to left text. Some characters may need to be mirrored.
227 textMirrored = GetMirroredText( utf32Characters,
232 mirroredUtf32Characters );
236 // There is no right to left characters. Clear the directions vector.
237 characterDirections.Clear();
240 // 7) Shape the text.
242 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
243 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
244 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
245 Vector<GlyphIndex> newParagraphGlyphs;
247 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
249 ShapeText( textToShape,
257 glyphsToCharactersMap,
259 newParagraphGlyphs );
261 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
262 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, characterCount );
263 visualModel->CreateCharacterToGlyphTable( 0u, 0u, characterCount );
265 const Length numberOfGlyphs = glyphs.Count();
267 // 8) Get the glyph metrics
268 metrics = Metrics::New( fontClient );
270 GlyphInfo* glyphsBuffer = glyphs.Begin();
271 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
273 // Update the width and advance of all new paragraph characters.
274 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
275 endIt = newParagraphGlyphs.End();
279 const GlyphIndex index = *it;
280 GlyphInfo& glyph = *( glyphsBuffer + index );
282 glyph.xBearing = 0.f;
287 // 9) Layout the text
288 Layout::Engine layoutEngine;
289 layoutEngine.SetMetrics( metrics );
290 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
292 // Set the layout parameters.
293 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
294 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
295 float outlineWidth = visualModel->GetOutlineWidth();
296 Layout::Parameters layoutParameters( textArea,
297 utf32Characters.Begin(),
298 lineBreakInfo.Begin(),
299 wordBreakInfo.Begin(),
300 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
302 glyphsToCharactersMap.Begin(),
303 charactersPerGlyph.Begin(),
304 charactersToGlyph.Begin(),
305 glyphsPerCharacter.Begin(),
307 Text::HorizontalAlignment::BEGIN,
308 Text::LineWrap::WORD,
313 Vector<LineRun>& lines = visualModel->mLines;
315 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
316 glyphPositions.Resize( numberOfGlyphs );
318 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( characterCount - 1u ) ) );
320 // The initial glyph and the number of glyphs to layout.
321 layoutParameters.startGlyphIndex = 0u;
322 layoutParameters.numberOfGlyphs = numberOfGlyphs;
323 layoutParameters.startLineIndex = 0u;
324 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
326 bool isAutoScroll = false;
327 layoutEngine.LayoutText( layoutParameters,
334 // 10) Reorder the lines
335 if( 0u != bidirectionalInfo.Count() )
337 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
340 const Length numberOfLines = lines.Count();
342 // Reorder the lines.
343 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
344 ReorderLines( bidirectionalInfo,
348 bidirectionalLineInfo );
350 // Set the bidirectional info per line into the layout parameters.
351 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
352 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
354 if( options.reorder )
356 // Re-layout the text. Reorder those lines with right to left characters.
357 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
366 float alignmentOffset = 0.f;
367 layoutEngine.Align( textArea,
370 Text::HorizontalAlignment::BEGIN,
373 Dali::LayoutDirection::LEFT_TO_RIGHT,
378 void ConfigureTextLabel( ControllerPtr controller )
380 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
381 fontClient.SetDpi( 93u, 93u );
383 // Set the text layout as multi-line.
384 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
386 // Set cursor's width to zero.
387 controller->GetLayoutEngine().SetCursorWidth( 0 );
389 InputMethodContext inputMethodContext = InputMethodContext::New();
390 // Disables the text input.
391 controller->EnableTextInput( NULL, inputMethodContext );
393 // Disables the vertical scrolling.
394 controller->SetVerticalScrollEnabled( false );
396 // Disables the horizontal scrolling.
397 controller->SetHorizontalScrollEnabled( false );
399 // Enable the text elide.
400 controller->SetTextElideEnabled( true );
403 void ConfigureTextField( ControllerPtr controller )
405 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
406 fontClient.SetDpi( 93u, 93u );
408 // Creates a decorator.
409 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
412 // Set the text layout as multi-line.
413 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
415 InputMethodContext inputMethodContext = InputMethodContext::New();
416 // Enables the text input.
417 controller->EnableTextInput( decorator, inputMethodContext );
419 // Enables the vertical scrolling after the text input has been enabled.
420 controller->SetVerticalScrollEnabled( false );
422 // Disables the horizontal scrolling.
423 controller->SetHorizontalScrollEnabled( true );
425 // No maximum number of characters.
426 controller->SetMaximumNumberOfCharacters( 50u );
428 // Disable the text elide.
429 controller->SetTextElideEnabled( false );
432 void ConfigureTextEditor( ControllerPtr controller )
434 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
435 fontClient.SetDpi( 93u, 93u );
437 // Creates a decorator.
438 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
441 // Set the text layout as multi-line.
442 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
444 InputMethodContext inputMethodContext = InputMethodContext::New();
445 // Enables the text input.
446 controller->EnableTextInput( decorator, inputMethodContext );
448 // Enables the vertical scrolling after the text input has been enabled.
449 controller->SetVerticalScrollEnabled( true );
451 // Disables the horizontal scrolling.
452 controller->SetHorizontalScrollEnabled( false );
454 // No maximum number of characters.
455 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
457 // Disable the text elide.
458 controller->SetTextElideEnabled( false );
463 } // namespace Toolkit