/* Copyright (c) 2008-2022 Jan W. Krieger () This software is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License (LGPL) for more details. You should have received a copy of the GNU Lesser General Public License (LGPL) along with this program. If not, see . */ #include "jkqtmathtext/nodes/jkqtmathtexttextnode.h" #include "jkqtmathtext/jkqtmathtexttools.h" #include "jkqtmathtext/nodes/jkqtmathtextnode.h" #include "jkqtmathtext/jkqtmathtext.h" #include "jkqtcommon/jkqtpcodestructuring.h" #include "jkqtcommon/jkqtpstringtools.h" #include #include #include #include #include #include #include JKQTMathTextTextNode::JKQTMathTextTextNode(JKQTMathText* _parent, const QString& textIn, bool addWhitespace, bool stripInnerWhitepace): JKQTMathTextNode(_parent) { QString text=textIn; if (stripInnerWhitepace) { text=""; for (int i=0; itext=text; // strip all whitespace from left while (this->text.size()>1 && this->text[0].isSpace()) { this->text=this->text.right(this->text.size()-1); } if (addWhitespace && (this->text.size()>0) && (!this->text[this->text.size()-1].isSpace())) this->text=this->text+" "; //qDebug()<<"JKQTMathTextTextNode( text="< this->text="<text<<"]"; } JKQTMathTextTextNode::~JKQTMathTextTextNode() = default; void JKQTMathTextTextNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos, const JKQTMathTextNodeSize* /*prevNodeSize*/) { QStringList textpart; QList fontForcedUpright; QList textpartXPos; getSizeInternalAndData(painter, currentEv, width, baselineHeight, overallHeight, strikeoutPos,textpart, fontForcedUpright, textpartXPos); } void JKQTMathTextTextNode::getSizeInternalAndData(QPainter &painter, JKQTMathTextEnvironment currentEv, double &width, double &baselineHeight, double &overallHeight, double &strikeoutPos, QStringList &textpart, QList &fontForcedUpright, QList &textpartXPos) { textpart.clear(); fontForcedUpright.clear(); const QString txt=textTransform(text, currentEv, true); if (currentEv.insideMath && currentEv.insideMathForceDigitsUpright) { splitTextForMathMode(txt, textpart, fontForcedUpright); } else { textpart.append(text); fontForcedUpright.append(false); } const QFont f=currentEv.getFont(parentMathText); const QFont fnonItalic=JKQTMathTextGetNonItalic(f); const QFontMetricsF fmNonItalic(fnonItalic, painter.device()); const QFontMetricsF fm(f, painter.device()); width=0; double ascent=0; double descent=0; for (int i=0; i &fontForcedUpright) { auto isForcedUprightChar=[](const QChar& c) { return c.isDigit() || c=='(' || c=='[' || c=='|' || c==']' || c==')' || c=='<' || c=='>'|| c=='{' || c=='}' || c=='|' || c==QChar(0x2329) || c==QChar(0x232A) || c==QChar(0x2308) || c==QChar(0x2309) || c==QChar(0x230A) || c==QChar(0x230B); }; textpart.clear(); fontForcedUpright.clear(); QString currentSection=""; bool currentSectionForcedUpright=false; int i=0; while (i0) { textpart.append(currentSection); fontForcedUpright.append(currentSectionForcedUpright); } } double JKQTMathTextTextNode::draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* /*prevNodeSize*/) { doDrawBoxes(painter, x, y, currentEv); double width=0; double baselineHeight=0; double overallHeight=0; double sp=0; QStringList textpart; QList fontForcedUpright; QList textpartXPos; getSizeInternalAndData(painter, currentEv, width, baselineHeight, overallHeight, sp, textpart, fontForcedUpright, textpartXPos); const QFont f=currentEv.getFont(parentMathText); const QFont fnonItalic=JKQTMathTextGetNonItalic(f); const QFontMetricsF fm(f, painter.device()); const QFontMetricsF fmNonItalic(fnonItalic, painter.device()); painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); painter.setFont(f); //qDebug()<<"JKQTMathTextTextNode: text="<getFontData(currentEv.font, currentEv.insideMath); if (fnt.second==MTFEUnicode || fnt.second==MTFEUnicode) { if (currentEv.insideMath) { txt=""; for (int i=0; i': txt+=QString(QString(" >")); break; case '=': txt+=QString(QString(" =")); break; case ';': txt+=QString(QString("; ")); break; case ',': txt+=QString(QString(", ")); break; default: txt+=c; break; } } txt=txt.replace(" ", " "); } } return txt; } MTplainTextNode::MTplainTextNode(JKQTMathText *_parent, const QString& _text, bool addWhitespace, bool stripInnerWhitepace): JKQTMathTextTextNode(_parent, _text, addWhitespace, stripInnerWhitepace) { } QString MTplainTextNode::getTypeName() const { return QLatin1String("MTplainTextNode(")+text+")"; } QString MTplainTextNode::textTransform(const QString &_text, JKQTMathTextEnvironment /*currentEv*/, bool /*forSize*/) { return _text; }