Skip to content

Commit

Permalink
Merge pull request #4779 from vakhoir/bugfix/font_bake_memory_leak
Browse files Browse the repository at this point in the history
Fix on-demand glyph loading
  • Loading branch information
Web-eWorks committed Feb 2, 2020
2 parents 105d06e + 13f7645 commit 8ff5080
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
15 changes: 6 additions & 9 deletions src/PiGui.cpp
Expand Up @@ -138,7 +138,7 @@ void PiGui::AddGlyph(ImFont *font, unsigned short glyph)
}
PiFont &pifont = pifont_iter->second;
for (PiFace &face : pifont.faces()) {
if (face.containsGlyph(glyph)) {
if (face.isValidGlyph(glyph)) {
face.addGlyph(glyph);
m_should_bake_fonts = true;
return;
Expand Down Expand Up @@ -743,9 +743,9 @@ PiGui::PiGui() :
m_should_bake_fonts(true)
{
PiFont uiheading("orbiteer", {
PiFace("DejaVuSans.ttf", /*18.0/20.0*/ 1.2, { { 0x400, 0x4ff }, { 0x500, 0x527 } }), PiFace("wqy-microhei.ttc", 1.0, { { 0x4e00, 0x9fff }, { 0x3400, 0x4dff } }), PiFace("Orbiteer-Bold.ttf", 1.0, { { 0, 0xffff } }) // imgui only supports 0xffff, not 0x10ffff
PiFace("DejaVuSans.ttf", /*18.0/20.0*/ 1.2), PiFace("wqy-microhei.ttc", 1.0), PiFace("Orbiteer-Bold.ttf", 1.0) // imgui only supports 0xffff, not 0x10ffff
});
PiFont guifont("pionillium", { PiFace("DejaVuSans.ttf", 13.0 / 14.0, { { 0x400, 0x4ff }, { 0x500, 0x527 } }), PiFace("wqy-microhei.ttc", 1.0, { { 0x4e00, 0x9fff }, { 0x3400, 0x4dff } }), PiFace("PionilliumText22L-Medium.ttf", 1.0, { { 0, 0xffff } }) });
PiFont guifont("pionillium", { PiFace("DejaVuSans.ttf", 13.0 / 14.0), PiFace("wqy-microhei.ttc", 1.0), PiFace("PionilliumText22L-Medium.ttf", 1.0) });
AddFontDefinition(uiheading);
AddFontDefinition(guifont);

Expand All @@ -759,14 +759,10 @@ PiGui::PiGui() :
GetFont("pionillium", 14);
};

const bool PiFace::containsGlyph(unsigned short glyph) const
const bool PiFace::isValidGlyph(unsigned short glyph) const
{
PROFILE_SCOPED()
for (auto range : m_ranges) {
if (range.first <= glyph && glyph <= range.second)
return true;
}
return false;
return (m_invalid_glyphs.count(glyph) == 0);
}

void PiFace::addGlyph(unsigned short glyph)
Expand All @@ -776,6 +772,7 @@ void PiFace::addGlyph(unsigned short glyph)
for (auto &range : m_used_ranges) {
if (range.first <= glyph && glyph <= range.second) {
// Output(" - already added, not adding again\n");
m_invalid_glyphs.insert(glyph); //if already added it once and trying to add it again, it's invalid
return;
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/PiGui.h
Expand Up @@ -8,28 +8,25 @@
#include "RefCounted.h"
#include "graphics/opengl/RendererGL.h"
#include "imgui/imgui.h"
#include <unordered_set>

class PiFace {
friend class PiGui; // need acces to some private data
std::string m_ttfname; // only the ttf name, it is automatically sought in data/fonts/
float m_sizefactor; // the requested pixelsize is multiplied by this factor
std::vector<std::pair<unsigned short, unsigned short>> m_ranges;
std::unordered_set<unsigned short> m_invalid_glyphs;
mutable std::vector<std::pair<unsigned short, unsigned short>> m_used_ranges;
ImVector<ImWchar> m_imgui_ranges;

public:
PiFace(const std::string &ttfname, float sizefactor) :
m_ttfname(ttfname),
m_sizefactor(sizefactor) {}
PiFace(const std::string &ttfname, float sizefactor, const std::vector<std::pair<unsigned short, unsigned short>> &ranges) :
m_ttfname(ttfname),
m_sizefactor(sizefactor),
m_ranges(ranges) {}
const std::string &ttfname() const { return m_ttfname; }
const float sizefactor() const { return m_sizefactor; }
const std::vector<std::pair<unsigned short, unsigned short>> &ranges() const { return m_ranges; }
//std::unordered_map<unsigned short, unsigned short> &invalid_glyphs() const { return m_invalid_glyphs; }
const std::vector<std::pair<unsigned short, unsigned short>> &used_ranges() const { return m_used_ranges; }
const bool containsGlyph(unsigned short glyph) const;
const bool isValidGlyph(unsigned short glyph) const;
void addGlyph(unsigned short glyph);
void sortUsedRanges() const;
};
Expand Down Expand Up @@ -129,7 +126,7 @@ class PiGui : public RefCounted {

void Cleanup();
static bool LowThrustButton(const char *label, const ImVec2 &size_arg, int thrust_level, const ImVec4 &bg_col, int frame_padding, ImColor gauge_fg, ImColor gauge_bg);
static bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& imgSize, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col);
static bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col);

static void ThrustIndicator(const std::string &id_string, const ImVec2 &size, const ImVec4 &thrust, const ImVec4 &velocity, const ImVec4 &bg_col, int frame_padding, ImColor vel_fg, ImColor vel_bg, ImColor thrust_fg, ImColor thrust_bg);

Expand Down

0 comments on commit 8ff5080

Please sign in to comment.