From 7441172d4d9a26eb4824bb8bee003f457ef34f1c Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Sun, 2 Aug 2020 18:58:40 +0200 Subject: [PATCH] Assure correct spacing of colored items in mathed When coloring a non-ordinary math atom, explicitly apply its math class, so that to leave unchanged the spacing around it. Fixes #11827. --- src/mathed/InsetMath.h | 2 ++ src/mathed/InsetMathClass.h | 4 ++++ src/mathed/InsetMathColor.cpp | 41 ++++++++++++++++++++++++++++++++++- src/mathed/InsetMathNest.cpp | 2 ++ src/mathed/MathStream.cpp | 2 +- src/mathed/MathStream.h | 6 +++++ 6 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/mathed/InsetMath.h b/src/mathed/InsetMath.h index 988d3c358e..067bf6e876 100644 --- a/src/mathed/InsetMath.h +++ b/src/mathed/InsetMath.h @@ -72,6 +72,7 @@ class InsetMathArray; class InsetMathAMSArray; class InsetMathBrace; class InsetMathChar; +class InsetMathClass; class InsetMathDelim; class InsetMathFracBase; class InsetMathFrac; @@ -150,6 +151,7 @@ public: virtual InsetMathBrace * asBraceInset() { return nullptr; } virtual InsetMathBrace const * asBraceInset() const { return nullptr; } virtual InsetMathChar const * asCharInset() const { return nullptr; } + virtual InsetMathClass const * asClassInset() const { return nullptr; } virtual InsetMathDelim * asDelimInset() { return nullptr; } virtual InsetMathDelim const * asDelimInset() const { return nullptr; } virtual InsetMathFracBase * asFracBaseInset() { return nullptr; } diff --git a/src/mathed/InsetMathClass.h b/src/mathed/InsetMathClass.h index f50040d3b4..1b30738f76 100644 --- a/src/mathed/InsetMathClass.h +++ b/src/mathed/InsetMathClass.h @@ -38,6 +38,10 @@ public: void infoize(odocstream & os) const; /// InsetCode lyxCode() const { return MATH_CLASS_CODE; } + /// + InsetMathClass * asClassInset() { return this; } + /// + InsetMathClass const * asClassInset() const { return this; } private: virtual Inset * clone() const; diff --git a/src/mathed/InsetMathColor.cpp b/src/mathed/InsetMathColor.cpp index d665734b51..94a6c42e64 100644 --- a/src/mathed/InsetMathColor.cpp +++ b/src/mathed/InsetMathColor.cpp @@ -96,7 +96,46 @@ void InsetMathColor::validate(LaTeXFeatures & features) const void InsetMathColor::write(WriteStream & os) const { - if (normalcolor(color_)) + // We have to ensure correct spacing when the front and/or back + // atoms are not ordinary ones (bug 11827). + docstring const frontclass = + cell(0).size() ? class_to_string(cell(0).front()->mathClass()) + : from_ascii("mathord"); + docstring const backclass = + cell(0).size() ? class_to_string(cell(0).back()->mathClass()) + : from_ascii("mathord"); + bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_); + bool adjust_front = frontclass != "mathord" && adjchk; + bool adjust_back = backclass != "mathord" && adjchk; + docstring const colswitch = + oldstyle_ ? from_ascii("{\\color{") + color_ + from_ascii("}") + : from_ascii("{\\normalcolor "); + + if (adjust_front && adjust_back) { + os << '\\' << frontclass << colswitch << cell(0).front() << '}'; + if (cell(0).size() > 2) { + os << colswitch; + for (size_t i = 1; i < cell(0).size() - 1; ++i) + os << cell(0)[i]; + os << '}'; + } + if (cell(0).size() > 1) + os << '\\' << backclass << colswitch << cell(0).back() << '}'; + } else if (adjust_front) { + os << '\\' << frontclass << colswitch << cell(0).front() << '}'; + if (cell(0).size() > 1) { + os << colswitch; + for (size_t i = 1; i < cell(0).size(); ++i) + os << cell(0)[i]; + os << '}'; + } + } else if (adjust_back) { + os << colswitch; + for (size_t i = 0; i < cell(0).size() - 1; ++i) + os << cell(0)[i]; + os << '}' << '\\' << backclass << colswitch << cell(0).back() + << '}'; + } else if (normalcolor(color_)) // reset to default color inside another color inset os << "{\\normalcolor " << cell(0) << '}'; else if (oldstyle_) diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 06d1c12e56..be6fecc526 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -330,6 +330,7 @@ void InsetMathNest::write(WriteStream & os) const ModeSpecifier specifier(os, currentMode(), lockedMode()); docstring const latex_name = name(); os << '\\' << latex_name; + os.inMathClass(asClassInset()); for (size_t i = 0; i < nargs(); ++i) { Changer dummy = os.changeRowEntry(TexRow::mathEntry(id(),i)); os << '{' << cell(i) << '}'; @@ -340,6 +341,7 @@ void InsetMathNest::write(WriteStream & os) const os << "\\lyxlock"; os.pendingSpace(true); } + os.inMathClass(false); } diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index 7d355f77b5..f93ff72cf0 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -131,7 +131,7 @@ WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex, output_(output), pendingspace_(false), pendingbrace_(false), textmode_(false), locked_(0), ascii_(0), canbreakline_(true), mathsout_(false), ulemcmd_(NONE), line_(0), encoding_(encoding), - row_entry_(TexRow::row_none) + row_entry_(TexRow::row_none), mathclass_(false) {} diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index bf3deb921d..9344dac497 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -101,6 +101,10 @@ public: void asciiOnly(bool ascii); /// tell whether to use only ascii chars when producing latex code bool asciiOnly() const { return ascii_; } + /// tell whether we are in a MathClass inset + void inMathClass(bool mathclass) { mathclass_ = mathclass; }; + /// tell whether we are in a MathClass inset + bool inMathClass() const { return mathclass_; } /// LaTeX encoding Encoding const * encoding() const { return encoding_; } @@ -142,6 +146,8 @@ private: Encoding const * encoding_; /// Row entry we are in TexRow::RowEntry row_entry_; + /// whether we are in a MathClass inset + bool mathclass_; }; /// -- 2.39.2