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