[dali_2.3.26] Merge branch 'devel/master'
[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::ClearCacheOnLocaleChanged()
99    */
100   void ClearCacheOnLocaleChanged() const;
101
102   /**
103    * @copydoc Dali::TextAbstraction::FontClient::SetDpi()
104    */
105   void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi);
106
107   /**
108    * @copydoc Dali::TextAbstraction::FontClient::ResetSystemDefaults()
109    */
110   void ResetSystemDefaults() const;
111
112   /**
113    * @copydoc Dali::TextAbstraction::FontClient::InitDefaultFontDescription()
114    */
115   void InitDefaultFontDescription() const;
116
117   /**
118    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultPlatformFontDescription()
119    */
120   void GetDefaultPlatformFontDescription(FontDescription& fontDescription) const;
121
122   /**
123    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultFonts()
124    */
125   void GetDefaultFonts(FontList& defaultFonts) const;
126
127   /**
128    * @copydoc Dali::TextAbstraction::FontClient::GetSystemFonts()
129    */
130   void GetSystemFonts(FontList& systemFonts) const;
131
132   /**
133    * @copydoc Dali::TextAbstraction::FontClient::GetDescription()
134    */
135   void GetDescription(FontId fontId, FontDescription& fontDescription) const;
136
137   /**
138    * @copydoc Dali::TextAbstraction::FontClient::GetPointSize()
139    */
140   PointSize26Dot6 GetPointSize(FontId fontId) const;
141
142   /**
143    * @copydoc Dali::TextAbstraction::FontClient::IsCharacterSupportedByFont()
144    */
145   bool IsCharacterSupportedByFont(FontId fontId, Character character) const;
146
147   /**
148    * @copydoc Dali::TextAbstraction::FontClient::FindDefaultFont()
149    */
150   FontId FindDefaultFont(Character       charcode,
151                          PointSize26Dot6 requestedPointSize,
152                          bool            preferColor) const;
153
154   /**
155    * @copydoc Dali::TextAbstraction::FontClient::FindFallbackFont()
156    */
157   FontId FindFallbackFont(Character              charcode,
158                           const FontDescription& preferredFontDescription,
159                           PointSize26Dot6        requestedPointSize,
160                           bool                   preferColor) const;
161
162   /**
163    * @see Dali::TextAbstraction::FontClient::GetFontId( const FontPath& path, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
164    *
165    * @param[in] cacheDescription Whether to cache the font description.
166    */
167   FontId GetFontIdByPath(const FontPath& path,
168                          PointSize26Dot6 requestedPointSize,
169                          FaceIndex       faceIndex,
170                          bool            cacheDescription) const;
171
172   /**
173    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const FontDescription& preferredFontDescription, PointSize26Dot6 requestedPointSize, FaceIndex faceIndex )
174    */
175   FontId GetFontId(const FontDescription& fontDescription,
176                    PointSize26Dot6        requestedPointSize,
177                    FaceIndex              faceIndex) const;
178
179   /**
180    * @copydoc Dali::TextAbstraction::FontClient::GetFontId( const BitmapFont& bitmapFont )
181    */
182   FontId GetFontId(const BitmapFont& bitmapFont) const;
183
184   /**
185    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontPath& path )
186    */
187   bool IsScalable(const FontPath& path) const;
188
189   /**
190    * @copydoc Dali::TextAbstraction::FontClient::IsScalable( const FontDescription& fontDescription )
191    */
192   bool IsScalable(const FontDescription& fontDescription) const;
193
194   /**
195    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
196    */
197   void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes) const;
198
199   /**
200    * @copydoc Dali::TextAbstraction::FontClient::GetFixedSizes()
201    */
202   void GetFixedSizes(const FontDescription&         fontDescription,
203                      Dali::Vector<PointSize26Dot6>& sizes) const;
204
205   /**
206    * @copydoc Dali::TextAbstraction::FontClient::HasItalicStyle()
207    */
208   bool HasItalicStyle(FontId fontId) const;
209
210   /**
211    * @copydoc Dali::TextAbstraction::FontClient::GetFontMetrics()
212    */
213   void GetFontMetrics(FontId fontId, FontMetrics& metrics) const;
214
215   /**
216    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
217    */
218   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode) const;
219
220   /**
221    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphIndex()
222    */
223   GlyphIndex GetGlyphIndex(FontId fontId, Character charcode, Character variantSelector) const;
224
225   /**
226    * @copydoc Dali::TextAbstraction::FontClient::GetGlyphMetrics()
227    */
228   bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal) const;
229
230   /**
231    * Helper for GetGlyphMetrics when using bitmaps
232    */
233   bool GetBitmapMetrics(GlyphInfo* array, uint32_t size, bool horizontal) const;
234
235   /**
236    * Helper for GetGlyphMetrics when using vectors
237    */
238   bool GetVectorMetrics(GlyphInfo* array, uint32_t size, bool horizontal) const;
239
240   /**
241    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::GlyphBufferData& data, int outlineWidth )
242    */
243   void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool isItalicRequired, bool isBoldRequired, Dali::TextAbstraction::GlyphBufferData& data, int outlineWidth) const;
244
245   /**
246    * @copydoc Dali::TextAbstraction::FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex, int outlineWidth )
247    */
248   PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth) const;
249
250   /**
251    * @copydoc Dali::TextAbstraction::FontClient::CreateVectorBlob()
252    */
253   void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight) const;
254
255   /**
256    * @copydoc Dali::TextAbstraction::FontClient::GetEllipsisGlyph()
257    */
258   const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 requestedPointSize) const;
259
260   /**
261    * @copydoc Dali::TextAbstraction::FontClient::IsColorGlyph()
262    */
263   bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex) const;
264
265   /**
266    * @copydoc Dali::TextAbstraction::FontClient::CreateEmbeddedItem()
267    */
268   GlyphIndex CreateEmbeddedItem(const TextAbstraction::FontClient::EmbeddedItemDescription& description, Pixel::Format& pixelFormat) const;
269
270   /**
271    * @copydoc Dali::TextAbstraction::FontClient::EnableAtlasLimitation(bool enabled)
272    */
273   void EnableAtlasLimitation(bool enabled);
274
275   /**
276    * @copydoc Dali::TextAbstraction::FontClient::IsAtlasLimitationEnabled()
277    */
278   bool IsAtlasLimitationEnabled() const;
279
280   /**
281    * @copydoc Dali::TextAbstraction::FontClient::GetMaximumTextAtlasSize()
282    */
283   Size GetMaximumTextAtlasSize() const;
284
285   /**
286    * @copydoc Dali::TextAbstraction::FontClient::GetDefaultTextAtlasSize()
287    */
288   Size GetDefaultTextAtlasSize() const;
289
290   /**
291    * @copydoc Dali::TextAbstraction::FontClient::GetCurrentMaximumBlockSizeFitInAtlas()
292    */
293   Size GetCurrentMaximumBlockSizeFitInAtlas() const;
294
295   /**
296    * @copydoc Dali::TextAbstraction::FontClient::SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas)
297    */
298   bool SetCurrentMaximumBlockSizeFitInAtlas(const Size& currentMaximumBlockSizeFitInAtlas);
299
300   /**
301    * @copydoc Dali::TextAbstraction::FontClient::GetNumberOfPointsPerOneUnitOfPointSize()
302    */
303   uint32_t GetNumberOfPointsPerOneUnitOfPointSize() const;
304
305   /**
306    * @copydoc Dali::TextAbstraction::FontClient::AddCustomFontDirectory()
307    */
308   bool AddCustomFontDirectory(const FontPath& path);
309
310 public: // Dali::TextAbstraction::Internal::FontClient
311   /**
312    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFreetypeFace()
313    */
314   FT_FaceRec_* GetFreetypeFace(FontId fontId) const;
315
316   /**
317    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetFontType()
318    */
319   FontDescription::Type GetFontType(FontId fontId) const;
320
321   /**
322    * @copydoc Dali::TextAbstraction::Internal::FontClient::GetHarfBuzzFont()
323    */
324   HarfBuzzFontHandle GetHarfBuzzFont(FontId fontId) const;
325
326   /**
327    * @copydoc Dali::TextAbstraction::Internal::FontClient::FontPreCache()
328    */
329   void FontPreCache(const FontFamilyList& fallbackFamilyList, const FontFamilyList& extraFamilyList, const FontFamily& localeFamily) const;
330
331   /**
332    * @copydoc Dali::TextAbstraction::Internal::FontClient::FontPreLoad()
333    */
334   void FontPreLoad(const FontPathList& fontPathList, const FontPathList& memoryFontPathList) const;
335
336 private:
337   /**
338    * Get the cached font item for the given font
339    * @param[in] fontId The font id to search for
340    * @return the matching cached font item
341    */
342   const FontCacheItemInterface* GetCachedFontItem(FontId fontId) const;
343
344   /**
345    * @brief Finds within the @p fontList a font which support the @p carcode.
346    *
347    * @param[in] fontList A list of font paths, family, width, weight and slant.
348    * @param[in] characterSetList A list that contains a character set for each description of the font list.
349    * @param[in] charcode The character for which a font is needed.
350    * @param[in] requestedPointSize The point size in 26.6 fractional points.
351    * @param[in] preferColor @e true if a color font is preferred.
352    *
353    * @return A valid font identifier, or zero if no font is found.
354    */
355   FontId FindFontForCharacter(const FontList&         fontList,
356                               const CharacterSetList& characterSetList,
357                               Character               charcode,
358                               PointSize26Dot6         requestedPointSize,
359                               bool                    preferColor) const;
360
361   /**
362    * @brief Creates a font.
363    *
364    * @param[in] path The path to the font file name.
365    * @param[in] requestedPointSize The requested point size.
366    * @param[in] faceIndex A face index.
367    * @param[in] cacheDescription Whether to cache the font description.
368    *
369    * @return The font identifier.
370    */
371   FontId CreateFont(const FontPath& path,
372                     PointSize26Dot6 requestedPointSize,
373                     FaceIndex       faceIndex,
374                     bool            cacheDescription) const;
375
376
377   /**
378    * @brief Caches font data for the specified font path if it is not already cached.
379    *
380    * If the font data is not already cached, this function will load the font file
381    * from disk and cache the data for future use.
382    *
383    * @param[in] fontPath The font path to cache the data for.
384    */
385   void CacheFontDataFromFile(const std::string& fontPath) const;
386
387   /**
388    * @brief Caches FreeType face for the specified font path if it is not already cached.
389    *
390    * If the font face is not already cached, this function will perform the new face
391    * from font file and cache the face for future use.
392    *
393    * @param[in] fontPath The font path to cache the face for.
394    */
395   void CacheFontFaceFromFile(const std::string& fontPath) const;
396
397 private:
398   Plugin(const Plugin&) = delete;
399   Plugin& operator=(const Plugin&) = delete;
400
401 private:
402   FT_Library mFreeTypeLibrary; ///< A handle to a FreeType library instance.
403
404   unsigned int mDpiHorizontal; ///< Horizontal dpi.
405   unsigned int mDpiVertical;   ///< Vertical dpi.
406
407   bool    mIsAtlasLimitationEnabled : 1;      ///< Whether the validation on maximum atlas block size, then reduce block size to fit into it is enabled or not.
408   Vector2 mCurrentMaximumBlockSizeFitInAtlas; ///< The current maximum size (width, height) of text-atlas-block.
409
410 private:
411   VectorFontCache* mVectorFontCache; ///< Separate cache for vector data blobs etc.
412
413   struct CacheHandler;
414   CacheHandler* mCacheHandler; ///< Seperate cache for font data.
415 };
416
417 } // namespace Internal
418
419 } // namespace TextAbstraction
420
421 } // namespace Dali
422
423 #endif // DALI_INTERNAL_TEXT_ABSTRACTION_FONT_CLIENT_PLUGIN_IMPL_H