/* 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 JKQTMathTextTextBaseNode::JKQTMathTextTextBaseNode(JKQTMathText *parent, const QString &text_): JKQTMathTextNode(parent), text(text_) { } JKQTMathTextTextBaseNode::~JKQTMathTextTextBaseNode() { } QString JKQTMathTextTextBaseNode::getText() const { return text; } void JKQTMathTextTextBaseNode::drawString(QPainter &painter, const JKQTMathTextEnvironment ¤tEv, double x, double y, const QString &txt) const { const QFont f=currentEv.getFont(parentMathText); drawString(painter, currentEv, f, x, y, txt); } void JKQTMathTextTextBaseNode::drawString(QPainter &painter, const JKQTMathTextEnvironment ¤tEv, const QFont &f, double x, double y, const QString &txt) const { const QFontMetricsF fm(f, painter.device()); const QPen p(currentEv.color, fm.lineWidth()/2.0, Qt::SolidLine); painter.setPen(p); if (currentEv.font==MTEblackboard && parentMathText->isFontBlackboardSimulated()) { QPainterPath path; path.addText(QPointF(x, y), f, txt); path.addText(QPointF(x+fm.lineWidth()/2.0, y), f, txt); painter.drawPath(path); } else { painter.setFont(f); painter.drawText(QPointF(x, y), txt); } } QString JKQTMathTextTextBaseNode::textTransform(const QString &text, const JKQTMathTextEnvironment &/*currentEv*/) const { return text; } bool JKQTMathTextTextBaseNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) { html=html +currentEv.toHtmlStart(defaultEv, parentMathText) +textTransform(text, currentEv).toHtmlEscaped() +currentEv.toHtmlAfter(defaultEv, parentMathText); return true; } JKQTMathTextTextNode::JKQTMathTextTextNode(JKQTMathText* _parent, const QString& textIn, bool addWhitespace, bool stripInnerWhitepace): JKQTMathTextTextBaseNode(_parent, "") { QString textTransformed=textIn; if (stripInnerWhitepace) { textTransformed=""; for (int i=0; i1 && text[0].isSpace()) { text=text.right(text.size()-1); } if (addWhitespace && (text.size()>0) && (!text[text.size()-1].isSpace())) text=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); 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()); #if (QT_VERSION>=QT_VERSION_CHECK(5, 15, 0)) const double sp=fm.horizontalAdvance(' '); #else const double sp=fm.width(' '); #endif width=0; double ascent=0; double descent=0; for (int i=0; i0 && textpart[i].at(textpart[i].size()-1).isSpace()) { // this correction is necessary, because it seems that QFontMetricsF::boundingRect() ignores trailing spaces width+=sp; } const double thisAscent=-tbr.top(); const double thisDescent=tbr.bottom(); ascent=qMax(ascent, thisAscent); descent=qMax(descent, thisDescent); } overallHeight=(ascent+descent); //fm.height(); baselineHeight=ascent; strikeoutPos=fm.strikeOutPos(); } void JKQTMathTextTextNode::splitTextForMathMode(const QString &txt, QStringList &textpart, QList &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); const QFontMetricsF fm(currentEv.getFont(parentMathText)); if (fnt.second==MTFELatin1 || 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; } JKQTMathTextVerbatimNode::JKQTMathTextVerbatimNode(JKQTMathText *_parent, const QString& _text, bool visibleWhitespace_, JKQTMathTextHorizontalAlignment _alignment, double _linespacingFactor, JKQTMathTextVerticalOrientation _verticalOrientation): JKQTMathTextTextBaseNode(_parent, _text), alignment(_alignment), lineSpacingFactor(_linespacingFactor), verticalOrientation(_verticalOrientation), visibleWhitespace(visibleWhitespace_) { } QString JKQTMathTextVerbatimNode::getTypeName() const { return QLatin1String("JKQTMathTextVerbatimNode"); } JKQTMathTextHorizontalAlignment JKQTMathTextVerbatimNode::getAlignment() const { return alignment; } JKQTMathTextVerticalOrientation JKQTMathTextVerbatimNode::getVerticalOrientation() const { return verticalOrientation; } double JKQTMathTextVerbatimNode::getLineSpacingFactor() const { return lineSpacingFactor; } bool JKQTMathTextVerbatimNode::getVisibleWhitespace() const { return visibleWhitespace; } double JKQTMathTextVerbatimNode::draw(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize *prevNodeSize) { doDrawBoxes(painter, x, y, currentEv); transformEnvironment(currentEv); const LayoutInfo l=calcLayout(painter, currentEv); QFont f=currentEv.getFont(parentMathText); f.setStyleStrategy(QFont::PreferDefault); f.setFixedPitch(true); painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); for (int i=0; i0; html+=currentEv.toHtmlStart(defaultEv, parentMathText); if (isMultiLine) { if (alignment==MTHALeft) { html+="
"; } else if (alignment==MTHACentered) { html+="
"; } else if (alignment==MTHARight) { html+="
"; } } html+="
";
    html+=textTransform(text, currentEv).toHtmlEscaped();
    html+="
"; if (isMultiLine) html+="
"; html+=currentEv.toHtmlAfter(defaultEv, parentMathText); return true; } void JKQTMathTextVerbatimNode::getSizeInternal(QPainter &painter, JKQTMathTextEnvironment currentEv, double &width, double &baselineHeight, double &overallHeight, double &strikeoutPos, const JKQTMathTextNodeSize *prevNodeSize) { transformEnvironment(currentEv); const LayoutInfo l=calcLayout(painter, currentEv); width=l.width; overallHeight=l.overallHeight; baselineHeight=l.baselineHeight; strikeoutPos=l.strikeoutPos; } void JKQTMathTextVerbatimNode::transformEnvironment(JKQTMathTextEnvironment ¤tEv) const { currentEv.font=MTEtypewriter; } JKQTMathTextVerbatimNode::LayoutInfo JKQTMathTextVerbatimNode::calcLayout(QPainter &painter, const JKQTMathTextEnvironment& currentEv) const { LayoutInfo l; QFont f=currentEv.getFont(parentMathText); f.setStyleStrategy(QFont::PreferDefault); f.setFixedPitch(true); const QFontMetricsF fm(f); const double linespacing=fm.lineSpacing()*lineSpacingFactor; const double fleading=fm.leading(); const double synLeading=fm.lineWidth(); const double lineLeading=((fabs(fleading)>1e-6)?fleading:synLeading)*lineSpacingFactor; if (text.size()<=0) { return l; } l.lines=textTransform(text, currentEv).split('\n'); // from now on we have at least one child node!!! QList widths, heights, ascents, descents, strikeouts; double heightSum=0; QList ysFromFirstLine; // y-position of each line, where the first line is always at y=0 (i.e. ysFromFirstLine[0]==0) double y=0; for (int i=0; i0) { const double deltaLine=qMax(linespacing, descents.last()+lineLeading+fm.ascent()); heightSum=heightSum+deltaLine; y=y+deltaLine; } widths<