2022-06-08 21:38:26 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2008-2022 Jan W. Krieger (<jan@jkrieger.de>)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "jkqtmathtext/nodes/jkqtmathtextsqrtnode.h"
|
|
|
|
#include "jkqtmathtext/jkqtmathtexttools.h"
|
|
|
|
#include "jkqtmathtext/jkqtmathtext.h"
|
|
|
|
#include "jkqtcommon/jkqtpcodestructuring.h"
|
|
|
|
#include "jkqtcommon/jkqtpstringtools.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <QFontMetricsF>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QFontInfo>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-29 03:59:50 +08:00
|
|
|
JKQTMathTextSqrtNode::JKQTMathTextSqrtNode(JKQTMathText* _parent, JKQTMathTextNode* child__, JKQTMathTextNode *childDegree__):
|
|
|
|
JKQTMathTextMultiChildNode(_parent),
|
|
|
|
child(child__), childDegree(childDegree__)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
2022-06-29 03:59:50 +08:00
|
|
|
if (child) child->setParentNode(this);
|
|
|
|
if (childDegree) childDegree->setParentNode(this);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextSqrtNode::~JKQTMathTextSqrtNode() {
|
2022-07-22 04:17:37 +08:00
|
|
|
if (child) { delete child; }
|
|
|
|
child=nullptr;
|
|
|
|
if (childDegree) { delete childDegree; }
|
|
|
|
childDegree=nullptr;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextSqrtNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos, const JKQTMathTextNodeSize* /*prevNodeSize*/) {
|
2022-06-28 18:00:20 +08:00
|
|
|
const QFontMetricsF fm(currentEv.getFont(parentMathText), painter.device());
|
2022-06-29 03:59:50 +08:00
|
|
|
JKQTMathTextEnvironment evSmall=currentEv;
|
|
|
|
evSmall.fontSize=currentEv.fontSize*parentMathText->getSqrtSmallFontFactor();
|
|
|
|
evSmall.italic=false;
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-20 04:36:38 +08:00
|
|
|
getChild()->getSize(painter, currentEv, width, baselineHeight, overallHeight, strikeoutPos);
|
2022-06-28 18:00:20 +08:00
|
|
|
const double descent=overallHeight-baselineHeight;
|
|
|
|
const double sqrtwidth=fm.boundingRect("X").width()*parentMathText->getSqrtWidthXFactor();
|
|
|
|
const double newAscent=qMax(baselineHeight*parentMathText->getSqrtHeightFactor(), fm.ascent());
|
|
|
|
const double newDescent=qMax(descent*parentMathText->getSqrtHeightFactor(), fm.descent());
|
|
|
|
|
|
|
|
overallHeight=newAscent+newDescent;;
|
|
|
|
baselineHeight=newAscent;
|
|
|
|
width=width+sqrtwidth;
|
2022-06-29 03:59:50 +08:00
|
|
|
if (childDegree) {
|
|
|
|
double degwidth=0, degBH=0, degOH=0, degSP=0;
|
|
|
|
childDegree->getSize(painter, evSmall, degwidth, degBH, degOH, degSP);
|
2022-06-28 18:00:20 +08:00
|
|
|
const double smalltextIndent=0.6*sqrtwidth;
|
|
|
|
if (degwidth>smalltextIndent) width=width+(degwidth-smalltextIndent);
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTMathTextSqrtNode::draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* /*prevNodeSize*/) {
|
|
|
|
doDrawBoxes(painter, x, y, currentEv);
|
2022-06-28 18:00:20 +08:00
|
|
|
|
|
|
|
const QFont f=currentEv.getFont(parentMathText);
|
|
|
|
const QFontMetricsF fm(f, painter.device());
|
2022-06-29 03:59:50 +08:00
|
|
|
JKQTMathTextEnvironment evSmall=currentEv;
|
|
|
|
evSmall.fontSize=currentEv.fontSize*parentMathText->getSqrtSmallFontFactor();
|
|
|
|
evSmall.italic=false;
|
2022-06-28 18:00:20 +08:00
|
|
|
|
|
|
|
double width=0, baselineHeight=0, overallHeight=0, strikeoutPos=0;
|
|
|
|
getChild()->getSize(painter, currentEv, width, baselineHeight, overallHeight, strikeoutPos);
|
|
|
|
const double descent=overallHeight-baselineHeight;
|
|
|
|
const double sqrtwidth=fm.boundingRect("X").width()*parentMathText->getSqrtWidthXFactor();
|
|
|
|
const double newAscent=qMax(baselineHeight*parentMathText->getSqrtHeightFactor(), fm.ascent());
|
|
|
|
const double newDescent=qMax(descent*parentMathText->getSqrtHeightFactor(), fm.descent());
|
|
|
|
const double linewidth=fm.lineWidth();
|
|
|
|
const double tinyhookSize=sqrtwidth*0.1;
|
|
|
|
const double smalltextIndent=0.6*sqrtwidth;
|
2022-06-29 03:59:50 +08:00
|
|
|
|
|
|
|
double degwidth=0, degBH=0, degOH=0, degSP=0;
|
|
|
|
if (childDegree) childDegree->getSize(painter, evSmall, degwidth, degBH, degOH, degSP);
|
|
|
|
const double degheight=degOH;
|
2022-06-28 18:00:20 +08:00
|
|
|
const double degree_overwidth=(degwidth>smalltextIndent)?(degwidth-smalltextIndent):0.0;
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
//painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();});
|
|
|
|
QPen p=painter.pen();
|
|
|
|
p.setColor(currentEv.color);
|
2022-06-28 18:00:20 +08:00
|
|
|
p.setWidthF(linewidth);
|
|
|
|
p.setCapStyle(Qt::RoundCap);
|
|
|
|
p.setJoinStyle(Qt::RoundJoin);
|
2022-06-08 21:38:26 +08:00
|
|
|
//painter.setPen(p);
|
2022-06-28 18:00:20 +08:00
|
|
|
|
|
|
|
double xnew=getChild()->draw(painter, x+sqrtwidth+degree_overwidth, y, currentEv);
|
|
|
|
|
|
|
|
const bool useAltForm=overallHeight>4.0*sqrtwidth;
|
|
|
|
const double y_tinyhooktop=y-strikeoutPos;
|
|
|
|
const double y_bottom=y+newDescent-linewidth/2.0;
|
|
|
|
const double y_top=y-newAscent+linewidth/2.0;
|
|
|
|
const double x_start=x+degree_overwidth+linewidth/2.0;
|
|
|
|
const double x_tinyhooktop=x_start+tinyhookSize;
|
|
|
|
const double x_hookbottom=(!useAltForm)?(x_start+0.33*sqrtwidth):(x_start+0.66*sqrtwidth);
|
|
|
|
const double x_hooktop=(!useAltForm)?(x_start+sqrtwidth):x_hookbottom;
|
|
|
|
const double x_smalltextend=x_start+smalltextIndent;
|
2022-06-29 03:59:50 +08:00
|
|
|
const double y_smalltext=y_top+degBH+(fabs(y_top-(y_tinyhooktop-linewidth))-degheight)/2.0;
|
2022-06-28 18:00:20 +08:00
|
|
|
if (sqrtwidth>0) {
|
|
|
|
QPainterPath path;
|
|
|
|
path.moveTo(x_start, y_tinyhooktop+tinyhookSize);
|
|
|
|
path.lineTo(x_tinyhooktop, y_tinyhooktop);
|
|
|
|
path.lineTo(x_hookbottom, y_bottom);
|
|
|
|
path.lineTo(x_hooktop, y_top);
|
|
|
|
path.lineTo(xnew-linewidth/2.0, y_top);
|
|
|
|
path.moveTo(x_tinyhooktop,y_tinyhooktop);
|
|
|
|
path.lineTo(x_tinyhooktop+linewidth*0.8, y_tinyhooktop-linewidth*0.8);
|
|
|
|
path.lineTo(x_hookbottom, y_bottom-2.0*linewidth);
|
|
|
|
painter.setPen(p);
|
|
|
|
painter.drawPath(path);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
2022-06-29 03:59:50 +08:00
|
|
|
if (childDegree) {
|
|
|
|
childDegree->draw(painter, x_smalltextend-degwidth, y_smalltext, evSmall);
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-28 18:00:20 +08:00
|
|
|
return xnew;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTMathTextSqrtNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) {
|
2022-06-29 03:59:50 +08:00
|
|
|
bool ok=true;
|
|
|
|
if (childDegree) {
|
|
|
|
JKQTMathTextEnvironment evSmall=currentEv;
|
|
|
|
evSmall.fontSize=currentEv.fontSize*parentMathText->getSqrtSmallFontFactor();
|
|
|
|
evSmall.italic=false;
|
|
|
|
html=html+"<sup>";
|
|
|
|
ok=ok&&childDegree->toHtml(html, evSmall, defaultEv);
|
|
|
|
html=html+"</sup>";
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
html=html+"√<span style=\"text-decoration:overline\">";
|
2022-06-29 03:59:50 +08:00
|
|
|
ok=ok&&getChild()->toHtml(html, currentEv, defaultEv);
|
2022-06-08 21:38:26 +08:00
|
|
|
html=html+" </span>";
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextSqrtNode::getTypeName() const
|
|
|
|
{
|
|
|
|
return "MTsqrtNode";
|
|
|
|
}
|
|
|
|
|
2022-06-29 03:59:50 +08:00
|
|
|
JKQTMathTextNode *JKQTMathTextSqrtNode::getChild()
|
|
|
|
{
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JKQTMathTextNode *JKQTMathTextSqrtNode::getChild() const
|
|
|
|
{
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextNode *JKQTMathTextSqrtNode::getChildDegree()
|
|
|
|
{
|
|
|
|
return childDegree;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JKQTMathTextNode *JKQTMathTextSqrtNode::getChildDegree() const
|
|
|
|
{
|
|
|
|
return childDegree;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextNode *JKQTMathTextSqrtNode::getChild(int i)
|
|
|
|
{
|
|
|
|
if (i==0) return child;
|
|
|
|
if (i==1) return childDegree;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JKQTMathTextNode *JKQTMathTextSqrtNode::getChild(int i) const
|
|
|
|
{
|
|
|
|
if (i==0) return child;
|
|
|
|
if (i==1) return childDegree;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextNode *JKQTMathTextSqrtNode::replaceChild(int i, JKQTMathTextNode *newChild)
|
|
|
|
{
|
|
|
|
if (i==0) {
|
|
|
|
JKQTMathTextNode* c=child;
|
|
|
|
child=newChild;
|
|
|
|
if (child) child->setParentNode(this);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
if (i==1) {
|
|
|
|
JKQTMathTextNode* c=childDegree;
|
|
|
|
childDegree=newChild;
|
|
|
|
if (childDegree) childDegree->setParentNode(this);
|
|
|
|
return c;
|
|
|
|
}
|
2022-07-07 03:00:24 +08:00
|
|
|
return nullptr;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-29 03:59:50 +08:00
|
|
|
int JKQTMathTextSqrtNode::childCount() const
|
|
|
|
{
|
|
|
|
if (childDegree) return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextSqrtNode::clearChildren(bool deleteChildren)
|
|
|
|
{
|
|
|
|
if (child) {
|
|
|
|
if (deleteChildren) delete child;
|
|
|
|
child=nullptr;
|
|
|
|
}
|
|
|
|
if (childDegree) {
|
|
|
|
if (deleteChildren) delete childDegree;
|
|
|
|
childDegree=nullptr;
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 21:38:26 +08:00
|
|
|
|