5d17658291ca7ab2d3c005e6a4f741b4edecfe9b
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / font-client-impl.cpp
1 /*
2  * Copyright (c) 2022 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/text-abstraction/font-client-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <thread>
23 #if defined(VCONF_ENABLED)
24 #include <vconf.h>
25 #endif
26
27 // INTERNAL INCLUDES
28 #include <dali/devel-api/common/singleton-service.h>
29 #include <dali/internal/system/common/logging.h>
30 #include <dali/internal/text/text-abstraction/plugin/font-client-plugin-impl.h>
31 #include <dali/internal/window-system/common/window-system.h>
32
33 #include <dali/devel-api/text-abstraction/glyph-info.h>
34
35 #define FONT_LOG_MESSAGE(level, format, ...)                                   \
36   do                                                                           \
37   {                                                                            \
38     char buffer[256];                                                          \
39     int result = std::snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
40     if (result >= static_cast<int>(sizeof(buffer)))                            \
41     {                                                                          \
42       std::string log("Font log message is too long to fit in the buffer.\n"); \
43       Dali::TizenPlatform::LogMessage(Dali::Integration::Log::ERROR, log);     \
44       break;                                                                   \
45     }                                                                          \
46     std::string log(buffer);                                                   \
47     Dali::TizenPlatform::LogMessage(level, log);                               \
48   } while(0)
49
50 namespace Dali
51 {
52 namespace TextAbstraction
53 {
54 namespace Internal
55 {
56 Dali::TextAbstraction::FontClient FontClient::gPreCreatedFontClient(NULL);
57 std::thread                       gPreCacheThread;
58 /* TODO: This is to prevent duplicate calls of font pre-cache.
59  * We may support this later, but currently we can't guarantee the behaviour
60  * if there is a pre-cache call from the user after the font client has been created. */
61 bool gFontPreCacheAvailable = true;
62
63 FontClient::FontClient()
64 : mPlugin(nullptr),
65   mDpiHorizontal(0),
66   mDpiVertical(0)
67 {
68 }
69
70 FontClient::~FontClient()
71 {
72   delete mPlugin;
73 }
74
75 Dali::TextAbstraction::FontClient FontClient::Get()
76 {
77   Dali::TextAbstraction::FontClient fontClientHandle;
78
79   Dali::SingletonService service(SingletonService::Get());
80   if(service)
81   {
82     // Check whether the singleton is already created
83     Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::TextAbstraction::FontClient));
84     if(handle)
85     {
86       // If so, downcast the handle
87       FontClient* impl = dynamic_cast<Dali::TextAbstraction::Internal::FontClient*>(handle.GetObjectPtr());
88       fontClientHandle = Dali::TextAbstraction::FontClient(impl);
89     }
90     else // create and register the object
91     {
92       if(gPreCacheThread.joinable())
93       {
94         gPreCacheThread.join();
95         FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "FontClient PreCache thread join\n");
96       }
97
98       if(gPreCreatedFontClient)
99       {
100         fontClientHandle = gPreCreatedFontClient;
101         gPreCreatedFontClient.Reset(); // No longer needed
102       }
103       else
104       {
105         fontClientHandle = Dali::TextAbstraction::FontClient(new FontClient);
106
107         // Make DefaultFontDescription cached
108         Dali::TextAbstraction::FontDescription defaultFontDescription;
109         fontClientHandle.GetDefaultPlatformFontDescription(defaultFontDescription);
110       }
111
112       gFontPreCacheAvailable = false;
113
114       uint32_t horizontalDpi, verticalDpi;
115       fontClientHandle.GetDpi(horizontalDpi, verticalDpi);
116       if(horizontalDpi == 0u || verticalDpi == 0u)
117       {
118         horizontalDpi = verticalDpi = 0u;
119         Dali::Internal::Adaptor::WindowSystem::GetDpi(horizontalDpi, verticalDpi);
120         fontClientHandle.SetDpi(horizontalDpi, verticalDpi);
121       }
122
123       service.Register(typeid(fontClientHandle), fontClientHandle);
124     }
125   }
126
127   return fontClientHandle;
128 }
129
130 Dali::TextAbstraction::FontClient FontClient::PreInitialize()
131 {
132   // Pre-cached font client already exists or pre-cache thread already running.
133   // Font client pre-cache includes caching of the default font description.
134   if((gPreCreatedFontClient && !gFontPreCacheAvailable) ||
135      (gPreCacheThread.joinable()))
136   {
137     return gPreCreatedFontClient;
138   }
139
140   gPreCreatedFontClient = Dali::TextAbstraction::FontClient(new FontClient);
141
142   // Make DefaultFontDescription cached
143   Dali::TextAbstraction::FontDescription defaultFontDescription;
144   gPreCreatedFontClient.GetDefaultPlatformFontDescription(defaultFontDescription);
145
146   return gPreCreatedFontClient;
147 }
148
149 void FontClient::PreCacheRun(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily)
150 {
151   if(gFontPreCacheAvailable)
152   {
153     gFontPreCacheAvailable = false;
154     FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "BEGIN: DALI_TEXT_PRECACHE_RUN\n");
155     if(!gPreCreatedFontClient)
156     {
157       gPreCreatedFontClient = Dali::TextAbstraction::FontClient(new FontClient);
158     }
159     GetImplementation(gPreCreatedFontClient).FontPreCache(fallbackFamilyList, extraFamilyList, localeFamily);
160     FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "END: DALI_TEXT_PRECACHE_RUN\n");
161   }
162   else
163   {
164     FONT_LOG_MESSAGE(Dali::Integration::Log::ERROR, "FontClient pre-cache run failed, as a pre-cached font client already exists.\n");
165   }
166 }
167
168 void FontClient::PreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily, bool useThread)
169 {
170   if(!gFontPreCacheAvailable)
171   {
172     FONT_LOG_MESSAGE(Dali::Integration::Log::ERROR, "FontClient pre-cache has been completed or the font client has already been created.\n");
173     return;
174   }
175
176   FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "FontClient PreCache fallbackFamilyList : %zu\n", fallbackFamilyList.size());
177   FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "FontClient PreCache extraFamilyList    : %zu\n", extraFamilyList.size());
178   FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "FontClient PreCache localeFamily       : %s\n", localeFamily.c_str());
179   FONT_LOG_MESSAGE(Dali::Integration::Log::INFO, "FontClient PreCache useThread          : %d\n", useThread);
180
181   if(gPreCacheThread.joinable())
182   {
183     FONT_LOG_MESSAGE(Dali::Integration::Log::ERROR, "FontClient pre-cache thread already running.\n");
184   }
185   else
186   {
187     if(useThread)
188     {
189       gPreCacheThread = std::thread(PreCacheRun, fallbackFamilyList, extraFamilyList, localeFamily);
190     }
191     else
192     {
193       PreCacheRun(fallbackFamilyList, extraFamilyList, localeFamily);
194     }
195   }
196 }
197
198 void FontClient::ClearCache()
199 {
200   if(mPlugin)
201   {
202     mPlugin->ClearCache();
203   }
204 }
205
206 void FontClient::SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi)
207 {
208   mDpiHorizontal = horizontalDpi;
209   mDpiVertical   = verticalDpi;
210
211   // Allow DPI to be set without loading plugin
212   if(mPlugin)
213   {
214     mPlugin->SetDpi(horizontalDpi, verticalDpi);
215   }
216 }
217
218 void FontClient::GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi)
219 {
220   horizontalDpi = mDpiHorizontal;
221   verticalDpi   = mDpiVertical;
222 }
223
224 int FontClient::GetDefaultFontSize()
225 {
226   int fontSize(-1);
227
228 #if defined(VCONF_ENABLED)
229   vconf_get_int(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_SIZE, &fontSize);
230 #endif
231
232   return fontSize;
233 }
234
235 void FontClient::ResetSystemDefaults()
236 {
237   CreatePlugin();
238
239   mPlugin->ResetSystemDefaults();
240 }
241
242 void FontClient::GetDefaultFonts(FontList& defaultFonts)
243 {
244   CreatePlugin();
245
246   mPlugin->GetDefaultFonts(defaultFonts);
247 }
248
249 void FontClient::FontPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily)
250 {
251   CreatePlugin();
252
253   mPlugin->FontPreCache(fallbackFamilyList, extraFamilyList, localeFamily);
254 }
255
256 void FontClient::GetDefaultPlatformFontDescription(FontDescription& fontDescription)
257 {
258   CreatePlugin();
259
260   mPlugin->GetDefaultPlatformFontDescription(fontDescription);
261 }
262
263 void FontClient::GetDescription(FontId fontId, FontDescription& fontDescription)
264 {
265   CreatePlugin();
266
267   mPlugin->GetDescription(fontId, fontDescription);
268 }
269
270 PointSize26Dot6 FontClient::GetPointSize(FontId fontId)
271 {
272   CreatePlugin();
273
274   return mPlugin->GetPointSize(fontId);
275 }
276
277 bool FontClient::IsCharacterSupportedByFont(FontId fontId, Character character)
278 {
279   CreatePlugin();
280
281   return mPlugin->IsCharacterSupportedByFont(fontId, character);
282 }
283
284 void FontClient::GetSystemFonts(FontList& systemFonts)
285 {
286   CreatePlugin();
287
288   mPlugin->GetSystemFonts(systemFonts);
289 }
290
291 FontId FontClient::FindDefaultFont(Character       charcode,
292                                    PointSize26Dot6 requestedPointSize,
293                                    bool            preferColor)
294 {
295   CreatePlugin();
296
297   return mPlugin->FindDefaultFont(charcode,
298                                   requestedPointSize,
299                                   preferColor);
300 }
301
302 FontId FontClient::FindFallbackFont(Character              charcode,
303                                     const FontDescription& preferredFontDescription,
304                                     PointSize26Dot6        requestedPointSize,
305                                     bool                   preferColor)
306 {
307   CreatePlugin();
308
309   return mPlugin->FindFallbackFont(charcode,
310                                    preferredFontDescription,
311                                    requestedPointSize,
312                                    preferColor);
313 }
314
315 bool FontClient::IsScalable(const FontPath& path)
316 {
317   CreatePlugin();
318
319   return mPlugin->IsScalable(path);
320 }
321
322 bool FontClient::IsScalable(const FontDescription& fontDescription)
323 {
324   CreatePlugin();
325
326   return mPlugin->IsScalable(fontDescription);
327 }
328
329 void FontClient::GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes)
330 {
331   CreatePlugin();
332
333   mPlugin->GetFixedSizes(path, sizes);
334 }
335
336 void FontClient::GetFixedSizes(const FontDescription&         fontDescription,
337                                Dali::Vector<PointSize26Dot6>& sizes)
338 {
339   CreatePlugin();
340
341   mPlugin->GetFixedSizes(fontDescription, sizes);
342 }
343
344 bool FontClient::HasItalicStyle(FontId fontId) const
345 {
346   if(!mPlugin)
347   {
348     return false;
349   }
350   return mPlugin->HasItalicStyle(fontId);
351 }
352
353 FontId FontClient::GetFontId(const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex)
354 {
355   CreatePlugin();
356
357   return mPlugin->GetFontIdByPath(path,
358                                   requestedPointSize,
359                                   faceIndex,
360                                   true);
361 }
362
363 FontId FontClient::GetFontId(const FontDescription& fontDescription,
364                              PointSize26Dot6        requestedPointSize,
365                              FaceIndex              faceIndex)
366 {
367   CreatePlugin();
368
369   return mPlugin->GetFontId(fontDescription,
370                             requestedPointSize,
371                             faceIndex);
372 }
373
374 FontId FontClient::GetFontId(const BitmapFont& bitmapFont)
375 {
376   CreatePlugin();
377
378   return mPlugin->GetFontId(bitmapFont);
379 }
380
381 void FontClient::GetFontMetrics(FontId fontId, FontMetrics& metrics)
382 {
383   CreatePlugin();
384
385   mPlugin->GetFontMetrics(fontId, metrics);
386 }
387
388 GlyphIndex FontClient::GetGlyphIndex(FontId fontId, Character charcode)
389 {
390   CreatePlugin();
391
392   return mPlugin->GetGlyphIndex(fontId, charcode);
393 }
394
395 GlyphIndex FontClient::GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector)
396 {
397   CreatePlugin();
398
399   return mPlugin->GetGlyphIndex(fontId, charcode, variantSelector);
400 }
401
402 bool FontClient::GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal)
403 {
404   CreatePlugin();
405
406   return mPlugin->GetGlyphMetrics(array, size, type, horizontal);
407 }
408
409 void FontClient::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth)
410 {
411   CreatePlugin();
412
413   mPlugin->CreateBitmap(fontId, glyphIndex, isItalicRequired, isBoldRequired, data, outlineWidth);
414 }
415
416 PixelData FontClient::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth)
417 {
418   CreatePlugin();
419
420   return mPlugin->CreateBitmap(fontId, glyphIndex, outlineWidth);
421 }
422
423 void FontClient::CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight)
424 {
425   CreatePlugin();
426
427   mPlugin->CreateVectorBlob(fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight);
428 }
429
430 const GlyphInfo& FontClient::GetEllipsisGlyph(PointSize26Dot6 requestedPointSize)
431 {
432   CreatePlugin();
433
434   return mPlugin->GetEllipsisGlyph(requestedPointSize);
435 }
436
437 bool FontClient::IsColorGlyph(FontId fontId, GlyphIndex glyphIndex)
438 {
439   CreatePlugin();
440
441   return mPlugin->IsColorGlyph(fontId, glyphIndex);
442 }
443
444 GlyphIndex FontClient::CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat)
445 {
446   CreatePlugin();
447
448   return mPlugin->CreateEmbeddedItem(description, pixelFormat);
449 }
450
451 void FontClient::EnableAtlasLimitation(bool enabled)
452 {
453   CreatePlugin();
454   return mPlugin->EnableAtlasLimitation(enabled);
455 }
456
457 bool FontClient::IsAtlasLimitationEnabled() const
458 {
459   if(mPlugin)
460   {
461     return mPlugin->IsAtlasLimitationEnabled();
462   }
463   return TextAbstraction::FontClient::DEFAULT_ATLAS_LIMITATION_ENABLED;
464 }
465
466 Size FontClient::GetMaximumTextAtlasSize() const
467 {
468   if(mPlugin)
469   {
470     return mPlugin->GetMaximumTextAtlasSize();
471   }
472   return TextAbstraction::FontClient::MAX_TEXT_ATLAS_SIZE;
473 }
474
475 Size FontClient::GetDefaultTextAtlasSize() const
476 {
477   if(mPlugin)
478   {
479     return mPlugin->GetDefaultTextAtlasSize();
480   }
481   return TextAbstraction::FontClient::DEFAULT_TEXT_ATLAS_SIZE;
482 }
483
484 Size FontClient::GetCurrentMaximumBlockSizeFitInAtlas() const
485 {
486   if(mPlugin)
487   {
488     return mPlugin->GetCurrentMaximumBlockSizeFitInAtlas();
489   }
490   return TextAbstraction::FontClient::DEFAULT_TEXT_ATLAS_SIZE;
491 }
492
493 bool FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
494 {
495   CreatePlugin();
496   return mPlugin->SetCurrentMaximumBlockSizeFitInAtlas(currentMaximumBlockSizeFitInAtlas);
497 }
498
499 uint32_t FontClient::GetNumberOfPointsPerOneUnitOfPointSize() const
500 {
501   if(mPlugin)
502   {
503     return mPlugin->GetNumberOfPointsPerOneUnitOfPointSize();
504   }
505   return TextAbstraction::FontClient::NUMBER_OF_POINTS_PER_ONE_UNIT_OF_POINT_SIZE;
506   ;
507 }
508
509 FT_FaceRec_* FontClient::GetFreetypeFace(FontId fontId)
510 {
511   CreatePlugin();
512
513   return mPlugin->GetFreetypeFace(fontId);
514 }
515
516 FontDescription::Type FontClient::GetFontType(FontId fontId)
517 {
518   CreatePlugin();
519
520   return mPlugin->GetFontType(fontId);
521 }
522
523 bool FontClient::AddCustomFontDirectory(const FontPath& path)
524 {
525   CreatePlugin();
526
527   return mPlugin->AddCustomFontDirectory(path);
528 }
529
530 HarfBuzzFontHandle FontClient::GetHarfBuzzFont(FontId fontId)
531 {
532   CreatePlugin();
533
534   return mPlugin->GetHarfBuzzFont(fontId);
535 }
536
537 void FontClient::CreatePlugin()
538 {
539   if(!mPlugin)
540   {
541     mPlugin = new Plugin(mDpiHorizontal, mDpiVertical);
542   }
543 }
544
545 } // namespace Internal
546
547 } // namespace TextAbstraction
548
549 } // namespace Dali