FIXED: JKQTMathText: symbol spacing in math mode (and text mode)

This commit is contained in:
jkriege2 2022-06-19 23:34:25 +02:00
parent 03c59d3507
commit f9c060af68
2 changed files with 17 additions and 16 deletions

View File

@ -30,6 +30,7 @@ Changes, compared to \ref page_whatsnew_V4_0_0 "v4.0.0" include:
<li>FIXED: JKQTMathText added a little whitespace before and after the LaTeX-string. This was removed as it disturbed the layout of text in plots</li> <li>FIXED: JKQTMathText added a little whitespace before and after the LaTeX-string. This was removed as it disturbed the layout of text in plots</li>
<li>FIXED: height-calculation of frac-like instructions: before the ascent and descent were equal, even if the numerator and denominator have different heights</li> <li>FIXED: height-calculation of frac-like instructions: before the ascent and descent were equal, even if the numerator and denominator have different heights</li>
<li>FIXED: strikeoutPos was not correctly calculated in sub-/superscript node</li> <li>FIXED: strikeoutPos was not correctly calculated in sub-/superscript node</li>
<li>FIXED: symbol spacing in math mode (and text mode)</li>
<li>FIXED/IMPROVED: JKQTMathText renders several LaTeX strings better (simple braces in math mode, +-*... as symbols with proper sizes in math mode, added some missing instruction aliases, improved size of \vec and \hat, corrrected fonts usage for mathrm</li> <li>FIXED/IMPROVED: JKQTMathText renders several LaTeX strings better (simple braces in math mode, +-*... as symbols with proper sizes in math mode, added some missing instruction aliases, improved size of \vec and \hat, corrrected fonts usage for mathrm</li>
<li>IMPROVED: high-dpr-support in JKQTMathText</li> <li>IMPROVED: high-dpr-support in JKQTMathText</li>
<li>MODIFIED: brace node now calculates the extension of the child height above or below the strikeoutPos, in order to center braces around the strikeoutPos</li> <li>MODIFIED: brace node now calculates the extension of the child height above or below the strikeoutPos, in order to center braces around the strikeoutPos</li>

View File

@ -758,7 +758,7 @@ JKQTMathTextSymbolNode::SymbolProps JKQTMathTextSymbolNode::getSymbolProp(const
<<"subsetnot"<<"bot"<<"leftharpoonup"<<"rightharpoonup"<<"upharpoonleft"<<"downharpoonleft"<<"leftrightharpoon"<<"rightleftharpoon"<<"coprod"<<"leftharpoondown" <<"subsetnot"<<"bot"<<"leftharpoonup"<<"rightharpoonup"<<"upharpoonleft"<<"downharpoonleft"<<"leftrightharpoon"<<"rightleftharpoon"<<"coprod"<<"leftharpoondown"
<<"rightharpoondown"<<"upharpoonright"<<"downharpoonright"<<"nwarrow"<<"nearrow"<<"searrow"<<"swarrow"<<"mapsto"<<"div"<<"multimap"<<"maporiginal"<<"mapimage" <<"rightharpoondown"<<"upharpoonright"<<"downharpoonright"<<"nwarrow"<<"nearrow"<<"searrow"<<"swarrow"<<"mapsto"<<"div"<<"multimap"<<"maporiginal"<<"mapimage"
<<"times"<<"propto"<<"bullet"<<"neq"<<"ne"<<"equiv"<<"approx"<<"otimes"<<"oplus"<<"oslash"<<"cap"<<"land"<<"cup"<<"lor"<<"supset"<<"supseteq"<<"supsetnot" <<"times"<<"propto"<<"bullet"<<"neq"<<"ne"<<"equiv"<<"approx"<<"otimes"<<"oplus"<<"oslash"<<"cap"<<"land"<<"cup"<<"lor"<<"supset"<<"supseteq"<<"supsetnot"
<<"subset"<<"subseteq"<<"in"<<"notin"<<"cdot"<<"wedge"<<"vee"<<"cong"<<"bot"<<"mid"<<"+"<<"-"<<"|"<<"*"<<"/"<<"<"<<">"; <<"subset"<<"subseteq"<<"in"<<"notin"<<"cdot"<<"wedge"<<"vee"<<"cong"<<"bot"<<"mid"<<"+"<<"-"<<"|"<<"*"<<"/"<<"<"<<">"<<"=";
} }
if (extendWInMM.contains(n)) { if (extendWInMM.contains(n)) {
@ -886,17 +886,17 @@ void JKQTMathTextSymbolNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvi
if (props.italic>0) f.setItalic(true); if (props.italic>0) f.setItalic(true);
if (props.bold<0) f.setBold(false); if (props.bold<0) f.setBold(false);
if (props.bold>0) f.setBold(true); if (props.bold>0) f.setBold(true);
QFontMetricsF fm(f, painter.device()); const QFontMetricsF fm(f, painter.device());
QString symb=props.symbol; QString symb=props.symbol;
width=0; width=0;
if (currentEv.insideMath) width=qMax(JKQTMathTextGetTightBoundingRect(f, symb, painter.device()).width(),JKQTMathTextGetTightBoundingRect(f, "i", painter.device()).width());//fm.width(symbol); if (currentEv.insideMath) {
else width=fm.boundingRect(symb).width();//fm.width(symbol); width=qMax(JKQTMathTextGetTightBoundingRect(f, symb, painter.device()).width(), JKQTMathTextGetTightBoundingRect(f, "(", painter.device()).width());
} else width=fm.boundingRect(symb).width();//fm.width(symbol);
width=qMax(fm.boundingRect("j").width(), width);
if (symb.isEmpty()) { if (symb.isEmpty()) {
width=fm.boundingRect("a").width(); width=fm.boundingRect("a").width();
if (symbolName=="|") width=fm.boundingRect("1").width()*0.8; if (symbolName=="|") width=fm.boundingRect("1").width()*0.8;
else if (symbolName=="infty") width=fm.boundingRect("M").width(); else if (symbolName=="infty") width=JKQTMathTextGetTightBoundingRect(f, "8", painter.device()).height();
else if (symbolName=="quad" || symbolName=="qquad") width=JKQTMathTextGetTightBoundingRect(f, "M", painter.device()).width(); else if (symbolName=="quad" || symbolName=="qquad") width=JKQTMathTextGetTightBoundingRect(f, "M", painter.device()).width();
else if (symbolName==" " || symbolName=="space") width=JKQTMathTextGetTightBoundingRect(f, "x", painter.device()).width(); else if (symbolName==" " || symbolName=="space") width=JKQTMathTextGetTightBoundingRect(f, "x", painter.device()).width();
else if (symbolName==";") width=JKQTMathTextGetTightBoundingRect(f, "x", painter.device()).width()*0.75; else if (symbolName==";") width=JKQTMathTextGetTightBoundingRect(f, "x", painter.device()).width()*0.75;
@ -910,7 +910,7 @@ void JKQTMathTextSymbolNode::getSizeInternal(QPainter& painter, JKQTMathTextEnvi
else if (symbolName=="longleftrightarrow") { width=JKQTMathTextGetTightBoundingRect(f, "X", painter.device()).width()*3.5; symb="x"; } else if (symbolName=="longleftrightarrow") { width=JKQTMathTextGetTightBoundingRect(f, "X", painter.device()).width()*3.5; symb="x"; }
else if (symbolName=="Longleftrightarrow") { width=JKQTMathTextGetTightBoundingRect(f, "X", painter.device()).width()*3.5; symb="x"; } else if (symbolName=="Longleftrightarrow") { width=JKQTMathTextGetTightBoundingRect(f, "X", painter.device()).width()*3.5; symb="x"; }
} }
QRectF tbr=JKQTMathTextGetTightBoundingRect(f, symb, painter.device()); const QRectF tbr=JKQTMathTextGetTightBoundingRect(f, symb, painter.device());
overallHeight=tbr.height();// fm.height(); overallHeight=tbr.height();// fm.height();
baselineHeight=tbr.height()-tbr.bottom(); baselineHeight=tbr.height()-tbr.bottom();
if (props.exactAscent) { if (props.exactAscent) {
@ -940,7 +940,7 @@ double JKQTMathTextSymbolNode::draw(QPainter& painter, double x, double y, JKQTM
QFont fold=painter.font(); QFont fold=painter.font();
QFont f=currentEv.getFont(parentMathText); QFont f=currentEv.getFont(parentMathText);
QFont f1=f; QFont f1=f;
auto props=getSymbolProp(symbolName, currentEv); const auto props=getSymbolProp(symbolName, currentEv);
f.setFamily(props.font); f.setFamily(props.font);
f.setPointSizeF(f.pointSizeF()*props.fontFactor); f.setPointSizeF(f.pointSizeF()*props.fontFactor);
if (props.italic<0) f.setItalic(false); if (props.italic<0) f.setItalic(false);
@ -948,14 +948,12 @@ double JKQTMathTextSymbolNode::draw(QPainter& painter, double x, double y, JKQTM
if (props.bold<0) f.setBold(false); if (props.bold<0) f.setBold(false);
if (props.bold>0) f.setBold(true); if (props.bold>0) f.setBold(true);
const QFontMetricsF fm(f, painter.device()); const QFontMetricsF fm(f, painter.device());
const QFontMetricsF fm1(f1, painter.device());
painter.setFont(f); painter.setFont(f);
double shift=0; double shift=0;
if (props.extendWidthInMathmode && currentEv.insideMath) { if (props.extendWidthInMathmode && currentEv.insideMath) {
const double origwidth=width/parentMathText->getMathoperatorWidthFactor(); const QRectF tbr=JKQTMathTextGetTightBoundingRect(f, props.symbol, painter.device());
shift=0.5*(width-origwidth); shift=0.5*width-tbr.width()/2.0-tbr.x();
//width=width*parent->getMathoperatorWidthFactor();
} }
//std::cout<<"symbol '"<<symbolName.toStdString()<<"' = "<<std::hex<<symbol.at(0).digitValue()<<" in font '"<<f.family().toStdString()<<"' ("<<QFontInfo(f).family().toStdString()<<"): "<<fm.inFont(symbol.at(0))<<std::endl; //std::cout<<"symbol '"<<symbolName.toStdString()<<"' = "<<std::hex<<symbol.at(0).digitValue()<<" in font '"<<f.family().toStdString()<<"' ("<<QFontInfo(f).family().toStdString()<<"): "<<fm.inFont(symbol.at(0))<<std::endl;
@ -968,19 +966,21 @@ double JKQTMathTextSymbolNode::draw(QPainter& painter, double x, double y, JKQTM
const double xwi=fm.boundingRect("x").width(); const double xwi=fm.boundingRect("x").width();
if (!props.symbol.isEmpty()) { if (!props.symbol.isEmpty()) {
// if the symbol has been recognized in the constructor: draw the symbol // if the symbol has been recognized in the constructor: draw the symbol
painter.drawText(QPointF(x+shift, y+props.yfactor*overallHeight), props.symbol); const QPointF x0(x+shift, y+props.yfactor*overallHeight);
painter.drawText(x0, props.symbol);
if (props.drawBar) { if (props.drawBar) {
const double xx=x+shift; const double xx=x+shift;
const double yy=y-fm.xHeight()-(JKQTMathTextGetTightBoundingRect(f, "M", painter.device()).height()-fm.xHeight())/3.0; const double yy=y-fm.xHeight()-(JKQTMathTextGetTightBoundingRect(f, "M", painter.device()).height()-fm.xHeight())/3.0;
const QLineF l(xx, yy, xx+xwi/3.0+((currentEv.italic)?(xwi/3.0):0), yy); const QLineF l(xx, yy, xx+xwi/3.0+((currentEv.italic)?(xwi/3.0):0), yy);
if (l.length()>0) painter.drawLine(l); if (l.length()>0) painter.drawLine(l);
} }
// try to draw some often used special symbols, by synthesizing them from // try to draw some often used special symbols, by synthesizing them from
// standard characters in the current drawing font // standard characters in the current drawing font
} else if (symbolName=="infty") { } else if (symbolName=="infty") {
//std::cout<<"draw infty\n"; //std::cout<<"draw infty\n";
f1.setItalic(false); f1.setItalic(false);
const QFontMetricsF fm1(f1, painter.device());
painter.setFont(f1); painter.setFont(f1);
painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();});
painter.translate(x+shift+fm1.boundingRect("8").width()/3.0, y-fm1.xHeight()); painter.translate(x+shift+fm1.boundingRect("8").width()/3.0, y-fm1.xHeight());
@ -988,10 +988,10 @@ double JKQTMathTextSymbolNode::draw(QPainter& painter, double x, double y, JKQTM
painter.drawText(QPointF(0,0), "8"); painter.drawText(QPointF(0,0), "8");
} else if (symbolName=="|") { } else if (symbolName=="|") {
//std::cout<<"draw infty\n"; //std::cout<<"draw ||\n";
f1.setItalic(false); f1.setItalic(false);
const QFontMetricsF fm1(f1, painter.device());
painter.setFont(f1); painter.setFont(f1);
painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();});
painter.translate(x+shift, y); painter.translate(x+shift, y);
painter.drawText(QPointF(0,0), "|"); painter.drawText(QPointF(0,0), "|");