/* 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; } 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; } QHash JKQTMathTextTextNode::blackboardUnicodeTable=QHash(); void JKQTMathTextTextNode::fillStaticTables() { if (blackboardUnicodeTable.size()>0) return; blackboardUnicodeTable['C']=0x2102; blackboardUnicodeTable['H']=0x210D; blackboardUnicodeTable['N']=0x2115; blackboardUnicodeTable['P']=0x2119; blackboardUnicodeTable['Q']=0x211A; blackboardUnicodeTable['R']=0x211D; blackboardUnicodeTable['Z']=0x2124; for (const QChar ch: QString("ABDEFGIJKLMOSTUVWXYZ")) { blackboardUnicodeTable[ch]=0x1D538+(ch.unicode()-QChar('A').unicode()); } for (const QChar ch: QString("abcdefghijklmnopqrstuvwxyz")) { blackboardUnicodeTable[ch]=0x1D552+(ch.unicode()-QChar('a').unicode()); } for (const QChar ch: QString("0123456789")) { blackboardUnicodeTable[ch]=0x1D7D8+(ch.unicode()-QChar('0').unicode()); } } JKQTMathTextTextNode::JKQTMathTextTextNode(JKQTMathText* _parent, const QString& textIn, bool addWhitespace, bool stripInnerWhitepace): JKQTMathTextTextBaseNode(_parent, "") { fillStaticTables(); 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 fontMode; QList textpartXPos; getSizeInternalAndData(painter, currentEv, width, baselineHeight, overallHeight, strikeoutPos,textpart, fontMode, textpartXPos); } void JKQTMathTextTextNode::getSizeInternalAndData(QPainter &painter, JKQTMathTextEnvironment currentEv, double &width, double &baselineHeight, double &overallHeight, double &strikeoutPos, QStringList &textpart, QList &fontMode, QList &textpartXPos) { textpart.clear(); fontMode.clear(); const QString txt=textTransform(text, currentEv); splitTextForLayout(painter, currentEv, txt, textpart, fontMode); const QFont f=currentEv.getFont(parentMathText); const QFont fUpright=JKQTMathTextGetNonItalic(f); const QFont fFallbackSym=currentEv.exchangedFontFor(MTEFallbackSymbols).getFont(parentMathText); const QFont fRoman=currentEv.exchangedFontForRoman().getFont(parentMathText); const QFontMetricsF fmUpright(fUpright, painter.device()); const QFontMetricsF fm(f, painter.device()); const QFontMetricsF fmFallbackSym(fFallbackSym, painter.device()); const QFontMetricsF fmRoman(fRoman, 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::splitTextForLayout(QPainter &painter, JKQTMathTextEnvironment currentEv, const QString &txt, QStringList &textpart, QList &fontMode) const { 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); }; //const QFont f=currentEv.getFont(parentMathText); //const QFont fUpright=JKQTMathTextGetNonItalic(f); const QFont fFallbackSym=currentEv.exchangedFontFor(MTEFallbackSymbols).getFont(parentMathText); const QFont fRoman=currentEv.exchangedFontForRoman().getFont(parentMathText); //const QFontMetricsF fm(f, painter.device()); //const QFontMetricsF fmUpright(fUpright, painter.device()); const QFontMetricsF fmFallbackSym(fFallbackSym, painter.device()); const QFontMetricsF fmRoman(fRoman, painter.device()); const JKQTMathTextBlackboradDrawingMode bbMode=parentMathText->getFontBlackboradMode(); textpart.clear(); fontMode.clear(); QString currentSection=""; FontMode currentSectionFontMode=FMasDefined; int i=0; while (i0) { textpart.append(currentSection); fontMode.append(currentSectionFontMode); } } 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 textpartXPos; QList fontMode; getSizeInternalAndData(painter, currentEv, width, baselineHeight, overallHeight, sp, textpart, fontMode, textpartXPos); const QFont f=currentEv.getFont(parentMathText); const QFont fUpright=JKQTMathTextGetNonItalic(f); const QFont fFallbackSym=currentEv.exchangedFontFor(MTEFallbackSymbols).getFont(parentMathText); const QFont fRoman=currentEv.exchangedFontForRoman().getFont(parentMathText); const QFontMetricsF fm(f, painter.device()); const QFontMetricsF fmUpright(fUpright, painter.device()); const QFontMetricsF fmFallbackSym(fFallbackSym, painter.device()); const QFontMetricsF fmRoman(fRoman, painter.device()); painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); painter.setFont(f); painter.setPen(currentEv.color); //qDebug()<<"JKQTMathTextTextNode: text="<0 && text[text.size()-1].isSpace()) text=text.left(text.size()-1); } void JKQTMathTextTextNode::removeLeadingWhitespace() { while (text.size()>0 && text[0].isSpace()) text=text.right(text.size()-1); } QString JKQTMathTextTextNode::textTransform(const QString &text, const JKQTMathTextEnvironment ¤tEv) const { QString txt=text; auto fnt=parentMathText->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();}); painter.setFont(f); 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<