2592653453a65f92895047a6dda4a1b57cc259fa
[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/shaper.h>
31 #include <dali-toolkit/public-api/text/text-view.h>
32 #include <dali-toolkit/public-api/text/visual-model.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Text
41 {
42
43 struct Controller::Impl
44 {
45   Impl()
46   : mNewTextArrived( false )
47   {
48     mLogicalModel = LogicalModel::New();
49     mVisualModel  = VisualModel::New();
50
51     mView.SetVisualModel( mVisualModel );
52
53     mFontClient = TextAbstraction::FontClient::Get();
54   }
55
56   std::string mNewText;
57   bool mNewTextArrived;
58
59   LogicalModelPtr mLogicalModel;
60   VisualModelPtr  mVisualModel;
61
62   View mView;
63
64   LayoutEngine mLayoutEngine;
65
66   TextAbstraction::FontClient mFontClient;
67 };
68
69 ControllerPtr Controller::New()
70 {
71   return ControllerPtr( new Controller() );
72 }
73
74 void Controller::SetText( const std::string& text )
75 {
76   // Keep until size negotiation
77   mImpl->mNewText = text;
78   mImpl->mNewTextArrived = true;
79 }
80
81 bool Controller::Relayout( const Vector2& size )
82 {
83   bool viewUpdated( false );
84
85   if( mImpl->mNewTextArrived )
86   {
87     std::string& text = mImpl->mNewText;
88
89     //  Convert text into UTF-32
90     Vector<Character> utf32Characters;
91     utf32Characters.Resize( text.size() );
92
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() );
95
96     Length characterCount = Utf8ToUtf32( utf8, text.size(), &utf32Characters[0] );
97     utf32Characters.Resize( characterCount );
98
99     Vector<ScriptRun> scripts;
100     MultilanguageSupport multilanguageSupport = MultilanguageSupport::Get();
101
102     multilanguageSupport.SetScripts( utf32Characters,
103                                      scripts );
104
105     Vector<FontRun> fonts;
106     multilanguageSupport.ValidateFonts( utf32Characters,
107                                         scripts,
108                                         fonts );
109
110     Vector<LineBreakInfo> lineBreakInfo;
111     lineBreakInfo.Resize( characterCount, TextAbstraction::LINE_NO_BREAK );
112
113     Vector<GlyphInfo> glyphs;
114     Vector<CharacterIndex> characterIndices;
115     Vector<Length> charactersPerGlyph;
116
117     ShapeText( utf32Characters,
118                lineBreakInfo,
119                scripts,
120                fonts,
121                glyphs,
122                characterIndices,
123                charactersPerGlyph );
124
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() );
130
131     if( TextAbstraction::FontClient::Get().GetGlyphMetrics( &glyphs[0], glyphs.Size() ) )
132     {
133       // Update the visual model
134       mImpl->mLayoutEngine.UpdateVisualModel( size,
135                                               glyphs,
136                                               characterIndices,
137                                               charactersPerGlyph,
138                                               *mImpl->mVisualModel );
139     }
140
141     // Discard temporary text
142     mImpl->mNewTextArrived = false;
143     text.clear();
144
145     viewUpdated = true;
146   }
147
148   return viewUpdated;
149 }
150
151 View& Controller::GetView()
152 {
153   return mImpl->mView;
154 }
155
156 LayoutEngine& Controller::GetLayoutEngine()
157 {
158   return mImpl->mLayoutEngine;
159 }
160
161 Controller::~Controller()
162 {
163   delete mImpl;
164 }
165
166 Controller::Controller()
167 : mImpl( NULL )
168 {
169   mImpl = new Controller::Impl();
170 }
171
172 } // namespace Text
173
174 } // namespace Toolkit
175
176 } // namespace Dali