Clean up some rendering.

This commit is contained in:
jmsgrogan 2023-01-25 17:38:38 +00:00
parent 5ddd54dd6d
commit 77ce58c612
6 changed files with 55 additions and 19 deletions

View file

@ -2,6 +2,7 @@
#include "LatexMathExpression.h" #include "LatexMathExpression.h"
#include "TextNode.h" #include "TextNode.h"
#include "LineNode.h"
#include "FileLogger.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) if (expression->getType() == LatexMathExpression::Type::LINEAR)
{ {
MLOG_INFO("Processing linear expr with : " << expression->getExpressions().size() << " children"); 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) else if(expression->getType() == LatexMathExpression::Type::LEAF)
{ {
std::string content = "_"; std::string content;
for (const auto& symbol : expression->getSymbols()) for (const auto& symbol : expression->getSymbols())
{ {
content += symbol.mUnicode; 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<TextNode>(content, Transform(mTextCursor)); auto node = std::make_unique<TextNode>(content, Transform(mTextCursor));
node->setWidth(100);
node->setFont(mDefaultFont); node->setFont(mDefaultFont);
addChild(node.get()); 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) else if (expression->getType() == LatexMathExpression::Type::FRACTION)
{ {
@ -63,13 +70,19 @@ void EquationNode::addExpression(const LatexMathExpression* expression)
if (expression->getExpressions().size() == 2) if (expression->getExpressions().size() == 2)
{ {
auto x_cache = mTextCursor.getX(); auto x_cache = mTextCursor.getX();
mTextCursor.move(0.0, -10.0); mTextCursor.move(0.0, -9.0);
addExpression(expression->getExpressions()[0].get()); addExpression(sceneInfo, expression->getExpressions()[0].get());
mTextCursor.move(mTextCursor.getX() - x_cache, 20.0); mTextCursor.move(x_cache - mTextCursor.getX(), 16.0);
addExpression(expression->getExpressions()[1].get()); std::vector<Point> end_loc{ { 25.0, 0.0 } };
auto dividing_line = std::make_unique<LineNode>(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(); mChildren.clear();
mText.clear(); mText.clear();
addExpression(mContent); addExpression(sceneInfo, mContent);
} }

View file

@ -4,6 +4,7 @@
#include "FontItem.h" #include "FontItem.h"
class TextNode; class TextNode;
class LineNode;
class LatexMathExpression; class LatexMathExpression;
class EquationNode : public AbstractVisualNode class EquationNode : public AbstractVisualNode
@ -20,10 +21,11 @@ public:
protected: protected:
void createOrUpdateGeometry(SceneInfo* sceneInfo); void createOrUpdateGeometry(SceneInfo* sceneInfo);
void addExpression(const LatexMathExpression* expression); void addExpression(SceneInfo* sceneInfo, const LatexMathExpression* expression);
std::vector<std::unique_ptr<TextNode> > mText; std::vector<std::unique_ptr<TextNode> > mText;
std::vector<std::unique_ptr<LineNode> > mLines;
FontItem mDefaultFont{ "Cambria Math", 14 }; FontItem mDefaultFont{ "Cambria Math", 14 };
//FontItem mDefaultFont{ "Latin Modern Math", 14 }; //FontItem mDefaultFont{ "Latin Modern Math", 14 };

View file

@ -158,7 +158,7 @@ double DirectWriteFontEngine::getHorizontalAdvance(const FontItem& fontItem, con
{ {
const auto design_width = metrics[idx].advanceWidth; const auto design_width = metrics[idx].advanceWidth;
// https://learn.microsoft.com/en-us/windows/win32/gdi/device-vs--design-units // https://learn.microsoft.com/en-us/windows/win32/gdi/device-vs--design-units
double device_width = (design_width / static_cast<double>(design_per_em)) * (fontItem.getSize() / 72.0) * 96.0 * 0.75; double device_width = (design_width / static_cast<double>(design_per_em)) * (fontItem.getSize() / 72.0) * 96.0 * 0.8;
MLOG_INFO("Got advance width: " << device_width); MLOG_INFO("Got advance width: " << device_width);
//total_advance += std::floor(device_width); //total_advance += std::floor(device_width);

View file

@ -54,6 +54,24 @@ double TextNode::getHeight() const
return mHeight; 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) void TextNode::setWidth(double width)
{ {
if (mWidth != width) if (mWidth != width)
@ -190,8 +208,8 @@ void TextNode::update(SceneInfo* sceneInfo)
mTextItem = std::make_unique<SceneText>(); mTextItem = std::make_unique<SceneText>();
mTextItem->setName(mName + "_SceneText"); mTextItem->setName(mName + "_SceneText");
mContentWidth = sceneInfo->mFontsManager->getFontEngine()->getHorizontalAdvance(mTextData.mFont, mTextData.mContent); getContentWidth(sceneInfo);
mContentHeight = mTextData.mFont.getSize(); getContentHeight(sceneInfo);
if (mWidth == 1.0 && mHeight == 1.0) if (mWidth == 1.0 && mHeight == 1.0)
{ {

View file

@ -31,6 +31,9 @@ public:
double getWidth() const; double getWidth() const;
double getHeight() const; double getHeight() const;
double getContentWidth(SceneInfo* sceneInfo);
double getContentHeight(SceneInfo* sceneInfo);
void setWidth(double width); void setWidth(double width);
void setHeight(double height); void setHeight(double height);

View file

@ -10,7 +10,7 @@
TEST_CASE(TestLatexConverter, "publishing") TEST_CASE(TestLatexConverter, "publishing")
{ {
auto expression = std::make_unique<LatexMathExpression>("\\psi = \\frac{\\alpha + \\beta}{c}"); auto expression = std::make_unique<LatexMathExpression>("\\psi = \\frac{\\alpha + \\beta}{c + d} + e");
auto equation_node = std::make_unique<EquationNode>(Point(10, 10)); auto equation_node = std::make_unique<EquationNode>(Point(10, 10));
equation_node->setContent(expression.get()); equation_node->setContent(expression.get());