]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathColor.cpp
be2c0f881aec07ebad4de0803d565dda71f4f502
[features.git] / src / mathed / InsetMathColor.cpp
1 /**
2  * \file InsetMathColor.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ColorSet.h"
14
15 #include "InsetMathColor.h"
16 #include "LaTeXFeatures.h"
17 #include "MathData.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
21
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24
25 #include <ostream>
26
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle, ColorCode color)
32         : InsetMathNest(buf, 1), oldstyle_(oldstyle),
33           color_(from_utf8(lcolor.getLaTeXName(color)))
34 {}
35
36
37 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle,
38                 docstring const & color)
39         : InsetMathNest(buf, 1), oldstyle_(oldstyle), color_(color),
40           current_mode_(UNDECIDED_MODE)
41 {}
42
43
44 Inset * InsetMathColor::clone() const
45 {
46         return new InsetMathColor(*this);
47 }
48
49
50 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
51 {
52         current_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
53         Changer dummy = mi.base.changeEnsureMath(current_mode_);
54
55         cell(0).metrics(mi, dim);
56 }
57
58
59 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
60 {
61         current_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
62         Changer dummy = pi.base.changeEnsureMath(current_mode_);
63
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);
68 }
69
70
71 /// color "none" (reset to default) needs special treatment
72 static bool normalcolor(docstring const & color)
73 {
74         return color == "none";
75 }
76
77
78 void InsetMathColor::validate(LaTeXFeatures & features) const
79 {
80         InsetMathNest::validate(features);
81         if (!normalcolor(color_)) {
82                 switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
83                         case Color_brown:
84                         case Color_darkgray:
85                         case Color_gray:
86                         case Color_lightgray:
87                         case Color_lime:
88                         case Color_olive:
89                         case Color_orange:
90                         case Color_pink:
91                         case Color_purple:
92                         case Color_teal:
93                         case Color_violet:
94                                 features.require("xcolor");
95                                 break;
96                         default:
97                                 features.require("color");
98                                 break;
99                 }
100         }
101 }
102
103
104 void InsetMathColor::write(TeXMathStream & os) const
105 {
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 ");
116
117         if (adjust_front && adjust_back) {
118                 os << '\\' << frontclass << colswitch << cell(0).front() << '}';
119                 if (cell(0).size() > 2) {
120                         os << colswitch;
121                         for (size_t i = 1; i < cell(0).size() - 1; ++i)
122                                 os << cell(0)[i];
123                         os << '}';
124                 }
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) {
130                         os << colswitch;
131                         for (size_t i = 1; i < cell(0).size(); ++i)
132                                 os << cell(0)[i];
133                         os << '}';
134                 }
135         } else if (adjust_back) {
136                 os << colswitch;
137                 for (size_t i = 0; i < cell(0).size() - 1; ++i)
138                         os << cell(0)[i];
139                 os << '}' << '\\' << backclass << colswitch << cell(0).back()
140                    << '}';
141         } else if (normalcolor(color_))
142                 // reset to default color inside another color inset
143                 os << "{\\normalcolor " << cell(0) << '}';
144         else if (oldstyle_)
145                 os << "{\\color{" << color_ << '}' << cell(0) << '}';
146         else
147                 os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
148 }
149
150
151 void InsetMathColor::normalize(NormalStream & os) const
152 {
153         os << "[color " << color_ << ' ' << cell(0) << ']';
154 }
155
156
157 void InsetMathColor::infoize(odocstream & os) const
158 {
159         os << bformat(_("Color: %1$s"), color_);
160 }
161
162
163 } // namespace lyx