1 /*
2 * Copyright (c) 2023 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_STYLE_H
17 #define FONT_STYLE_H
18
19 #include <algorithm>
20 #include <cstdint>
21 #include "utils/drawing_macros.h"
22
23 template<typename T>
TPin(const T & x,const T & lo,const T & hi)24 static constexpr const T& TPin(const T& x, const T& lo, const T& hi)
25 {
26 return std::max(lo, std::min(x, hi));
27 }
28
29 namespace OHOS {
30 namespace Rosen {
31 namespace Drawing {
32 // BIT16 and BIT24 used to shift operation.
33 constexpr static int BIT16 = 16;
34 constexpr static int BIT24 = 24;
35
36 class DRAWING_API FontStyle {
37 public:
38 enum Weight {
39 INVISIBLE_WEIGHT = 0,
40 THIN_WEIGHT = 100,
41 EXTRA_LIGHT_WEIGHT = 200,
42 LIGHT_WEIGHT = 300,
43 NORMAL_WEIGHT = 400,
44 MEDIUM_WEIGHT = 500,
45 SEMI_BOLD_WEIGHT = 600,
46 BOLD_WEIGHT = 700,
47 EXTRA_BOLD_WEIGHT = 800,
48 BLACK_WEIGHT = 900,
49 EXTRA_BLACK_WEIGHT = 1000,
50 };
51
52 enum Width {
53 ULTRA_CONDENSED_WIDTH = 1,
54 EXTRA_CONDENSED_WIDTH = 2,
55 CONDENSED_WIDTH = 3,
56 SEMI_CONDENSED_WIDTH = 4,
57 NORMAL_WIDTH = 5,
58 SEMI_EXPANDED_WIDTH = 6,
59 EXPANDED_WIDTH = 7,
60 EXTRA_EXPANDED_WIDTH = 8,
61 ULTRA_EXPANDED_WIDTH = 9,
62 };
63
64 enum Slant {
65 UPRIGHT_SLANT,
66 ITALIC_SLANT,
67 OBLIQUE_SLANT,
68 };
69
FontStyle(int weight,int width,Slant slant)70 constexpr FontStyle(int weight, int width, Slant slant) : fValue(
71 (TPin<int>(weight, INVISIBLE_WEIGHT, EXTRA_BLACK_WEIGHT)) +
72 (TPin<int>(width, ULTRA_CONDENSED_WIDTH, ULTRA_EXPANDED_WIDTH) << BIT16) +
73 (TPin<int>(slant, UPRIGHT_SLANT, OBLIQUE_SLANT) << BIT24)) {}
74
FontStyle()75 constexpr FontStyle() : FontStyle{NORMAL_WEIGHT, NORMAL_WIDTH, UPRIGHT_SLANT} {}
76
77 bool operator==(const FontStyle& rhs) const
78 {
79 return fValue == rhs.fValue;
80 }
81
GetWeight()82 int GetWeight() const { return fValue & 0xFFFF; }
GetWidth()83 int GetWidth() const { return (fValue >> BIT16) & 0xFF; }
GetSlant()84 Slant GetSlant() const { return (Slant)((fValue >> BIT24) & 0xFF); }
85
86 private:
87 uint32_t fValue;
88 };
89 } // namespace Drawing
90 } // namespace Rosen
91 } // namespace OHOS
92 #endif