/* 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/jkqtmathtextdecoratednode.h" #include "jkqtmathtext/jkqtmathtexttools.h" #include "jkqtmathtext/jkqtmathtext.h" #include "jkqtcommon/jkqtpcodestructuring.h" #include "jkqtcommon/jkqtpstringtools.h" #include #include #include #include #include #include #include QString JKQTMathTextDecoratedNode::DecorationType2String(JKQTMathTextDecoratedNode::DecorationType mode) { switch(mode) { case MTDvec: return "vec"; case MTDgrave: return "grave"; case MTDacute: return "acute"; case MTDtilde: return "tilde"; case MTDbreve: return "breve"; case MTDwidetilde: return "widetilde"; case MTDhat: return "hat"; case MTDwidehat: return "widehat"; case MTDcheck: return "check"; case MTDwidecheck: return "widecheck"; case MTDocirc: return "ocirc"; case MTDdot: return "dot"; case MTDddot: return "ddot"; case MTDbar: return "bar"; case MTDarrow: return "arrow"; case MTDoverline: return "overline"; case MTDdoubleoverline: return "double overline"; case MTDunderline: return "underline"; case MTDdoubleunderline: return "double underline"; case MTDcancel: return "cancel"; case MTDbcancel: return "bcancel"; case MTDxcancel: return "xcancel"; case MTDstrike: return "strike"; case MTDoverleftarrow: return "overleftarrow"; case MTDoverrightarrow: return "overrightarrow"; case MTDoverleftrightarrow: return "overleftrightarrow"; case MTDunderleftarrow: return "underleftarrow"; case MTDunderrightarrow: return "underrightarrow"; case MTDunderleftrightarrow: return "underleftrightarrow"; case MTDunderlineDashed: return "underlineDashed"; case MTDunderlineDotted: return "MTDunderlineDotted"; } return "unknown"; } JKQTMathTextDecoratedNode::DecorationType JKQTMathTextDecoratedNode::InstructionName2DecorationType(const QString &mode) { fillInstructions(); return instructions[mode]; } bool JKQTMathTextDecoratedNode::supportsInstructionName(const QString &instructionName) { fillInstructions(); return instructions.contains(instructionName); } JKQTMathTextDecoratedNode::JKQTMathTextDecoratedNode(JKQTMathText* _parent, DecorationType decoration, JKQTMathTextNode* child): JKQTMathTextSingleChildNode(child, _parent) { this->decoration=decoration; } JKQTMathTextDecoratedNode::~JKQTMathTextDecoratedNode() { } void JKQTMathTextDecoratedNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos, const JKQTMathTextNodeSize* /*prevNodeSize*/) { JKQTMathTextEnvironment ev=currentEv; double cwidth=0, cbaselineHeight=0, coverallHeight=0, cstrikeoutPos=0; getChild()->getSize(painter, ev, cwidth, cbaselineHeight, coverallHeight, cstrikeoutPos); const double cDescent=coverallHeight-cbaselineHeight; const QFont font=ev.getFont(parentMathText); const QFontMetricsF fm(font, painter.device()); const double decoSeparation=parentMathText->getDecorationSeparationFactor()*fm.ascent(); const double deco_height=parentMathText->getDecorationHeightFactor()*fm.ascent(); const double deco_ypos=cbaselineHeight+decoSeparation; const double decoAboveAscent_ypos=fm.ascent()+decoSeparation; const double decobelow_ypos=cDescent+decoSeparation; const double italic_xcorrection=getNonItalicXCorretion(painter, cwidth, ev, getChild()); const double deco_miniwidth=((decoration==MTDtilde||decoration==MTDbreve)?fm.boundingRect("~").width():fm.boundingRect("^").width())-italic_xcorrection; const double linewidth=qMax(parentMathText->ABS_MIN_LINEWIDTH, fm.lineWidth()); double descent=coverallHeight-cbaselineHeight; double ascent=cbaselineHeight; if (decoration==MTDbar) { ascent=std::max(baselineHeight+decoSeparation, decoAboveAscent_ypos)+linewidth/2.0; } else if (decoration==MTDunderline || decoration==MTDunderlineDashed || decoration==MTDunderlineDotted) { descent=std::max(decobelow_ypos, cDescent)+linewidth/2.0; } else if (decoration==MTDdoubleunderline) { descent=std::max(decobelow_ypos, cDescent)+3.5*linewidth; } else if (decoration==MTDunderleftarrow || decoration==MTDunderrightarrow || decoration==MTDunderleftrightarrow) { descent=std::max(decobelow_ypos, cDescent)+0.5*linewidth+deco_height; } else { ascent=deco_ypos+deco_height; } overallHeight=ascent+descent; baselineHeight=ascent; strikeoutPos=cstrikeoutPos; width=std::max(deco_miniwidth,cwidth); } QHash JKQTMathTextDecoratedNode::instructions; void JKQTMathTextDecoratedNode::fillInstructions() { if (instructions.size()>0) return; instructions["vec"]=MTDvec; instructions["overline"]=MTDoverline; instructions["oline"]=MTDoverline; instructions["ol"]=MTDoverline; instructions["underline"]=MTDunderline; instructions["uline"]=MTDunderline; instructions["ul"]=MTDunderline; instructions["dashuline"]=MTDunderlineDashed; instructions["dotuline"]=MTDunderlineDotted; instructions["uuline"]=MTDdoubleunderline; instructions["uul"]=MTDdoubleunderline; instructions["ooline"]=MTDdoubleoverline; instructions["ool"]=MTDdoubleoverline; instructions["arrow"]=MTDarrow; instructions["overrightarrow"]=MTDarrow; instructions["overarrow"]=MTDarrow; instructions["hat"]=MTDhat; instructions["^"]=MTDhat; instructions["widehat"]=MTDwidehat; instructions["check"]=MTDcheck; instructions["v"]=MTDcheck; instructions["widecheck"]=MTDwidecheck; instructions["bar"]=MTDbar; instructions["="]=MTDbar; instructions["dot"]=MTDdot; instructions["."]=MTDdot; instructions["ocirc"]=MTDocirc; instructions["tilde"]=MTDtilde; instructions["~"]=MTDtilde; instructions["acute"]=MTDacute; instructions["'"]=MTDacute; instructions["grave"]=MTDgrave; instructions["`"]=MTDgrave; instructions["breve"]=MTDbreve; instructions["u"]=MTDbreve; instructions["widetilde"]=MTDwidetilde; instructions["ddot"]=MTDddot; instructions["cancel"]=MTDcancel; instructions["xcancel"]=MTDxcancel; instructions["bcancel"]=MTDbcancel; instructions["strike"]=MTDstrike; instructions["st"]=MTDstrike; instructions["sout"]=MTDstrike; instructions["overleftarrow"]=MTDoverleftarrow; instructions["overrightarrow"]=MTDoverrightarrow; instructions["overleftrightarrow"]=MTDoverleftrightarrow; instructions["underleftarrow"]=MTDunderleftarrow; instructions["underrightarrow"]=MTDunderrightarrow; instructions["underleftrightarrow"]=MTDunderleftrightarrow; } double JKQTMathTextDecoratedNode::draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* /*prevNodeSize*/) { doDrawBoxes(painter, x, y, currentEv); JKQTMathTextEnvironment ev=currentEv; double cwidth=0, cbaselineHeight=0, coverallHeight=0, cstrikeoutPos=0; getChild()->getSize(painter, ev, cwidth, cbaselineHeight, coverallHeight, cstrikeoutPos); const double cDescent=coverallHeight-cbaselineHeight; const QFont font=ev.getFont(parentMathText); const QFontMetricsF fm(font, painter.device()); const double width_X=fm.boundingRect("X").width(); const double width_x=fm.boundingRect("x").width(); const double width_dot=fm.boundingRect(".").width()/2.0; const double decoSeparation=parentMathText->getDecorationSeparationFactor()*fm.ascent(); const double linewidth=qMax(parentMathText->ABS_MIN_LINEWIDTH, fm.lineWidth()); const double linewidthArrow=linewidth*0.65; const double deco_height=parentMathText->getDecorationHeightFactor()*fm.ascent(); const double decoAboveAscent_ypos=y-fm.ascent()-decoSeparation; const double strike_ypos=y-cbaselineHeight/2.0; const double decobelow_ypos=y+cDescent+decoSeparation; const double italic_xcorrection=getNonItalicXCorretion(painter, cwidth, ev, getChild()); const double deco_xoffset=parentMathText->getDecorationWidthReductionXFactor()*width_X/2.0; const double deco_width=std::max(width_x*0.5,cwidth-2.0*deco_xoffset-italic_xcorrection); const double deco_vecwidth=width_x*0.18; const double deco_vecheight=deco_height*0.25; const double deco_accentwidth=deco_height/4.0; const double deco_miniwidth=((decoration==MTDtilde||decoration==MTDbreve)?fm.boundingRect("j").width():fm.boundingRect("^").width())-italic_xcorrection; const double decotop_xcenter=x+italic_xcorrection+(cwidth-italic_xcorrection)/2.0; const double decotop_xstart=decotop_xcenter-deco_width/2.0+linewidth/2.0; const double decotop_xend=decotop_xcenter+deco_width/2.0-linewidth/2.0; const double decobot_xstart=x+linewidth/2.0; const double decobot_xend=x+cwidth-italic_xcorrection-linewidth/2.0; //const double decobot_xcenter=(decobot_xstart+decobot_xend)/2.0; const double deco_ytopbot=y-cbaselineHeight-decoSeparation-linewidth/2.0; const double deco_ytoptop=y-cbaselineHeight-decoSeparation-deco_height+linewidth/2.0; const double deco_ytopcenter=y-cbaselineHeight-decoSeparation-deco_height/2.0; QPen pold=painter.pen(); QPen p=pold; p.setColor(ev.color); p.setWidthF(linewidthArrow); p.setCapStyle(Qt::SquareCap); p.setJoinStyle(Qt::RoundJoin); QPen pul=p; pul.setWidthF(linewidth); pul.setCapStyle(Qt::SquareCap); double xnew=getChild()->draw(painter, x, y, ev); auto fDrawFontAccent=[&](QChar aDirect=QChar(), QChar aFallback=QChar()) -> bool { if (!aDirect.isNull() && fm.inFont(aDirect)) { painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); const QRectF tbra=fm.tightBoundingRect(aDirect); painter.translate(decotop_xcenter-tbra.width()/2.0, (deco_ytopcenter+deco_ytoptop)/2.0); //painter.setPen("red"); //painter.drawEllipse(0-2,0-2,4,4); painter.translate(-tbra.x(), -tbra.y()); //painter.setPen("magenta"); //painter.drawEllipse(0-2,0-2,4,4); if (tbra.width()>0 && tbra.height()>0) { painter.setFont(font); painter.drawText(0,0,aDirect); //painter.setPen("blue"); //painter.drawRect(tbra); //painter.drawEllipse(0-2,0-2,4,4); return true; } } if (!aFallback.isNull() && fm.inFont(aFallback)) { painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); const QRectF tbra=fm.tightBoundingRect(aFallback); painter.translate(decotop_xcenter-tbra.width()/2.0, (deco_ytopcenter+deco_ytoptop)/2.0); //painter.setPen("yellow"); //painter.drawEllipse(0-2,0-2,4,4); painter.translate(-tbra.x(), -tbra.y()); //painter.setPen("cyan"); //painter.drawEllipse(0-2,0-2,4,4); if (tbra.width()>0 && tbra.height()>0) { painter.setFont(font); painter.drawText(0,0,aFallback); //painter.setPen("green"); //painter.drawRect(tbra); //painter.drawEllipse(0-2,0-2,4,4); return true; } } return false; }; if (decoration==MTDvec) { painter.setPen(p); QPolygonF poly; poly<0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDbar) { painter.setPen(pul); const QLineF l(decotop_xstart, decoAboveAscent_ypos, decotop_xend, decoAboveAscent_ypos); if (l.length()>0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDdoubleoverline) { painter.setPen(pul); const QLineF l(decotop_xstart, deco_ytopbot, decotop_xend, deco_ytopbot); if (l.length()>0) painter.drawLine(l); const QLineF l2(decotop_xstart, deco_ytopbot-2.5*pul.widthF(), decotop_xend, deco_ytopbot-2.5*pul.widthF()); if (l2.length()>0) painter.drawLine(l2); painter.setPen(pold); } else if (decoration==MTDunderline || decoration==MTDunderlineDashed || decoration==MTDunderlineDotted) { if (decoration==MTDunderlineDashed) pul.setStyle(Qt::DashLine); if (decoration==MTDunderlineDotted) pul.setStyle(Qt::DotLine); painter.setPen(pul); const QLineF l(decobot_xstart, decobelow_ypos, decobot_xend, decobelow_ypos); if (l.length()>0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDdoubleunderline) { painter.setPen(pul); const QLineF l(decobot_xstart, decobelow_ypos, decobot_xend, decobelow_ypos); if (l.length()>0) painter.drawLine(l); QLineF l2(decobot_xstart, decobelow_ypos+2.5*pul.widthF(), decobot_xend, decobelow_ypos+2.5*pul.widthF()); if (l2.length()>0) painter.drawLine(l2); painter.setPen(pold); } else if (decoration==MTDarrow) { painter.setPen(p); const QLineF l(decotop_xstart, deco_ytopcenter, decotop_xend, deco_ytopcenter); if (l.length()>0) painter.drawLine(l); QPolygonF poly; poly<0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDcancel) { painter.setPen(pul); const QLineF l(decobot_xstart, decobelow_ypos, decotop_xend, deco_ytopbot); if (l.length()>0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDbcancel) { painter.setPen(pul); const QLineF l(decobot_xstart, deco_ytopbot, decotop_xend, decobelow_ypos); if (l.length()>0) painter.drawLine(l); painter.setPen(pold); } else if (decoration==MTDxcancel) { painter.setPen(pul); const QLineF l(decobot_xstart, deco_ytopbot, decotop_xend, decobelow_ypos); if (l.length()>0) painter.drawLine(l); const QLineF l1(decobot_xstart, decobelow_ypos, decotop_xend, deco_ytopbot); if (l1.length()>0) painter.drawLine(l1); painter.setPen(pold); } return xnew; } bool JKQTMathTextDecoratedNode::toHtml(QString &/*html*/, JKQTMathTextEnvironment /*currentEv*/, JKQTMathTextEnvironment /*defaultEv*/) { //QString f; //JKQTMathTextEnvironment ev=currentEv; //bool ok=child->toHtml(html, ev, defaultEv); return false; } QString JKQTMathTextDecoratedNode::getTypeName() const { return "MTdecoratedNode"; } JKQTMathTextDecoratedNode::DecorationType JKQTMathTextDecoratedNode::getDecoration() const { return this->decoration; }