/* 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/jkqtmathtexthorizontallistnode.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 "jkqtcommon/jkqtpdebuggingtools.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 ev) const { JKQTMathTextEnvironment currentEv=ev; JKQTMathTextNodeSize outSize; //QRectF tbr=parent->getTightBoundingRect(currentEv.getFont(parent), "M", painter.device()); double xnew=0; //bool wasBrace=false; for (int i=0; igetOperatorsubsuperDistanceFactor(); const double subsuperextrawidth=tbr_x.width()*parentMathText->getOperatorsubsuperExtraSpaceFactor(); const double subsuperSpecialModeAscent=JKQTMathTextGetFontAscent(f, painter.device())*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double subsuperSpecialModeDecent=JKQTMathTextGetFontDescent(f, painter.device())*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double spaceWidth=JKQTMathTextGetBoundingRect(f, " ", painter.device()).width(); JKQTMathTextSymbolNode::NodeSize prevNodeSize; JKQTMathTextNodeSize* prevNodeSizePtrForSubscript=nullptr; JKQTMathTextNodeSize* prevNodeSizePtrForSuperscript=nullptr; if (i>0) { 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; } const double subscript_xcorrection=prevNodeSize.baselineXCorrection+JKQTMathTextGetFontLineWidth(f, painter.device())*0.5; 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=tbr_x.width()*parentMathText->getOperatorsubsuperExtraSpaceFactor(); const double subsuperSpecialModeAscent=JKQTMathTextGetFontAscent(f, painter.device())*parentMathText->getSubsuperModeSelectionBySizeFactor(); const double subsuperSpecialModeDecent=JKQTMathTextGetFontDescent(f, painter.device())*parentMathText->getSubsuperModeSelectionBySizeFactor(); JKQTMathTextSymbolNode::NodeSize prevNodeSize; JKQTMathTextNodeSize* prevNodeSizePtrForSubscript=nullptr; JKQTMathTextNodeSize* prevNodeSizePtrForSuperscript=nullptr; if (i>0) { 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; } const double subscript_xcorrection=prevNodeSize.baselineXCorrection+JKQTMathTextGetFontLineWidth(f, painter.device())*0.5; 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+subscript_xcorrection, 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+subscript_xcorrection, 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+subscript_xcorrection, 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.subSuperAboveBelowXCorrection, ynew+sub.baselineHeight+descent1+subsupershift, ev); i++; //double xnew3= const double xnsup=supn->getChild()->draw(painter, xnew+(neww-sup.width)/2.0+nodeSize.subSuperAboveBelowXCorrection, 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.subSuperAboveBelowXCorrection, 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.subSuperAboveBelowXCorrection, 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+subscript_xcorrection, ynew, currentEv, prevNodeSizePtrForSubscript); else xnew=nodes[i]->draw(painter, xnew, ynew, currentEv); JKQTMathTextModifyEnvironmentNodeMixIn* modEnvNode=dynamic_cast(nodes[i]); if (modEnvNode) { modEnvNode->modifyEnvironment(currentEv); } } } return xnew; } void JKQTMathTextHorizontalListNode::addChild(JKQTMathTextNode *n) { n->setParentNode(this); nodes.append(n); } bool JKQTMathTextHorizontalListNode::toHtml(QString &html, JKQTMathTextEnvironment ev, JKQTMathTextEnvironment defaultEv) const { JKQTMathTextEnvironment currentEv=ev; bool ok=true; for (int i=0; itoHtml(h, currentEv, defaultEv); html=html+h; JKQTMathTextModifyEnvironmentNodeMixIn* modEnvNode=dynamic_cast(nodes[i]); if (modEnvNode) { modEnvNode->modifyEnvironment(currentEv); } } 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; } void JKQTMathTextHorizontalListNode::modifyEnvironment(JKQTMathTextEnvironment ¤tEv) const { for (int i=0; i(nodes[i]); if (modEnvNode) { modEnvNode->modifyEnvironment(currentEv); } } }