[Tizen] Add EnsureFontClientCreated to StyleMonitor
[platform/core/uifw/dali-adaptor.git] / dali / internal / styling / common / style-monitor-impl.cpp
1 /*
2  * Copyright (c) 2014 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/internal/styling/common/style-monitor-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/file-loader.h>
23 #include <dali/devel-api/common/singleton-service.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/object/type-registry.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace Adaptor
35 {
36 namespace
37 {
38 #if defined(DEBUG_ENABLED)
39 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_STYLE_MONITOR");
40 #endif
41
42 /**
43  * Use font client to get the system default font family
44  * @param[in] fontClient handle to font client
45  * @param[out] fontFamily string representing font family
46  */
47 void GetSystemDefaultFontFamily(TextAbstraction::FontClient& fontClient, std::string& fontFamily)
48 {
49   TextAbstraction::FontDescription defaultFontDescription;
50   if(fontClient)
51   {
52     fontClient.GetDefaultPlatformFontDescription(defaultFontDescription);
53     fontFamily = defaultFontDescription.family;
54   }
55 }
56
57 } // unnamed namespace
58
59 Dali::StyleMonitor StyleMonitor::Get()
60 {
61   Dali::StyleMonitor styleMonitor;
62
63   Dali::SingletonService service(SingletonService::Get());
64   if(service)
65   {
66     // Check whether the singleton is already created
67     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::StyleMonitor));
68     if(handle)
69     {
70       // If so, downcast the handle
71       styleMonitor = Dali::StyleMonitor(dynamic_cast<StyleMonitor*>(handle.GetObjectPtr()));
72     }
73     else
74     {
75       styleMonitor = Dali::StyleMonitor(new StyleMonitor());
76       service.Register(typeid(styleMonitor), styleMonitor);
77     }
78   }
79
80   return styleMonitor;
81 }
82
83 StyleMonitor::StyleMonitor()
84 : mDefaultFontSize(-1)
85 {
86 }
87
88 StyleMonitor::~StyleMonitor()
89 {
90 }
91
92 bool StyleMonitor::EnsureFontClientCreated()
93 {
94   if(!mFontClient)
95   {
96     mFontClient = TextAbstraction::FontClient::Get();
97     GetSystemDefaultFontFamily(mFontClient, mDefaultFontFamily);
98     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "StyleMonitor::StyleMonitor::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str());
99     mDefaultFontSize = mFontClient.GetDefaultFontSize();
100   }
101
102   return mFontClient != nullptr ? true : false;
103 }
104
105 void StyleMonitor::StyleChanged(StyleChange::Type styleChange)
106 {
107   switch(styleChange)
108   {
109     case StyleChange::DEFAULT_FONT_CHANGE:
110     {
111       if(EnsureFontClientCreated())
112       {
113         mFontClient.ResetSystemDefaults();
114         GetSystemDefaultFontFamily(mFontClient, mDefaultFontFamily);
115       }
116       DALI_LOG_INFO(gLogFilter, Debug::Verbose, "StyleMonitor::StyleChanged::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str());
117       break;
118     }
119
120     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
121     {
122       if(EnsureFontClientCreated())
123       {
124         mDefaultFontSize = mFontClient.GetDefaultFontSize();
125       }
126       break;
127     }
128
129     case StyleChange::THEME_CHANGE:
130     {
131       break;
132     }
133   }
134
135   EmitStyleChangeSignal(styleChange);
136 }
137
138 std::string StyleMonitor::GetDefaultFontFamily() const
139 {
140   return mDefaultFontFamily;
141 }
142
143 std::string StyleMonitor::GetDefaultFontStyle() const
144 {
145   return mDefaultFontStyle;
146 }
147
148 int StyleMonitor::GetDefaultFontSize() const
149 {
150   return mDefaultFontSize;
151 }
152
153 const std::string& StyleMonitor::GetTheme() const
154 {
155   return mUserDefinedThemeFilePath;
156 }
157
158 void StyleMonitor::SetTheme(const std::string& path)
159 {
160   mUserDefinedThemeFilePath = path;
161   EmitStyleChangeSignal(StyleChange::THEME_CHANGE);
162 }
163
164 bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
165 {
166   bool retval(false);
167
168   std::streampos     bufferSize = 0;
169   Dali::Vector<char> fileBuffer;
170   if(Dali::FileLoader::ReadFile(filename, bufferSize, fileBuffer, FileLoader::FileType::BINARY))
171   {
172     output.assign(&fileBuffer[0], bufferSize);
173     retval = true;
174   }
175
176   return retval;
177 }
178
179 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
180 {
181   return mStyleChangeSignal;
182 }
183
184 void StyleMonitor::EmitStyleChangeSignal(StyleChange::Type styleChange)
185 {
186   if(!mStyleChangeSignal.Empty())
187   {
188     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "StyleMonitor::EmitStyleChangeSignal\n");
189     Dali::StyleMonitor handle(this);
190     mStyleChangeSignal.Emit(handle, styleChange);
191   }
192 }
193
194 } // namespace Adaptor
195
196 } // namespace Internal
197
198 } // namespace Dali