1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef FONT_DEFS_H
17 #define FONT_DEFS_H
18
19 #include <cstdint>
20
21 #include <base/containers/unordered_map.h>
22 #include <base/math/matrix.h>
23 #include <core/log.h>
24 #include <font/namespace.h>
25
26 FONT_BEGIN_NAMESPACE()
27
28 #ifndef VERBOSE
29 #define CORE_LOG_N(...) ;
30 #endif
31 #ifndef CORE_LOG_N
32 #define CORE_LOG_N CORE_LOG_I
33 #endif
34
35 // class DrawBatcher;
36 // class IPaint;
37 using RenderDrawKey = uint64_t;
38
39 namespace FontDefs {
40 // implementation specific API
41 struct RenderData {
42 float x;
43 float y;
44 BASE_NS::Math::Mat4X4 matrix;
45 // DrawBatcher& drawBatcher;
46 // const IPaint& paint;
47 // RenderDrawKey renderDrawKey;
48 // RENDER_NS::ScissorDesc scissor;
49 // ClipData clipData;
50
51 RenderData(const RenderData&) = delete;
52 RenderData& operator=(const RenderData&) = delete;
53 };
54
55 struct IRect {
56 uint16_t x;
57 uint16_t y;
58 uint16_t w;
59 uint16_t h;
60 };
61
62 struct AtlasSlot {
63 IRect rect;
64 uint16_t index;
65 };
66
67 struct Glyph {
68 int16_t xMin;
69 int16_t xMax;
70 int16_t yMin;
71 int16_t yMax;
72 int16_t hlsb; // left side bearing
73 int16_t htsb; // top side bearing
74 int16_t hAdv;
75 int16_t vlsb; // left side bearing
76 int16_t vtsb; // top side bearing
77 int16_t vAdv;
78 AtlasSlot atlas;
79 };
80
81 using GlyphCache = BASE_NS::unordered_map<uint32_t, FontDefs::Glyph>;
82
83 constexpr uint32_t ATLAS_SIZE = 2048; // 2048 : param
84
85 constexpr int ATLAS_ERROR = -1;
86 constexpr int ATLAS_OK = 0;
87 constexpr int ATLAS_RESET = 1;
88
89 constexpr uint8_t BORDER_WIDTH = 1u;
90 constexpr uint8_t BORDER_WIDTH_X2 = 2u * BORDER_WIDTH; // 2 : param
91
92 constexpr const int GLYPH_FIT_THRESHOLD = 4; // 4 : thd
93
rshift_u64(uint64_t val,uint8_t bits)94 inline uint64_t rshift_u64(uint64_t val, uint8_t bits)
95 {
96 return val >> bits;
97 }
98
lshift_u64(uint64_t val,uint8_t bits)99 inline uint64_t lshift_u64(uint64_t val, uint8_t bits)
100 {
101 return val << bits;
102 }
103
rshift_u32(uint32_t val,uint8_t bits)104 inline uint32_t rshift_u32(uint32_t val, uint8_t bits)
105 {
106 return val >> bits;
107 }
108
lshift_u32(uint32_t val,uint8_t bits)109 inline uint32_t lshift_u32(uint32_t val, uint8_t bits)
110 {
111 return val << bits;
112 }
113
rshift_i32(int32_t val,uint8_t bits)114 inline int32_t rshift_i32(int32_t val, uint8_t bits)
115 {
116 return val >> bits;
117 }
118
lshift_i32(int32_t val,uint8_t bits)119 inline int32_t lshift_i32(int32_t val, uint8_t bits)
120 {
121 return val << bits;
122 }
123
lshift_u16(uint16_t val,uint8_t bits)124 inline uint16_t lshift_u16(uint16_t val, uint8_t bits)
125 {
126 return uint16_t(val << bits);
127 }
128
GetStrength(uint64_t glyphKey)129 inline uint8_t GetStrength(uint64_t glyphKey)
130 {
131 return rshift_u64(glyphKey, 48) & UINT8_MAX; // 48 : param
132 }
133
GetSkewX(uint64_t glyphKey)134 inline uint8_t GetSkewX(uint64_t glyphKey)
135 {
136 return rshift_u64(glyphKey, 56) & UINT8_MAX; // 56 : param
137 }
138
IntToFp26(int32_t val)139 inline int32_t IntToFp26(int32_t val)
140 {
141 // convert to 26.6 fractional pixels values used by freetype library
142 return lshift_i32(val, 6); // 6 : param
143 }
144
Fp26ToInt(int32_t val)145 inline int32_t Fp26ToInt(int32_t val)
146 {
147 // convert from 26.6 fractional pixels values used by freetype library
148 return rshift_i32(val, 6); // 6 : param
149 }
150 // font size in pixel
MakeGlyphKey(float size,uint32_t idx)151 inline uint64_t MakeGlyphKey(float size, uint32_t idx)
152 {
153 return (static_cast<uint64_t>(size) << 32) | idx; // 32 : param
154 }
155
156 // FT_Pos is a 26.6 fixed point value, 2^6 = 64
157 constexpr float FLOAT_DIV = 64.f; // 64.f ;param
158
FloatToFTPos(float x)159 inline int32_t FloatToFTPos(float x)
160 {
161 return static_cast<int32_t>(x * FLOAT_DIV);
162 }
163
FTPosToFloat(int32_t x)164 inline float FTPosToFloat(int32_t x)
165 {
166 return static_cast<float>(x) / FLOAT_DIV;
167 }
168
169 // 13.3 fixed point value, 2^3 = 8
170 // valid range is -4096.0...4,095.875
171 constexpr float FLOAT16_DIV = 8.f; // 8.0 : param
172
Int16ToFloat(int16_t x)173 inline float Int16ToFloat(int16_t x)
174 {
175 return static_cast<float>(x) / FLOAT16_DIV;
176 }
177
FTPosToInt16(int32_t x)178 inline int16_t FTPosToInt16(int32_t x)
179 {
180 #if defined(CORE2D_VALIDATION_ENABLED) && (CORE2D_VALIDATION_ENABLED)
181 CORE_ASSERT((x >= (INT16_MIN * static_cast<int32_t>(FLOAT16_DIV))) &&
182 (x <= (INT16_MAX * static_cast<int32_t>(FLOAT16_DIV))));
183 #endif
184 return static_cast<int16_t>(x / static_cast<int32_t>(FLOAT_DIV / FLOAT16_DIV));
185 }
186
Int16ToFTPos(int16_t x)187 inline int32_t Int16ToFTPos(int16_t x)
188 {
189 return static_cast<int32_t>(static_cast<int32_t>(x) * static_cast<int32_t>(FLOAT_DIV / FLOAT16_DIV));
190 }
191 } // namespace FontDefs
192
193 FONT_END_NAMESPACE()
194 #endif // FONT_DEFS_H
195