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