Task 4 (FreeType + HarfBuzz Wrapper) completed
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
add_executable(pdfengine_smoke
|
||||
smoke_test.cpp
|
||||
fonts_test.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(pdfengine_smoke
|
||||
@@ -11,6 +12,11 @@ target_link_libraries(pdfengine_smoke
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
target_include_directories(pdfengine_smoke
|
||||
PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../src"
|
||||
)
|
||||
|
||||
pdfengine_set_warnings(pdfengine_smoke)
|
||||
pdfengine_enable_sanitizers(pdfengine_smoke)
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user