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/jkqtmathtextsubsupernode.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>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTMathTextSuperscriptNode::JKQTMathTextSuperscriptNode(JKQTMathText* _parent, JKQTMathTextNode* child):
|
2022-06-09 05:52:22 +08:00
|
|
|
JKQTMathTextSingleChildNode(child,_parent)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextSuperscriptNode::~JKQTMathTextSuperscriptNode() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextSuperscriptNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos, const JKQTMathTextNodeSize* prevNodeSize) {
|
|
|
|
JKQTMathTextEnvironment ev=currentEv;
|
2022-06-09 05:52:22 +08:00
|
|
|
ev.fontSize=ev.fontSize*parentMathText->getSubsuperSizeFactor();
|
2022-06-09 20:30:15 +08:00
|
|
|
const QFontMetricsF fm(currentEv.getFont(parentMathText), painter.device());
|
2022-06-26 06:28:49 +08:00
|
|
|
const QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
|
|
|
double cStrikeoutPos=0, cWidth=0, cBaselineHeight=0, cOverallHeight=0;
|
|
|
|
getChild()->getSize(painter, ev, cWidth, cBaselineHeight, cOverallHeight, cStrikeoutPos);
|
|
|
|
const double childDescent=cOverallHeight-cBaselineHeight;
|
|
|
|
double shiftToChildBottom=parentMathText->getSuperShiftFactor()*tbr_of_letterM.height();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
if (prevNodeSize!=nullptr) {
|
|
|
|
const double modifiedShift=prevNodeSize->baselineHeight-childDescent-parentMathText->getSpecialSuperShiftFactor()*cBaselineHeight-childDescent;
|
|
|
|
if (modifiedShift>shiftToChildBottom) shiftToChildBottom=modifiedShift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
baselineHeight=overallHeight=cOverallHeight+shiftToChildBottom;
|
|
|
|
width=cWidth;
|
2022-06-09 20:30:15 +08:00
|
|
|
if (prevNodeSize!=nullptr) strikeoutPos=prevNodeSize->strikeoutPos;
|
|
|
|
else strikeoutPos=fm.strikeOutPos();
|
2022-06-09 05:52:22 +08:00
|
|
|
if (currentEv.italic && prevNodeSize==nullptr) width=width+double(fm.boundingRect(' ').width())*parentMathText->getItalicCorrectionFactor();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTMathTextSuperscriptNode::draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSize) {
|
|
|
|
doDrawBoxes(painter, x, y, currentEv);
|
|
|
|
JKQTMathTextEnvironment ev=currentEv;
|
2022-06-09 05:52:22 +08:00
|
|
|
ev.fontSize=ev.fontSize*parentMathText->getSubsuperSizeFactor();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
double cWidth, cBaselineHeight, cOverallHeight, cStrikeoutPos;
|
2022-06-20 04:36:38 +08:00
|
|
|
getChild()->getSize(painter, ev, cWidth, cBaselineHeight, cOverallHeight, cStrikeoutPos);
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
const QFontMetricsF fm(currentEv.getFont(parentMathText), painter.device());
|
|
|
|
QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
|
|
|
const double childDescent=cOverallHeight-cBaselineHeight;
|
|
|
|
double shiftToChildBottom=parentMathText->getSuperShiftFactor()*tbr_of_letterM.height();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
if (prevNodeSize!=nullptr) {
|
|
|
|
const double modifiedShift=prevNodeSize->baselineHeight-childDescent-parentMathText->getSpecialSuperShiftFactor()*cBaselineHeight-childDescent;
|
|
|
|
if (modifiedShift>shiftToChildBottom) shiftToChildBottom=modifiedShift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double xx=x;
|
2022-06-09 05:52:22 +08:00
|
|
|
if (currentEv.italic && prevNodeSize==nullptr) xx=xx+double(fm.boundingRect(' ').width())*parentMathText->getItalicCorrectionFactor();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
return getChild()->draw(painter, xx, y-(shiftToChildBottom+childDescent), ev);//+0.5*fm.boundingRect("A").width();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString JKQTMathTextSuperscriptNode::getTypeName() const
|
|
|
|
{
|
|
|
|
return "MTsuperscriptNode";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool JKQTMathTextSuperscriptNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv)
|
|
|
|
{
|
|
|
|
html=html+"<sup>";
|
2022-06-20 04:36:38 +08:00
|
|
|
bool ok=getChild()->toHtml(html, currentEv, defaultEv);
|
2022-06-08 21:38:26 +08:00
|
|
|
html=html+"</sup>";
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JKQTMathTextSubscriptNode::JKQTMathTextSubscriptNode(JKQTMathText* _parent, JKQTMathTextNode* child):
|
2022-06-09 05:52:22 +08:00
|
|
|
JKQTMathTextSingleChildNode(child, _parent)
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JKQTMathTextSubscriptNode::~JKQTMathTextSubscriptNode() {
|
2022-06-09 05:52:22 +08:00
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void JKQTMathTextSubscriptNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos, const JKQTMathTextNodeSize* prevNodeSize) {
|
|
|
|
JKQTMathTextEnvironment ev=currentEv;
|
2022-06-09 05:52:22 +08:00
|
|
|
ev.fontSize=ev.fontSize*parentMathText->getSubsuperSizeFactor();
|
2022-06-26 06:28:49 +08:00
|
|
|
const QFontMetricsF fm(ev.getFont(parentMathText), painter.device());
|
|
|
|
const QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
double cWidth=0, cBaselineHeight=0, cOverallHeight=0, cStrikeoutPos=0;
|
|
|
|
getChild()->getSize(painter, ev, cWidth, cBaselineHeight, cOverallHeight, cStrikeoutPos);
|
2022-07-22 04:17:37 +08:00
|
|
|
//const double childDescent=cOverallHeight-cBaselineHeight;
|
2022-06-26 06:28:49 +08:00
|
|
|
double shift_to_childBaseline=cBaselineHeight-parentMathText->getSubShiftFactor()*tbr_of_letterM.height();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
if (prevNodeSize!=nullptr) {
|
|
|
|
//qDebug()<<"oldshift="<<shift<<", prevNodeSize->overallHeight="<<prevNodeSize->overallHeight<<", prevNodeSize->baselineHeight="<<prevNodeSize->baselineHeight;
|
|
|
|
const double parentDescent=prevNodeSize->overallHeight-prevNodeSize->baselineHeight;
|
|
|
|
const double newShift=parentDescent+parentMathText->getSpecialSubShiftFactor()*cBaselineHeight;
|
|
|
|
if (newShift>shift_to_childBaseline) shift_to_childBaseline=newShift;
|
|
|
|
//qDebug()<<"newshift="<<shift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
baselineHeight=cBaselineHeight-shift_to_childBaseline;
|
|
|
|
overallHeight=cOverallHeight;
|
2022-06-09 20:30:15 +08:00
|
|
|
if (prevNodeSize!=nullptr) strikeoutPos=prevNodeSize->strikeoutPos;
|
|
|
|
else strikeoutPos=fm.strikeOutPos();
|
2022-06-26 06:28:49 +08:00
|
|
|
width=cWidth;
|
2022-06-09 05:52:22 +08:00
|
|
|
if (currentEv.italic && prevNodeSize==nullptr) width=width-double(fm.boundingRect(' ').width())*parentMathText->getItalicCorrectionFactor();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double JKQTMathTextSubscriptNode::draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSize) {
|
|
|
|
doDrawBoxes(painter, x, y, currentEv);
|
|
|
|
JKQTMathTextEnvironment ev=currentEv;
|
2022-06-09 05:52:22 +08:00
|
|
|
ev.fontSize=ev.fontSize*parentMathText->getSubsuperSizeFactor();
|
2022-06-26 06:28:49 +08:00
|
|
|
const QFontMetricsF fm(ev.getFont(parentMathText), painter.device());
|
|
|
|
const QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
double cWidth=0, cBaselineHeight=0, cOverallHeight=0, cStrikeoutPos=0;
|
|
|
|
getChild()->getSize(painter, ev, cWidth, cBaselineHeight, cOverallHeight, cStrikeoutPos);
|
|
|
|
double shift_to_childBaseline=cBaselineHeight-parentMathText->getSubShiftFactor()*tbr_of_letterM.height();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
if (prevNodeSize!=nullptr) {
|
2022-06-08 21:38:26 +08:00
|
|
|
//qDebug()<<"oldshift="<<shift<<", prevNodeSize->overallHeight="<<prevNodeSize->overallHeight<<", prevNodeSize->baselineHeight="<<prevNodeSize->baselineHeight;
|
2022-06-26 06:28:49 +08:00
|
|
|
const double parentDescent=prevNodeSize->overallHeight-prevNodeSize->baselineHeight;
|
|
|
|
const double newShift=parentDescent+parentMathText->getSpecialSubShiftFactor()*cBaselineHeight;
|
|
|
|
if (newShift>shift_to_childBaseline) shift_to_childBaseline=newShift;
|
2022-06-08 21:38:26 +08:00
|
|
|
//qDebug()<<"newshift="<<shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
//qDebug()<<"baselineHeight="<<baselineHeight<<", overallHeight="<<overallHeight<<", strikeoutPos="<<strikeoutPos;
|
|
|
|
//qDebug()<<"shift="<<shift<<", yshift="<<yshift;
|
|
|
|
double xx=x;
|
2022-06-09 05:52:22 +08:00
|
|
|
if (currentEv.italic && prevNodeSize==nullptr) xx=xx-double(fm.boundingRect(' ').width())*parentMathText->getItalicCorrectionFactor();
|
2022-06-26 06:28:49 +08:00
|
|
|
return getChild()->draw(painter, xx, y+shift_to_childBaseline, ev);//+0.5*fm.boundingRect("A").width();
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString JKQTMathTextSubscriptNode::getTypeName() const
|
|
|
|
{
|
|
|
|
return "MTsubscriptNode";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JKQTMathTextSubscriptNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) {
|
|
|
|
html=html+"<sub>";
|
2022-06-20 04:36:38 +08:00
|
|
|
bool ok=getChild()->toHtml(html, currentEv, defaultEv);
|
2022-06-08 21:38:26 +08:00
|
|
|
html=html+"</sub>";
|
|
|
|
return ok;
|
|
|
|
}
|