From 200c538a49104a5cff72be5cff354b744902e0fb Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Tue, 2 Aug 2022 11:38:40 +0200 Subject: [PATCH] JKQTMathText: improved HTML export (now color and font-family are supported) --- lib/jkqtmathtext/jkqtmathtexttools.cpp | 29 +++++++++++++++++-- lib/jkqtmathtext/jkqtmathtexttools.h | 10 +++++-- .../nodes/jkqtmathtextsymbolnode.cpp | 13 ++++++++- .../nodes/jkqtmathtextsymbolnode.h | 6 +++- .../nodes/jkqtmathtexttextnode.cpp | 2 +- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/lib/jkqtmathtext/jkqtmathtexttools.cpp b/lib/jkqtmathtext/jkqtmathtexttools.cpp index 5386b3317f..0ca129faa5 100644 --- a/lib/jkqtmathtext/jkqtmathtexttools.cpp +++ b/lib/jkqtmathtext/jkqtmathtexttools.cpp @@ -446,6 +446,29 @@ JKQTMathTextEnvironment::JKQTMathTextEnvironment() { overline=false; strike=false; insideMath=false; + insideMathForceDigitsUpright=true; +} + +void JKQTMathTextEnvironment::beginMathMode() +{ + insideMath=true; + insideMathForceDigitsUpright=true; + italic=true; + smallCaps=false; + underlined=false; + overline=false; + strike=false; +} + +void JKQTMathTextEnvironment::endMathMode() +{ + insideMath=false; + insideMathForceDigitsUpright=true; + italic=false; + smallCaps=false; + underlined=false; + overline=false; + strike=false; } JKQTMathTextFontEncoding JKQTMathTextEnvironment::getFontEncoding(JKQTMathText* parent) const { @@ -514,7 +537,7 @@ QFont JKQTMathTextEnvironment::getFont(JKQTMathText* parent) const { return f; } -QString JKQTMathTextEnvironment::toHtmlStart(JKQTMathTextEnvironment defaultEv) const { +QString JKQTMathTextEnvironment::toHtmlStart(JKQTMathTextEnvironment defaultEv, JKQTMathText* parentMathText) const { QString s; if (fontSizeUnit==POINTS) s=s+"font-size: "+QLocale::c().toString(fontSize)+"pt; "; else if (fontSizeUnit==PIXELS) s=s+"font-size: "+QLocale::c().toString(fontSize)+"px; "; @@ -530,6 +553,8 @@ QString JKQTMathTextEnvironment::toHtmlStart(JKQTMathTextEnvironment defaultEv) if (!defaultEv.italic && italic) s=s+"font-style: italic; "; } if (bold && !defaultEv.bold) s=s+"font-weight: bold"; + if (color!=defaultEv.color) s=s+"color: "+color.name(); + if (font!=defaultEv.font) s=s+"font-family: "+getFont(parentMathText).family(); QStringList td; if (underlined && !defaultEv.underlined) td<<"underline"; @@ -539,7 +564,7 @@ QString JKQTMathTextEnvironment::toHtmlStart(JKQTMathTextEnvironment defaultEv) return ""; } -QString JKQTMathTextEnvironment::toHtmlAfter(JKQTMathTextEnvironment /*defaultEv*/) const { +QString JKQTMathTextEnvironment::toHtmlAfter(JKQTMathTextEnvironment /*defaultEv*/, JKQTMathText */*parentMathText*/) const { return ""; } diff --git a/lib/jkqtmathtext/jkqtmathtexttools.h b/lib/jkqtmathtext/jkqtmathtexttools.h index 91c21b3f50..80944d1eaa 100644 --- a/lib/jkqtmathtext/jkqtmathtexttools.h +++ b/lib/jkqtmathtext/jkqtmathtexttools.h @@ -266,6 +266,12 @@ struct JKQTMATHTEXT_LIB_EXPORT JKQTMathTextEnvironment { bool strike; /** \brief is the text currently are we inside a math environment? */ bool insideMath; + /** \brief if \a insideMath \c ==true and this is \c true (the default), then digits are forced to be typeset in upright, otherwise they are typeset as defined by the other properties */ + bool insideMathForceDigitsUpright; + /** \brief sets insideMath \c =true and insideMathForceDigitsUpright \c =true */ + void beginMathMode(); + /** \brief sets insideMath \c =false and insideMathForceDigitsUpright \c =true */ + void endMathMode(); /** \brief build a QFont object from the settings in this object */ @@ -277,13 +283,13 @@ struct JKQTMATHTEXT_LIB_EXPORT JKQTMathTextEnvironment { * \param defaultEv environment before applying the current object (to detect changes) * \see toHtmlAfter() */ - QString toHtmlStart(JKQTMathTextEnvironment defaultEv) const; + QString toHtmlStart(JKQTMathTextEnvironment defaultEv, JKQTMathText *parentMathText) const; /** \brief generate a HTML postfix that formats the text in front of it according to the settings in this object * * \param defaultEv environment before applying the current object (to detect changes) * \see toHtmlAfter() */ - QString toHtmlAfter(JKQTMathTextEnvironment defaultEv) const; + QString toHtmlAfter(JKQTMathTextEnvironment defaultEv, JKQTMathText *parentMathText) const; }; /** \brief beschreibt die Größe eines Knotens diff --git a/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.cpp b/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.cpp index 4f69718447..f7a3438ddf 100644 --- a/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.cpp +++ b/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.cpp @@ -279,7 +279,7 @@ bool JKQTMathTextSymbolNode::toHtml(QString &html, JKQTMathTextEnvironment curre ev.fontSize=ev.fontSize*props.html.fontScalingFactor; if (has(props.globalFlags, ExtendWidthInMathmode)) s=" "+s+" "; if (has(props.globalFlags, MakeWhitespaceHalf)) s.replace(" ", " "); - if (ok) html=html+ev.toHtmlStart(defaultEv)+s+ev.toHtmlAfter(defaultEv); + if (ok) html=html+ev.toHtmlStart(defaultEv, parentMathText)+s+ev.toHtmlAfter(defaultEv, parentMathText); return ok; } @@ -361,6 +361,16 @@ JKQTMathTextSymbolNode::SymbolFullProps JKQTMathTextSymbolNode::NarrowMathOperat return SymbolFullProps(MTFEUnicode, SymbolProps(unicode, ItalicOff|BoldOff, 1.0, 0.0)).addGlobalFlags(SmallExtendWidthInMathmode|MakeWhitespaceHalf); } +JKQTMathTextSymbolNode::SymbolFullProps JKQTMathTextSymbolNode::NarrowMathOperatorSymbolStd(const QString &symbol) +{ + return NarrowMathOperatorSymbolStd(symbol,symbol); +} + +JKQTMathTextSymbolNode::SymbolFullProps JKQTMathTextSymbolNode::NarrowMathOperatorSymbolStd(const QString &symbol, const QString &symbolHTML) +{ + return SymbolFullProps(MTFEStandard, SymbolProps(symbol, ItalicOff|BoldOff, 1.0, 0.0)).addHtml(symbol, ItalicOff|BoldOff, 1.0, 0.0).addGlobalFlags(SmallExtendWidthInMathmode|MakeWhitespaceHalf); +} + JKQTMathTextSymbolNode::SymbolFullProps JKQTMathTextSymbolNode::GreekLetter_WinSymbol_Unicode_Html(const QString &letterWinSymbol, const QString &letterUnicode, const QString &html) { return SymbolFullProps(MTFEUnicode, SymbolProps(letterUnicode), MTFEWinSymbol, SymbolProps(letterWinSymbol), html); @@ -640,6 +650,7 @@ void JKQTMathTextSymbolNode::fillSymbolTables() **************************************************************************************/ { auto s=MathOperatorSymbolUnicode(QChar(0x2217)).addMathOperatorStd("*").addMathOperatorHtml("*"); symbols["*"]=s; symbols["ast"]=s; symbols["asterisk"]=s; } + symbols["/"]=NarrowMathOperatorSymbolStd("/"); symbols["+"]=MathOperatorSymbolUnicode(QChar(0x2B)).addMathOperatorHtml("+").addMathOperatorStd("+"); symbols["-"]=MathOperatorSymbolUnicode(QChar(0x2212)).addMathOperatorHtml("-").addMathOperatorStd("-"); symbols["<"]=MathOperatorSymbol("<", "<"); diff --git a/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.h b/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.h index f9b08bb10e..68ee594797 100644 --- a/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.h +++ b/lib/jkqtmathtext/nodes/jkqtmathtextsymbolnode.h @@ -275,8 +275,12 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathTextSymbolNode: public JKQTMathTextNode { static SymbolFullProps UprightSymbolUnicode(const QString& symbol, const QString& html=QString()); /** \brief constructs a SymbolProps for a math-operator symbol like \c \\pm ... in unicode-full-encoding, i.e. ItalicOff, BoldOff, ExtendWidthInMathmode */ static SymbolFullProps MathOperatorSymbolUnicode(const QString& unicode); - /** \brief constructs a SymbolProps for a math-operator symbol like \c \\pm ... in unicode-full-encoding, i.e. ItalicOff, BoldOff, SmallExtendWidthInMathmode */ + /** \brief constructs a SymbolProps for a narrow math-operator symbol like \c \\pm ... in unicode-full-encoding, i.e. ItalicOff, BoldOff, SmallExtendWidthInMathmode */ static SymbolFullProps NarrowMathOperatorSymbolUnicode(const QString& unicode); + /** \brief constructs a SymbolProps for a narrow math-operator symbol like \c \\pm ... in unicode-full-encoding, i.e. ItalicOff, BoldOff, SmallExtendWidthInMathmode */ + static SymbolFullProps NarrowMathOperatorSymbolStd(const QString& symbol); + /** \brief constructs a SymbolProps for a narrow math-operator symbol like \c \\pm ... in unicode-full-encoding, i.e. ItalicOff, BoldOff, SmallExtendWidthInMathmode */ + static SymbolFullProps NarrowMathOperatorSymbolStd(const QString& symbol, const QString& symbolHTML); /** \brief symbols that can be generated in any standard-font */ diff --git a/lib/jkqtmathtext/nodes/jkqtmathtexttextnode.cpp b/lib/jkqtmathtext/nodes/jkqtmathtexttextnode.cpp index 9c278a78b6..11e3885aca 100644 --- a/lib/jkqtmathtext/nodes/jkqtmathtexttextnode.cpp +++ b/lib/jkqtmathtext/nodes/jkqtmathtexttextnode.cpp @@ -182,7 +182,7 @@ double JKQTMathTextTextNode::draw(QPainter& painter, double x, double y, JKQTMat } bool JKQTMathTextTextNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) { - html=html+currentEv.toHtmlStart(defaultEv)+text+currentEv.toHtmlAfter(defaultEv); + html=html+currentEv.toHtmlStart(defaultEv, parentMathText)+text+currentEv.toHtmlAfter(defaultEv, parentMathText); return true; }