aboutsummaryrefslogtreecommitdiff
path: root/src/common/Font.hpp
blob: 876b03a11dce2ef0fa50d810d31b64be78d38e39 (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
#pragma once

#include <common/defs.hpp>
#include <stb_truetype.h>

#include <memory>


struct AlphaBitmap {
    int32_t w;
    int32_t h;
    std::unique_ptr<uint8_t> pixels;
};


struct Glyph {
    int32_t xoff;
    int32_t yoff;
    int32_t xadvance;
    AlphaBitmap bitmap;
};


class Font {
public:
    explicit Font(const char* path, int size);
    ~Font();

    AlphaBitmap& GetAlphaBitmap(char32_t c);
    Glyph& GetGlyph(char32_t c);



private:
    bool ReadFile(const char* path);
    void InitGlyph(Glyph& glyph, char32_t c);


private:
    static constexpr char first_ascii_ch = ' ';
    static constexpr char last_ascii_ch = '~';
    static constexpr char ascii_glyph_count = last_ascii_ch - first_ascii_ch + 1;


    const char* m_file_content = nullptr;
    stbtt_fontinfo m_font_info;

    float m_font_scale;
    int m_font_baseline;
    int m_font_yadvance;

    Glyph m_glyphs[ascii_glyph_count];
    Glyph m_fail_glyph;
};