2 * Copyright (c) 2015 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 <dali-toolkit/public-api/text/text-controller.h>
22 #include <dali/public-api/text-abstraction/font-client.h>
25 #include <dali-toolkit/public-api/text/character-set-conversion.h>
26 #include <dali-toolkit/public-api/text/layouts/layout-engine.h>
27 #include <dali-toolkit/public-api/text/logical-model.h>
28 #include <dali-toolkit/public-api/text/multi-language-support.h>
29 #include <dali-toolkit/public-api/text/script-run.h>
30 #include <dali-toolkit/public-api/text/shaper.h>
31 #include <dali-toolkit/public-api/text/text-view.h>
32 #include <dali-toolkit/public-api/text/visual-model.h>
43 struct Controller::Impl
46 : mNewTextArrived( false )
48 mLogicalModel = LogicalModel::New();
49 mVisualModel = VisualModel::New();
51 mView.SetVisualModel( mVisualModel );
53 mFontClient = TextAbstraction::FontClient::Get();
59 LogicalModelPtr mLogicalModel;
60 VisualModelPtr mVisualModel;
64 LayoutEngine mLayoutEngine;
66 TextAbstraction::FontClient mFontClient;
69 ControllerPtr Controller::New()
71 return ControllerPtr( new Controller() );
74 void Controller::SetText( const std::string& text )
76 // Keep until size negotiation
77 mImpl->mNewText = text;
78 mImpl->mNewTextArrived = true;
81 bool Controller::Relayout( const Vector2& size )
83 bool viewUpdated( false );
85 if( mImpl->mNewTextArrived )
87 std::string& text = mImpl->mNewText;
89 // Convert text into UTF-32
90 Vector<Character> utf32Characters;
91 utf32Characters.Resize( text.size() );
93 // This is a bit horrible but std::string returns a (signed) char*
94 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
96 Length characterCount = Utf8ToUtf32( utf8, text.size(), &utf32Characters[0] );
97 utf32Characters.Resize( characterCount );
99 Vector<ScriptRun> scripts;
100 MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
102 multilanguageSupport.SetScripts( utf32Characters,
105 Vector<FontRun> fonts;
106 multilanguageSupport.ValidateFonts( utf32Characters,
110 Vector<LineBreakInfo> lineBreakInfo;
111 lineBreakInfo.Resize( characterCount, TextAbstraction::LINE_NO_BREAK );
113 Vector<GlyphInfo> glyphs;
114 Vector<CharacterIndex> characterIndices;
115 Vector<Length> charactersPerGlyph;
117 ShapeText( utf32Characters,
123 charactersPerGlyph );
125 // Manipulate the logical model
126 mImpl->mLogicalModel->SetText( &utf32Characters[0], characterCount );
127 mImpl->mLogicalModel->SetLineBreakInfo( &lineBreakInfo[0], characterCount );
128 mImpl->mLogicalModel->SetScripts( &scripts[0], scripts.Count() );
129 mImpl->mLogicalModel->SetFonts( &fonts[0], fonts.Count() );
131 if( TextAbstraction::FontClient::Get().GetGlyphMetrics( &glyphs[0], glyphs.Size() ) )
133 // Update the visual model
134 mImpl->mLayoutEngine.UpdateVisualModel( size,
138 *mImpl->mVisualModel );
141 // Discard temporary text
142 mImpl->mNewTextArrived = false;
151 View& Controller::GetView()
156 LayoutEngine& Controller::GetLayoutEngine()
158 return mImpl->mLayoutEngine;
161 Controller::~Controller()
166 Controller::Controller()
169 mImpl = new Controller::Impl();
174 } // namespace Toolkit