Skip to content

Commit

Permalink
Merge pull request #1208 from lairworks/addFontNull
Browse files Browse the repository at this point in the history
Add `Font::null()` static constructor
  • Loading branch information
DanRStevens authored Feb 10, 2025
2 parents b4152d2 + 96ea6ed commit b8664e7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion NAS2D/Resource/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ namespace
}


Font Font::null()
{
return Font{};
}


Font::Font() :
mFontInfo{}
{
}

/**
* Instantiate a Font using a TrueType or OpenType font.
*
Expand All @@ -91,7 +102,12 @@ Font::Font(const std::string& filePath) :

Font::~Font()
{
glDeleteTextures(1, &mFontInfo.textureId);
// Documentation for `glDeleteTextures` says it should be safe to delete 0
// However, MacOS shows a segmentation fault when 0 is passed
if (mFontInfo.textureId)
{
glDeleteTextures(1, &mFontInfo.textureId);
}
}


Expand Down
4 changes: 4 additions & 0 deletions NAS2D/Resource/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace NAS2D
std::vector<GlyphMetrics> metrics{};
};

static Font null();

Font(const std::string& filePath, unsigned int ptSize);
explicit Font(const std::string& filePath);
Expand All @@ -79,6 +80,9 @@ namespace NAS2D
// As it is so specific, it should not be part of the Font class, nor FontInfo
unsigned int textureId() const;

protected:
Font();

private:
FontInfo mFontInfo;
};
Expand Down
29 changes: 29 additions & 0 deletions test/Resource/Font.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "NAS2D/Resource/Font.h"

#include <gtest/gtest.h>


TEST(Font, null) {
EXPECT_NO_THROW(NAS2D::Font::null());
}

TEST(Font, nullSafelyUsable) {
const auto font = NAS2D::Font::null();

EXPECT_EQ((NAS2D::Vector{0, 0}), font.glyphCellSize());
EXPECT_EQ((NAS2D::Vector{0, 0}), font.size(""));
EXPECT_EQ(0, font.width(""));
EXPECT_EQ(0, font.height());
EXPECT_EQ(0, font.ascent());
EXPECT_EQ(0, font.ptSize());

const auto& metrics = font.metrics();
EXPECT_EQ(0, metrics.size());

EXPECT_EQ(0, font.textureId());
}

TEST(Font, nullWidthNonEmptyString) {
const auto font = NAS2D::Font::null();
EXPECT_EQ(0, font.width("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
}

0 comments on commit b8664e7

Please sign in to comment.