Reduce the number of resize for emoji
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / font-client-utils.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 #include <dali/internal/text/text-abstraction/plugin/font-client-utils.h>
18
19 #include <dali/integration-api/debug.h>
20 #include <dali/internal/imaging/common/image-operations.h>
21
22 #include <memory>
23
24 #if defined(DEBUG_ENABLED)
25 extern Dali::Integration::Log::Filter* gFontClientLogFilter;
26 #endif
27
28 namespace Dali::TextAbstraction::Internal
29 {
30 namespace
31 {
32 // http://www.freedesktop.org/software/fontconfig/fontconfig-user.html
33
34 // NONE            -1  --> DEFAULT_FONT_WIDTH (NORMAL) will be used.
35 // ULTRA_CONDENSED 50
36 // EXTRA_CONDENSED 63
37 // CONDENSED       75
38 // SEMI_CONDENSED  87
39 // NORMAL         100
40 // SEMI_EXPANDED  113
41 // EXPANDED       125
42 // EXTRA_EXPANDED 150
43 // ULTRA_EXPANDED 200
44 const int          FONT_WIDTH_TYPE_TO_INT[] = {-1, 50, 63, 75, 87, 100, 113, 125, 150, 200};
45 const unsigned int NUM_FONT_WIDTH_TYPE      = sizeof(FONT_WIDTH_TYPE_TO_INT) / sizeof(int);
46
47 // NONE                       -1  --> DEFAULT_FONT_WEIGHT (NORMAL) will be used.
48 // THIN                        0
49 // ULTRA_LIGHT, EXTRA_LIGHT   40
50 // LIGHT                      50
51 // DEMI_LIGHT, SEMI_LIGHT     55
52 // BOOK                       75
53 // NORMAL, REGULAR            80
54 // MEDIUM                    100
55 // DEMI_BOLD, SEMI_BOLD      180
56 // BOLD                      200
57 // ULTRA_BOLD, EXTRA_BOLD    205
58 // BLACK, HEAVY, EXTRA_BLACK 210
59 const int          FONT_WEIGHT_TYPE_TO_INT[] = {-1, 0, 40, 50, 55, 75, 80, 100, 180, 200, 205, 210};
60 const unsigned int NUM_FONT_WEIGHT_TYPE      = sizeof(FONT_WEIGHT_TYPE_TO_INT) / sizeof(int);
61
62 // NONE             -1 --> DEFAULT_FONT_SLANT (NORMAL) will be used.
63 // NORMAL, ROMAN     0
64 // ITALIC          100
65 // OBLIQUE         110
66 const int          FONT_SLANT_TYPE_TO_INT[] = {-1, 0, 100, 110};
67 const unsigned int NUM_FONT_SLANT_TYPE      = sizeof(FONT_SLANT_TYPE_TO_INT) / sizeof(int);
68
69 } // namespace
70
71 /**
72  * @brief Returns the FontWidth's enum index for the given width value.
73  *
74  * @param[in] width The width value.
75  *
76  * @return The FontWidth's enum index.
77  */
78 const FontWidth::Type IntToWidthType(int width)
79 {
80   return static_cast<FontWidth::Type>(ValueToIndex(width, FONT_WIDTH_TYPE_TO_INT, NUM_FONT_WIDTH_TYPE - 1u));
81 }
82
83 /**
84  * @brief Returns the FontWeight's enum index for the given weight value.
85  *
86  * @param[in] weight The weight value.
87  *
88  * @return The FontWeight's enum index.
89  */
90 const FontWeight::Type IntToWeightType(int weight)
91 {
92   return static_cast<FontWeight::Type>(ValueToIndex(weight, FONT_WEIGHT_TYPE_TO_INT, NUM_FONT_WEIGHT_TYPE - 1u));
93 }
94
95 /**
96  * @brief Returns the FontSlant's enum index for the given slant value.
97  *
98  * @param[in] slant The slant value.
99  *
100  * @return The FontSlant's enum index.
101  */
102 const FontSlant::Type IntToSlantType(int slant)
103 {
104   return static_cast<FontSlant::Type>(ValueToIndex(slant, FONT_SLANT_TYPE_TO_INT, NUM_FONT_SLANT_TYPE - 1u));
105 }
106
107 const int DEFAULT_FONT_WIDTH(100);
108 const int DEFAULT_FONT_WEIGHT(80);
109 const int DEFAULT_FONT_SLANT(0);
110
111 const FontWidth::Type DefaultFontWidth()
112 {
113   return IntToWidthType(DEFAULT_FONT_WIDTH);
114 }
115 const FontWeight::Type DefaultFontWeight()
116 {
117   return IntToWeightType(DEFAULT_FONT_WEIGHT);
118 }
119 const FontSlant::Type DefaultFontSlant()
120 {
121   return IntToSlantType(DEFAULT_FONT_SLANT);
122 }
123
124 /**
125  * @brief Copy the color bitmap given in @p srcBuffer to @p data.
126  *
127  * @param[out] data The bitmap data.
128  * @param[in] srcWidth The width of the bitmap.
129  * @param[in] srcHeight The height of the bitmap.
130  * @param[in] srcBuffer The buffer of the bitmap.
131  */
132 void ConvertBitmap(TextAbstraction::FontClient::GlyphBufferData& data, unsigned int srcWidth, unsigned int srcHeight, const unsigned char* const srcBuffer)
133 {
134   // Set the input dimensions.
135   const ImageDimensions inputDimensions(srcWidth, srcHeight);
136
137   // Set the output dimensions.
138   // If the output dimension is not given, the input dimension is set
139   // and won't be downscaling.
140   data.width  = (data.width == 0) ? srcWidth : data.width;
141   data.height = (data.height == 0) ? srcHeight : data.height;
142   const ImageDimensions desiredDimensions(data.width, data.height);
143
144   // Creates the output buffer
145   const unsigned int bufferSize = data.width * data.height * 4u;
146   data.buffer                   = new uint8_t[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
147
148   if(inputDimensions == desiredDimensions)
149   {
150     // There isn't downscaling.
151     memcpy(data.buffer, srcBuffer, bufferSize);
152   }
153   else
154   {
155     Dali::Internal::Platform::LanczosSample4BPP(srcBuffer,
156                                                 inputDimensions,
157                                                 srcWidth,
158                                                 data.buffer,
159                                                 desiredDimensions);
160   }
161 }
162
163 /**
164  * @brief Copy the FreeType bitmap to the given buffer.
165  *
166  * @param[out] data The bitmap data.
167  * @param[in] srcBitmap The FreeType bitmap.
168  * @param[in] isShearRequired Whether the bitmap needs a shear transform (for software italics).
169  */
170 void ConvertBitmap(TextAbstraction::FontClient::GlyphBufferData& data, FT_Bitmap srcBitmap, bool isShearRequired)
171 {
172   if(srcBitmap.width * srcBitmap.rows > 0)
173   {
174     switch(srcBitmap.pixel_mode)
175     {
176       case FT_PIXEL_MODE_GRAY:
177       {
178         if(srcBitmap.pitch == static_cast<int>(srcBitmap.width))
179         {
180           uint8_t*     pixelsIn = srcBitmap.buffer;
181           unsigned int width    = srcBitmap.width;
182           unsigned     height   = srcBitmap.rows;
183
184           std::unique_ptr<uint8_t, void (*)(void*)> pixelsOutPtr(nullptr, free);
185
186           if(isShearRequired)
187           {
188             /**
189              * Glyphs' bitmaps with no slant retrieved from FreeType:
190              * __________     ____
191              * |XXXXXXXX|     |XX|
192              * |   XX   |     |XX|
193              * |   XX   |     |XX|
194              * |   XX   |     |XX|
195              * |   XX   |     |XX|
196              * |   XX   |     |XX|
197              * ----------     ----
198              *
199              * Expected glyphs' bitmaps with italic slant:
200              * ____________   ______
201              * |  XXXXXXXX|   |  XX|
202              * |     XX   |   |  XX|
203              * |    XX    |   | XX |
204              * |    XX    |   | XX |
205              * |   XX     |   |XX  |
206              * |   XX     |   |XX  |
207              * ------------   ------
208              *
209              * Glyphs' bitmaps with software italic slant retrieved from FreeType:
210              * __________     ______
211              * |XXXXXXXX|     |  XX|
212              * |   XX   |     |  XX|
213              * |  XX    |     | XX |
214              * |  XX    |     | XX |
215              * | XX     |     |XX  |
216              * | XX     |     |XX  |
217              * ----------     ------
218              *
219              * This difference in some bitmaps' width causes an overlap of some glyphs. This is the reason why a shear operation is done here instead of relying on the experimental FT_GlyphSlot_Oblique() implementation.
220              */
221             unsigned int widthOut  = 0u;
222             unsigned int heightOut = 0u;
223             uint8_t*     pixelsOut = nullptr;
224
225             Dali::Internal::Platform::HorizontalShear(pixelsIn,
226                                                       width,
227                                                       height,
228                                                       width,
229                                                       1u,
230                                                       -TextAbstraction::FontClient::DEFAULT_ITALIC_ANGLE,
231                                                       pixelsOut,
232                                                       widthOut,
233                                                       heightOut);
234
235             width    = widthOut;
236             height   = heightOut;
237             pixelsIn = pixelsOut;
238             pixelsOutPtr.reset(pixelsOut);
239           }
240
241           const unsigned int bufferSize = width * height;
242           data.buffer                   = new unsigned char[bufferSize]; // @note The caller is responsible for deallocating the bitmap data using delete[].
243           data.width                    = width;
244           data.height                   = height;
245           data.format                   = Pixel::L8; // Sets the pixel format.
246           memcpy(data.buffer, pixelsIn, bufferSize);
247         }
248         break;
249       }
250
251 #ifdef FREETYPE_BITMAP_SUPPORT
252       case FT_PIXEL_MODE_BGRA:
253       {
254         if(srcBitmap.pitch == static_cast<int>(srcBitmap.width << 2u))
255         {
256           ConvertBitmap(data, srcBitmap.width, srcBitmap.rows, srcBitmap.buffer);
257
258           // Sets the pixel format.
259           data.format = Pixel::BGRA8888;
260         }
261         break;
262       }
263 #endif
264       default:
265       {
266         DALI_LOG_INFO(gFontClientLogFilter, Debug::General, "FontClient::Plugin::ConvertBitmap. FontClient Unable to create Bitmap of this PixelType\n");
267         break;
268       }
269     }
270   }
271 }
272
273 FcPattern* CreateFontFamilyPattern(const FontDescription& fontDescription)
274 {
275   // create the cached font family lookup pattern
276   // a pattern holds a set of names, each name refers to a property of the font
277   FcPattern* fontFamilyPattern = FcPatternCreate(); // FcPatternCreate creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
278
279   if(!fontFamilyPattern)
280   {
281     return nullptr;
282   }
283
284   // add a property to the pattern for the font family
285   FcPatternAddString(fontFamilyPattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(fontDescription.family.c_str()));
286
287   // add a property to the pattern for local setting.
288   const char* locale = setlocale(LC_MESSAGES, nullptr);
289   if(locale != nullptr)
290   {
291     FcPatternAddString(fontFamilyPattern, FC_LANG, reinterpret_cast<const FcChar8*>(locale));
292   }
293
294   int width = FONT_WIDTH_TYPE_TO_INT[fontDescription.width];
295   if(width < 0)
296   {
297     // Use default.
298     width = DEFAULT_FONT_WIDTH;
299   }
300
301   int weight = FONT_WEIGHT_TYPE_TO_INT[fontDescription.weight];
302   if(weight < 0)
303   {
304     // Use default.
305     weight = DEFAULT_FONT_WEIGHT;
306   }
307
308   int slant = FONT_SLANT_TYPE_TO_INT[fontDescription.slant];
309   if(slant < 0)
310   {
311     // Use default.
312     slant = DEFAULT_FONT_SLANT;
313   }
314
315   FcPatternAddInteger(fontFamilyPattern, FC_WIDTH, width);
316   FcPatternAddInteger(fontFamilyPattern, FC_WEIGHT, weight);
317   FcPatternAddInteger(fontFamilyPattern, FC_SLANT, slant);
318
319   // modify the config, with the mFontFamilyPatterm
320   FcConfigSubstitute(nullptr /* use default configure */, fontFamilyPattern, FcMatchPattern);
321
322   // provide default values for unspecified properties in the font pattern
323   // e.g. patterns without a specified style or weight are set to Medium
324   FcDefaultSubstitute(fontFamilyPattern);
325
326   return fontFamilyPattern;
327 }
328
329 FcCharSet* CreateCharacterSetFromDescription(const FontDescription& description)
330 {
331   FcCharSet* characterSet = nullptr;
332
333   FcPattern* pattern = CreateFontFamilyPattern(description); // Creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
334
335   if(nullptr != pattern)
336   {
337     FcResult   result = FcResultMatch;
338     FcPattern* match  = FcFontMatch(nullptr, pattern, &result); // FcFontMatch creates a new pattern that needs to be destroyed by calling FcPatternDestroy.
339
340     FcPatternGetCharSet(match, FC_CHARSET, 0u, &characterSet);
341
342     // Destroys the created patterns.
343     FcPatternDestroy(match);
344     FcPatternDestroy(pattern);
345   }
346
347   return characterSet;
348 }
349
350 } // namespace Dali::TextAbstraction::Internal