-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFont.cpp
More file actions
126 lines (111 loc) · 3.07 KB
/
Copy pathFont.cpp
File metadata and controls
126 lines (111 loc) · 3.07 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
#include "Font.h"
namespace sh::render
{
Font::Font(const CreateInfo& ci) :
atlases(ci.atlases),
glyphs(ci.glyphs),
ascent(ci.ascent), descent(ci.descent), scale(ci.scale), lineGap(ci.lineGap)
{
}
Font::Font(CreateInfo&& ci) noexcept :
atlases(std::move(ci.atlases)),
glyphs(std::move(ci.glyphs)),
ascent(ci.ascent), descent(ci.descent), scale(ci.scale), lineGap(ci.lineGap)
{
}
Font::Font(Font&& other) noexcept :
atlases(std::move(other.atlases)),
glyphs(std::move(other.glyphs)),
ascent(other.ascent), descent(other.descent), scale(other.scale), lineGap(other.lineGap)
{
}
Font::~Font()
{
}
SH_RENDER_API auto Font::operator=(Font&& other) noexcept -> Font&
{
if (this == &other)
return *this;
atlases = std::move(other.atlases);
glyphs = std::move(other.glyphs);
ascent = (other.ascent);
descent = (other.descent);
scale = (other.scale);
lineGap = (other.lineGap);
return *this;
}
SH_RENDER_API auto Font::Serialize() const -> core::Json
{
core::Json mainJson = Super::Serialize();
core::Json& fontJson = mainJson["Font"];
fontJson["ascent"] = ascent;
fontJson["descent"] = descent;
fontJson["lineGap"] = lineGap;
fontJson["scale"] = scale;
core::Json& glyphJsons = mainJson["glyphs"];
for (auto& [unicode, glyph] : glyphs)
{
core::Json glyphJson;
glyphJson["unicode"] = unicode;
glyphJson["w"] = glyph.w;
glyphJson["h"] = glyph.h;
glyphJson["bearingX"] = glyph.bearingX;
glyphJson["bearingY"] = glyph.bearingY;
glyphJson["advance"] = glyph.advance;
glyphJson["page"] = glyph.page;
glyphJson["u0"] = glyph.u0;
glyphJson["v0"] = glyph.v0;
glyphJson["u1"] = glyph.u1;
glyphJson["v1"] = glyph.v1;
glyphJsons.push_back(std::move(glyphJson));
}
return mainJson;
}
SH_RENDER_API void Font::Deserialize(const core::Json& json)
{
Clear();
Super::Deserialize(json);
const core::Json& fontJson = json["Font"];
ascent = fontJson.value("ascent", 0.f);
descent = fontJson.value("descent", 0.f);
lineGap = fontJson.value("lineGap", 0.f);
scale = fontJson.value("scale", 1.f);
if (!json.contains("glyphs"))
return;
const core::Json& glyphJsons = json["glyphs"];
for (const auto& glyphJson : glyphJsons)
{
Glyph glyph{};
glyph.unicode = glyphJson["unicode"];
glyph.w = glyphJson["w"];
glyph.h = glyphJson["h"];
glyph.bearingX = glyphJson["bearingX"];
glyph.bearingY = glyphJson["bearingY"];
glyph.advance = glyphJson["advance"];
glyph.page = glyphJson["page"];
glyph.u0 = glyphJson["u0"];
glyph.v0 = glyphJson["v0"];
glyph.u1 = glyphJson["u1"];
glyph.v1 = glyphJson["v1"];
glyphs.emplace(glyph.unicode, glyph);
}
}
SH_RENDER_API void Font::Clear()
{
atlases.clear();
glyphs.clear();
ascent = descent = lineGap = 0.f;
scale = 1.f;
}
SH_RENDER_API auto Font::GetGlyph(uint32_t unicode) const -> const Glyph*
{
auto it = glyphs.find(unicode);
if (it == glyphs.end())
return nullptr;
return &it->second;
}
SH_RENDER_API auto Font::GetLineHeightPx() const -> float
{
return (ascent - descent + lineGap) * scale;
}
}//namespace