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 
InsetMathColor(Buffer * buf,bool oldstyle,ColorCode color)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 
InsetMathColor(Buffer * buf,bool oldstyle,docstring const & color)37 InsetMathColor::InsetMathColor(Buffer * buf, bool oldstyle,
38 		docstring const & color)
39 	: InsetMathNest(buf, 1), oldstyle_(oldstyle), color_(color)
40 {}
41 
42 
clone() const43 Inset * InsetMathColor::clone() const
44 {
45 	return new InsetMathColor(*this);
46 }
47 
48 
metrics(MetricsInfo & mi,Dimension & dim) const49 void InsetMathColor::metrics(MetricsInfo & mi, Dimension & dim) const
50 {
51 	cell(0).metrics(mi, dim);
52 }
53 
54 
draw(PainterInfo & pi,int x,int y) const55 void InsetMathColor::draw(PainterInfo & pi, int x, int y) const
56 {
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);
61 }
62 
63 
64 /// color "none" (reset to default) needs special treatment
normalcolor(docstring const & color)65 static bool normalcolor(docstring const & color)
66 {
67 	return color == "none";
68 }
69 
70 
validate(LaTeXFeatures & features) const71 void InsetMathColor::validate(LaTeXFeatures & features) const
72 {
73 	InsetMathNest::validate(features);
74 	if (!normalcolor(color_)) {
75 		switch (lcolor.getFromLaTeXName(to_utf8(color_))) {
76 			case Color_brown:
77 			case Color_darkgray:
78 			case Color_gray:
79 			case Color_lightgray:
80 			case Color_lime:
81 			case Color_olive:
82 			case Color_orange:
83 			case Color_pink:
84 			case Color_purple:
85 			case Color_teal:
86 			case Color_violet:
87 				features.require("xcolor");
88 				break;
89 			default:
90 				features.require("color");
91 				break;
92 		}
93 	}
94 }
95 
96 
write(WriteStream & os) const97 void InsetMathColor::write(WriteStream & os) const
98 {
99 	if (normalcolor(color_))
100 		// reset to default color inside another color inset
101 		os << "{\\normalcolor " << cell(0) << '}';
102 	else if (oldstyle_)
103 		os << "{\\color{" << color_ << '}' << cell(0) << '}';
104 	else
105 		os << "\\textcolor{" << color_ << "}{" << cell(0) << '}';
106 }
107 
108 
normalize(NormalStream & os) const109 void InsetMathColor::normalize(NormalStream & os) const
110 {
111 	os << "[color " << color_ << ' ' << cell(0) << ']';
112 }
113 
114 
infoize(odocstream & os) const115 void InsetMathColor::infoize(odocstream & os) const
116 {
117 	os << bformat(_("Color: %1$s"), color_);
118 }
119 
120 
121 } // namespace lyx
122