/* 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/jkqtmathtextlistnode.h" #include "jkqtmathtext/nodes/jkqtmathtextsymbolnode.h" #include "jkqtmathtext/nodes/jkqtmathtextbracenode.h" #include "jkqtmathtext/nodes/jkqtmathtextsubsupernode.h" #include "jkqtmathtext/jkqtmathtexttools.h" #include "jkqtmathtext/jkqtmathtext.h" #include "jkqtcommon/jkqtpcodestructuring.h" #include "jkqtcommon/jkqtpstringtools.h" #include #include #include #include #include #include #include JKQTMathTextHorizontalListNode::JKQTMathTextHorizontalListNode(JKQTMathText* _parent): JKQTMathTextMultiChildNode(_parent) { nodes.clear(); // these operations cause sub/sup script to be typeset over/under the operator, not right besides! } JKQTMathTextHorizontalListNode::~JKQTMathTextHorizontalListNode() { clearChildrenImpl(true); } QString JKQTMathTextHorizontalListNode::getTypeName() const { return "MTHorizontalListNode"; } JKQTMathTextNodeSize JKQTMathTextHorizontalListNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv) const { JKQTMathTextNodeSize outSize; const QFontMetricsF fm(currentEv.getFont(parentMathText)); const double subsupershift=fm.xHeight()*parentMathText->getOperatorsubsuperDistanceFactor(); const double subsuperextrawidth=fm.boundingRect('x').width()*parentMathText->getOperatorsubsuperExtraSpaceFactor(); const double subsuperSpecialModeAscent=fm.ascent()*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double subsuperSpecialModeDecent=fm.descent()*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double spaceWidth=fm.boundingRect(' ').width(); //QRectF tbr=parent->getTightBoundingRect(currentEv.getFont(parent), "M", painter.device()); double xnew=0; //bool wasBrace=false; for (int i=0; i0) { JKQTMathTextSymbolNode* symbN=dynamic_cast(nodes[i-1]); if (symbN) prevNodeSize=symbN->getSymbolSize(painter, currentEv); else prevNodeSize = nodes[i-1]->getSize(painter, currentEv); const double prevAscent=prevNodeSize.baselineHeight; const double prevDescent=prevNodeSize.overallHeight-prevNodeSize.baselineHeight; const bool shouldUseSpecialSubscriptMode=prevAscent>=subsuperSpecialModeAscent; const bool shouldUseSpecialSuperscriptMode=prevDescent>=subsuperSpecialModeDecent; if (shouldUseSpecialSubscriptMode) prevNodeSizePtrForSubscript=&prevNodeSize; if (shouldUseSpecialSuperscriptMode) prevNodeSizePtrForSuperscript=&prevNodeSize; } JKQTMathTextSuperscriptNode* nodeI_SuperScript=dynamic_cast(nodes[i]); JKQTMathTextSubscriptNode* nodeI_SubScript=dynamic_cast(nodes[i]); bool doDraw=true; // if we find a subscript/superscript node we check whether the next node is super/subscript // if so, we typeset them at the same x-psotion, so sub/superscripts appear correctly if (nodeI_SuperScript) { if (i+1(nodes[i+1]); if (nodeISucc_SubScript) { // is this subscript? const JKQTMathTextNodeSize ns=nodeI_SuperScript->getSizeWithSpecialPlacement(painter, currentEv, prevNodeSizePtrForSuperscript); if (ns.baselineHeight>outSize.baselineHeight) { outSize.overallHeight=outSize.overallHeight+ns.baselineHeight-outSize.baselineHeight; outSize.baselineHeight=ns.baselineHeight; outSize.strikeoutPos=ns.strikeoutPos; } if (outSize.baselineHeight+ns.getDescent()>outSize.overallHeight) { outSize.overallHeight=outSize.baselineHeight+ns.getDescent(); outSize.strikeoutPos=ns.strikeoutPos; } i++; const JKQTMathTextNodeSize ns2=nodeISucc_SubScript->getSizeWithSpecialPlacement(painter, currentEv, prevNodeSizePtrForSubscript); //qDebug()<<"super_sub: sub: "<getTypeName()<<" w2="<getOperatorsubsuperSizeFactor(); const JKQTMathTextNodeSize ns=nodes[i]->getSize(painter, currentEv); //qDebug()<<"sub_super: node: "<getTypeName()<<" ns.width="<getTypeName()<<" w2="<getTypeName()<<" w3="<getOperatorsubsuperDistanceFactor(); const double subsuperextrawidth=fm.boundingRect('x').width()*parentMathText->getOperatorsubsuperExtraSpaceFactor(); const double subsuperSpecialModeAscent=fm.ascent()*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double subsuperSpecialModeDecent=fm.descent()*parentMathText->getSubsuperModeSelectionBySizeFactor(); for (int i=0; i0) { JKQTMathTextSymbolNode* symbN=dynamic_cast(nodes[i-1]); if (symbN) { prevNodeSize=symbN->getSymbolSize(painter, currentEv); } else { prevNodeSize=nodes[i-1]->getSize(painter, currentEv); } const double prevAscent=prevNodeSize.baselineHeight; const double prevDescent=prevNodeSize.overallHeight-prevNodeSize.baselineHeight; const bool shouldUseSpecialSubscriptMode=prevAscent>=subsuperSpecialModeAscent; const bool shouldUseSpecialSuperscriptMode=prevDescent>=subsuperSpecialModeDecent; if (shouldUseSpecialSubscriptMode) prevNodeSizePtrForSubscript=&prevNodeSize; if (shouldUseSpecialSuperscriptMode) prevNodeSizePtrForSuperscript=&prevNodeSize; } JKQTMathTextSuperscriptNode* nodeI_SuperScript=dynamic_cast(nodes[i]); JKQTMathTextSubscriptNode* nodeI_SubScript=dynamic_cast(nodes[i]); // if we find a subscript/superscript node we check whether the next node is super/subscript // if so, we typeset them at the same x-psotion, so sub/superscripts appear correctly if (nodeI_SuperScript) { if (i+1(nodes[i+1]); if (nodeISucc_SubScript) { // is this subscript? //painter.setPen(QPen("red")); //painter.drawEllipse(xnew-4,ynew+shift-(ccOverallHeight-ccBaselineHeight)-4,8,8); const double xnewidth=nodeI_SuperScript->drawWithSpecialPlacement(painter, xnew, ynew, currentEv, prevNodeSizePtrForSuperscript); i++; //painter.setPen(QPen("magenta")); //painter.drawEllipse(xnew-4,ynew-4,8,8); const double xnew2=nodeISucc_SubScript->drawWithSpecialPlacement(painter, xnew-prevNodeSize.subSuperXCorrection, ynew, currentEv, prevNodeSizePtrForSubscript); //i++; xnew=qMax(xnewidth, xnew2); doDraw=false; } } } else if (nodeI_SubScript) { if (i+1(nodes[i+1]); if (nodeISucc_SuperScript) { // is this subscript? //painter.setPen(QPen("red")); //painter.drawEllipse(xnew-4,ynew+shift-(ccOverallHeight-ccBaselineHeight)-4,8,8); double xnewidth=nodeI_SubScript->drawWithSpecialPlacement(painter, xnew-prevNodeSize.subSuperXCorrection, ynew, currentEv, prevNodeSizePtrForSubscript); i++; //painter.setPen(QPen("magenta")); //painter.drawEllipse(xnew-4,ynew-4,8,8); double xnew2=nodeISucc_SuperScript->drawWithSpecialPlacement(painter, xnew, ynew, currentEv, prevNodeSizePtrForSuperscript); //i++; xnew=qMax(xnewidth, xnew2); doDraw=false; } } else { xnew=nodeI_SubScript->drawWithSpecialPlacement(painter, xnew-prevNodeSize.subBesidesXCorrection, ynew, currentEv, prevNodeSizePtrForSubscript); doDraw=false; } } else { if (nodes[i]->isSubSuperscriptAboveBelowNode()) { JKQTMathTextSubscriptNode* subn=nullptr; JKQTMathTextSuperscriptNode* supn=nullptr; JKQTMathTextSubscriptNode* subn2=nullptr; JKQTMathTextSuperscriptNode* supn2=nullptr; if (i+1(nodes[i+1]); supn=dynamic_cast(nodes[i+1]); } if (i+2(nodes[i+2]); supn2=dynamic_cast(nodes[i+2]); } //std::cout<<"symbol ='"<getOperatorsubsuperSizeFactor(); JKQTMathTextSymbolNode::NodeSize nodeSize; JKQTMathTextSymbolNode* symbN=dynamic_cast(nodes[i]); if (symbN) { nodeSize=symbN->getSymbolSize(painter, currentEv); } else { nodeSize=nodes[i]->getSize(painter, currentEv); } const JKQTMathTextNodeSize sub=subn->getChild()->getSize(painter, ev); const JKQTMathTextNodeSize sup=supn->getChild()->getSize(painter, ev); const double descent1=nodeSize.getDescent(); //double d2=oh2-bh2; const double descent3=sup.getDescent(); const double neww=qMax(qMax(nodeSize.width, sub.width), sup.width)+subsuperextrawidth; //double xnewidth= const double xn1=nodes[i]->draw(painter, xnew+(neww-nodeSize.width)/2.0, ynew, currentEv); i++; //double xnew2= const double xnsub=subn->getChild()->draw(painter, xnew+(neww-sub.width)/2.0-nodeSize.subSuperXCorrection, ynew+sub.baselineHeight+descent1+subsupershift, ev); i++; //double xnew3= const double xnsup=supn->getChild()->draw(painter, xnew+(neww-sup.width)/2.0+nodeSize.subSuperXCorrection, ynew-nodeSize.baselineHeight-descent3-subsupershift, ev); doDraw=false; xnew=qMax(qMax(xn1, xnsub), xnsup)+subsuperextrawidth/2.0; } else if (subn) { // is this subscript and no following superscript? JKQTMathTextEnvironment ev=currentEv; ev.fontSize=ev.fontSize*parentMathText->getOperatorsubsuperSizeFactor(); JKQTMathTextSymbolNode::NodeSize nodeSize; JKQTMathTextSymbolNode* symbN=dynamic_cast(nodes[i]); if (symbN) { nodeSize=symbN->getSymbolSize(painter, currentEv); } else { nodeSize=nodes[i]->getSize(painter, currentEv); } const JKQTMathTextNodeSize sub=subn->getChild()->getSize(painter, ev); const double descent1=nodeSize.getDescent(); //double d2=oh2-bh2; const double neww=qMax(nodeSize.width, sub.width)+subsuperextrawidth; //double xnewidth= const double xn1=nodes[i]->draw(painter, xnew+(neww-nodeSize.width)/2.0, ynew, currentEv); i++; //double xnew2= const double xnsub=subn->getChild()->draw(painter, xnew+(neww-sub.width)/2.0-nodeSize.subSuperXCorrection, ynew+sub.baselineHeight+descent1+subsupershift, ev)+subsupershift; doDraw=false; //xnew+=w; xnew=qMax(xnsub, xn1)+subsuperextrawidth/2.0; } else if (supn) { // is this superscript and no following subscript? JKQTMathTextEnvironment ev=currentEv; ev.fontSize=ev.fontSize*parentMathText->getOperatorsubsuperSizeFactor(); JKQTMathTextSymbolNode::NodeSize nodeSize; JKQTMathTextSymbolNode* symbN=dynamic_cast(nodes[i]); if (symbN) { nodeSize=symbN->getSymbolSize(painter, currentEv); } else { nodeSize=nodes[i]->getSize(painter, currentEv); } const JKQTMathTextNodeSize sup=supn->getChild()->getSize(painter, ev); //double d1=ns.overallHeight-ns.baselineHeight; //double d2=oh2-bh2; const double descent3=sup.getDescent(); const double neww=qMax(nodeSize.width, sup.width)+subsuperextrawidth; //double xnewidth= const double xn1=nodes[i]->draw(painter, xnew+(neww-nodeSize.width)/2.0, ynew, currentEv); i++; //double xnew3= const double xnsup=supn->getChild()->draw(painter, xnew+(neww-sup.width)/2.0+nodeSize.subSuperXCorrection, ynew-nodeSize.baselineHeight-descent3-subsupershift, ev); doDraw=false; xnew=qMax(xn1, xnsup)+subsuperextrawidth/2.0; } } } if (idrawWithSpecialPlacement(painter, xnew, ynew, currentEv, prevNodeSizePtrForSuperscript); else if (nodeI_SubScript) xnew=nodeI_SubScript->drawWithSpecialPlacement(painter, xnew-prevNodeSize.subBesidesXCorrection, ynew, currentEv, prevNodeSizePtrForSubscript); else xnew=nodes[i]->draw(painter, xnew, ynew, currentEv); } } return xnew; } void JKQTMathTextHorizontalListNode::addChild(JKQTMathTextNode *n) { n->setParentNode(this); nodes.append(n); } bool JKQTMathTextHorizontalListNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const { bool ok=true; for (int i=0; itoHtml(h, currentEv, defaultEv); html=html+h; } return ok; } QList JKQTMathTextHorizontalListNode::getChildren() { return this->nodes; } int JKQTMathTextHorizontalListNode::childCount() const { return nodes.size(); } void JKQTMathTextHorizontalListNode::clearChildrenImpl(bool deleteChildren) { if (deleteChildren) { for (int i=0; i=0 && isetParentNode(this); return c; } JKQTMathTextVerticalListNode::JKQTMathTextVerticalListNode(JKQTMathText *_parent, JKQTMathTextHorizontalAlignment _alignment, double _linespacingFactor, JKQTMathTextLineSpacingMode spacingMode_, JKQTMathTextVerticalOrientation _verticalOrientation): JKQTMathTextMultiChildNode(_parent), alignment(_alignment), lineSpacingFactor(_linespacingFactor), verticalOrientation(_verticalOrientation), spacingMode(spacingMode_) { nodes.clear(); // these operations cause sub/sup script to be typeset over/under the operator, not right besides! } JKQTMathTextVerticalListNode::~JKQTMathTextVerticalListNode() { clearChildrenImpl(true); } QString JKQTMathTextVerticalListNode::getTypeName() const { return "MTVerticalListNode"; } JKQTMathTextNodeSize JKQTMathTextVerticalListNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv) const { return calcLayout(painter, currentEv); } JKQTMathTextVerticalListNode::LayoutInfo JKQTMathTextVerticalListNode::calcLayout(QPainter &painter, JKQTMathTextEnvironment currentEv) const { LayoutInfo l; const QFontMetricsF fm(currentEv.getFont(parentMathText)); 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 (nodes.size()<=0) { return l; } // 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; igetSize(painter, currentEv); if (i==0) { heightSum=loc.baselineHeight; } else if (i>0) { double deltaLine=0; if (spacingMode==MTSMMinimalSpacing) { deltaLine=descents.last()+lineLeading+loc.baselineHeight; } else if (spacingMode==MTSMDefaultSpacing) { deltaLine=qMax(linespacing, descents.last()+lineLeading+loc.baselineHeight); } heightSum=heightSum+deltaLine; y=y+deltaLine; } widths<draw(painter, x+l.X.at(i).x(), y+l.X.at(i).y(), currentEv); } return x+l.width; } void JKQTMathTextVerticalListNode::addChild(JKQTMathTextNode *n) { n->setParentNode(this); nodes.append(n); } bool JKQTMathTextVerticalListNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const { bool ok=true; if (alignment==MTHALeft) { html+="
"; } else if (alignment==MTHACentered) { html+="
"; } else if (alignment==MTHARight) { html+="
"; } for (int i=0; itoHtml(h, currentEv, defaultEv); if (i==0) html=html+h; else html=html+"
"+h; } html+="
"; return ok; } QList JKQTMathTextVerticalListNode::getChildren() { return this->nodes; } int JKQTMathTextVerticalListNode::childCount() const { return nodes.size(); } void JKQTMathTextVerticalListNode::clearChildrenImpl(bool deleteChildren) { if (deleteChildren) { for (int i=0; i=0 && isetParentNode(this); return c; } JKQTMathTextHorizontalAlignment JKQTMathTextVerticalListNode::getAlignment() const { return alignment; } JKQTMathTextVerticalOrientation JKQTMathTextVerticalListNode::getVerticalOrientation() const { return verticalOrientation; } double JKQTMathTextVerticalListNode::getLineSpacingFactor() const { return lineSpacingFactor; } JKQTMathTextLineSpacingMode JKQTMathTextVerticalListNode::getSpacingMode() const { return spacingMode; } void JKQTMathTextVerticalListNode::setAlignment(JKQTMathTextHorizontalAlignment value) { alignment=value; } void JKQTMathTextVerticalListNode::setVerticalOrientation(JKQTMathTextVerticalOrientation value) { verticalOrientation=value; } void JKQTMathTextVerticalListNode::setLineSpacingFactor(double value) { lineSpacingFactor=value; } void JKQTMathTextVerticalListNode::setSpacingMode(JKQTMathTextLineSpacingMode value) { spacingMode=value; } JKQTMathTextVerticalListNode::LayoutInfo::LayoutInfo(): JKQTMathTextNodeSize(), X() {} JKQTMathTextVerticalListNode::LayoutInfo::LayoutInfo(const JKQTMathTextNodeSize &other): JKQTMathTextNodeSize(other), X() { } JKQTMathTextVerticalListNode::LayoutInfo &JKQTMathTextVerticalListNode::LayoutInfo::operator=(const JKQTMathTextNodeSize &other) { JKQTMathTextNodeSize::operator=(other); X.clear(); return *this; } JKQTMathTextVerticalListNode::LayoutInfo &JKQTMathTextVerticalListNode::LayoutInfo::operator=(const LayoutInfo &other) { JKQTMathTextNodeSize::operator=(dynamic_cast(other)); X=other.X; return *this; } JKQTMathTextVerticalListNode::LayoutInfo::LayoutInfo(const LayoutInfo &other): JKQTMathTextNodeSize(dynamic_cast(other)), X(other.X) { }