Skip to content

Commit b4f58a7

Browse files
committed
font stuff
1 parent 5101877 commit b4f58a7

2 files changed

Lines changed: 24 additions & 39 deletions

File tree

src/include/sndx/render/atlas.hpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
namespace sndx::render {
1414

1515
template <class IdT = std::string>
16-
class ImageAtlas {
17-
private:
16+
struct ImageAtlas {
1817
struct Entry {
1918
glm::vec<2, size_t> pos, dims;
2019
};
@@ -27,33 +26,20 @@ namespace sndx::render {
2726

2827
ImageAtlas(decltype(m_entries)&& entries, ImageData&& image) :
2928
m_entries(std::move(entries)), m_image(std::move(image)) {}
30-
public:
31-
32-
[[nodiscard]]
33-
const auto& getImage() const {
34-
return m_image;
35-
}
36-
37-
[[nodiscard]]
38-
const auto& getEntry(const IdT& id) const {
39-
return m_entries.at(id);
40-
}
29+
};
4130

42-
[[nodiscard]]
43-
auto size() const {
44-
return m_entries.size();
45-
}
31+
template <class IdT> [[nodiscard]]
32+
inline std::unordered_map<IdT, std::pair<glm::vec2, glm::vec2>> normalizeAtlasEntries(const std::unordered_map<IdT, typename ImageAtlas<IdT>::Entry>& entries, glm::vec2 size) {
33+
std::unordered_map<IdT, std::pair<glm::vec2, glm::vec2>> out{};
34+
out.reserve(entries.size());
4635

47-
[[nodiscard]]
48-
auto begin() const {
49-
return m_entries.begin();
36+
const glm::vec2 scaling = 1.0f / size;
37+
for (const auto& [id, entry] : entries) {
38+
out.emplace(id, std::pair{ entry.pos * scaling, entry.dims * scaling });
5039
}
5140

52-
[[nodiscard]]
53-
auto end() const {
54-
return m_entries.end();
55-
}
56-
};
41+
return out;
42+
}
5743

5844
template <class TextureT, class IdT = std::string>
5945
class TextureAtlas {
@@ -70,17 +56,22 @@ namespace sndx::render {
7056
public:
7157

7258
TextureAtlas(const ImageAtlas<IdT>& atlas, bool compress = false):
73-
m_entries{}, m_texture{atlas.getImage(), 0, compress } {
74-
m_entries.reserve(atlas.size());
75-
const auto& image = atlas.getImage();
59+
m_entries{}, m_texture{atlas.m_image, 0, compress } {
60+
m_entries.reserve(atlas.m_entries.size());
61+
const auto& image = atlas.m_image;
7662

7763
glm::vec2 scaling = 1.0f / glm::vec2{ image.width(), image.height()};
7864

79-
for (const auto& [id, entry] : atlas) {
65+
for (const auto& [id, entry] : atlas.m_entries) {
8066
Entry e{ glm::vec2{entry.pos} *scaling, glm::vec2{entry.dims} *scaling };
8167
m_entries.emplace(id, std::move(e));
8268
}
8369
}
70+
71+
[[nodiscard]]
72+
const auto& getEntries() const {
73+
return m_entries;
74+
}
8475

8576
[[nodiscard]]
8677
const auto& getTexture() const {

src/include/sndx/render/font.hpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace sndx::render {
1414
struct GlyphMetric {
15-
glm::ivec2 bearing, dims;
16-
float advance;
15+
glm::ivec2 bearing{}, dims{};
16+
float advance{};
1717

1818
constexpr GlyphMetric() = default;
1919

@@ -23,7 +23,7 @@ namespace sndx::render {
2323

2424
template <class AtlasT = TextureAtlas<FT_ULong>>
2525
class Font {
26-
private:
26+
public:
2727
AtlasT m_atlas;
2828
std::unordered_map<FT_ULong, GlyphMetric> m_metrics{};
2929

@@ -35,7 +35,6 @@ namespace sndx::render {
3535
Font(AtlasT&& atlas, const std::unordered_map<FT_ULong, GlyphMetric>& metrics, int maxBearingY, bool sdf) :
3636
m_atlas(std::move(atlas)), m_metrics(metrics), m_maxBearingY(maxBearingY), m_sdf(sdf) {}
3737

38-
public:
3938
// returns charcode if it exists, the charcode "white square" otherwise
4039
[[nodiscard]]
4140
FT_ULong getChar(FT_ULong charcode) const {
@@ -53,11 +52,6 @@ namespace sndx::render {
5352
const GlyphMetric& getCharMetrics(FT_ULong charcode) const {
5453
return m_metrics.at(getChar(charcode));
5554
}
56-
57-
[[nodiscard]]
58-
const auto& getAtlas() const {
59-
return m_atlas;
60-
}
6155
};
6256

6357
class FontBuilder {
@@ -73,7 +67,7 @@ namespace sndx::render {
7367

7468
using DefaultPacker = decltype(m_builder)::DefaultPacker;
7569

76-
friend class FreetypeContext;
70+
friend struct FreetypeContext;
7771

7872
FontBuilder(MetricsT&& metrics, GlyphsT&& glyphs, int maxBearingY, bool sdf) :
7973
m_metrics(std::move(metrics)), m_glyphs(std::move(glyphs)), m_maxBearingY(maxBearingY), m_sdf(sdf) {

0 commit comments

Comments
 (0)