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
25FONT_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
34class DrawBatcher;
35class IPaint;
36using RenderDrawKey = uint64_t;
37
38namespace FontDefs {
39// implementation specific API
40struct 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
54struct IRect {
55    uint16_t x;
56    uint16_t y;
57    uint16_t w;
58    uint16_t h;
59};
60
61struct AtlasSlot {
62    IRect rect;
63    uint16_t index;
64};
65
66struct 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
80using GlyphCache = BASE_NS::unordered_map<uint32_t, FontDefs::Glyph>;
81
82constexpr uint32_t ATLAS_SIZE = 2048;
83
84constexpr int ATLAS_ERROR = -1;
85constexpr int ATLAS_OK = 0;
86constexpr int ATLAS_RESET = 1;
87
88constexpr uint8_t BORDER_WIDTH = 1u;
89constexpr uint8_t BORDER_WIDTH_X2 = 2u * BORDER_WIDTH;
90
91constexpr const int GLYPH_FIT_THRESHOLD = 4;
92
93constexpr uint32_t NUM_6 = 6;
94constexpr uint32_t NUM_32 = 32;
95constexpr uint32_t NUM_48 = 48;
96constexpr uint32_t NUM_56 = 56;
97
98inline uint64_t rshift_u64(uint64_t val, uint8_t bits)
99{
100    return val >> bits;
101}
102
103inline uint64_t lshift_u64(uint64_t val, uint8_t bits)
104{
105    return val << bits;
106}
107
108inline uint32_t rshift_u32(uint32_t val, uint8_t bits)
109{
110    return val >> bits;
111}
112
113inline uint32_t lshift_u32(uint32_t val, uint8_t bits)
114{
115    return val << bits;
116}
117
118inline int32_t rshift_i32(int32_t val, uint8_t bits)
119{
120    return val >> bits;
121}
122
123inline int32_t lshift_i32(int32_t val, uint8_t bits)
124{
125    return val << bits;
126}
127
128inline uint16_t lshift_u16(uint16_t val, uint8_t bits)
129{
130    return uint16_t(val << bits);
131}
132
133inline uint8_t GetStrength(uint64_t glyphKey)
134{
135    return rshift_u64(glyphKey, NUM_48) & UINT8_MAX;
136}
137
138inline uint8_t GetSkewX(uint64_t glyphKey)
139{
140    return rshift_u64(glyphKey, NUM_56) & UINT8_MAX;
141}
142
143inline 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
149inline 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
155inline uint64_t MakeGlyphKey(float size, uint32_t idx)
156{
157    return (static_cast<uint64_t>(size) << NUM_32) | idx;
158}
159
160constexpr float FLOAT_DIV = 64.f;
161
162inline int32_t FloatToFTPos(float x)
163{
164    return static_cast<int32_t>(x * FLOAT_DIV);
165}
166
167inline float FTPosToFloat(int32_t x)
168{
169    return static_cast<float>(x) / FLOAT_DIV;
170}
171} // namespace FontDefs
172
173FONT_END_NAMESPACE()
174#endif // FONT_DEFS_H
175