Repository navigation
Expand file tree
/
Copy pathfont_pack.cpp
More file actions
144 lines (121 loc) · 4.8 KB
/
Copy pathfont_pack.cpp
File metadata and controls
144 lines (121 loc) · 4.8 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "font_pack.h"
#include "utf8.h"
#include "composer.h"
#include "font_resolver.h"
#include <std/mem/obj_pool.h>
#include <std/str/builder.h>
#include <std/sys/throw.h>
#include <errno.h>
using namespace stl;
namespace {
struct FontpackImpl final: public Fontpack {
FontpackImpl(Composer& composer, ObjPool& pool, StringView fontname, StringView dwfontname, u16 size);
u16 getPx() const override;
u16 getPy() const override;
bool hasBold() const override;
bool hasItalic() const override;
bool hasBoldItalic() const override;
bool hasDoubleWidth() const override;
FontGlyph glyph(const u32* codepoints, size_t count, FontStyle style, bool doubleWidth) override;
Font* createOptional(Composer& composer, ObjPool& pool, StringView name, u16 size, FontStyle style, FontKind kind, FontMetrics metrics);
Font* select(FontStyle style) const noexcept;
FontGlyph fallback(Font* font, Font* base, const u32* codepoints, size_t count);
FontMetrics metrics_;
Font* regular_ = nullptr;
Font* bold_ = nullptr;
Font* italic_ = nullptr;
Font* boldItalic_ = nullptr;
Font* doubleWidth_ = nullptr;
};
}
FontpackImpl::FontpackImpl(Composer& composer, ObjPool& pool, StringView fontname, StringView dwfontname, u16 size) {
regular_ = composer.loadFont(pool, {fontname, size, FontStyle::Regular, FontKind::Primary}, metrics_);
if (regular_ == nullptr) {
Errno(EINVAL).raise(StringBuilder() << StringView(u8"no suitable font found for ") << fontname);
}
bold_ = createOptional(composer, pool, fontname, size, FontStyle::Bold, FontKind::Overlay, metrics_);
italic_ = createOptional(composer, pool, fontname, size, FontStyle::Italic, FontKind::Overlay, metrics_);
boldItalic_ = createOptional(composer, pool, fontname, size, FontStyle::BoldItalic, FontKind::Overlay, metrics_);
if (!dwfontname.empty()) {
FontMetrics wideMetrics{
.width = (u16)(2 * metrics_.width),
.height = metrics_.height,
.baseline = metrics_.baseline,
};
doubleWidth_ = createOptional(composer, pool, dwfontname, size, FontStyle::Regular, FontKind::DoubleWidth, wideMetrics);
}
}
Font* FontpackImpl::createOptional(Composer& composer, ObjPool& pool, StringView name, u16 size, FontStyle style, FontKind kind, FontMetrics metrics) {
try {
return composer.loadFont(pool, {name, size, style, kind}, metrics);
} catch (Exception&) {
return nullptr;
}
}
u16 FontpackImpl::getPx() const {
return metrics_.width;
}
u16 FontpackImpl::getPy() const {
return metrics_.height;
}
bool FontpackImpl::hasBold() const {
return bold_ != nullptr;
}
bool FontpackImpl::hasItalic() const {
return italic_ != nullptr;
}
bool FontpackImpl::hasBoldItalic() const {
return boldItalic_ != nullptr;
}
bool FontpackImpl::hasDoubleWidth() const {
return doubleWidth_ != nullptr;
}
Font* FontpackImpl::select(FontStyle style) const noexcept {
switch (style) {
case FontStyle::Bold:
return bold_ != nullptr ? bold_ : regular_;
case FontStyle::Italic:
return italic_ != nullptr ? italic_ : regular_;
case FontStyle::BoldItalic:
if (boldItalic_ != nullptr) {
return boldItalic_;
}
if (italic_ != nullptr) {
return italic_;
}
return bold_ != nullptr ? bold_ : regular_;
case FontStyle::Regular:
return regular_;
}
return regular_;
}
FontGlyph FontpackImpl::fallback(Font* font, Font* base, const u32* codepoints, size_t count) {
FontGlyph result = font->glyph(codepoints, count);
if (result.len == 0 && font != base) {
result = base->glyph(codepoints, count);
font = base;
}
const u32 replacement = Unicode_Replacement_Character;
if (result.len == 0 && (count != 1 || codepoints[0] != replacement)) {
result = font->glyph(&replacement, 1);
}
const u32 missing = Missing_Glyph_Marker;
if (result.len == 0 && (count != 1 || codepoints[0] != missing)) {
result = font->glyph(&missing, 1);
}
return result;
}
FontGlyph FontpackImpl::glyph(const u32* codepoints, size_t count, FontStyle style, bool doubleWidth) {
if (doubleWidth) {
return doubleWidth_ != nullptr ? fallback(doubleWidth_, doubleWidth_, codepoints, count) : FontGlyph{};
}
return fallback(select(style), regular_, codepoints, count);
}
Fontpack* Fontpack::create(Composer& composer, ObjPool& pool, StringView fontname, StringView dwfontname, u16 size) {
return pool.make<FontpackImpl>(composer, pool, fontname, dwfontname, size);
}