JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtmathtextlatexparser.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3 with contributions from: Razi Alavizadeh
4
5
6
7 This software is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License (LGPL) as published by
9 the Free Software Foundation, either version 2.1 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License (LGPL) for more details.
16
17 You should have received a copy of the GNU Lesser General Public License (LGPL)
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22
23
24
25#ifndef JKQTMATHTEXTLATEXPARSER_H
26#define JKQTMATHTEXTLATEXPARSER_H
27
28#include "jkqtmathtext/parsers/jkqtmathtextparser.h"
29#include "jkqtmathtext/jkqtmathtext.h"
30#include "jkqtmathtext/jkqtmathtext_imexport.h"
31#include <QHash>
32#include <QSet>
33#include <QMap>
34#include <QString>
35
36
37class JKQTMathTextNode; // forward
38class JKQTMathTextVerticalListNode; // forward
39
40/*! \brief a LaTeX parser for JKQTMathText
41 \ingroup jkqtmathtext_parser
42
43
44 JKQTMathText is a self-contained mathematical markup renderer for Qt. It is used to renderer
45 labels in JKQTPlotter/JKQTBasePlotter, but can be used independently.
46
47 That class is responsible for rendering a memory representation of mathematical markup,
48 but relies on the current class JKQTMathTextLatexParser to parse a LaTeX string into
49 the mentioned memory representation.
50
51 \see See \ref jkqtmathtext_supportedlatex for a description of the supported LaTeX subset
52 and \ref jkqtmathtext_renderingmodel for a description of the rendering model.
53
54 In particular JKQTMathTextLatexParser actually parses e.g. a LaTeX string and draws it in pure C++. It does NOT rely
55 on an installed LaTeX for the rendering!
56
57
58 */
60 Q_OBJECT
61 public:
62
63 /** \brief class constructor */
65 /** \brief class destructor */
67
69
70
71 protected:
72
73 /** \brief the token types that may arrise in the string */
74 enum tokenType {
75 MTTnone, /*!< \brief no token */
76 MTTtext, /*!< \brief a piece of general text */
77 MTTinstruction, /*!< \brief an instruction, started by \c "\", e.g. \c "\\textbf", ... */
78 MTTinstructionNewline, /*!< \brief a newline instruction \c "\\" */
79 MTTinstructionVerbatim, /*!< \brief a verbatim instruction, e.g. \c \\verb!verbatimtext! was found: currentTokenName will contain the text enclode by the verbatim delimiters */
80 MTTinstructionVerbatimVisibleSpace, /*!< \brief a verbatim instruction that generates visible whitespaces, e.g. \c \\begin{verbatim}...\\end{verbatim} was found: currentTokenName will contain the text enclode by the verbatim delimiters */
81 MTTinstructionBegin, /*!< \brief a \c '\\begin{...}' instruction, currentTokenName is the name of the environment */
82 MTTinstructionEnd, /*!< \brief a \c '\\end{...}' instruction, currentTokenName is the name of the environment */
83 MTTunderscore, /*!< \brief the character \c "_" */
84 MTThat, /*!< \brief the character \c "^" */
85 MTTdollar, /*!< \brief the character \c "$" */
86 MTTopenbrace, /*!< \brief the character \c "{" */
87 MTTclosebrace, /*!< \brief the character \c "}" */
88 MTTopenbracket, /*!< \brief the character \c "[" */
89 MTTclosebracket, /*!< \brief the character \c "]" */
90 MTTwhitespace, /*!< \brief some whitespace */
91 MTTampersand, /*!< \brief the character \c "&" */
92 MTThyphen, /*!< \brief the single hyphen character \c "-" in text-mode \note MTTendash and MTTemdash take precedence over MTThypen */
93 MTTendash, /*!< \brief the en-dash character sequence \c "--" in text-mode */
94 MTTemdash, /*!< \brief the em-dash character sequence \c "---" in text-mode */
95 MTTtilde, /*!< \brief the tilde character \c "~" */
96
97 };
98 /** \brief convert a tokenType into a string, e.g. for debugging output */
99 static QString tokenType2String(tokenType type);
100
101 /** \brief tokenizer for the LaTeX parser */
103 /** \brief returns some characters to the Tokenizer */
104 void giveBackToTokenizer(size_t count);
105 /** \brief parse a LaTeX string
106 *
107 * \param get if \c true this calls getToken()
108 * \param quitOnClosingBrace if unequal MTBTAny, this returns if the given closing brace is found
109 * \param quitOnEnvironmentEnd wuit if \c \\end{quitOnEnvironmentEnd} is found
110 * \param quitOnClosingBracket if \c true, quits on encountering a MTTclosebracket token
111 */
112 JKQTMathTextNode* parseLatexString(bool get, JKQTMathTextBraceType quitOnClosingBrace=JKQTMathTextBraceType::MTBTAny, const QString& quitOnEnvironmentEnd=QString(""), bool quitOnClosingBracket=false);
113 /** \brief parse a LaTeX string with linebreaks
114 *
115 * \param get if \c true this calls getToken()
116 * \param quitOnEnvironmentEnd wuit if \c \\end{quitOnEnvironmentEnd} is found
117 * \param _alignment horizontal alignment of the JKQTMathTextVerticalListNode \see JKQTMathTextVerticalListNode::alignment and JKQTMathTextHorizontalAlignment
118 * \param _linespacingFactor line spacing factor of the lines \see JKQTMathTextVerticalListNode::linespacingFactor
119 * \param spacingMode_ spacing mode/algorithm for the lines \see JKQTMathTextLineSpacingMode and JKQTMathTextLineSpacingMode
120 * \param _verticalOrientation vertical orientation of the block of all lines, see JKQTMathTextVerticalListNode::verticalOrientation and JKQTMathTextVerticalOrientation
121 *
122 * \returns JKQTMathTextVerticalListNode with the lines as children
123 */
124 JKQTMathTextVerticalListNode *parseMultilineLatexString(bool get, const QString& quitOnEnvironmentEnd=QString(""), JKQTMathTextHorizontalAlignment _alignment=MTHALeft, double _linespacingFactor=1.0, JKQTMathTextLineSpacingMode spacingMode_=MTSMDefaultSpacing, JKQTMathTextVerticalOrientation _verticalOrientation=MTVOFirstLine);
125 /** \brief parses a list of string-arguments, i.e. \c {p1}{p2}{...}
126 *
127 * \param get call getToken() at the start, otherwise it is expected that currentToken==MTTopenbrace
128 * \param Nparams the number of parameters to expect
129 * \param[out] foundError will be set to \c true if an error occured (unexpected token) or \c false otherwise
130 * \return the list of parameter strings with Nparam entries or an empty or partial list on error
131 */
132 QStringList parseStringParams(bool get, size_t Nparams, bool *foundError=nullptr);
133 /** \brief parses a string, i.e. a sequence of text and whitespaces. returns after any other token was found */
134 QString parseSingleString(bool get);
135 /** \brief read all text without tokenizing, until the sequence \a endsequence is found.
136 *
137 * \param get if \c true the functions begins by reading a new character, otherwise the current character is used as first character
138 * \param endsequence the sequence, ending the read
139 * \param removeFirstlineWhitespace if \c true the returned string does not contain the first whitespace-line and a possible trailing whitespace line
140 * \return the read string, excluding the \a endsequence
141 */
142 QString readUntil(bool get, const QString& endsequence, bool removeFirstlineWhitespace=false);
143 /** \brief parses a single instruction (including it's parameters)
144 *
145 * \param[out] _foundError will be set to \c true if an error occured (unexpected token) or \c false otherwise
146 * \param[out] getNew returns \c true if the parser has to call getToken() to go on
147 * \return the instruction node or \c nullptr on error (then also \a _foundError is set \c true )
148 * \note This method expects the current token currentToken to be MTTinstruction
149 */
150 JKQTMathTextNode* parseInstruction(bool *_foundError=nullptr, bool* getNew=nullptr);
151 /** \brief parse a LaTeX math environment */
153
154 /** \brief used by the tokenizer. type of the current token */
156 /** \brief the JKQTMathTextBraceType associated with the last \c \\right command the parser encountered */
158 /** \brief returns the number of \c \\hline , \c \\hdashline , ... commands in the last parseLatexString() call */
159 QMap<QString,size_t> lastMatrixLineCommandCount;
160 /** \brief used by the tokenizer. Name of the current token, id applicable */
162 /** \brief used by the tokenizer. Points to the currently read character in parseString */
164 /** \brief used by the tokenizer. The string to be parsed */
165 QString parseString;
166 /** \brief used by the parser. indicates whether we are in a math environment */
168 /** \brief used by the parser. indicates whether to use textstyle or displaystyle in math-mode */
170
171 /** \brief characters that initiate a new token */
172 static QSet<QChar> TokenCharacters;
173 /** \brief characters that require special treatment in math mode */
174 static QSet<QChar> mathEnvironmentSpecialChars;
175 /** \brief characters that require special treatment in math mode */
177 /** \brief single character instructions */
178 static QSet<QChar> SingleCharInstructions;
179 /** \brief maps instructions for accents in text-mode (e.g. \c \\'a or \c \\ae ) to the corresponding unicode character */
180 static QHash<QString, QChar> accentLetters;
181 /** \brief lists all lengths of keys in accentLetters that start with a backslash */
183 /** \brief lists all lengths of keys in accentLetters that start with a curly brace */
184 static QSet<int> accentLetters_LenCurly;
185 /** \brief characters that have to be replaced by the correspcoting JKQTMathTextSymbolNode in math mode */
186 static QSet<QString> mathEnvironmentSpecialText;
187 /** \brief maps instructions for braces with fixed size, e.g \c \\bigl to the magnification factor of the brace versus the base font */
188 static QHash<QString,double> big_instructions_family;
189
190
191 /** \brief fills all static data structures, if they are still empty */
192 static void initStaticStructures();
193};
194
195
196#endif // JKQTMATHTEXTLATEXPARSER_H
197
198
199
200
201
202
203
204
205
this class parses a mathematical markup string and can then draw the contained text/equation onto a Q...
Definition jkqtmathtext.h:192
QFlags< ParseOption > ParseOptions
Definition jkqtmathtext.h:217
@ DefaultParseOptions
Definition jkqtmathtext.h:215
a LaTeX parser for JKQTMathText
Definition jkqtmathtextlatexparser.h:59
JKQTMathTextNode * parseLatexString(bool get, JKQTMathTextBraceType quitOnClosingBrace=JKQTMathTextBraceType::MTBTAny, const QString &quitOnEnvironmentEnd=QString(""), bool quitOnClosingBracket=false)
parse a LaTeX string
int currentTokenID
used by the tokenizer. Points to the currently read character in parseString
Definition jkqtmathtextlatexparser.h:163
JKQTMathTextBraceType lastRightBraceType
the JKQTMathTextBraceType associated with the last \right command the parser encountered
Definition jkqtmathtextlatexparser.h:157
void giveBackToTokenizer(size_t count)
returns some characters to the Tokenizer
static QSet< int > accentLetters_LenCurly
lists all lengths of keys in accentLetters that start with a curly brace
Definition jkqtmathtextlatexparser.h:184
tokenType
the token types that may arrise in the string
Definition jkqtmathtextlatexparser.h:74
@ MTTendash
the en-dash character sequence "--" in text-mode
Definition jkqtmathtextlatexparser.h:93
@ MTTtext
a piece of general text
Definition jkqtmathtextlatexparser.h:76
@ MTTinstructionVerbatim
a verbatim instruction, e.g. \verb!verbatimtext! was found: currentTokenName will contain the text en...
Definition jkqtmathtextlatexparser.h:79
@ MTTinstructionBegin
a '\begin{...}' instruction, currentTokenName is the name of the environment
Definition jkqtmathtextlatexparser.h:81
@ MTThyphen
the single hyphen character "-" in text-mode
Definition jkqtmathtextlatexparser.h:92
@ MTTopenbracket
the character "["
Definition jkqtmathtextlatexparser.h:88
@ MTTdollar
the character "$"
Definition jkqtmathtextlatexparser.h:85
@ MTTinstructionVerbatimVisibleSpace
a verbatim instruction that generates visible whitespaces, e.g. \begin{verbatim}.....
Definition jkqtmathtextlatexparser.h:80
@ MTTclosebracket
the character "]"
Definition jkqtmathtextlatexparser.h:89
@ MTThat
the character "^"
Definition jkqtmathtextlatexparser.h:84
@ MTTopenbrace
the character "{"
Definition jkqtmathtextlatexparser.h:86
@ MTTinstruction
an instruction, started by "\", e.g. \c "\textbf", ...
Definition jkqtmathtextlatexparser.h:77
@ MTTampersand
the character "&"
Definition jkqtmathtextlatexparser.h:91
@ MTTwhitespace
some whitespace
Definition jkqtmathtextlatexparser.h:90
@ MTTemdash
the em-dash character sequence "---" in text-mode
Definition jkqtmathtextlatexparser.h:94
@ MTTinstructionEnd
a '\end{...}' instruction, currentTokenName is the name of the environment
Definition jkqtmathtextlatexparser.h:82
@ MTTunderscore
the character "_"
Definition jkqtmathtextlatexparser.h:83
@ MTTinstructionNewline
a newline instruction "\\"
Definition jkqtmathtextlatexparser.h:78
@ MTTnone
no token
Definition jkqtmathtextlatexparser.h:75
@ MTTclosebrace
the character "}"
Definition jkqtmathtextlatexparser.h:87
@ MTTtilde
the tilde character "~"
Definition jkqtmathtextlatexparser.h:95
JKQTMathTextVerticalListNode * parseMultilineLatexString(bool get, const QString &quitOnEnvironmentEnd=QString(""), JKQTMathTextHorizontalAlignment _alignment=MTHALeft, double _linespacingFactor=1.0, JKQTMathTextLineSpacingMode spacingMode_=MTSMDefaultSpacing, JKQTMathTextVerticalOrientation _verticalOrientation=MTVOFirstLine)
parse a LaTeX string with linebreaks
QStringList parseStringParams(bool get, size_t Nparams, bool *foundError=nullptr)
parses a list of string-arguments, i.e. {p1}{p2}{...}
virtual JKQTMathTextNode * parse(const QString &text, JKQTMathText::ParseOptions options=JKQTMathText::DefaultParseOptions) override
parse the given mathematical markup string.
bool parsingMathEnvironment
used by the parser. indicates whether we are in a math environment
Definition jkqtmathtextlatexparser.h:167
static QSet< int > accentLetters_LenBackslash
lists all lengths of keys in accentLetters that start with a backslash
Definition jkqtmathtextlatexparser.h:182
~JKQTMathTextLatexParser()
class destructor
QString parseSingleString(bool get)
parses a string, i.e. a sequence of text and whitespaces. returns after any other token was found
static QHash< QString, double > big_instructions_family
maps instructions for braces with fixed size, e.g \bigl to the magnification factor of the brace vers...
Definition jkqtmathtextlatexparser.h:188
static QSet< QChar > SingleCharInstructions
single character instructions
Definition jkqtmathtextlatexparser.h:178
static QString tokenType2String(tokenType type)
convert a tokenType into a string, e.g. for debugging output
tokenType currentToken
used by the tokenizer. type of the current token
Definition jkqtmathtextlatexparser.h:155
QMap< QString, size_t > lastMatrixLineCommandCount
returns the number of \hline , \hdashline , ... commands in the last parseLatexString() call
Definition jkqtmathtextlatexparser.h:159
JKQTMathTextNode * parseInstruction(bool *_foundError=nullptr, bool *getNew=nullptr)
parses a single instruction (including it's parameters)
JKQTMathTextLatexParser(JKQTMathText *parent=nullptr)
class constructor
static QHash< QString, QChar > accentLetters
maps instructions for accents in text-mode (e.g. \'a or \ae ) to the corresponding unicode character
Definition jkqtmathtextlatexparser.h:180
tokenType getToken()
tokenizer for the LaTeX parser
QString readUntil(bool get, const QString &endsequence, bool removeFirstlineWhitespace=false)
read all text without tokenizing, until the sequence endsequence is found.
JKQTMathTextNode * parseMath(bool get)
parse a LaTeX math environment
static void initStaticStructures()
fills all static data structures, if they are still empty
static QSet< QChar > mathEnvironmentSpecialChars
characters that require special treatment in math mode
Definition jkqtmathtextlatexparser.h:174
static QSet< QChar > TokenCharacters
characters that initiate a new token
Definition jkqtmathtextlatexparser.h:172
bool parsinginMathTextStyle
used by the parser. indicates whether to use textstyle or displaystyle in math-mode
Definition jkqtmathtextlatexparser.h:169
static QSet< QChar > mathEnvironmentSpecialEndChars
characters that require special treatment in math mode
Definition jkqtmathtextlatexparser.h:176
QString parseString
used by the tokenizer. The string to be parsed
Definition jkqtmathtextlatexparser.h:165
QString currentTokenName
used by the tokenizer. Name of the current token, id applicable
Definition jkqtmathtextlatexparser.h:161
static QSet< QString > mathEnvironmentSpecialText
characters that have to be replaced by the correspcoting JKQTMathTextSymbolNode in math mode
Definition jkqtmathtextlatexparser.h:186
subclass representing one node in the syntax tree
Definition jkqtmathtextnode.h:37
base class for all math markup parsers used by JKQTMathText
Definition jkqtmathtextparser.h:43
subclass representing a list of nodes in the syntax tree, layed out vertically
Definition jkqtmathtextverticallistnode.h:50
#define JKQTMATHTEXT_LIB_EXPORT
Definition jkqtmathtext_imexport.h:108
JKQTMathTextVerticalOrientation
type of ffractions represented by JKQTMathTextFracNode
Definition jkqtmathtexttools.h:682
JKQTMathTextHorizontalAlignment
types of horizontal alignment
Definition jkqtmathtexttools.h:661
JKQTMathTextBraceType
types of available braces
Definition jkqtmathtexttools.h:222
@ MTVOFirstLine
baseline of the whole block is at the baseline of the first line
Definition jkqtmathtexttools.h:684
@ MTHALeft
align left
Definition jkqtmathtexttools.h:662
@ MTBTAny
any bracket, used by JKQTMathText::parseLatexString()
Definition jkqtmathtexttools.h:234