• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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; }
65 
identifier()66     constexpr uint32_t identifier() const {
67         return (static_cast<uint32_t>(weight()) << 16) | static_cast<uint32_t>(slant());
68     }
69 
70 private:
71     uint16_t mWeight;
72     Slant mSlant;
73 };
74 
75 inline std::ostream& operator<<(std::ostream& os, const FontStyle::Slant& slant) {
76     switch (slant) {
77         case FontStyle::Slant::ITALIC:
78             return os << "italic";
79         case FontStyle::Slant::UPRIGHT:
80             return os << "upright";
81         default:
82             return os << "[UNKNOWN]";
83     }
84 }
85 
86 inline std::ostream& operator<<(std::ostream& os, const FontStyle& style) {
87     return os << "{weight=" << style.weight() << ", slant=" << style.slant() << "}";
88 }
89 
90 constexpr bool operator==(const FontStyle& l, const FontStyle& r) {
91     return l.weight() == r.weight() && l.slant() == r.slant();
92 }
93 
94 constexpr bool operator!=(const FontStyle& l, const FontStyle& r) {
95     return !(l == r);
96 }
97 
98 }  // namespace minikin
99 
100 #endif  // MINIKIN_FONT_STYLE_H
101