Add initial font metrics and equation rendering.

This commit is contained in:
jmsgrogan 2023-01-25 16:51:36 +00:00
parent c2027801be
commit 5ddd54dd6d
24 changed files with 868 additions and 63 deletions

View file

@ -1,5 +1,6 @@
add_subdirectory(test_utils)
add_subdirectory(fonts)
add_subdirectory(geometry)
add_subdirectory(graphics)
add_subdirectory(publishing)
@ -13,7 +14,6 @@ set(TEST_MODULES
compression
core
database
fonts
image
ipc
network

View file

@ -4,23 +4,24 @@ if(UNIX)
find_package(Freetype QUIET)
if(Freetype_FOUND)
set(PLATFORM_UNIT_TEST_FILES
fonts/TestFreeTypeFontEngine.cpp
TestFreeTypeFontEngine.cpp
)
endif()
else()
set(PLATFORM_UNIT_TEST_FILES
fonts/TestDirectWriteFontEngine.cpp
TestDirectWriteFontEngine.cpp
)
endif()
set(MODULE_NAME fonts)
list(APPEND UNIT_TEST_FILES
TestFontReader.cpp
${PLATFORM_UNIT_TEST_FILES}
)
set(FONTS_UNIT_TEST_FILES
fonts/TestFontReader.cpp
${PLATFORM_UNIT_TEST_FILES}
PARENT_SCOPE
)
set(FONTS_UNIT_TEST_DEPENDENCIES
fonts
PARENT_SCOPE
)
set(UNIT_TEST_TARGET_NAME ${MODULE_NAME}_unit_tests)
add_executable(${UNIT_TEST_TARGET_NAME} ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES})
target_link_libraries(${UNIT_TEST_TARGET_NAME} PUBLIC test_utils fonts)
set_property(TARGET ${UNIT_TEST_TARGET_NAME} PROPERTY FOLDER test/${MODULE_NAME})

View file

@ -9,6 +9,7 @@
#include "SvgWriter.h"
#include "SvgShapeElements.h"
#include "File.h"
#include "FontItem.h"
TEST_CASE(TestDirectWriteFontEngine, "fonts")
{
@ -33,4 +34,17 @@ TEST_CASE(TestDirectWriteFontEngine, "fonts")
File file(TestUtils::getTestOutputDir(__FILE__) / "out.svg");
file.writeText(doc_string);
}
TEST_CASE(TestDirectWriteFontEngine_GlyphMetrics, "fonts")
{
DirectWriteFontEngine font_engine;
font_engine.initialize();
FontItem font("Verdana", 16);
const auto advance = font_engine.getHorizontalAdvance(font, "abc");
MLOG_INFO("Advance is: " << advance);
REQUIRE(advance != 0.0);
}

View file

@ -23,6 +23,7 @@ endif()
set(UNIT_TEST_FILES
TestRasterizer.cpp
TestTextRendering.cpp
${PLATFORM_UNIT_TEST_FILES}
)

View file

@ -0,0 +1,22 @@
#include "TestFramework.h"
#include "TestUtils.h"
#include "TestRenderUtils.h"
#include "TextNode.h"
TEST_CASE(TestTextRendeing_WithBoundingBoxes, "graphics")
{
TestRenderer renderer(800, 800);
auto loc = Point(10, 10);
TextNode text_node("abcdefgh", Transform(loc));
text_node.setRenderNodeBounds(true);
text_node.setRenderTextBounds(true);
auto scene = renderer.getScene();
scene->addNode(&text_node);
renderer.writeSvg(TestUtils::getTestOutputDir(__FILE__) / "text_with_bounding_box.svg");
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "text_with_bounding_box.png");
};

View file

@ -4,13 +4,16 @@
#include "TestRenderUtils.h"
#include "StringUtils.h"
#include "FontItem.h"
#include "TextNode.h"
#include "LatexSymbols.h"
#include "EquationNode.h"
#include "LatexMathExpression.h"
TEST_CASE(TestLatexConverter, "publishing")
{
FontItem font("Cambria Math", 14);
auto expression = std::make_unique<LatexMathExpression>("\\psi = \\frac{\\alpha + \\beta}{c}");
auto equation_node = std::make_unique<EquationNode>(Point(10, 10));
equation_node->setContent(expression.get());
auto psi = LatexSymbolLookup::getSymbolUtf8("psi");
auto alpha = LatexSymbolLookup::getSymbolUtf8("alpha");
@ -20,10 +23,7 @@ TEST_CASE(TestLatexConverter, "publishing")
TestRenderer renderer(800, 800);
auto text = std::make_unique<TextNode>(content, Point(10, 10));
text->setFont(font);
renderer.getScene()->addNode(text.get());
renderer.getScene()->addNode(equation_node.get());
renderer.writeSvg(TestUtils::getTestOutputDir(__FILE__) / "out.svg");
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "out.png");
};

View file

@ -68,12 +68,23 @@ bool TestCaseRunner::run(const std::vector<std::string>& args)
sLastTestFailed = false;
std::cout << "TestFramework: Running Test - " << test_case->getName() << std::endl;
try{
try
{
test_case->run();
}
catch (const std::runtime_error& re)
{
std::cout << "Failed with runtime error: " << re.what() << std::endl;
mFailingTests.push_back(test_case->getName());
}
catch (const std::exception& e)
{
std::cout << "Failed with exception: " << e.what() << std::endl;
mFailingTests.push_back(test_case->getName());
}
catch(...)
{
std::cout << "Failed with exception" << std::endl;
std::cout << "Failed with unknown exception" << std::endl;
mFailingTests.push_back(test_case->getName());
};

View file

@ -12,6 +12,8 @@
#include "Image.h"
#include "PngWriter.h"
#include "FontsManager.h"
#include "File.h"
class TestRenderer
@ -19,10 +21,14 @@ class TestRenderer
public:
TestRenderer(unsigned width = 1000, unsigned height = 1000)
{
mFontsManager = std::make_unique<FontsManager>();
mSurface = std::make_unique<DrawingSurface>();
mSurface->setSize(width, height);
mDrawingContext = std::make_unique<DrawingContext>(mSurface.get());
getScene()->setFontsManager(mFontsManager.get());
}
Scene* getScene() const
@ -60,4 +66,6 @@ public:
private:
std::unique_ptr<DrawingSurface> mSurface;
std::unique_ptr<DrawingContext> mDrawingContext;
std::unique_ptr<FontsManager> mFontsManager;
};