diff --git a/src/publishing/plotting/EquationNode.cpp b/src/publishing/plotting/EquationNode.cpp index 1f226c6..2b1e63f 100644 --- a/src/publishing/plotting/EquationNode.cpp +++ b/src/publishing/plotting/EquationNode.cpp @@ -2,6 +2,7 @@ #include "LatexMathExpression.h" #include "TextNode.h" +#include "LineNode.h" #include "FileLogger.h" @@ -31,31 +32,37 @@ void EquationNode::update(SceneInfo* sceneInfo) } } -void EquationNode::addExpression(const LatexMathExpression* expression) +void EquationNode::addExpression(SceneInfo* sceneInfo, const LatexMathExpression* expression) { if (expression->getType() == LatexMathExpression::Type::LINEAR) { MLOG_INFO("Processing linear expr with : " << expression->getExpressions().size() << " children"); - for (const auto& expression : expression->getExpressions()) + for (const auto& child : expression->getExpressions()) { - addExpression(expression.get()); + addExpression(sceneInfo, child.get()); } } else if(expression->getType() == LatexMathExpression::Type::LEAF) { - std::string content = "_"; + std::string content; for (const auto& symbol : expression->getSymbols()) { content += symbol.mUnicode; } - MLOG_INFO("Processing leaf expr with content: " << content); + + if (content.empty()) + { + return; + } + + MLOG_INFO("Processing leaf expr with content: " << content << " and size " << content.size()); auto node = std::make_unique(content, Transform(mTextCursor)); - node->setWidth(100); node->setFont(mDefaultFont); addChild(node.get()); - mText.push_back(std::move(node)); - mTextCursor.move(20.0, 0.0); + mTextCursor.move(node->getContentWidth(sceneInfo), 0.0); + + mText.push_back(std::move(node)); } else if (expression->getType() == LatexMathExpression::Type::FRACTION) { @@ -63,13 +70,19 @@ void EquationNode::addExpression(const LatexMathExpression* expression) if (expression->getExpressions().size() == 2) { auto x_cache = mTextCursor.getX(); - mTextCursor.move(0.0, -10.0); - addExpression(expression->getExpressions()[0].get()); + mTextCursor.move(0.0, -9.0); + addExpression(sceneInfo, expression->getExpressions()[0].get()); - mTextCursor.move(mTextCursor.getX() - x_cache, 20.0); - addExpression(expression->getExpressions()[1].get()); + mTextCursor.move(x_cache - mTextCursor.getX(), 16.0); + std::vector end_loc{ { 25.0, 0.0 } }; + auto dividing_line = std::make_unique(Transform(mTextCursor), end_loc); + addChild(dividing_line.get()); + mLines.push_back(std::move(dividing_line)); - mTextCursor.move(0.0, -10.0); + mTextCursor.move(0.0, 2.0); + addExpression(sceneInfo, expression->getExpressions()[1].get()); + + mTextCursor.move(0.0, -9.0); } } } @@ -79,5 +92,5 @@ void EquationNode::createOrUpdateGeometry(SceneInfo* sceneInfo) mChildren.clear(); mText.clear(); - addExpression(mContent); + addExpression(sceneInfo, mContent); } \ No newline at end of file diff --git a/src/publishing/plotting/EquationNode.h b/src/publishing/plotting/EquationNode.h index 7b4a94f..da4ee51 100644 --- a/src/publishing/plotting/EquationNode.h +++ b/src/publishing/plotting/EquationNode.h @@ -4,6 +4,7 @@ #include "FontItem.h" class TextNode; +class LineNode; class LatexMathExpression; class EquationNode : public AbstractVisualNode @@ -20,10 +21,11 @@ public: protected: void createOrUpdateGeometry(SceneInfo* sceneInfo); - void addExpression(const LatexMathExpression* expression); + void addExpression(SceneInfo* sceneInfo, const LatexMathExpression* expression); std::vector > mText; + std::vector > mLines; FontItem mDefaultFont{ "Cambria Math", 14 }; //FontItem mDefaultFont{ "Latin Modern Math", 14 }; diff --git a/src/rendering/fonts/directx/DirectWriteFontEngine.cpp b/src/rendering/fonts/directx/DirectWriteFontEngine.cpp index 658cf39..f3e0519 100644 --- a/src/rendering/fonts/directx/DirectWriteFontEngine.cpp +++ b/src/rendering/fonts/directx/DirectWriteFontEngine.cpp @@ -158,7 +158,7 @@ double DirectWriteFontEngine::getHorizontalAdvance(const FontItem& fontItem, con { const auto design_width = metrics[idx].advanceWidth; // https://learn.microsoft.com/en-us/windows/win32/gdi/device-vs--design-units - double device_width = (design_width / static_cast(design_per_em)) * (fontItem.getSize() / 72.0) * 96.0 * 0.75; + double device_width = (design_width / static_cast(design_per_em)) * (fontItem.getSize() / 72.0) * 96.0 * 0.8; MLOG_INFO("Got advance width: " << device_width); //total_advance += std::floor(device_width); diff --git a/src/rendering/visual_elements/nodes/TextNode.cpp b/src/rendering/visual_elements/nodes/TextNode.cpp index 3deba63..494f0e4 100644 --- a/src/rendering/visual_elements/nodes/TextNode.cpp +++ b/src/rendering/visual_elements/nodes/TextNode.cpp @@ -54,6 +54,24 @@ double TextNode::getHeight() const return mHeight; } +double TextNode::getContentWidth(SceneInfo* sceneInfo) +{ + if (mContentWidth == 1.0) + { + mContentWidth = sceneInfo->mFontsManager->getFontEngine()->getHorizontalAdvance(mTextData.mFont, mTextData.mContent); + } + return mContentWidth; +} + +double TextNode::getContentHeight(SceneInfo* sceneInfo) +{ + if (mContentHeight == 1.0) + { + mContentHeight = mTextData.mFont.getSize(); + } + return mContentHeight; +} + void TextNode::setWidth(double width) { if (mWidth != width) @@ -190,8 +208,8 @@ void TextNode::update(SceneInfo* sceneInfo) mTextItem = std::make_unique(); mTextItem->setName(mName + "_SceneText"); - mContentWidth = sceneInfo->mFontsManager->getFontEngine()->getHorizontalAdvance(mTextData.mFont, mTextData.mContent); - mContentHeight = mTextData.mFont.getSize(); + getContentWidth(sceneInfo); + getContentHeight(sceneInfo); if (mWidth == 1.0 && mHeight == 1.0) { diff --git a/src/rendering/visual_elements/nodes/TextNode.h b/src/rendering/visual_elements/nodes/TextNode.h index c0ffeb3..90fd9cc 100644 --- a/src/rendering/visual_elements/nodes/TextNode.h +++ b/src/rendering/visual_elements/nodes/TextNode.h @@ -31,6 +31,9 @@ public: double getWidth() const; double getHeight() const; + double getContentWidth(SceneInfo* sceneInfo); + double getContentHeight(SceneInfo* sceneInfo); + void setWidth(double width); void setHeight(double height); diff --git a/test/publishing/TestLatexConverter.cpp b/test/publishing/TestLatexConverter.cpp index 37b288c..70e1085 100644 --- a/test/publishing/TestLatexConverter.cpp +++ b/test/publishing/TestLatexConverter.cpp @@ -10,7 +10,7 @@ TEST_CASE(TestLatexConverter, "publishing") { - auto expression = std::make_unique("\\psi = \\frac{\\alpha + \\beta}{c}"); + auto expression = std::make_unique("\\psi = \\frac{\\alpha + \\beta}{c + d} + e"); auto equation_node = std::make_unique(Point(10, 10)); equation_node->setContent(expression.get());