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))),
34 current_mode_(UNDECIDED_MODE)
38 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle,
39 docstring const & color)
40 : InsetMathNest(buf, 1), oldstyle_(oldstyle), color_(color),
41 current_mode_(UNDECIDED_MODE)
45 Inset * InsetMathColor::clone() const
47 return new InsetMathColor(*this);
51 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
53 current_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
54 Changer dummy = mi.base.changeEnsureMath(current_mode_);
56 cell(0).metrics(mi, dim);
60 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
62 current_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
63 Changer dummy = pi.base.changeEnsureMath(current_mode_);
65 ColorCode origcol = pi.base.font.color();
66 pi.base.font.setColor(lcolor.getFromLaTeXName(to_utf8(color_)));
67 cell(0).draw(pi, x, y);
68 pi.base.font.setColor(origcol);
72 /// color "none" (reset to default) needs special treatment
73 static bool normalcolor(docstring const & color)
75 return color == "none";
79 void InsetMathColor::validate(LaTeXFeatures & features) const
81 InsetMathNest::validate(features);
82 if (!normalcolor(color_)) {
83 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
95 features.require("xcolor");
98 features.require("color");
105 void InsetMathColor::write(TeXMathStream & os) const
107 // We have to ensure correct spacing when the front and/or back
108 // atoms are not ordinary ones (bug 11827).
109 docstring const frontclass = class_to_string(cell(0).firstMathClass());
110 docstring const backclass = class_to_string(cell(0).lastMathClass());
111 bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_);
112 bool adjust_front = frontclass != "mathord" && adjchk;
113 bool adjust_back = backclass != "mathord" && adjchk;
114 docstring const colswitch = normalcolor(color_)
115 ? from_ascii("{\\normalcolor ")
116 : from_ascii("{\\color{") + color_ + from_ascii("}");
118 if (adjust_front && adjust_back) {
119 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
120 if (cell(0).size() > 2) {
122 for (size_t i = 1; i < cell(0).size() - 1; ++i)
126 if (cell(0).size() > 1)
127 os << '\\' << backclass << colswitch << cell(0).back() << '}';
128 } else if (adjust_front) {
129 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
130 if (cell(0).size() > 1) {
132 for (size_t i = 1; i < cell(0).size(); ++i)
136 } else if (adjust_back) {
138 for (size_t i = 0; i < cell(0).size() - 1; ++i)
140 os << '}' << '\\' << backclass << colswitch << cell(0).back()
142 } else if (normalcolor(color_))
143 // reset to default color inside another color inset
144 os << "{\\normalcolor " << cell(0) << '}';
146 os << "{\\color{" << color_ << '}' << cell(0) << '}';
148 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
152 void InsetMathColor::normalize(NormalStream & os) const
154 os << "[color " << color_ << ' ' << cell(0) << ']';
158 void InsetMathColor::infoize(odocstream & os) const
160 os << bformat(_("Color: %1$s"), color_);