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 <font/namespace.h>
24
25 FONT_BEGIN_NAMESPACE()
26
27 #ifndef VERBOSE
28 #define CORE_LOG_N(...)
29 #endif
30 #ifndef CORE_LOG_N
31 #define CORE_LOG_N CORE_LOG_I
32 #endif
33
34 class DrawBatcher;
35 class IPaint;
36 using RenderDrawKey = uint64_t;
37
38 namespace FontDefs {
39 // implementation specific API
40 struct RenderData {
41 float x;
42 float y;
43 BASE_NS::Math::Mat4X4 matrix;
44 DrawBatcher& drawBatcher;
45 const IPaint& paint;
46 RenderDrawKey renderDrawKey;
47 RENDER_NS::ScissorDesc scissor;
48 ClipData clipData;
49
50 RenderData(const RenderData&) = delete;
51 RenderData& operator=(const RenderData&) = delete;
52 };
53
54 struct IRect {
55 uint16_t x;
56 uint16_t y;
57 uint16_t w;
58 uint16_t h;
59 };
60
61 struct AtlasSlot {
62 IRect rect;
63 uint16_t index;
64 };
65
66 struct Glyph {
67 int16_t xMin;
68 int16_t xMax;
69 int16_t yMin;
70 int16_t yMax;
71 int16_t hlsb; // left side bearing
72 int16_t htsb; // top side bearing
73 int16_t hAdv;
74 int16_t vlsb; // left side bearing
75 int16_t vtsb; // top side bearing
76 int16_t vAdv;
77 AtlasSlot atlas;
78 };
79
80 using GlyphCache = BASE_NS::unordered_map<uint32_t, FontDefs::Glyph>;
81
82 constexpr uint32_t ATLAS_SIZE = 2048;
83
84 constexpr int ATLAS_ERROR = -1;
85 constexpr int ATLAS_OK = 0;
86 constexpr int ATLAS_RESET = 1;
87
88 constexpr uint8_t BORDER_WIDTH = 1u;
89 constexpr uint8_t BORDER_WIDTH_X2 = 2u * BORDER_WIDTH;
90
91 constexpr const int GLYPH_FIT_THRESHOLD = 4;
92
93 constexpr uint32_t NUM_6 = 6;
94 constexpr uint32_t NUM_32 = 32;
95 constexpr uint32_t NUM_48 = 48;
96 constexpr uint32_t NUM_56 = 56;
97
rshift_u64(uint64_t val, uint8_t bits)98 inline uint64_t rshift_u64(uint64_t val, uint8_t bits)
99 {
100 return val >> bits;
101 }
102
lshift_u64(uint64_t val, uint8_t bits)103 inline uint64_t lshift_u64(uint64_t val, uint8_t bits)
104 {
105 return val << bits;
106 }
107
rshift_u32(uint32_t val, uint8_t bits)108 inline uint32_t rshift_u32(uint32_t val, uint8_t bits)
109 {
110 return val >> bits;
111 }
112
lshift_u32(uint32_t val, uint8_t bits)113 inline uint32_t lshift_u32(uint32_t val, uint8_t bits)
114 {
115 return val << bits;
116 }
117
rshift_i32(int32_t val, uint8_t bits)118 inline int32_t rshift_i32(int32_t val, uint8_t bits)
119 {
120 return val >> bits;
121 }
122
lshift_i32(int32_t val, uint8_t bits)123 inline int32_t lshift_i32(int32_t val, uint8_t bits)
124 {
125 return val << bits;
126 }
127
lshift_u16(uint16_t val, uint8_t bits)128 inline uint16_t lshift_u16(uint16_t val, uint8_t bits)
129 {
130 return uint16_t(val << bits);
131 }
132
GetStrength(uint64_t glyphKey)133 inline uint8_t GetStrength(uint64_t glyphKey)
134 {
135 return rshift_u64(glyphKey, NUM_48) & UINT8_MAX;
136 }
137
GetSkewX(uint64_t glyphKey)138 inline uint8_t GetSkewX(uint64_t glyphKey)
139 {
140 return rshift_u64(glyphKey, NUM_56) & UINT8_MAX;
141 }
142
IntToFp26(int32_t val)143 inline int32_t IntToFp26(int32_t val)
144 {
145 // convert to 26.6 fractional pixels values used by freetype library
146 return lshift_i32(val, NUM_6);
147 }
148
Fp26ToInt(int32_t val)149 inline int32_t Fp26ToInt(int32_t val)
150 {
151 // convert from 26.6 fractional pixels values used by freetype library
152 return rshift_i32(val, NUM_6);
153 }
154 // font size in pixel
MakeGlyphKey(float size, uint32_t idx)155 inline uint64_t MakeGlyphKey(float size, uint32_t idx)
156 {
157 return (static_cast<uint64_t>(size) << NUM_32) | idx;
158 }
159
160 constexpr float FLOAT_DIV = 64.f;
161
FloatToFTPos(float x)162 inline int32_t FloatToFTPos(float x)
163 {
164 return static_cast<int32_t>(x * FLOAT_DIV);
165 }
166
FTPosToFloat(int32_t x)167 inline float FTPosToFloat(int32_t x)
168 {
169 return static_cast<float>(x) / FLOAT_DIV;
170 }
171 } // namespace FontDefs
172
173 FONT_END_NAMESPACE()
174 #endif // FONT_DEFS_H
175