xref: /aosp_15_r20/frameworks/minikin/include/minikin/FontStyle.h (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINIKIN_FONT_STYLE_H
18 #define MINIKIN_FONT_STYLE_H
19 
20 #include <minikin/Buffer.h>
21 
22 #include <ostream>
23 
24 namespace minikin {
25 
26 // FontStyle represents style information.
27 class FontStyle {
28 public:
29     enum class Weight : uint16_t {
30         THIN = 100,
31         EXTRA_LIGHT = 200,
32         LIGHT = 300,
33         NORMAL = 400,
34         MEDIUM = 500,
35         SEMI_BOLD = 600,
36         BOLD = 700,
37         EXTRA_BOLD = 800,
38         BLACK = 900,
39         EXTRA_BLACK = 1000,
40     };
41 
42     enum class Slant : bool {
43         ITALIC = true,
44         UPRIGHT = false,
45     };
46 
FontStyle()47     constexpr FontStyle() : FontStyle(Weight::NORMAL, Slant::UPRIGHT) {}
FontStyle(Weight weight)48     constexpr explicit FontStyle(Weight weight) : FontStyle(weight, Slant::UPRIGHT) {}
FontStyle(Slant slant)49     constexpr explicit FontStyle(Slant slant) : FontStyle(Weight::NORMAL, slant) {}
FontStyle(Weight weight,Slant slant)50     constexpr FontStyle(Weight weight, Slant slant)
51             : FontStyle(static_cast<uint16_t>(weight), slant) {}
FontStyle(uint16_t weight,Slant slant)52     constexpr FontStyle(uint16_t weight, Slant slant) : mWeight(weight), mSlant(slant) {}
FontStyle(BufferReader * reader)53     explicit FontStyle(BufferReader* reader) {
54         mWeight = reader->read<uint16_t>();
55         mSlant = static_cast<Slant>(reader->read<uint8_t>());
56     }
57 
writeTo(BufferWriter * writer)58     void writeTo(BufferWriter* writer) const {
59         writer->write<uint16_t>(mWeight);
60         writer->write<uint8_t>(static_cast<uint8_t>(mSlant));
61     }
62 
weight()63     constexpr uint16_t weight() const { return mWeight; }
slant()64     constexpr Slant slant() const { return mSlant; }
isItalic()65     bool isItalic() const { return mSlant == Slant::ITALIC; }
66 
identifier()67     constexpr uint32_t identifier() const {
68         return (static_cast<uint32_t>(weight()) << 16) | static_cast<uint32_t>(slant());
69     }
70 
71 private:
72     uint16_t mWeight;
73     Slant mSlant;
74 };
75 
76 inline std::ostream& operator<<(std::ostream& os, const FontStyle::Slant& slant) {
77     switch (slant) {
78         case FontStyle::Slant::ITALIC:
79             return os << "italic";
80         case FontStyle::Slant::UPRIGHT:
81             return os << "upright";
82         default:
83             return os << "[UNKNOWN]";
84     }
85 }
86 
87 inline std::ostream& operator<<(std::ostream& os, const FontStyle& style) {
88     return os << "{weight=" << style.weight() << ", slant=" << style.slant() << "}";
89 }
90 
91 constexpr bool operator==(const FontStyle& l, const FontStyle& r) {
92     return l.weight() == r.weight() && l.slant() == r.slant();
93 }
94 
95 constexpr bool operator!=(const FontStyle& l, const FontStyle& r) {
96     return !(l == r);
97 }
98 
99 }  // namespace minikin
100 
101 #endif  // MINIKIN_FONT_STYLE_H
102