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() {
|
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
JKQTMathTextNodeSize JKQTMathTextSuperscriptNode::getSizeWithSpecialPlacement(QPainter& painter, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSizeForSpecialPlacement) const{
|
|
|
|
JKQTMathTextNodeSize s;
|
2022-06-08 21:38:26 +08:00
|
|
|
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());
|
2022-08-17 05:05:04 +08:00
|
|
|
const JKQTMathTextNodeSize cs=getChild()->getSize(painter, ev);
|
|
|
|
const double childDescent=cs.getDescent();
|
2022-08-12 22:50:26 +08:00
|
|
|
double shiftToChildBottom=parentMathText->getSuperShiftFactor()*fm.xHeight();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-14 22:59:03 +08:00
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) {
|
2022-08-17 05:05:04 +08:00
|
|
|
const double modifiedShift=prevNodeSizeForSpecialPlacement->baselineHeight-childDescent-parentMathText->getSpecialSuperShiftFactor()*cs.baselineHeight-childDescent;
|
2022-06-26 06:28:49 +08:00
|
|
|
if (modifiedShift>shiftToChildBottom) shiftToChildBottom=modifiedShift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
s.baselineHeight=s.overallHeight=cs.overallHeight+shiftToChildBottom;
|
|
|
|
s.width=cs.width;
|
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) s.strikeoutPos=prevNodeSizeForSpecialPlacement->strikeoutPos;
|
|
|
|
else s.strikeoutPos=fm.strikeOutPos();
|
|
|
|
return s;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
JKQTMathTextNodeSize JKQTMathTextSuperscriptNode::getSizeInternal(QPainter &painter, JKQTMathTextEnvironment currentEv) const
|
2022-08-14 22:59:03 +08:00
|
|
|
{
|
2022-08-17 05:05:04 +08:00
|
|
|
return getSizeWithSpecialPlacement(painter, currentEv, nullptr);
|
2022-08-14 22:59:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
double JKQTMathTextSuperscriptNode::drawWithSpecialPlacement(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSizeForSpecialPlacement) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
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
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
const JKQTMathTextNodeSize cs=getChild()->getSize(painter, ev);
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-06-26 06:28:49 +08:00
|
|
|
const QFontMetricsF fm(currentEv.getFont(parentMathText), painter.device());
|
2022-08-17 05:05:04 +08:00
|
|
|
//QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
|
|
|
const double childDescent=cs.overallHeight-cs.baselineHeight;
|
2022-08-12 22:50:26 +08:00
|
|
|
double shiftToChildBottom=parentMathText->getSuperShiftFactor()*fm.xHeight();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-14 22:59:03 +08:00
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) {
|
2022-08-17 05:05:04 +08:00
|
|
|
const double modifiedShift=prevNodeSizeForSpecialPlacement->baselineHeight-childDescent-parentMathText->getSpecialSuperShiftFactor()*cs.baselineHeight-childDescent;
|
2022-06-26 06:28:49 +08:00
|
|
|
if (modifiedShift>shiftToChildBottom) shiftToChildBottom=modifiedShift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double xx=x;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
double JKQTMathTextSuperscriptNode::draw(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv) const
|
2022-08-14 22:59:03 +08:00
|
|
|
{
|
|
|
|
return drawWithSpecialPlacement(painter, x, y, currentEv, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
|
|
|
|
QString JKQTMathTextSuperscriptNode::getTypeName() const
|
|
|
|
{
|
|
|
|
return "MTsuperscriptNode";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
bool JKQTMathTextSuperscriptNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const
|
2022-06-08 21:38:26 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
JKQTMathTextNodeSize JKQTMathTextSubscriptNode::getSizeWithSpecialPlacement(QPainter& painter, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSizeForSpecialPlacement) const {
|
|
|
|
JKQTMathTextNodeSize s;
|
2022-06-08 21:38:26 +08:00
|
|
|
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());
|
2022-08-14 22:59:03 +08:00
|
|
|
//const QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
const JKQTMathTextNodeSize cs=getChild()->getSize(painter, ev);
|
|
|
|
//const double childDescent=cs.overallHeight-cs.baselineHeight;
|
|
|
|
double shift_to_childBaseline=cs.baselineHeight-parentMathText->getSubShiftFactor()*fm.xHeight();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-14 22:59:03 +08:00
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) {
|
2022-06-26 06:28:49 +08:00
|
|
|
//qDebug()<<"oldshift="<<shift<<", prevNodeSize->overallHeight="<<prevNodeSize->overallHeight<<", prevNodeSize->baselineHeight="<<prevNodeSize->baselineHeight;
|
2022-08-14 22:59:03 +08:00
|
|
|
const double parentDescent=prevNodeSizeForSpecialPlacement->overallHeight-prevNodeSizeForSpecialPlacement->baselineHeight;
|
2022-08-17 05:05:04 +08:00
|
|
|
const double newShift=parentDescent+parentMathText->getSpecialSubShiftFactor()*cs.baselineHeight;
|
2022-06-26 06:28:49 +08:00
|
|
|
if (newShift>shift_to_childBaseline) shift_to_childBaseline=newShift;
|
|
|
|
//qDebug()<<"newshift="<<shift;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
s.baselineHeight=cs.baselineHeight-shift_to_childBaseline;
|
|
|
|
s.overallHeight=cs.overallHeight;
|
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) s.strikeoutPos=prevNodeSizeForSpecialPlacement->strikeoutPos;
|
|
|
|
else s.strikeoutPos=fm.strikeOutPos();
|
|
|
|
s.width=cs.width;
|
|
|
|
return s;
|
2022-06-08 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
JKQTMathTextNodeSize JKQTMathTextSubscriptNode::getSizeInternal(QPainter &painter, JKQTMathTextEnvironment currentEv) const
|
2022-08-14 22:59:03 +08:00
|
|
|
{
|
2022-08-17 05:05:04 +08:00
|
|
|
return getSizeWithSpecialPlacement(painter, currentEv, nullptr);
|
2022-08-14 22:59:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
double JKQTMathTextSubscriptNode::drawWithSpecialPlacement(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv, const JKQTMathTextNodeSize* prevNodeSizeForSpecialPlacement) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
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());
|
2022-08-14 22:59:03 +08:00
|
|
|
//const QRectF tbr_of_letterM=JKQTMathTextGetTightBoundingRect(currentEv.getFont(parentMathText), "M", painter.device());
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
const JKQTMathTextNodeSize cs=getChild()->getSize(painter, ev);
|
|
|
|
double shift_to_childBaseline=cs.baselineHeight-parentMathText->getSubShiftFactor()*fm.xHeight();
|
2022-06-08 21:38:26 +08:00
|
|
|
|
2022-08-14 22:59:03 +08:00
|
|
|
if (prevNodeSizeForSpecialPlacement!=nullptr) {
|
2022-06-08 21:38:26 +08:00
|
|
|
//qDebug()<<"oldshift="<<shift<<", prevNodeSize->overallHeight="<<prevNodeSize->overallHeight<<", prevNodeSize->baselineHeight="<<prevNodeSize->baselineHeight;
|
2022-08-14 22:59:03 +08:00
|
|
|
const double parentDescent=prevNodeSizeForSpecialPlacement->overallHeight-prevNodeSizeForSpecialPlacement->baselineHeight;
|
2022-08-17 05:05:04 +08:00
|
|
|
const double newShift=parentDescent+parentMathText->getSpecialSubShiftFactor()*cs.baselineHeight;
|
2022-06-26 06:28:49 +08:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
double JKQTMathTextSubscriptNode::draw(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv) const
|
2022-08-14 22:59:03 +08:00
|
|
|
{
|
|
|
|
return drawWithSpecialPlacement(painter, x, y, currentEv, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:38:26 +08:00
|
|
|
QString JKQTMathTextSubscriptNode::getTypeName() const
|
|
|
|
{
|
|
|
|
return "MTsubscriptNode";
|
|
|
|
}
|
|
|
|
|
2022-08-17 05:05:04 +08:00
|
|
|
bool JKQTMathTextSubscriptNode::toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const {
|
2022-06-08 21:38:26 +08:00
|
|
|
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;
|
|
|
|
}
|