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),
40 current_mode_(UNDECIDED_MODE)
44 Inset * InsetMathColor::clone() const
46 return new InsetMathColor(*this);
50 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
52 current_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
53 Changer dummy = mi.base.changeEnsureMath(current_mode_);
55 cell(0).metrics(mi, dim);
59 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
61 current_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
62 Changer dummy = pi.base.changeEnsureMath(current_mode_);
64 ColorCode origcol = pi.base.font.color();
65 pi.base.font.setColor(lcolor.getFromLaTeXName(to_utf8(color_)));
66 cell(0).draw(pi, x, y);
67 pi.base.font.setColor(origcol);
71 /// color "none" (reset to default) needs special treatment
72 static bool normalcolor(docstring const & color)
74 return color == "none";
78 void InsetMathColor::validate(LaTeXFeatures & features) const
80 InsetMathNest::validate(features);
81 if (!normalcolor(color_)) {
82 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
94 features.require("xcolor");
97 features.require("color");
104 void InsetMathColor::write(TeXMathStream & os) const
106 // We have to ensure correct spacing when the front and/or back
107 // atoms are not ordinary ones (bug 11827).
108 docstring const frontclass = class_to_string(cell(0).firstMathClass());
109 docstring const backclass = class_to_string(cell(0).lastMathClass());
110 bool adjchk = os.latex() && !os.inMathClass() && (normalcolor(color_) || oldstyle_);
111 bool adjust_front = frontclass != "mathord" && adjchk;
112 bool adjust_back = backclass != "mathord" && adjchk;
113 docstring const colswitch =
114 oldstyle_ ? from_ascii("{\\color{") + color_ + from_ascii("}")
115 : from_ascii("{\\normalcolor ");
117 if (adjust_front && adjust_back) {
118 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
119 if (cell(0).size() > 2) {
121 for (size_t i = 1; i < cell(0).size() - 1; ++i)
125 if (cell(0).size() > 1)
126 os << '\\' << backclass << colswitch << cell(0).back() << '}';
127 } else if (adjust_front) {
128 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
129 if (cell(0).size() > 1) {
131 for (size_t i = 1; i < cell(0).size(); ++i)
135 } else if (adjust_back) {
137 for (size_t i = 0; i < cell(0).size() - 1; ++i)
139 os << '}' << '\\' << backclass << colswitch << cell(0).back()
141 } else if (normalcolor(color_))
142 // reset to default color inside another color inset
143 os << "{\\normalcolor " << cell(0) << '}';
145 os << "{\\color{" << color_ << '}' << cell(0) << '}';
147 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
151 void InsetMathColor::normalize(NormalStream & os) const
153 os << "[color " << color_ << ' ' << cell(0) << ']';
157 void InsetMathColor::infoize(odocstream & os) const
159 os << bformat(_("Color: %1$s"), color_);