From c3114515180de263597be65cc9b8724798278b7f Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Mon, 8 Feb 2021 13:13:21 +0100 Subject: [PATCH] Fix math class code in InsetMathColor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current code is too naïve and asserts on MC_UNKNOWN class. See discussion here: https://marc.info/?l=lyx-devel&m=161267112524889&w=2 --- src/mathed/InsetMathColor.cpp | 8 ++------ src/mathed/MathData.cpp | 11 +++++++++++ src/mathed/MathData.h | 2 ++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/mathed/InsetMathColor.cpp b/src/mathed/InsetMathColor.cpp index f2f9116bf9..13e6fa41e7 100644 --- a/src/mathed/InsetMathColor.cpp +++ b/src/mathed/InsetMathColor.cpp @@ -98,12 +98,8 @@ void InsetMathColor::write(TeXMathStream & os) const { // We have to ensure correct spacing when the front and/or back // atoms are not ordinary ones (bug 11827). - docstring const frontclass = - !cell(0).empty() ? class_to_string(cell(0).front()->mathClass()) - : from_ascii("mathord"); - docstring const backclass = - !cell(0).empty() ? class_to_string(cell(0).back()->mathClass()) - : from_ascii("mathord"); + docstring const frontclass = class_to_string(cell(0).firstMathClass()); + docstring const backclass = class_to_string(cell(0).lastMathClass()); bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_); bool adjust_front = frontclass != "mathord" && adjchk; bool adjust_back = backclass != "mathord" && adjchk; diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp index c9db36c170..d3ce3a31c8 100644 --- a/src/mathed/MathData.cpp +++ b/src/mathed/MathData.cpp @@ -969,6 +969,17 @@ MathClass MathData::mathClass() const } +MathClass MathData::firstMathClass() const +{ + for (MathAtom const & at : *this) { + MathClass mc = at->mathClass(); + if (mc != MC_UNKNOWN) + return mc; + } + return MC_ORD; +} + + MathClass MathData::lastMathClass() const { MathClass res = MC_ORD; diff --git a/src/mathed/MathData.h b/src/mathed/MathData.h index ebc59d477a..865742df4c 100644 --- a/src/mathed/MathData.h +++ b/src/mathed/MathData.h @@ -144,6 +144,8 @@ public: void drawT(TextPainter & pi, int x, int y) const; /// approximate mathclass of the data MathClass mathClass() const; + /// math class of first interesting element + MathClass firstMathClass() const; /// math class of last interesting element MathClass lastMathClass() const; /// is the cell in display style