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