new options interface for JKQTMathText::parse()

breaking: removed JKQTMathtext::unparsedNode
This commit is contained in:
jkriege2 2022-08-13 15:32:31 +02:00
parent ce6637fb4b
commit 9819a8c4be
5 changed files with 42 additions and 62 deletions

View File

@ -48,10 +48,12 @@ Changes, compared to \ref page_whatsnew_V4_0_0 "v4.0.0" include:
<li>IMPROVED: rendering of sqrt</li>
<li>IMPROVED: rendering and size calculation of decorations</li>
<li>IMPROVED: tokenizing and parsing of text in text-mode: now a lot of accents with commands like \c \\\"a, \c \\'e and variants (e.g. \c {\\\"a}, \c \\\"{a}, ...) are supported now</li>
<li>IMPROVED/breaking: refactored symbol node JKQTMathTextSymbolNode and changed font-lookup!</li>
<li>IMPROVED/NEW/breaking: refactored whitespace-processing node JKQTMathTextWhitespaceNode, now all major LaTeX whitespace commands are supported properly</li>
<li>IMPROVED/NEW/breaking: refactored LaTeX parser in JKQTMathText</li>
<li>REMOVED/breaking: \c \\v[a-zA-Z] and shorthand for \c \\vec{a-zA-Z} was removed, implementation of \c \\bbR,\c \\bbC,... changed</li>
<li>IMPROVED/BREAKING: refactored symbol node JKQTMathTextSymbolNode and changed font-lookup!</li>
<li>IMPROVED/NEW/BREAKING: refactored whitespace-processing node JKQTMathTextWhitespaceNode, now all major LaTeX whitespace commands are supported properly</li>
<li>IMPROVED/NEW/BREAKING: refactored LaTeX parser in JKQTMathText</li>
<li>REMOVED/BREAKING: \c \\v[a-zA-Z] and shorthand for \c \\vec{a-zA-Z} was removed, implementation of \c \\bbR,\c \\bbC,... changed</li>
<li>MODIFIED/BREAKING: new options interface for JKQTMathText::parse()</li>
<li>EMOVED/BREAKING: removed JKQTMathtext::unparsedNode</li>
<li>IMPROVED/REWORKED rendering of text in text- and math-mode. Now it is more consistent with the output of LaTeX itself</li>
<li>IMPROVED/REWORKED rendering of blackboard font: now several different rendering modes can be selected using JKQTMathText::setFontBlackboradMode()</li>
<li>BREAKING/REWORKED: The \\verb!...!-command now works the same as in LaTeX</li>
@ -123,25 +125,25 @@ Changes, compared to \ref page_whatsnew_V2019_11 "v2019.11" include:
<li> fixed issue <a href="https://github.com/jkriege2/JKQtPlotter/issues/52">#52: 'runtime_error': is not a member of 'std'</a>, thanks to <a href="https://github.com/gomgomi">user:gomgomi</a></li>
<li> fixed issue <a href="https://github.com/jkriege2/JKQtPlotter/issues/56">#56: Logscale zoom multiple zeros? </a>, thanks to <a href="https://github.com/sufuk">user:sufuk</a></li>
<li> merged PR <a href="https://github.com/jkriege2/JKQtPlotter/pull/51">#51: Bug: JKMathParser exception return local variable </a>, thanks to <a href="https://github.com/StephanOostveen">user:StephanOostveen</a></li>
<li>renamed/breaking change: renamed JKQTPPlotObject->JKQTPGeometricPlotElement and added new base class JKQTPPlotAnnotationElement</li>
<li>renamed/breaking change: renamed JKQTPColorPaletteStyleAndToolsMixin::setPalette() -> JKQTPColorPaletteStyleAndToolsMixin::setColorPalette()</li>
<li>removed/breaking change: removed the usage of some deprecated functions and objects (e.g. QMatrix)</li>
<li>removed/breaking change: removed the overlay elements (derived from JKQTPOverlayElement), which were not very well set up and are more confusing than useful.</li>
<li>improved/breaking change: geometric objects now use an adaptive drawing algorithm to represent curves (before e.g. ellipses were always separated into a fixed number of line-segments)</li>
<li>improved/breaking change: geometric elements constructor: removed all styling properties, added setStyle()-functions to replace them. This is necessary to better work with the extended Styling system</li>
<li>renamed/BREAKING change: renamed JKQTPPlotObject->JKQTPGeometricPlotElement and added new base class JKQTPPlotAnnotationElement</li>
<li>renamed/BREAKING change: renamed JKQTPColorPaletteStyleAndToolsMixin::setPalette() -> JKQTPColorPaletteStyleAndToolsMixin::setColorPalette()</li>
<li>removed/BREAKING change: removed the usage of some deprecated functions and objects (e.g. QMatrix)</li>
<li>removed/BREAKING change: removed the overlay elements (derived from JKQTPOverlayElement), which were not very well set up and are more confusing than useful.</li>
<li>improved/BREAKING change: geometric objects now use an adaptive drawing algorithm to represent curves (before e.g. ellipses were always separated into a fixed number of line-segments)</li>
<li>improved/BREAKING change: geometric elements constructor: removed all styling properties, added setStyle()-functions to replace them. This is necessary to better work with the extended Styling system</li>
<li>improved: constructors and access functions for several geometric objects (e.g. more constructors, additional functions to retrieve parameters in diferent forms, iterators for polygons, ...)</li>
<li>improved/breaking change: reworked class hierarchy of parsed function plots and declared several setters as slots.</li>
<li>improved/breaking change: reworked class hierarchy of bar & impulse charts.</li>
<li>improved/breaking change: reworked class hierarchy of range charts.</li>
<li>improved/breaking change: reworked class hierarchy of special line (step) graphs.</li>
<li>improved/breaking change: reworked class hierarchy of filled line graphs.</li>
<li>improved/breaking change: reworked class hierarchy of range plot elements (JKQTPVerticalRange and JKQTPHorizontalRange).</li>
<li>improved/breaking change: reworked class hierarchy of boxplots.</li>
<li>improved/breaking change: reworked class hierarchy of violin plots.</li>
<li>improved/breaking change: extended styling system for graphs.</li>
<li>improved/breaking change: reworked graph Base-Classes (promoted several setters to slots, added Q_PROPERTY- and Q_ENUM-declarations...)</li>
<li>improved/breaking change: made more functions and function parameters const</li>
<li>improved/breaking change: image plots now manage CONST-data, not plain pointer arrays... This is OK, since the raw data is never owned nor modified by the plot, only referenced!.</li>
<li>improved/BREAKING change: reworked class hierarchy of parsed function plots and declared several setters as slots.</li>
<li>improved/BREAKING change: reworked class hierarchy of bar & impulse charts.</li>
<li>improved/BREAKING change: reworked class hierarchy of range charts.</li>
<li>improved/BREAKING change: reworked class hierarchy of special line (step) graphs.</li>
<li>improved/BREAKING change: reworked class hierarchy of filled line graphs.</li>
<li>improved/BREAKING change: reworked class hierarchy of range plot elements (JKQTPVerticalRange and JKQTPHorizontalRange).</li>
<li>improved/BREAKING change: reworked class hierarchy of boxplots.</li>
<li>improved/BREAKING change: reworked class hierarchy of violin plots.</li>
<li>improved/BREAKING change: extended styling system for graphs.</li>
<li>improved/BREAKING change: reworked graph Base-Classes (promoted several setters to slots, added Q_PROPERTY- and Q_ENUM-declarations...)</li>
<li>improved/BREAKING change: made more functions and function parameters const</li>
<li>improved/BREAKING change: image plots now manage CONST-data, not plain pointer arrays... This is OK, since the raw data is never owned nor modified by the plot, only referenced!.</li>
<li>bugfixed/improved: aspect ratio handling in JKQTPlotter.</li>
<li>new: switching to semantic versioning ... starting with v4.0.0</li>
<li>new: Compatibility with Qt 5.15 and Qt6</li>

View File

@ -185,10 +185,8 @@ JKQTMathText::JKQTMathText(QObject* parent):
//qDebug()<<"set fonts: "<<std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-t0).count()/1000.0<<"ms"; t0=std::chrono::high_resolution_clock::now();
useXITS();
//qDebug()<<"useXITS: "<<std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-t0).count()/1000.0<<"ms"; t0=std::chrono::high_resolution_clock::now();
useUnparsed=false;
parsedNode=nullptr;
unparsedNode=nullptr;
currentToken=MTTnone;
currentTokenName="";
@ -200,8 +198,6 @@ JKQTMathText::JKQTMathText(QObject* parent):
JKQTMathText::~JKQTMathText() {
if (parsedNode!=nullptr) delete parsedNode;
parsedNode=nullptr;
if (unparsedNode!=nullptr) delete unparsedNode;
unparsedNode=nullptr;
}
void JKQTMathText::loadSettings(const QSettings& settings, const QString& group){
@ -1058,16 +1054,6 @@ void JKQTMathText::setMatrixYPaddingFactor(double factor)
matrix_yPadding_factor=factor;
}
void JKQTMathText::setUseUnparsed(bool __value)
{
this->useUnparsed = __value;
}
bool JKQTMathText::isUsingUnparsed() const
{
return this->useUnparsed;
}
QStringList JKQTMathText::getErrorList() const {
return this->error_list;
}
@ -2288,15 +2274,15 @@ JKQTMathTextNode *JKQTMathText::getParsedNode() const {
bool JKQTMathText::parse(const QString& text, bool addSpaceBeforeAndAfter, bool allowLinebreaks){
bool JKQTMathText::parse(const QString& text, ParseOptions options){
QString ntext;
if (addSpaceBeforeAndAfter) ntext=QString("\\;")+text+QString("\\;");
if (options.testFlag(StartWithMathMode)) ntext=QString("$")+text+QString("$");
if (options.testFlag(AddSpaceBeforeAndAfter)) ntext=QString("\\;")+text+QString("\\;");
else ntext=text;
if (parsedNode && parseString==ntext) return true;
if (parsedNode!=nullptr) delete parsedNode;
if (unparsedNode!=nullptr) delete unparsedNode;
parseString=ntext;
currentTokenID=-1;
@ -2306,12 +2292,11 @@ bool JKQTMathText::parse(const QString& text, bool addSpaceBeforeAndAfter, bool
lastLineCount.clear();
error_list.clear();
if (allowLinebreaks) {
if (options.testFlag(AllowLinebreaks)) {
parsedNode=parseMultilineLatexString(true, QString(""), MTHALeft, 1.0, MTSMDefaultSpacing, MTVOFirstLine);
} else {
parsedNode=parseLatexString(true);
}
unparsedNode=new JKQTMathTextVerbatimNode(this, text);
return (parsedNode!=nullptr);
}
@ -2516,7 +2501,6 @@ QPicture JKQTMathText::drawIntoPicture(bool drawBoxes)
JKQTMathTextNode *JKQTMathText::getNodeTree() const {
if (useUnparsed) return unparsedNode;
return parsedNode;
}

View File

@ -25,6 +25,7 @@
#ifndef JKQTMATHTEXT_H
#define JKQTMATHTEXT_H
#include <QFlags>
#include <QObject>
#include <QSettings>
#include <QPainter>
@ -214,14 +215,24 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject {
void loadSettings(const QSettings& settings, const QString& group=QString("mathtext/"));
/** \brief store the object settings to the given QSettings object with the given name prefix */
void saveSettings(QSettings& settings, const QString& group=QString("mathtext/")) const;
/** \brief options for parse() */
enum ParseOption {
AddSpaceBeforeAndAfter = 0x01, /*!< \brief If set, a little bit of space is added before and after the text. */
StartWithMathMode = 0x02, /*!< \brief if set, the parser assumes the LaTeX string is in math-mode (as if surrounded by \c $ ) */
AllowLinebreaks = 0x04, /*!< \brief If set, linebreak (i.e. \c \\ or \c \\newline ) are allowed, otherwise a single line wihtout such linebreak commands is expected */
DefaultParseOptions=AllowLinebreaks,
};
Q_DECLARE_FLAGS(ParseOptions, ParseOption)
Q_FLAG(ParseOptions)
/** \brief parse the given LaTeX string.
*
* \param addSpaceBeforeAndAfter If \ctrue a little bit of space is added before and after the text.
* \param allowLinebreaks If \c true , linebreak (i.e. \c \\ or \c \\newline ) are allowed, otherwise a single line wihtout such linebreak commands is expected
* \param options Options for parsing, \see ParseOptions
* \param allowLinebreaks
*
* \returns \c true on success.
*/
bool parse(const QString &text, bool addSpaceBeforeAndAfter=false, bool allowLinebreaks=true);
bool parse(const QString &text, ParseOptions options=DefaultParseOptions);
/** \brief get the size of the drawn representation. returns an invalid size if no text has been parsed. */
QSizeF getSize(QPainter& painter);
/** \brief return the descent, i.e. the distance from the baseline to the lowest part of the representation */
@ -643,11 +654,6 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject {
void setMatrixYPaddingFactor(double factor);
/** \copydoc useUnparsed */
void setUseUnparsed(bool __value);
/** \copydoc useUnparsed */
bool isUsingUnparsed() const;
/** \copydoc error_list */
QStringList getErrorList() const;
/** \brief returns \c true when errors were registered in the system \see error_list */
@ -883,10 +889,6 @@ class JKQTMATHTEXT_LIB_EXPORT JKQTMathText : public QObject {
/** \brief the result of parsing the last string supplied to the object via parse() */
JKQTMathTextNode* parsedNode;
/** \brief a tree containing the unparsed text as a single node */
JKQTMathTextNode* unparsedNode;
/** \brief if true, the unparsedNode is drawn \see unparsedNode */
bool useUnparsed;
/** \brief returns the syntax tree of JKQTMathTextNode's that was created by the last parse call */
JKQTMathTextNode* getNodeTree() const;

View File

@ -1738,7 +1738,6 @@ bool JKQTBasePlotter::printpreviewNew(QPaintDevice* paintDevice, bool setAbsolut
plotterStyle.widgetBackgroundBrush=bc;
paintMagnification=oldP;
mathText.setUseUnparsed(false);
return res;
@ -3744,7 +3743,6 @@ void JKQTBasePlotter::saveImage(const QString& filename, bool displayPreview) {
QFile::copy(fn, tempFM);
}
mathText.setUseUnparsed(!jkqtpPaintDeviceAdapters[adapterID]->useLatexParser());
gridPrintingCalc();
QPaintDevice* paintDevice=jkqtpPaintDeviceAdapters[adapterID]->createPaintdevice(fn, jkqtp_roundTo<int>(gridPrintingSize.width()), jkqtp_roundTo<int>(gridPrintingSize.height()));
@ -5104,11 +5102,6 @@ JKQTBasePlotter::textSizeData::textSizeData():
bool JKQTPPaintDeviceAdapter::useLatexParser() const
{
return true;
}
QPaintDevice *JKQTPPaintDeviceAdapter::createPaintdeviceMM(const QString &filename, double widthMM, double heightMM) const
{
#if QT_VERSION>=QT_VERSION_CHECK(6,0,0)

View File

@ -87,7 +87,6 @@ class JKQTPLOTTER_LIB_EXPORT JKQTPPaintDeviceAdapter {
virtual double getPrintSizeXInMM() const =0;
virtual double getPrintSizeYInMM() const =0;
virtual bool isPrinter() const=0;
virtual bool useLatexParser() const;
/** \brief create a paint device with a given size in pt */
virtual QPaintDevice* createPaintdevice(const QString& filename, int widthPix, int heightPix) const=0;
/** \brief create a paint device with a given size in millimeters ... the default implementation call createPaintdevice(), assuming the standard logical resolution of the desktop!!!) */