Merge "Bidirectional support interface added." into new_text
[platform/core/uifw/dali-toolkit.git] / base / 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/text-view.h>
29 #include <dali-toolkit/public-api/text/visual-model.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Text
38 {
39
40 struct Controller::Impl
41 {
42   Impl()
43   : mNewTextArrived( false )
44   {
45     mLogicalModel = LogicalModel::New();
46     mVisualModel  = VisualModel::New();
47
48     mView.SetVisualModel( mVisualModel );
49
50     mFontClient = TextAbstraction::FontClient::Get();
51   }
52
53   std::string mNewText;
54   bool mNewTextArrived;
55
56   LogicalModelPtr mLogicalModel;
57   VisualModelPtr  mVisualModel;
58
59   View mView;
60
61   LayoutEngine mLayoutEngine;
62
63   TextAbstraction::FontClient mFontClient;
64 };
65
66 ControllerPtr Controller::New()
67 {
68   return ControllerPtr( new Controller() );
69 }
70
71 void Controller::SetText( const std::string& text )
72 {
73   // Keep until size negotiation
74   mImpl->mNewText = text;
75   mImpl->mNewTextArrived = true;
76 }
77
78 bool Controller::Relayout( const Vector2& size )
79 {
80   bool viewUpdated( false );
81
82   if( mImpl->mNewTextArrived )
83   {
84     std::string& text = mImpl->mNewText;
85
86     //  Convert text into UTF-32
87     Vector<Character> utf32Characters;
88     utf32Characters.Resize( text.size() );
89
90     // This is a bit horrible but std::string returns a (signed) char*
91     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
92
93     Length characterCount = Utf8ToUtf32( utf8, text.size(), &utf32Characters[0] );
94
95     // Manipulate the logical model
96     mImpl->mLogicalModel->SetText( &utf32Characters[0], characterCount );
97
98     // Update the visual model
99     mImpl->mLayoutEngine.UpdateVisualModel( size, *mImpl->mLogicalModel, *mImpl->mVisualModel );
100
101     // Discard temporary text
102     mImpl->mNewTextArrived = false;
103     text.clear();
104
105     viewUpdated = true;
106   }
107
108   return viewUpdated;
109 }
110
111 View& Controller::GetView()
112 {
113   return mImpl->mView;
114 }
115
116 LayoutEngine& Controller::GetLayoutEngine()
117 {
118   return mImpl->mLayoutEngine;
119 }
120
121 Controller::~Controller()
122 {
123   delete mImpl;
124 }
125
126 Controller::Controller()
127 : mImpl( NULL )
128 {
129   mImpl = new Controller::Impl();
130 }
131
132 } // namespace Text
133
134 } // namespace Toolkit
135
136 } // namespace Dali