2 * Copyright (c) 2016 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>
45 * @brief Frees previously allocated bidirectional resources.
47 * @param[in] bidirectionalLineInfo Bidirectional info per line.
48 * @param[in] index Index to the first line with bidirectional info to be freed.
50 void FreeBidirectionalLineInfoResources( Vector<BidirectionalLineInfoRun> bidirectionalLineInfo,
53 // Free the allocated memory used to store the conversion table in the bidirectional line info run.
54 for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin() + index,
55 endIt = bidirectionalLineInfo.End();
59 BidirectionalLineInfoRun& bidiLineInfo = *it;
61 free( bidiLineInfo.visualToLogicalMap );
66 * @brief Clear all the model data except for LogicalModel::mText.
68 * @param[in] characterIndex Clear data starting from the index.
70 void ClearModelData( CharacterIndex characterIndex,
71 LogicalModelPtr logicalModel,
72 VisualModelPtr visualModel )
74 // n.b. This does not Clear the mText from mLogicalModel
76 // Frees previously allocated resources.
77 FreeBidirectionalLineInfoResources( logicalModel->mBidirectionalLineInfo, 0u );
79 logicalModel->mScriptRuns.Clear();
80 logicalModel->mFontRuns.Clear();
81 logicalModel->mWordBreakInfo.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,
101 LogicalModelPtr& logicalModel,
102 VisualModelPtr& visualModel,
103 MetricsPtr& metrics )
105 logicalModel = LogicalModel::New();
106 visualModel = VisualModel::New();
108 // 1) Convert to utf32
109 Vector<Character>& utf32Characters = logicalModel->mText;
110 utf32Characters.Resize( text.size() );
112 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( text.c_str() ),
114 &utf32Characters[0u] );
115 utf32Characters.Resize( numberOfCharacters );
117 // 2) Set the break and paragraph info.
118 Vector<LineBreakInfo>& lineBreakInfo = logicalModel->mLineBreakInfo;
119 lineBreakInfo.Resize( numberOfCharacters );
121 SetLineBreakInfo( utf32Characters,
126 if( 0u == numberOfCharacters )
128 // Nothing else to do if the number of characters is zero.
132 // Retrieves the word break info. The word break info is used to layout the text (where to wrap the text in lines).
133 Vector<WordBreakInfo>& wordBreakInfo = logicalModel->mWordBreakInfo;
134 wordBreakInfo.Resize( numberOfCharacters );
136 SetWordBreakInfo( utf32Characters,
141 // 3) Set the script info.
142 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
144 Vector<ScriptRun>& scripts = logicalModel->mScriptRuns;
145 multilanguageSupport.SetScripts( utf32Characters,
150 // 4) Set the font info
151 Vector<FontDescriptionRun>& fontDescriptionRuns = logicalModel->mFontDescriptionRuns;
152 fontDescriptionRuns = fontDescriptions;
153 Vector<FontRun>& validFonts = logicalModel->mFontRuns;
155 // The default font description.
156 TextAbstraction::FontDescription fontDescription;
158 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
159 fontClient.SetDpi( 96u, 96u );
161 // Validates the fonts. If there is a character with no assigned font it sets a default one.
162 // After this call, fonts are validated.
163 multilanguageSupport.ValidateFonts( utf32Characters,
167 TextAbstraction::FontClient::DEFAULT_POINT_SIZE,
172 // 5) Set the bidirectional info per paragraph.
173 Vector<Character> mirroredUtf32Characters;
174 bool textMirrored = false;
176 // Reserve some space for the vector of paragraph's bidirectional info.
177 Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = logicalModel->mBidirectionalParagraphInfo;
179 // Calculates the bidirectional info for the whole paragraph if it contains right to left scripts.
180 SetBidirectionalInfo( utf32Characters,
187 // Create the paragraph info.
188 logicalModel->CreateParagraphInfo( 0u,
189 numberOfCharacters );
191 // 6) Set character directions.
192 Vector<CharacterDirection>& characterDirections = logicalModel->mCharacterDirections;
193 if( 0u != bidirectionalInfo.Count() )
195 // Only set the character directions if there is right to left characters.
196 GetCharactersDirection( bidirectionalInfo,
200 characterDirections );
203 // This paragraph has right to left text. Some characters may need to be mirrored.
204 textMirrored = GetMirroredText( utf32Characters,
209 mirroredUtf32Characters );
213 // There is no right to left characters. Clear the directions vector.
214 characterDirections.Clear();
217 // 7) Shape the text.
219 Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
220 Vector<CharacterIndex>& glyphsToCharactersMap = visualModel->mGlyphsToCharacters;
221 Vector<Length>& charactersPerGlyph = visualModel->mCharactersPerGlyph;
222 Vector<GlyphIndex> newParagraphGlyphs;
224 const Vector<Character>& textToShape = textMirrored ? mirroredUtf32Characters : utf32Characters;
226 ShapeText( textToShape,
234 glyphsToCharactersMap,
236 newParagraphGlyphs );
238 // Create the 'number of glyphs' per character and the glyph to character conversion tables.
239 visualModel->CreateGlyphsPerCharacterTable( 0u, 0u, numberOfCharacters );
240 visualModel->CreateCharacterToGlyphTable( 0u, 0u, numberOfCharacters );
242 const Length numberOfGlyphs = glyphs.Count();
244 // 8) Get the glyph metrics
245 metrics = Metrics::New( fontClient );
247 GlyphInfo* glyphsBuffer = glyphs.Begin();
248 metrics->GetGlyphMetrics( glyphsBuffer, numberOfGlyphs );
250 // Update the width and advance of all new paragraph characters.
251 for( Vector<GlyphIndex>::ConstIterator it = newParagraphGlyphs.Begin(),
252 endIt = newParagraphGlyphs.End();
256 const GlyphIndex index = *it;
257 GlyphInfo& glyph = *( glyphsBuffer + index );
259 glyph.xBearing = 0.f;
264 // 9) Layout the text
265 Layout::Engine layoutEngine;
266 layoutEngine.SetMetrics( metrics );
267 layoutEngine.SetLayout( Layout::Engine::MULTI_LINE_BOX );
269 // Set the layout parameters.
270 const Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
271 const Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
273 Layout::Parameters layoutParameters( textArea,
274 utf32Characters.Begin(),
275 lineBreakInfo.Begin(),
276 wordBreakInfo.Begin(),
277 ( 0u != characterDirections.Count() ) ? characterDirections.Begin() : NULL,
279 glyphsToCharactersMap.Begin(),
280 charactersPerGlyph.Begin(),
281 charactersToGlyph.Begin(),
282 glyphsPerCharacter.Begin(),
284 Layout::HORIZONTAL_ALIGN_BEGIN );
286 Vector<LineRun>& lines = visualModel->mLines;
288 Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
289 glyphPositions.Resize( numberOfGlyphs );
291 layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( utf32Characters.Begin() + ( numberOfCharacters - 1u ) ) );
293 // The initial glyph and the number of glyphs to layout.
294 layoutParameters.startGlyphIndex = 0u;
295 layoutParameters.numberOfGlyphs = numberOfGlyphs;
296 layoutParameters.startLineIndex = 0u;
297 layoutParameters.estimatedNumberOfLines = logicalModel->mParagraphInfo.Count();
299 layoutEngine.LayoutText( layoutParameters,
305 // 10) Reorder the lines
306 if( 0u != bidirectionalInfo.Count() )
308 Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = logicalModel->mBidirectionalLineInfo;
311 const Length numberOfLines = lines.Count();
313 // Reorder the lines.
314 bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
315 ReorderLines( bidirectionalInfo,
319 bidirectionalLineInfo );
321 // Set the bidirectional info per line into the layout parameters.
322 layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
323 layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
325 if( options.reorder )
327 // Re-layout the text. Reorder those lines with right to left characters.
328 layoutEngine.ReLayoutRightToLeftLines( layoutParameters,
337 float alignmentOffset = 0.f;
338 layoutEngine.Align( textArea,
341 Layout::HORIZONTAL_ALIGN_BEGIN,
347 void ConfigureTextLabel( ControllerPtr controller )
349 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
350 fontClient.SetDpi( 93u, 93u );
352 // Set the text layout as multi-line.
353 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
355 // Set cursor's width to zero.
356 controller->GetLayoutEngine().SetCursorWidth( 0 );
358 // Disables the text input.
359 controller->EnableTextInput( NULL );
361 // Disables the vertical scrolling.
362 controller->SetVerticalScrollEnabled( false );
364 // Disables the horizontal scrolling.
365 controller->SetHorizontalScrollEnabled( false );
367 // Enable the text elide.
368 controller->SetTextElideEnabled( true );
371 void ConfigureTextField( ControllerPtr controller )
373 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
374 fontClient.SetDpi( 93u, 93u );
376 // Creates a decorator.
377 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
380 // Set the text layout as multi-line.
381 controller->GetLayoutEngine().SetLayout( Layout::Engine::SINGLE_LINE_BOX );
383 // Enables the text input.
384 controller->EnableTextInput( decorator );
386 // Enables the vertical scrolling after the text input has been enabled.
387 controller->SetVerticalScrollEnabled( false );
389 // Disables the horizontal scrolling.
390 controller->SetHorizontalScrollEnabled( true );
392 // No maximum number of characters.
393 controller->SetMaximumNumberOfCharacters( 50u );
395 // Disable the text elide.
396 controller->SetTextElideEnabled( false );
399 void ConfigureTextEditor( ControllerPtr controller )
401 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
402 fontClient.SetDpi( 93u, 93u );
404 // Creates a decorator.
405 Text::DecoratorPtr decorator = Text::Decorator::New( *controller,
408 // Set the text layout as multi-line.
409 controller->GetLayoutEngine().SetLayout( Layout::Engine::MULTI_LINE_BOX );
411 // Enables the text input.
412 controller->EnableTextInput( decorator );
414 // Enables the vertical scrolling after the text input has been enabled.
415 controller->SetVerticalScrollEnabled( true );
417 // Disables the horizontal scrolling.
418 controller->SetHorizontalScrollEnabled( false );
420 // No maximum number of characters.
421 controller->SetMaximumNumberOfCharacters( std::numeric_limits<Length>::max() );
423 // Disable the text elide.
424 controller->SetTextElideEnabled( false );
429 } // namespace Toolkit