/* 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 "jkqtcommon/jkqtpdebuggingtools.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) const { html=html +currentEv.toHtmlStart(defaultEv, parentMathText) +textTransform(text, currentEv).toHtmlEscaped() +currentEv.toHtmlAfter(defaultEv, parentMathText); return true; } const QHash& JKQTMathTextTextNode::blackboardUnicodeTable(){ static QHash table=[]() { QHash blackboardUnicodeTable; const QString ALPHA="ABDEFGIJKLMOSTUVWXYZ"; for (const QChar ch: ALPHA) { blackboardUnicodeTable[ch]=0x1D538+(ch.unicode()-QChar('A').unicode()); } const QString alpha="abcdefghijklmnopqrstuvwxyz"; for (const QChar ch: alpha) { blackboardUnicodeTable[ch]=0x1D552+(ch.unicode()-QChar('a').unicode()); } const QString nums="0123456789"; for (const QChar ch: nums) { blackboardUnicodeTable[ch]=0x1D7D8+(ch.unicode()-QChar('0').unicode()); } blackboardUnicodeTable['C']=0x2102; blackboardUnicodeTable['H']=0x210D; blackboardUnicodeTable['N']=0x2115; blackboardUnicodeTable['P']=0x2119; blackboardUnicodeTable['Q']=0x211A; blackboardUnicodeTable['R']=0x211D; blackboardUnicodeTable['Z']=0x2124; return blackboardUnicodeTable; }(); return table; } 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; JKQTMathTextNodeSize JKQTMathTextTextNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv) const { return calcLayout(painter, currentEv); } JKQTMathTextTextNode::LayoutInfo JKQTMathTextTextNode::calcLayout(QPainter &painter, JKQTMathTextEnvironment currentEv) const { LayoutInfo l; const QString txt=textTransform(text, currentEv); splitTextForLayout(painter, currentEv, txt, l.textpart, l.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 double sp=JKQTMathTextGetHorAdvance(f, " ", painter.device()); l.width=0; double ascent=0; double descent=0; for (int i=0; i0) l.baselineXCorrection=JKQTMathTextGetRightBearing(f,l.textpart[i].operator[](l.textpart[i].size()-1),painter.device()); break; case FMasDefinedForceUpright: br=JKQTMathTextGetBoundingRect(fUpright, l.textpart[i], painter.device()); tbr=JKQTMathTextGetTightBoundingRect(fUpright, l.textpart[i], painter.device()); hadv=JKQTMathTextGetHorAdvance(fUpright, l.textpart[i], painter.device()); break; case FMroman: br=JKQTMathTextGetBoundingRect(fRoman, l.textpart[i], painter.device()); tbr=JKQTMathTextGetTightBoundingRect(fRoman, l.textpart[i], painter.device()); if (fRoman.italic() && l.textpart[i].size()>0) l.baselineXCorrection=JKQTMathTextGetRightBearing(fRoman,l.textpart[i].operator[](l.textpart[i].size()-1),painter.device()); hadv=JKQTMathTextGetHorAdvance(fRoman, l.textpart[i], painter.device()); break; case FMfallbackSymbol: br=JKQTMathTextGetBoundingRect(fFallbackSym, l.textpart[i], painter.device()); tbr=JKQTMathTextGetTightBoundingRect(fFallbackSym, l.textpart[i], painter.device()); if (fFallbackSym.italic() && l.textpart[i].size()>0) l.baselineXCorrection=JKQTMathTextGetRightBearing(fFallbackSym,l.textpart[i].operator[](l.textpart[i].size()-1),painter.device()); hadv=JKQTMathTextGetHorAdvance(fFallbackSym, l.textpart[i], painter.device()); break; } l.textpartXPos.append(l.width); if (i==l.textpart.size()-1) l.width+=tbr.width()+qMax(0.0,tbr.x()); else l.width+=hadv; /* if (l.textpart[i].size()>0 && l.textpart[i].at(l.textpart[i].size()-1).isSpace()) { // this correction is necessary, because it seems that QFontMetricsF::boundingRect() ignores trailing spaces l.width+=sp; }*/ const double thisAscent=-tbr.top(); const double thisDescent=tbr.bottom(); ascent=qMax(ascent, thisAscent); descent=qMax(descent, thisDescent); } l.overallHeight=(ascent+descent); //fm.height(); l.baselineHeight=ascent; l.strikeoutPos=JKQTMathTextGetFontStrikoutPos(f, painter.device()); return l; } 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 { #ifdef JKQTBP_AUTOTIMER JKQTPAutoOutputTimer jkaat(QString("JKQTMathTextTextNode[]::draw()")); #endif const LayoutInfo l=calcLayout(painter, currentEv); doDrawBoxes(painter, x, y, l); 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)); const bool ch2212InFont=fm.inFont(QChar(0x2212)); 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; } JKQTMathTextTextNode::LayoutInfo::LayoutInfo(): JKQTMathTextNodeSize(), textpart(), fontMode(), textpartXPos() { } JKQTMathTextTextNode::LayoutInfo::LayoutInfo(const LayoutInfo &other): LayoutInfo() { operator=(other); } JKQTMathTextTextNode::LayoutInfo::LayoutInfo(const JKQTMathTextNodeSize &other): LayoutInfo() { operator=(other); } JKQTMathTextTextNode::LayoutInfo &JKQTMathTextTextNode::LayoutInfo::operator=(const LayoutInfo &other) { JKQTMathTextNodeSize::operator=(other); textpart=other.textpart; fontMode=other.fontMode; textpartXPos=other.textpartXPos; return *this; } JKQTMathTextTextNode::LayoutInfo &JKQTMathTextTextNode::LayoutInfo::operator=(const JKQTMathTextNodeSize &other) { JKQTMathTextNodeSize::operator=(other); textpart.clear(); fontMode.clear(); textpartXPos.clear(); return *this; }