6fe0f33832bfd9c0f4d5bc90204112cbf65dba87
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / text / text-controller.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/public-api/text/text-controller.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/text-abstraction/font-client.h>
23
24 // INTERNAL INCLUDES
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/text-view.h>
31 #include <dali-toolkit/public-api/text/visual-model.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Text
40 {
41
42 struct Controller::Impl
43 {
44   Impl()
45   : mNewTextArrived( false )
46   {
47     mLogicalModel = LogicalModel::New();
48     mVisualModel  = VisualModel::New();
49
50     mView.SetVisualModel( mVisualModel );
51
52     mFontClient = TextAbstraction::FontClient::Get();
53   }
54
55   std::string mNewText;
56   bool mNewTextArrived;
57
58   LogicalModelPtr mLogicalModel;
59   VisualModelPtr  mVisualModel;
60
61   View mView;
62
63   LayoutEngine mLayoutEngine;
64
65   TextAbstraction::FontClient mFontClient;
66 };
67
68 ControllerPtr Controller::New()
69 {
70   return ControllerPtr( new Controller() );
71 }
72
73 void Controller::SetText( const std::string& text )
74 {
75   // Keep until size negotiation
76   mImpl->mNewText = text;
77   mImpl->mNewTextArrived = true;
78 }
79
80 bool Controller::Relayout( const Vector2& size )
81 {
82   bool viewUpdated( false );
83
84   if( mImpl->mNewTextArrived )
85   {
86     std::string& text = mImpl->mNewText;
87
88     //  Convert text into UTF-32
89     Vector<Character> utf32Characters;
90     utf32Characters.Resize( text.size() );
91
92     // This is a bit horrible but std::string returns a (signed) char*
93     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
94
95     Length characterCount = Utf8ToUtf32( utf8, text.size(), &utf32Characters[0] );
96     utf32Characters.Resize( characterCount );
97
98     Vector<ScriptRun> scripts;
99     MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
100
101     multilanguageSupport.SetScripts( utf32Characters,
102                                      scripts );
103
104     Vector<FontRun> fonts;
105     multilanguageSupport.ValidateFonts( utf32Characters,
106                                         scripts,
107                                         fonts );
108
109     // Manipulate the logical model
110     mImpl->mLogicalModel->SetText( &utf32Characters[0], characterCount );
111     mImpl->mLogicalModel->SetScripts( &scripts[0], scripts.Count() );
112     mImpl->mLogicalModel->SetFonts( &fonts[0], fonts.Count() );
113
114     // Update the visual model
115     mImpl->mLayoutEngine.UpdateVisualModel( size, *mImpl->mLogicalModel, *mImpl->mVisualModel );
116
117     // Discard temporary text
118     mImpl->mNewTextArrived = false;
119     text.clear();
120
121     viewUpdated = true;
122   }
123
124   return viewUpdated;
125 }
126
127 View& Controller::GetView()
128 {
129   return mImpl->mView;
130 }
131
132 LayoutEngine& Controller::GetLayoutEngine()
133 {
134   return mImpl->mLayoutEngine;
135 }
136
137 Controller::~Controller()
138 {
139   delete mImpl;
140 }
141
142 Controller::Controller()
143 : mImpl( NULL )
144 {
145   mImpl = new Controller::Impl();
146 }
147
148 } // namespace Text
149
150 } // namespace Toolkit
151
152 } // namespace Dali