diff --git a/NAS2D/Resource/Font.cpp b/NAS2D/Resource/Font.cpp index 2ad5166c..0232b0c0 100644 --- a/NAS2D/Resource/Font.cpp +++ b/NAS2D/Resource/Font.cpp @@ -66,6 +66,17 @@ namespace } +Font Font::null() +{ + return Font{}; +} + + +Font::Font() : + mFontInfo{} +{ +} + /** * Instantiate a Font using a TrueType or OpenType font. * @@ -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); + } } diff --git a/NAS2D/Resource/Font.h b/NAS2D/Resource/Font.h index bcc6da85..c91a2677 100644 --- a/NAS2D/Resource/Font.h +++ b/NAS2D/Resource/Font.h @@ -59,6 +59,7 @@ namespace NAS2D std::vector metrics{}; }; + static Font null(); Font(const std::string& filePath, unsigned int ptSize); explicit Font(const std::string& filePath); @@ -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; }; diff --git a/test/Resource/Font.test.cpp b/test/Resource/Font.test.cpp new file mode 100644 index 00000000..83adf558 --- /dev/null +++ b/test/Resource/Font.test.cpp @@ -0,0 +1,29 @@ +#include "NAS2D/Resource/Font.h" + +#include + + +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")); +}