Files
pdf/engine/tests/fonts_test.cpp
T

132 lines
3.6 KiB
C++

#include "fonts/font_face.hpp"
#include "fonts/hb_shaper.hpp"
#include <gtest/gtest.h>
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
namespace {
std::string getSystemFontPath() {
#if defined(_WIN32)
// Common Windows fonts
std::vector<std::string> paths = {
"C:\\Windows\\Fonts\\arial.ttf",
"C:\\Windows\\Fonts\\consola.ttf",
"C:\\Windows\\Fonts\\tahoma.ttf"
};
#elif defined(__APPLE__)
// Common macOS fonts
std::vector<std::string> paths = {
"/Library/Fonts/Arial.ttf",
"/System/Library/Fonts/Geneva.ttf",
"/System/Library/Fonts/Helvetica.ttc",
"/System/Library/Fonts/Supplemental/Arial.ttf"
};
#else
// Common Linux fonts
std::vector<std::string> paths = {
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
"/usr/share/fonts/truetype/freefont/FreeSans.ttf"
};
#endif
for (const auto& path : paths) {
if (std::filesystem::exists(path)) {
return path;
}
}
return "";
}
} // namespace
namespace pdfengine::fonts {
TEST(FontTest, FontFaceInitialization) {
FontFace face;
EXPECT_EQ(face.getFace(), nullptr);
}
TEST(FontTest, FontFaceLoadNonExistentFile) {
FontFace face;
EXPECT_FALSE(face.loadFromFile("this_file_does_not_exist_12345.ttf"));
EXPECT_EQ(face.getFace(), nullptr);
}
TEST(FontTest, FontFaceMoveSemantics) {
std::string fontPath = getSystemFontPath();
if (fontPath.empty()) {
GTEST_SKIP() << "No system font found to run move semantics test.";
}
FontFace face1;
ASSERT_TRUE(face1.loadFromFile(fontPath));
FT_Face rawFace = face1.getFace();
ASSERT_NE(rawFace, nullptr);
// Move construction
FontFace face2(std::move(face1));
EXPECT_EQ(face1.getFace(), nullptr);
EXPECT_EQ(face2.getFace(), rawFace);
// Move assignment
FontFace face3;
face3 = std::move(face2);
EXPECT_EQ(face2.getFace(), nullptr);
EXPECT_EQ(face3.getFace(), rawFace);
}
TEST(FontTest, HbShaperEmptyInput) {
std::string fontPath = getSystemFontPath();
if (fontPath.empty()) {
GTEST_SKIP() << "No system font found to run empty input shaper test.";
}
FontFace face;
ASSERT_TRUE(face.loadFromFile(fontPath));
HbShaper shaper;
auto glyphs = shaper.shapeText(face, "");
EXPECT_TRUE(glyphs.empty());
}
TEST(FontTest, HbShaperNullFace) {
FontFace face; // Null face
HbShaper shaper;
auto glyphs = shaper.shapeText(face, "Hello");
EXPECT_TRUE(glyphs.empty());
}
TEST(FontTest, HbShaperShapeTextSuccess) {
std::string fontPath = getSystemFontPath();
if (fontPath.empty()) {
std::cout << "[ WARNING ] Skipping shape success test: no system font found." << std::endl;
GTEST_SKIP() << "No system font found to run text shaping test.";
}
FontFace face;
ASSERT_TRUE(face.loadFromFile(fontPath));
ASSERT_NE(face.getFace(), nullptr);
HbShaper shaper;
std::string testText = "Hello World!";
auto glyphs = shaper.shapeText(face, testText);
// Validate that some glyphs were shaped.
// Note that the number of glyphs doesn't strictly have to match testText.length() (e.g. ligatures),
// but for simple English it's usually 1:1.
EXPECT_FALSE(glyphs.empty());
for (const auto& g : glyphs) {
// Glyph index should be non-zero for valid glyphs (0 is usually .notdef)
// Note: some fonts might not map all characters, but Arial/DejaVu/Consolas should map ASCII.
EXPECT_GT(g.xAdvance, 0.0);
}
}
} // namespace pdfengine::fonts