17ff1d70b84e60ff3283c1fb64940dc94c9929dc
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / font-client-utils.h
1 #ifndef DALI_TEXT_ABSTRACTION_INTERNAL_FONT_CLIENT_UTILS_H
2 #define DALI_TEXT_ABSTRACTION_INTERNAL_FONT_CLIENT_UTILS_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 // INTERNAL INCLUDES
21 #include <dali/devel-api/text-abstraction/font-client.h>
22
23 // EXTERNAL INCLUDES
24 #include <fontconfig/fontconfig.h>
25
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28 #include FT_GLYPH_H
29 #include FT_OUTLINE_H
30 #include FT_STROKER_H
31 #include FT_SYNTHESIS_H
32
33 namespace Dali::TextAbstraction::Internal
34 {
35 void ConvertBitmap(TextAbstraction::FontClient::GlyphBufferData& data,
36                    unsigned int                                  srcWidth,
37                    unsigned int                                  srcHeight,
38                    const unsigned char* const                    srcBuffer,
39                    const Pixel::Format                           srcFormat);
40
41 void ConvertBitmap(TextAbstraction::FontClient::GlyphBufferData& data,
42                    FT_Bitmap&                                    srcBitmap,
43                    bool                                          isShearRequired,
44                    bool                                          moveBuffer = false);
45
46 /**
47  * @brief Creates a font family pattern used to match fonts.
48  *
49  * @note Need to call FcPatternDestroy to free the resources.
50  *
51  * @param[in] fontDescription The font to cache.
52  *
53  * @return The pattern.
54  */
55 FcPattern* CreateFontFamilyPattern(const FontDescription& fontDescription);
56
57 /**
58  * @brief Creates a character set from a given font's @p description.
59  *
60  * @note Need to call FcCharSetDestroy to free the resources.
61  *
62  * @param[in] description The font's description.
63  *
64  * @return A character set.
65  */
66 FcCharSet* CreateCharacterSetFromDescription(const FontDescription& description);
67
68 /**
69  * @brief Gets the FontDescription which matches the given pattern.
70  *
71  * @note The reference counter of the @p characterSet has been increased. Call FcCharSetDestroy to decrease it.
72  *
73  * @param[in] pattern pattern to match against.
74  * @param[out] fontDescription the resultant fontDescription that matched.
75  * @param[out] characterSet The character set for that pattern.
76  * @return true if match found.
77  */
78 bool MatchFontDescriptionToPattern(FcPattern* pattern, Dali::TextAbstraction::FontDescription& fontDescription, FcCharSet** characterSet);
79
80 /**
81  * @brief Retrieves a font config object's value from a pattern.
82  *
83  * @param[in] pattern The font config pattern.
84  * @param[in] n The object.
85  * @param[out] string The object's value.
86  *
87  * @return @e true if the operation is successful.
88  */
89 bool GetFcString(const FcPattern* const pattern, const char* const n, std::string& string);
90
91 /**
92  * @brief Retrieves a font config object's value from a pattern.
93  *
94  * @param[in] pattern The font config pattern.
95  * @param[in] n The object.
96  * @param[out] intVal The object's value.
97  *
98  * @return @e true if the operation is successful.
99  */
100 bool GetFcInt(const _FcPattern* const pattern, const char* const n, int& intVal);
101
102 constexpr int ValueToIndex(int value, const int* const table, unsigned int maxIndex)
103 {
104   if(nullptr == table)
105   {
106     // Return an invalid index if there is no table.
107     return -1;
108   }
109
110   if(value <= table[0])
111   {
112     return 0;
113   }
114
115   if(value >= table[maxIndex])
116   {
117     return maxIndex;
118   }
119
120   for(unsigned int index = 0u; index < maxIndex; ++index)
121   {
122     const int          v1        = table[index];
123     const unsigned int indexPlus = index + 1u;
124     const int          v2        = table[indexPlus];
125     if((v1 < value) && (value <= v2))
126     {
127       const int result = ((v1 > 0) && ((value - v1) < (v2 - value))) ? index : indexPlus;
128       return result;
129     }
130   }
131   return 0;
132 }
133
134 /**
135  * @brief Returns the FontWidth's enum index for the given width value.
136  *
137  * @param[in] width The width value.
138  *
139  * @return The FontWidth's enum index.
140  */
141 const FontWidth::Type IntToWidthType(int width);
142
143 /**
144  * @brief Returns the FontWeight's enum index for the given weight value.
145  *
146  * @param[in] weight The weight value.
147  *
148  * @return The FontWeight's enum index.
149  */
150 const FontWeight::Type IntToWeightType(int weight);
151
152 /**
153  * @brief Returns the FontSlant's enum index for the given slant value.
154  *
155  * @param[in] slant The slant value.
156  *
157  * @return The FontSlant's enum index.
158  */
159 const FontSlant::Type IntToSlantType(int slant);
160
161 const std::string_view DefaultFontFamily();
162 const FontWidth::Type  DefaultFontWidth();
163 const FontWeight::Type DefaultFontWeight();
164 const FontSlant::Type  DefaultFontSlant();
165
166 } // namespace Dali::TextAbstraction::Internal
167
168 #endif // DALI_TEXT_ABSTRACTION_INTERNAL_FONT_CLIENT_UTILS_H