-
Notifications
You must be signed in to change notification settings - Fork 5
/
Font.hpp
78 lines (63 loc) · 2.14 KB
/
Font.hpp
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
#include <stb/TrueType.hpp>
#include <psf/Font.hpp>
#include "Rect.hpp"
using ScaledHMetrics = stb::ScaledHMetrics;
using Char = stb::Char;
struct Font {
virtual ~Font() = default;
virtual float size() = 0;
virtual ScaledHMetrics codepointHMetrics(char32_t codepoint) = 0;
virtual float codepointKernAdvance(char32_t ch1, char32_t ch2) = 0;
virtual Char renderCodepoint(char32_t codepoint, float xpos) = 0;
};
struct TTFont: Font {
stb::ScaledFont font;
bool use_antialiasing;
float letter_spacing;
TTFont(stb::ScaledFont font_, bool use_antialiasing_, float letter_spacing_):
font(font_), use_antialiasing(use_antialiasing_), letter_spacing(letter_spacing_) {}
virtual float size() override {
return font.ascent() - font.descent();
}
virtual ScaledHMetrics codepointHMetrics(char32_t codepoint) override {
return font.codepointHMetrics(codepoint);
}
virtual float codepointKernAdvance(char32_t ch1, char32_t ch2) override {
return font.codepointKernAdvance(ch1, ch2) + letter_spacing;
}
virtual Char renderCodepoint(char32_t codepoint, float xpos) override {
return font.renderCodepoint(codepoint, xpos, use_antialiasing);
}
};
struct PSFFont: Font {
const psf::Font& font;
PSFFont(const psf::Font& font_): font(font_) {}
virtual float size() override {
return font.height();
}
virtual ScaledHMetrics codepointHMetrics(char32_t codepoint) override {
ScaledHMetrics metrics;
metrics.advanceWidth = font.width();
metrics.leftSideBearing = 0;
return metrics;
}
virtual float codepointKernAdvance(char32_t ch1, char32_t ch2) override {
return 0;
}
virtual Char renderCodepoint(char32_t codepoint, float xpos) override {
stb::Image<uint8_t> bitmap = font.codepointBitmap(codepoint);
Rect bbox = Rect::get_bitmap_bbox(bitmap);
Char ch(codepoint);
ch.x = xpos + bbox.x;
ch.y = bbox.y;
ch.bitmap = stb::Image<stb::RGB>(bbox.w, bbox.h);
for (int y = 0; y < bbox.h; ++y) {
for (int x = 0; x < bbox.w; ++x) {
ch.bitmap.at(x, y).r = bitmap.at(bbox.x + x, bbox.y + y);
ch.bitmap.at(x, y).g = bitmap.at(bbox.x + x, bbox.y + y);
ch.bitmap.at(x, y).b = bitmap.at(bbox.x + x, bbox.y + y);
}
}
return ch;
}
};