2 * \file InsetMathColor.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "InsetMathColor.h"
16 #include "LaTeXFeatures.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
27 using namespace lyx::support;
31 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle, ColorCode color)
32 : InsetMathNest(buf, 1), oldstyle_(oldstyle),
33 color_(from_utf8(lcolor.getLaTeXName(color)))
37 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle,
38 docstring const & color)
39 : InsetMathNest(buf, 1), oldstyle_(oldstyle), color_(color)
43 Inset * InsetMathColor::clone() const
45 return new InsetMathColor(*this);
49 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
51 cell(0).metrics(mi, dim);
55 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
57 ColorCode origcol = pi.base.font.color();
58 pi.base.font.setColor(lcolor.getFromLaTeXName(to_utf8(color_)));
59 cell(0).draw(pi, x, y);
60 pi.base.font.setColor(origcol);
64 /// color "none" (reset to default) needs special treatment
65 static bool normalcolor(docstring const & color)
67 return color == "none";
71 void InsetMathColor::validate(LaTeXFeatures & features) const
73 InsetMathNest::validate(features);
74 if (!normalcolor(color_)) {
75 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
87 features.require("xcolor");
90 features.require("color");
97 void InsetMathColor::write(TeXMathStream & os) const
99 // We have to ensure correct spacing when the front and/or back
100 // atoms are not ordinary ones (bug 11827).
101 docstring const frontclass = class_to_string(cell(0).firstMathClass());
102 docstring const backclass = class_to_string(cell(0).lastMathClass());
103 bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_);
104 bool adjust_front = frontclass != "mathord" && adjchk;
105 bool adjust_back = backclass != "mathord" && adjchk;
106 docstring const colswitch =
107 oldstyle_ ? from_ascii("{\\color{") + color_ + from_ascii("}")
108 : from_ascii("{\\normalcolor ");
110 if (adjust_front && adjust_back) {
111 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
112 if (cell(0).size() > 2) {
114 for (size_t i = 1; i < cell(0).size() - 1; ++i)
118 if (cell(0).size() > 1)
119 os << '\\' << backclass << colswitch << cell(0).back() << '}';
120 } else if (adjust_front) {
121 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
122 if (cell(0).size() > 1) {
124 for (size_t i = 1; i < cell(0).size(); ++i)
128 } else if (adjust_back) {
130 for (size_t i = 0; i < cell(0).size() - 1; ++i)
132 os << '}' << '\\' << backclass << colswitch << cell(0).back()
134 } else if (normalcolor(color_))
135 // reset to default color inside another color inset
136 os << "{\\normalcolor " << cell(0) << '}';
138 os << "{\\color{" << color_ << '}' << cell(0) << '}';
140 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
144 void InsetMathColor::normalize(NormalStream & os) const
146 os << "[color " << color_ << ' ' << cell(0) << ']';
150 void InsetMathColor::infoize(odocstream & os) const
152 os << bformat(_("Color: %1$s"), color_);