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(WriteStream & 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 =
102 cell(0).size() ? class_to_string(cell(0).front()->mathClass())
103 : from_ascii("mathord");
104 docstring const backclass =
105 cell(0).size() ? class_to_string(cell(0).back()->mathClass())
106 : from_ascii("mathord");
107 bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_);
108 bool adjust_front = frontclass != "mathord" && adjchk;
109 bool adjust_back = backclass != "mathord" && adjchk;
110 docstring const colswitch =
111 oldstyle_ ? from_ascii("{\\color{") + color_ + from_ascii("}")
112 : from_ascii("{\\normalcolor ");
114 if (adjust_front && adjust_back) {
115 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
116 if (cell(0).size() > 2) {
118 for (size_t i = 1; i < cell(0).size() - 1; ++i)
122 if (cell(0).size() > 1)
123 os << '\\' << backclass << colswitch << cell(0).back() << '}';
124 } else if (adjust_front) {
125 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
126 if (cell(0).size() > 1) {
128 for (size_t i = 1; i < cell(0).size(); ++i)
132 } else if (adjust_back) {
134 for (size_t i = 0; i < cell(0).size() - 1; ++i)
136 os << '}' << '\\' << backclass << colswitch << cell(0).back()
138 } else if (normalcolor(color_))
139 // reset to default color inside another color inset
140 os << "{\\normalcolor " << cell(0) << '}';
142 os << "{\\color{" << color_ << '}' << cell(0) << '}';
144 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
148 void InsetMathColor::normalize(NormalStream & os) const
150 os << "[color " << color_ << ' ' << cell(0) << ']';
154 void InsetMathColor::infoize(odocstream & os) const
156 os << bformat(_("Color: %1$s"), color_);