From ed8c1361855aafb927e60aab5c4316ca05a38c73 Mon Sep 17 00:00:00 2001 From: Razi Alavizadeh Date: Sat, 8 Dec 2018 16:49:52 +0330 Subject: [PATCH 1/2] MTinstruction1Node: Move repeated code to a function --- lib/jkqtmathtext/jkqtmathtext.cpp | 68 +++++++++---------------------- lib/jkqtmathtext/jkqtmathtext.h | 2 + 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/lib/jkqtmathtext/jkqtmathtext.cpp b/lib/jkqtmathtext/jkqtmathtext.cpp index 25ffdb9bfc..dfea086c48 100644 --- a/lib/jkqtmathtext/jkqtmathtext.cpp +++ b/lib/jkqtmathtext/jkqtmathtext.cpp @@ -458,26 +458,8 @@ QString JKQTmathText::MTinstruction1Node::getTypeName() const void JKQTmathText::MTinstruction1Node::getSizeInternal(QPainter& painter, JKQTmathText::MTenvironment currentEv, double& width, double& baselineHeight, double& overallHeight, double& strikeoutPos) { JKQTmathText::MTenvironment ev=currentEv; - if (name=="bf" || name=="textbf" || name=="mathbf") ev.bold=true; - else if (name=="em") ev.italic=!ev.italic; - else if (name=="it" || name=="textit" || name=="mathit") ev.italic=true; - else if (name=="textcolor" || name=="mathcolor" || name=="color") ev.color=QColor(parameters.value(0, ev.color.name())); - else if (name=="equation") { ev.italic=true; ev.insideMath=true; } - else if (name=="sc" || name=="textsc" || name=="mathsc") ev.smallCaps=true; - else if (name=="ul" || name=="underline" || name=="underlined") ev.underlined=true; - else if (name=="ol" || name=="overline" || name=="overlined") ev.overline=true; - else if (name=="strike") ev.strike=true; - else if (name=="rm" || name=="textrm") { ev.font=JKQTmathText::MTEroman; } - else if (name=="mathrm" || name=="text" || name=="mbox" || name=="operatorname") { ev.font=JKQTmathText::MTEroman; ev.italic=false; } - else if (name=="mat") { ev.font=JKQTmathText::MTEroman; ev.italic=false; ev.bold=true; } - else if (name=="cal" || name=="textcal" || name=="mathcal") { ev.font=JKQTmathText::MTEcaligraphic; } - else if (name=="bb" || name=="textbb" || name=="mathbb") { ev.font=JKQTmathText::MTEblackboard; } - else if (name=="tt" || name=="texttt" || name=="mathtt") { ev.font=JKQTmathText::MTEtypewriter; } - else if (name=="sf" || name=="textsf" || name=="mathsf") { ev.font=JKQTmathText::MTEsans; } - else if (name=="script" || name=="textscript" || name=="mathscript") { ev.font=JKQTmathText::MTEscript; } - else if (name=="displaystyle") { ev.fontSize=ev.fontSize/0.8; } - else if (name=="scriptstyle") { ev.fontSize=ev.fontSize*0.8; } - else if (name=="scriptscriptstyle") { ev.fontSize=ev.fontSize*0.8*0.8; } + + setupMTenvironment(ev); child->getSize(painter, ev, width, baselineHeight, overallHeight, strikeoutPos); if (name=="colorbox" || name=="fbox" || name=="boxed") { @@ -491,27 +473,9 @@ void JKQTmathText::MTinstruction1Node::getSizeInternal(QPainter& painter, JKQTma double JKQTmathText::MTinstruction1Node::draw(QPainter& painter, double x, double y, JKQTmathText::MTenvironment currentEv) { doDrawBoxes(painter, x, y, currentEv); - JKQTmathText::MTenvironment ev=currentEv; - if (name=="bf" || name=="textbf" || name=="mathbf") ev.bold=true; - else if (name=="em") ev.italic=!ev.italic; - else if (name=="it" || name=="textit" || name=="mathit") ev.italic=true; - else if (name=="textcolor" || name=="mathcolor" || name=="color") ev.color=QColor(parameters.value(0, ev.color.name())); - else if (name=="equation") { ev.italic=true; ev.insideMath=true; } - else if (name=="sc" || name=="textsc" || name=="mathsc") ev.smallCaps=true; - else if (name=="ul" || name=="underline" || name=="underlined") ev.underlined=true; - else if (name=="ol" || name=="overline" || name=="overlined") ev.overline=true; - else if (name=="strike") ev.strike=true; - else if (name=="rm" || name=="textrm") { ev.font=JKQTmathText::MTEroman; } - else if (name=="mathrm" || name=="text" || name=="mbox" || name=="operatorname") { ev.font=JKQTmathText::MTEroman; ev.italic=false; } - else if (name=="mat") { ev.font=JKQTmathText::MTEroman; ev.italic=false; ev.bold=true; } - else if (name=="cal" || name=="textcal" || name=="mathcal") { ev.font=JKQTmathText::MTEcaligraphic; } - else if (name=="bb" || name=="textbb" || name=="mathbb") { ev.font=JKQTmathText::MTEblackboard; } - else if (name=="tt" || name=="texttt" || name=="mathtt") { ev.font=JKQTmathText::MTEtypewriter; } - else if (name=="sf" || name=="textsf" || name=="mathsf") { ev.font=JKQTmathText::MTEsans; } - else if (name=="script" || name=="textscript" || name=="mathscript") { ev.font=JKQTmathText::MTEscript; } - else if (name=="displaystyle") { ev.fontSize=ev.fontSize/0.8; } - else if (name=="scriptstyle") { ev.fontSize=ev.fontSize*0.8; } - else if (name=="scriptscriptstyle") { ev.fontSize=ev.fontSize*0.8*0.8; } + JKQTmathText::MTenvironment ev=currentEv; + + setupMTenvironment(ev); QPen oldPen=painter.pen(); double shiftX=0; @@ -537,6 +501,20 @@ double JKQTmathText::MTinstruction1Node::draw(QPainter& painter, double x, doubl bool JKQTmathText::MTinstruction1Node::toHtml(QString &html, JKQTmathText::MTenvironment currentEv, JKQTmathText::MTenvironment defaultEv) { JKQTmathText::MTenvironment ev=currentEv; + + setupMTenvironment(ev); + + return child->toHtml(html, ev, defaultEv); +} + +void JKQTmathText::MTinstruction1Node::set_drawBoxes(bool draw) +{ + drawBoxes=draw; + child->set_drawBoxes(draw); +} + +void JKQTmathText::MTinstruction1Node::setupMTenvironment(JKQTmathText::MTenvironment &ev) +{ if (name=="bf" || name=="textbf" || name=="mathbf") ev.bold=true; else if (name=="em") ev.italic=!ev.italic; else if (name=="it" || name=="textit" || name=="mathit") ev.italic=true; @@ -557,14 +535,6 @@ bool JKQTmathText::MTinstruction1Node::toHtml(QString &html, JKQTmathText::MTenv else if (name=="displaystyle") { ev.fontSize=ev.fontSize/0.8; } else if (name=="scriptstyle") { ev.fontSize=ev.fontSize*0.8; } else if (name=="scriptscriptstyle") { ev.fontSize=ev.fontSize*0.8*0.8; } - - return child->toHtml(html, ev, defaultEv); -} - -void JKQTmathText::MTinstruction1Node::set_drawBoxes(bool draw) -{ - drawBoxes=draw; - child->set_drawBoxes(draw); } diff --git a/lib/jkqtmathtext/jkqtmathtext.h b/lib/jkqtmathtext/jkqtmathtext.h index 5b96e905bc..84719d8fd4 100644 --- a/lib/jkqtmathtext/jkqtmathtext.h +++ b/lib/jkqtmathtext/jkqtmathtext.h @@ -388,6 +388,8 @@ class LIB_EXPORT JKQTmathText : public QObject { JKQTPGET_MACRO(QString, name) JKQTPGET_MACRO(QStringList, parameters) protected: + void setupMTenvironment(JKQTmathText::MTenvironment &ev); + MTnode* child; QString name; QStringList parameters; From 4578b66de8154e0242b56ae6ae32dc31138b26a2 Mon Sep 17 00:00:00 2001 From: Razi Alavizadeh Date: Wed, 12 Dec 2018 22:53:06 +0330 Subject: [PATCH 2/2] Add unknown symbols and instructions to error_list --- lib/jkqtmathtext/jkqtmathtext.cpp | 27 +++++++++++++++++++++++---- lib/jkqtmathtext/jkqtmathtext.h | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/jkqtmathtext/jkqtmathtext.cpp b/lib/jkqtmathtext/jkqtmathtext.cpp index dfea086c48..e74df6871b 100644 --- a/lib/jkqtmathtext/jkqtmathtext.cpp +++ b/lib/jkqtmathtext/jkqtmathtext.cpp @@ -445,6 +445,10 @@ JKQTmathText::MTinstruction1Node::MTinstruction1Node(JKQTmathText* parent, QStri this->name=name; this->child=child; this->parameters=parameters; + + if (!setupMTenvironment()) { + parent->error_list.append(tr("unknown instruction '%1' found!").arg(name)); + } } JKQTmathText::MTinstruction1Node::~MTinstruction1Node() { @@ -513,7 +517,7 @@ void JKQTmathText::MTinstruction1Node::set_drawBoxes(bool draw) child->set_drawBoxes(draw); } -void JKQTmathText::MTinstruction1Node::setupMTenvironment(JKQTmathText::MTenvironment &ev) +bool JKQTmathText::MTinstruction1Node::setupMTenvironment(JKQTmathText::MTenvironment &ev) { if (name=="bf" || name=="textbf" || name=="mathbf") ev.bold=true; else if (name=="em") ev.italic=!ev.italic; @@ -535,13 +539,16 @@ void JKQTmathText::MTinstruction1Node::setupMTenvironment(JKQTmathText::MTenviro else if (name=="displaystyle") { ev.fontSize=ev.fontSize/0.8; } else if (name=="scriptstyle") { ev.fontSize=ev.fontSize*0.8; } else if (name=="scriptscriptstyle") { ev.fontSize=ev.fontSize*0.8*0.8; } + else { + return false; + } + + return true; } - - JKQTmathText::MTsubscriptNode::MTsubscriptNode(JKQTmathText* parent, MTnode* child): JKQTmathText::MTnode(parent) { @@ -2683,6 +2690,19 @@ JKQTmathText::MTsymbolNode::MTsymbolNode(JKQTmathText* parent, QString name, boo } } if (addWhitespace) symbol=symbol+" "; + + static QSet extraSymbolName = { + "infty", + "|", " ", "quad", ";", ":", ",", "!", + "longleftarrow", "longrightarrow", + "Longleftarrow", "Longrightarrow", + "longleftrightarrow", "Longleftrightarrow" + }; + + if (symbol.simplified().isEmpty() && !extraSymbolName.contains(n)) { + parent->error_list.append(tr("unknown symbol '%1' found!").arg(n)); + } + //std::cout<<"symbol node '"<getTBR(f, "M", painter.device()); painter.drawRect(QRectF(x+shift,y-tbr.height(), xwi, tbr.height()*0.8)); - parent->error_list<<"unknown symbol '"<