summaryrefslogtreecommitdiff
path: root/include/minikin/FontStyle.h
blob: 51e4ad86013e87717eae3e173d07b32131f38024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINIKIN_FONT_STYLE_H
#define MINIKIN_FONT_STYLE_H

#include <minikin/Buffer.h>

namespace minikin {

// FontStyle represents style information.
class FontStyle {
public:
    enum class Weight : uint16_t {
        THIN = 100,
        EXTRA_LIGHT = 200,
        LIGHT = 300,
        NORMAL = 400,
        MEDIUM = 500,
        SEMI_BOLD = 600,
        BOLD = 700,
        EXTRA_BOLD = 800,
        BLACK = 900,
        EXTRA_BLACK = 1000,
    };

    enum class Slant : bool {
        ITALIC = true,
        UPRIGHT = false,
    };

    constexpr FontStyle() : FontStyle(Weight::NORMAL, Slant::UPRIGHT) {}
    constexpr explicit FontStyle(Weight weight) : FontStyle(weight, Slant::UPRIGHT) {}
    constexpr explicit FontStyle(Slant slant) : FontStyle(Weight::NORMAL, slant) {}
    constexpr FontStyle(Weight weight, Slant slant)
            : FontStyle(static_cast<uint16_t>(weight), slant) {}
    constexpr FontStyle(uint16_t weight, Slant slant) : mWeight(weight), mSlant(slant) {}
    explicit FontStyle(BufferReader* reader) {
        mWeight = reader->read<uint16_t>();
        mSlant = static_cast<Slant>(reader->read<uint8_t>());
    }

    void writeTo(BufferWriter* writer) const {
        writer->write<uint16_t>(mWeight);
        writer->write<uint8_t>(static_cast<uint8_t>(mSlant));
    }

    constexpr uint16_t weight() const { return mWeight; }
    constexpr Slant slant() const { return mSlant; }

    constexpr bool operator==(const FontStyle& other) const {
        return weight() == other.weight() && slant() == other.slant();
    }

    constexpr uint32_t identifier() const {
        return (static_cast<uint32_t>(weight()) << 16) | static_cast<uint32_t>(slant());
    }

private:
    uint16_t mWeight;
    Slant mSlant;
};

}  // namespace minikin

#endif  // MINIKIN_FONT_STYLE_H