[Tizen] Add support for FontClientFontPreLoad API
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / font-client-plugin-impl.h
1 #ifndef DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H
2 #define DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
23 #include <dali/devel-api/text-abstraction/bitmap-font.h>
24 #include <dali/devel-api/text-abstraction/font-metrics.h>
25 #include <dali/devel-api/text-abstraction/glyph-info.h>
26 #include <dali/internal/text/text-abstraction/font-client-impl.h>
27 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
28 #include <dali/internal/text/text-abstraction/plugin/embedded-item.h>
29 #include <dali/internal/text/text-abstraction/plugin/font-face-cache-item.h>
30 #include <dali/internal/text/text-abstraction/plugin/pixel-buffer-cache-item.h>
31
32 #ifdef ENABLE_VECTOR_BASED_TEXT_RENDERING
33 #include <third-party/glyphy/vector-font-cache.h>
34 #else
35 class VectorFontCache;
36 #endif
37
38 // EXTERNAL INCLUDES
39 #include <unordered_map>
40
41 #include <ft2build.h>
42 #include FT_FREETYPE_H
43 #include FT_GLYPH_H
44 #include FT_STROKER_H
45 #include FT_SYNTHESIS_H
46
47 // forward declarations of font config types.
48 struct _FcCharSet;
49 struct _FcFontSet;
50 struct _FcPattern;
51
52 namespace Dali
53 {
54 namespace TextAbstraction
55 {
56 namespace Internal
57 {
58 /**
59  * @brief Type used for indices addressing the vector with front descriptions of validated fonts.
60  */
61 typedef uint32_t FontDescriptionId;
62
63 /**
64  * @brief Vector of character sets.
65  */
66 typedef Vector<_FcCharSet*> CharacterSetList;
67
68 /**
69  * @brief FontClient implementation.
70  */
71 struct FontClient::Plugin
72 {
73 public: // Dali::TextAbstraction::FontClient
74   /**
75    * Constructor.
76    *
77    * Initializes the FreeType library.
78    * Initializes the dpi values.
79    *
80    * @param[in] horizontalDpi The horizontal dpi.
81    * @param[in] verticalDpi The vertical dpi.
82    */
83   Plugin(unsigned int horizontalDpi, unsigned int verticalDpi);
84
85   /**
86    * Default destructor.
87    *
88    * Frees any allocated resource.
89    */
90   ~Plugin();
91
92   /**
93    * @copydoc Dali::TextAbstraction::FontClient::ClearCache()
94    */
95   void ClearCache() const;
96
97   /**
98    * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
99    */
100   void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
101
102   /**
103    * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
104    */
105   void ResetSystemDefaults() const;
106
107   /**
108    * @copydoc Dali::TextAbstraction::FontClient::InitDefaultFontDescription()
109    */
110   void InitDefaultFontDescription() const;
111
112   /**
113    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
114    */
115   void GetDefaultPlatformFontDescription(FontDescription& fontDescription) const;
116
117   /**
118    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
119    */
120   void GetDefaultFonts(FontList& defaultFonts) const;
121
122   /**
123    * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
124    */
125   void GetSystemFonts(FontList& systemFonts) const;
126
127   /**
128    * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
129    */
130   void GetDescription(FontId fontId, FontDescription& fontDescription) const;
131
132   /**
133    * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
134    */
135   PointSize26Dot6 GetPointSize(FontId fontId) const;
136
137   /**
138    * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
139    */
140   bool IsCharacterSupportedByFont(FontId fontId, Character character) const;
141
142   /**
143    * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
144    */
145   FontId FindDefaultFont(Character       charcode,
146                          PointSize26Dot6 requestedPointSize,
147                          bool            preferColor) const;
148
149   /**
150    * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
151    */
152   FontId FindFallbackFont(Character              charcode,
153                           const FontDescription& preferredFontDescription,
154                           PointSize26Dot6        requestedPointSize,
155                           bool                   preferColor) const;
156
157   /**
158    * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
159    *
160    * @param[in] cacheDescription Whether to cache the font description.
161    */
162   FontId GetFontIdByPath(const FontPath& path,
163                          PointSize26Dot6 requestedPointSize,
164                          FaceIndex       faceIndex,
165                          bool            cacheDescription) const;
166
167   /**
168    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
169    */
170   FontId GetFontId(const FontDescription& fontDescription,
171                    PointSize26Dot6        requestedPointSize,
172                    FaceIndex              faceIndex) const;
173
174   /**
175    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const BitmapFont& bitmapFont )
176    */
177   FontId GetFontId(const BitmapFont& bitmapFont) const;
178
179   /**
180    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
181    */
182   bool IsScalable(const FontPath& path) const;
183
184   /**
185    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
186    */
187   bool IsScalable(const FontDescription& fontDescription) const;
188
189   /**
190    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
191    */
192   void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes) const;
193
194   /**
195    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
196    */
197   void GetFixedSizes(const FontDescription&         fontDescription,
198                      Dali::Vector<PointSize26Dot6>& sizes) const;
199
200   /**
201    * @copydoc Dali::TextAbstraction::FontClient::HasItalicStyle()
202    */
203   bool HasItalicStyle(FontId fontId) const;
204
205   /**
206    * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
207    */
208   void GetFontMetrics(FontId fontId, FontMetrics& metrics) const;
209
210   /**
211    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
212    */
213   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode) const;
214
215   /**
216    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
217    */
218   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector) const;
219
220   /**
221    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
222    */
223   bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal) const;
224
225   /**
226    * Helper for GetGlyphMetrics when using bitmaps
227    */
228   bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal) const;
229
230   /**
231    * Helper for GetGlyphMetrics when using vectors
232    */
233   bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal) const;
234
235   /**
236    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth )
237    */
238   void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth) const;
239
240   /**
241    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
242    */
243   PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth) const;
244
245   /**
246    * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
247    */
248   void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight) const;
249
250   /**
251    * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
252    */
253   const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize) const;
254
255   /**
256    * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
257    */
258   bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex) const;
259
260   /**
261    * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
262    */
263   GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat) const;
264
265   /**
266    * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
267    */
268   void EnableAtlasLimitation(bool enabled);
269
270   /**
271    * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
272    */
273   bool IsAtlasLimitationEnabled() const;
274
275   /**
276    * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
277    */
278   Size GetMaximumTextAtlasSize() const;
279
280   /**
281    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
282    */
283   Size GetDefaultTextAtlasSize() const;
284
285   /**
286    * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
287    */
288   Size GetCurrentMaximumBlockSizeFitInAtlas() const;
289
290   /**
291    * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
292    */
293   bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
294
295   /**
296    * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
297    */
298   uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
299
300   /**
301    * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
302    */
303   bool AddCustomFontDirectory(const FontPath& path);
304
305 public: // Dali::TextAbstraction::Internal::FontClient
306   /**
307    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
308    */
309   FT_FaceRec_* GetFreetypeFace(FontId fontId) const;
310
311   /**
312    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
313    */
314   FontDescription::Type GetFontType(FontId fontId) const;
315
316   /**
317    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetHarfBuzzFont()
318    */
319   HarfBuzzFontHandle GetHarfBuzzFont(FontId fontId) const;
320
321   /**
322    * @copydoc Dali::TextAbstraction::Internal::FontClient::FontPreCache()
323    */
324   void FontPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily) const;
325
326   /**
327    * @copydoc Dali::TextAbstraction::Internal::FontClient::FontPreLoad()
328    */
329   void FontPreLoad(const FontPathList& fontPathList, const FontPathList& memoryFontPathList) const;
330
331 private:
332   /**
333    * Get the cached font item for the given font
334    * @param[in] fontId The font id to search for
335    * @return the matching cached font item
336    */
337   const FontCacheItemInterface* GetCachedFontItem(FontId fontId) const;
338
339   /**
340    * @brief Finds within the @p fontList a font which support the @p carcode.
341    *
342    * @param[in] fontList A list of font paths, family, width, weight and slant.
343    * @param[in] characterSetList A list that contains a character set for each description of the font list.
344    * @param[in] charcode The character for which a font is needed.
345    * @param[in] requestedPointSize The point size in 26.6 fractional points.
346    * @param[in] preferColor @e true if a color font is preferred.
347    *
348    * @return A valid font identifier, or zero if no font is found.
349    */
350   FontId FindFontForCharacter(const FontList&         fontList,
351                               const CharacterSetList& characterSetList,
352                               Character               charcode,
353                               PointSize26Dot6         requestedPointSize,
354                               bool                    preferColor) const;
355
356   /**
357    * @brief Creates a font.
358    *
359    * @param[in] path The path to the font file name.
360    * @param[in] requestedPointSize The requested point size.
361    * @param[in] faceIndex A face index.
362    * @param[in] cacheDescription Whether to cache the font description.
363    *
364    * @return The font identifier.
365    */
366   FontId CreateFont(const FontPath& path,
367                     PointSize26Dot6 requestedPointSize,
368                     FaceIndex       faceIndex,
369                     bool            cacheDescription) const;
370
371
372   /**
373    * @brief Caches font data for the specified font path if it is not already cached.
374    *
375    * If the font data is not already cached, this function will load the font file
376    * from disk and cache the data for future use.
377    *
378    * @param[in] fontPath The font path to cache the data for.
379    */
380   void CacheFontDataFromFile(const std::string& fontPath) const;
381
382   /**
383    * @brief Caches FreeType face for the specified font path if it is not already cached.
384    *
385    * If the font face is not already cached, this function will perform the new face
386    * from font file and cache the face for future use.
387    *
388    * @param[in] fontPath The font path to cache the face for.
389    */
390   void CacheFontFaceFromFile(const std::string& fontPath) const;
391
392 private:
393   Plugin(const Plugin&) = delete;
394   Plugin& operator=(const Plugin&) = delete;
395
396 private:
397   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
398
399   unsigned int mDpiHorizontal; ///< Horizontal dpi.
400   unsigned int mDpiVertical;   ///< Vertical dpi.
401
402   bool    mIsAtlasLimitationEnabled : 1;      ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
403   Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
404
405 private:
406   VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
407
408   struct CacheHandler;
409   CacheHandler* mCacheHandler; ///< Seperate cache for font data.
410 };
411
412 } // namespace Internal
413
414 } // namespace TextAbstraction
415
416 } // namespace Dali
417
418 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H