]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
629a440abf32cafbab88c5fb5bc9d8b4aa74d4f3
[lyx.git] / src / mathed / InsetMathDecoration.cpp
1 /**
2  * \file InsetMathDecoration.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathDecoration.h"
15
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathSupport.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
21
22 #include "LaTeXFeatures.h"
23
24 #include "support/debug.h"
25 #include "support/lassert.h"
26
27 #include <ostream>
28
29 using namespace std;
30
31 namespace lyx {
32
33
34 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
35         : InsetMathNest(buf, 1), key_(key)
36 {
37 //      lyxerr << " creating deco " << key->name << endl;
38 }
39
40
41 Inset * InsetMathDecoration::clone() const
42 {
43         return new InsetMathDecoration(*this);
44 }
45
46
47 bool InsetMathDecoration::upper() const
48 {
49         return key_->name.substr(0, 5) != "under";
50 }
51
52
53 bool InsetMathDecoration::isScriptable() const
54 {
55         return
56                         key_->name == "overbrace" ||
57                         key_->name == "underbrace" ||
58                         key_->name == "overleftarrow" ||
59                         key_->name == "overrightarrow" ||
60                         key_->name == "overleftrightarrow" ||
61                         key_->name == "underleftarrow" ||
62                         key_->name == "underrightarrow" ||
63                         key_->name == "underleftrightarrow";
64 }
65
66
67 bool InsetMathDecoration::protect() const
68 {
69         return
70                         key_->name == "overbrace" ||
71                         key_->name == "underbrace" ||
72                         key_->name == "overleftarrow" ||
73                         key_->name == "overrightarrow" ||
74                         key_->name == "overleftrightarrow" ||
75                         key_->name == "underleftarrow" ||
76                         key_->name == "underrightarrow" ||
77                         key_->name == "underleftrightarrow";
78 }
79
80
81 bool InsetMathDecoration::wide() const
82 {
83         return
84                         key_->name == "overline" ||
85                         key_->name == "underline" ||
86                         key_->name == "overbrace" ||
87                         key_->name == "underbrace" ||
88                         key_->name == "overleftarrow" ||
89                         key_->name == "overrightarrow" ||
90                         key_->name == "overleftrightarrow" ||
91                         key_->name == "widehat" ||
92                         key_->name == "widetilde" ||
93                         key_->name == "underleftarrow" ||
94                         key_->name == "underrightarrow" ||
95                         key_->name == "underleftrightarrow";
96 }
97
98
99 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
100 {
101         cell(0).metrics(mi, dim);
102
103         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
104         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
105
106         if (upper()) {
107                 dy_ = -dim.asc - dh_;
108                 dim.asc += dh_ + 1;
109         } else {
110                 dy_ = dim.des + 1;
111                 dim.des += dh_ + 2;
112         }
113
114         metricsMarkers(dim);
115 }
116
117
118 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
119 {
120         cell(0).draw(pi, x + 1, y);
121         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
122         if (wide())
123                 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
124         else
125                 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
126                         y + dy_, dw_, dh_, key_->name);
127         drawMarkers(pi, x, y);
128         setPosCache(pi, x, y);
129 }
130
131
132 void InsetMathDecoration::write(WriteStream & os) const
133 {
134         MathEnsurer ensurer(os);
135         if (os.fragile() && protect())
136                 os << "\\protect";
137         os << '\\' << key_->name << '{' << cell(0) << '}';
138 }
139
140
141 void InsetMathDecoration::normalize(NormalStream & os) const
142 {
143         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
144 }
145
146
147 void InsetMathDecoration::infoize(odocstream & os) const
148 {
149         os << "Deco: " << key_->name;
150 }
151
152
153 void InsetMathDecoration::validate(LaTeXFeatures & features) const
154 {
155         if (!key_->requires.empty())
156                 features.require(to_utf8(key_->requires));
157         InsetMathNest::validate(features);
158 }
159
160 namespace {
161         struct Attributes {
162                 Attributes() {}
163                 Attributes(bool o, string t)
164                         : over(o), tag(t) {}
165                 bool over;
166                 string tag;
167         };
168
169         typedef map<string, Attributes> Translator;
170
171         void buildTranslator(Translator & t) {
172                 // the decorations we need to support are listed in lib/symbols
173                 t["acute"] = Attributes(true, "&acute;");
174                 t["bar"]   = Attributes(true, "&OverBar;");
175                 t["breve"] = Attributes(true, "&breve;");
176                 t["check"] = Attributes(true, "&caron;");
177                 t["ddddot"] = Attributes(true, "&DotDot;");
178                 t["dddot"] = Attributes(true, "&TripleDot;");
179                 t["ddot"] = Attributes(true, "&Dot;");
180                 t["dot"] = Attributes(true, "&dot;");
181                 t["grave"] = Attributes(true, "&grave;");
182                 t["hat"] = Attributes(true, "&circ;");
183                 t["mathring"] = Attributes(true, "&ring;");
184                 t["overbrace"] = Attributes(true, "&OverBrace;");
185                 t["overleftarrow"] = Attributes(true, "&xlarr;");
186                 t["overleftrightarrow"] = Attributes(true, "&xharr;");
187                 t["overrightarrow"] = Attributes(true, "&xrarr;");
188                 t["tilde"] = Attributes(true, "&tilde;");
189                 t["underbar"] = Attributes(false, "&UnderBar;");
190                 t["underbrace"] = Attributes(false, "&UnderBrace;");
191                 t["underleftarrow"] = Attributes(false, "&xlarr;");
192                 t["underleftrightarrow"] = Attributes(false, "&xharr;");
193                 t["underline"] = Attributes(false, "&;");
194                 t["underrightarrow"] = Attributes(false, "&xrarr;");
195                 t["vec"] = Attributes(true, "&rarr;");
196                 t["widehat"] = Attributes(true, "&Hat;");
197                 t["widetilde"] = Attributes(true, "&Tilde;");
198         }
199
200         Translator const & translator() {
201                 static Translator t;
202                 if (t.empty())
203                         buildTranslator(t);
204                 return t;
205         }
206 }
207
208 void InsetMathDecoration::mathmlize(MathStream & os) const
209 {
210         Translator const & t = translator();
211         Translator::const_iterator cur = t.find(to_utf8(key_->name));
212         LASSERT(cur != t.end(), return);
213         char const * const outag = cur->second.over ? "mover" : "munder";
214         os << MTag(outag)
215                  << MTag("mrow") << cell(0) << ETag("mrow")
216                  << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
217                  << ETag(outag);
218 }
219
220
221 } // namespace lyx