[4.0] FreeType fonts/library - Stop creating new instances.
[platform/core/uifw/dali-adaptor.git] / text / dali / internal / text-abstraction / font-client-impl.cpp
1 /*
2  * Copyright (c) 2017 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/text-abstraction/font-client-impl.h>
20
21 // EXTERNAL INCLUDES
22 #ifndef DALI_PROFILE_UBUNTU
23 #include <vconf.h>
24 #endif
25
26 // INTERNAL INCLUDES
27 #include <singleton-service.h>
28 #include <dali/internal/text-abstraction/font-client-plugin-impl.h>
29
30 namespace Dali
31 {
32
33 namespace TextAbstraction
34 {
35
36 namespace Internal
37 {
38
39 FontClient::FontClient()
40 : mPlugin( nullptr ),
41   mDpiHorizontal( 0 ),
42   mDpiVertical( 0 )
43 {
44 }
45
46 FontClient::~FontClient()
47 {
48   delete mPlugin;
49 }
50
51 Dali::TextAbstraction::FontClient FontClient::Get()
52 {
53   Dali::TextAbstraction::FontClient fontClientHandle;
54
55   Dali::SingletonService service( SingletonService::Get() );
56   if ( service )
57   {
58     // Check whether the singleton is already created
59     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::TextAbstraction::FontClient ) );
60     if(handle)
61     {
62       // If so, downcast the handle
63       FontClient* impl = dynamic_cast< Dali::TextAbstraction::Internal::FontClient* >( handle.GetObjectPtr() );
64       fontClientHandle = Dali::TextAbstraction::FontClient( impl );
65     }
66     else // create and register the object
67     {
68       fontClientHandle = Dali::TextAbstraction::FontClient( new FontClient );
69       service.Register( typeid( fontClientHandle ), fontClientHandle );
70     }
71   }
72
73   return fontClientHandle;
74 }
75
76 void FontClient::ClearCache()
77 {
78   if( mPlugin )
79   {
80     mPlugin->ClearCache();
81   }
82 }
83
84
85 void FontClient::SetDpi( unsigned int horizontalDpi, unsigned int verticalDpi  )
86 {
87   mDpiHorizontal = horizontalDpi;
88   mDpiVertical = verticalDpi;
89
90   // Allow DPI to be set without loading plugin
91   if( mPlugin )
92   {
93     mPlugin->SetDpi( horizontalDpi, verticalDpi  );
94   }
95 }
96
97 void FontClient::GetDpi( unsigned int& horizontalDpi, unsigned int& verticalDpi )
98 {
99   horizontalDpi = mDpiHorizontal;
100   verticalDpi = mDpiVertical;
101 }
102
103 int FontClient::GetDefaultFontSize()
104 {
105   int fontSize( -1 );
106
107 #ifndef DALI_PROFILE_UBUNTU
108   vconf_get_int( VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize );
109 #endif // DALI_PROFILE_UBUNTU
110
111   return fontSize;
112 }
113
114 void FontClient::ResetSystemDefaults()
115 {
116   CreatePlugin();
117
118   mPlugin->ResetSystemDefaults();
119 }
120
121 void FontClient::GetDefaultFonts( FontList& defaultFonts )
122 {
123   CreatePlugin();
124
125   mPlugin->GetDefaultFonts( defaultFonts );
126 }
127
128 void FontClient::GetDefaultPlatformFontDescription( FontDescription& fontDescription )
129 {
130   CreatePlugin();
131
132   mPlugin->GetDefaultPlatformFontDescription( fontDescription );
133 }
134
135 void FontClient::GetDescription( FontId id, FontDescription& fontDescription )
136 {
137   CreatePlugin();
138
139   mPlugin->GetDescription( id, fontDescription );
140 }
141
142 PointSize26Dot6 FontClient::GetPointSize( FontId id )
143 {
144   CreatePlugin();
145
146   return mPlugin->GetPointSize( id );
147 }
148
149 bool FontClient::IsCharacterSupportedByFont( FontId fontId, Character character )
150 {
151   CreatePlugin();
152
153   return mPlugin->IsCharacterSupportedByFont( fontId, character );
154 }
155
156 void FontClient::GetSystemFonts( FontList& systemFonts )
157 {
158   CreatePlugin();
159
160   mPlugin->GetSystemFonts( systemFonts );
161 }
162
163 FontId FontClient::FindDefaultFont( Character charcode,
164                                     PointSize26Dot6 requestedPointSize,
165                                     bool preferColor )
166 {
167   CreatePlugin();
168
169   return mPlugin->FindDefaultFont( charcode,
170                                    requestedPointSize,
171                                    preferColor );
172 }
173
174 FontId FontClient::FindFallbackFont( Character charcode,
175                                      const FontDescription& preferredFontDescription,
176                                      PointSize26Dot6 requestedPointSize,
177                                      bool preferColor )
178 {
179   CreatePlugin();
180
181   return mPlugin->FindFallbackFont( charcode,
182                                     preferredFontDescription,
183                                     requestedPointSize,
184                                     preferColor );
185 }
186
187 bool FontClient::IsScalable( const FontPath& path )
188 {
189   CreatePlugin();
190
191   return mPlugin->IsScalable( path );
192 }
193
194 bool FontClient::IsScalable( const FontDescription& fontDescription )
195 {
196   CreatePlugin();
197
198   return mPlugin->IsScalable( fontDescription );
199 }
200
201 void FontClient::GetFixedSizes( const FontPath& path, Dali::Vector< PointSize26Dot6>& sizes )
202 {
203   CreatePlugin();
204
205   mPlugin->GetFixedSizes( path, sizes );
206 }
207
208 void FontClient::GetFixedSizes( const FontDescription& fontDescription,
209                                 Dali::Vector< PointSize26Dot6 >& sizes )
210 {
211   CreatePlugin();
212
213   mPlugin->GetFixedSizes( fontDescription, sizes );
214 }
215
216 FontId FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
217 {
218   CreatePlugin();
219
220   return mPlugin->GetFontId( path,
221                              requestedPointSize,
222                              faceIndex,
223                              true );
224 }
225
226 FontId FontClient::GetFontId( const FontDescription& fontDescription,
227                               PointSize26Dot6 requestedPointSize,
228                               FaceIndex faceIndex )
229 {
230   CreatePlugin();
231
232   return mPlugin->GetFontId( fontDescription,
233                              requestedPointSize,
234                              faceIndex );
235 }
236
237 void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
238 {
239   CreatePlugin();
240
241   return mPlugin->GetFontMetrics( fontId, metrics );
242 }
243
244 GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
245 {
246   CreatePlugin();
247
248   return mPlugin->GetGlyphIndex( fontId, charcode );
249 }
250
251 bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal )
252 {
253   CreatePlugin();
254
255   return mPlugin->GetGlyphMetrics( array, size, type, horizontal );
256 }
257
258 void FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
259 {
260   CreatePlugin();
261
262   mPlugin->CreateBitmap( fontId, glyphIndex, data, outlineWidth );
263 }
264
265 PixelData FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
266 {
267   CreatePlugin();
268
269   return mPlugin->CreateBitmap( fontId, glyphIndex, outlineWidth );
270 }
271
272 void FontClient::CreateVectorBlob( FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight )
273 {
274   CreatePlugin();
275
276   return mPlugin->CreateVectorBlob( fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight );
277 }
278
279 const GlyphInfo& FontClient::GetEllipsisGlyph( PointSize26Dot6 requestedPointSize )
280 {
281   CreatePlugin();
282
283   return mPlugin->GetEllipsisGlyph( requestedPointSize );
284 }
285
286 bool FontClient::IsColorGlyph( FontId fontId, GlyphIndex glyphIndex )
287 {
288   CreatePlugin();
289
290   return mPlugin->IsColorGlyph( fontId, glyphIndex );
291 }
292
293 bool FontClient::AddCustomFontDirectory( const FontPath& path )
294 {
295   CreatePlugin();
296
297   return mPlugin->AddCustomFontDirectory( path );
298 }
299
300 FT_FaceRec_* FontClient::GetFreetypeFace( FontId fontId )
301 {
302   CreatePlugin();
303
304   return mPlugin->GetFreetypeFace( fontId );
305 }
306
307 void FontClient::CreatePlugin()
308 {
309   if( !mPlugin )
310   {
311     mPlugin = new Plugin( mDpiHorizontal, mDpiVertical );
312   }
313 }
314
315 } // namespace Internal
316
317 } // namespace TextAbstraction
318
319 } // namespace Dali