Skip to content

Commit

Permalink
Rename m_ranges to m_invalid_glyphs
Browse files Browse the repository at this point in the history
Rename containsGlyph to isValidGlyph
Change the approach to checking if a font can handle a glyph. The original approach was to pass the valid glyph range to the constructor. When it's not found by ImGui we call AddGlyph where we check if it's valid for the font. In the event where ImGui couldn't handle a glyph that was defined as valid in the constructor, that led to constant re-baking of fonts. The new approach is to change m_ranges (which used to contain valid glyph ranges) to m_invalid_glyphs, which contains only invalid glyphs that we tried to add to the font but failed. The next time AddGlyph is called, we skip the font since it's unable to handle the glyph.
  • Loading branch information
vakhoir committed Jan 30, 2020
1 parent 99a2e0b commit 13a161f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
15 changes: 6 additions & 9 deletions src/PiGui.cpp
Original file line number Diff line number Diff line change
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
11 changes: 4 additions & 7 deletions src/PiGui.h
Original file line number Diff line number Diff line change
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

0 comments on commit 13a161f

Please sign in to comment.