]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMath.cpp
e072b487a0ec128ea12520292f7c9673af4fbf75
[lyx.git] / src / mathed / InsetMath.cpp
1 /**
2  * \file InsetMath.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 "InsetMath.h"
15 #include "MathData.h"
16 #include "MathRow.h"
17 #include "MathStream.h"
18
19 #include "MetricsInfo.h"
20
21 #include "support/debug.h"
22 #include "support/docstream.h"
23 #include "support/gettext.h"
24 #include "support/lassert.h"
25 #include "support/lstrings.h"
26 #include "support/textutils.h"
27
28
29 using namespace std;
30
31 namespace lyx {
32
33 HullType hullType(docstring const & name)
34 {
35         if (name == "none")      return hullNone;
36         if (name == "simple")    return hullSimple;
37         if (name == "equation")  return hullEquation;
38         if (name == "eqnarray")  return hullEqnArray;
39         if (name == "align")     return hullAlign;
40         if (name == "alignat")   return hullAlignAt;
41         if (name == "xalignat")  return hullXAlignAt;
42         if (name == "xxalignat") return hullXXAlignAt;
43         if (name == "multline")  return hullMultline;
44         if (name == "gather")    return hullGather;
45         if (name == "flalign")   return hullFlAlign;
46         if (name == "regexp")    return hullRegexp;
47         lyxerr << "unknown hull type '" << to_utf8(name) << "'" << endl;
48         return hullUnknown;
49 }
50
51
52 docstring hullName(HullType type)
53 {
54         switch (type) {
55         case hullNone:       return from_ascii("none");
56         case hullSimple:     return from_ascii("simple");
57         case hullEquation:   return from_ascii("equation");
58         case hullEqnArray:   return from_ascii("eqnarray");
59         case hullAlign:      return from_ascii("align");
60         case hullAlignAt:    return from_ascii("alignat");
61         case hullXAlignAt:   return from_ascii("xalignat");
62         case hullXXAlignAt:  return from_ascii("xxalignat");
63         case hullMultline:   return from_ascii("multline");
64         case hullGather:     return from_ascii("gather");
65         case hullFlAlign:    return from_ascii("flalign");
66         case hullRegexp:     return from_ascii("regexp");
67         case hullUnknown:
68                 lyxerr << "unknown hull type" << endl;
69                 break;
70         }
71         return from_ascii("none");
72 }
73
74
75 docstring InsetMath::name() const
76 {
77         return from_utf8("Unknown");
78 }
79
80
81 MathData & InsetMath::cell(idx_type)
82 {
83         static MathData dummyCell(&buffer());
84         LYXERR0("I don't have any cell");
85         return dummyCell;
86 }
87
88
89 MathData const & InsetMath::cell(idx_type) const
90 {
91         static MathData dummyCell;
92         LYXERR0("I don't have any cell");
93         return dummyCell;
94 }
95
96
97 marker_type InsetMath::marker(BufferView const *) const
98 {
99         return nargs() > 0 ? marker_type::MARKER : marker_type::NO_MARKER;
100 }
101
102
103 bool InsetMath::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
104 {
105         MathRow::Element e(mi, MathRow::INSET, mathClass());
106         e.inset = this;
107         e.marker = mi.base.macro_nesting ? marker_type::NO_MARKER : marker(mi.base.bv);
108         mrow.push_back(e);
109         return true;
110 }
111
112
113 /// write LaTeX and LyX code
114 void InsetMath::writeLimits(TeXMathStream & os) const
115 {
116         if (limits() == LIMITS) {
117                 os << "\\limits";
118                 os.pendingSpace(true);
119         } else if (limits() == NO_LIMITS) {
120                 os << "\\nolimits";
121                 os.pendingSpace(true);
122         }
123 }
124
125
126 void InsetMath::dump() const
127 {
128         lyxerr << "---------------------------------------------" << endl;
129         odocstringstream os;
130         otexrowstream ots(os);
131         TeXMathStream wi(ots, false, true, TeXMathStream::wsDefault);
132         write(wi);
133         lyxerr << to_utf8(os.str());
134         lyxerr << "\n---------------------------------------------" << endl;
135 }
136
137
138 void InsetMath::metricsT(TextMetricsInfo const &, Dimension &) const
139 {
140         LYXERR0("InsetMath::metricsT(Text) called directly!");
141 }
142
143
144 void InsetMath::drawT(TextPainter &, int, int) const
145 {
146         LYXERR0("InsetMath::drawT(Text) called directly!");
147 }
148
149
150 void InsetMath::write(TeXMathStream & os) const
151 {
152         MathEnsurer ensurer(os);
153         docstring const s = name();
154         os << "\\" << s;
155         // We need an extra ' ' unless this is a single-char-non-ASCII name
156         // or anything non-ASCII follows
157         if (s.size() != 1 || isAlphaASCII(s[0]))
158                 os.pendingSpace(true);
159 }
160
161
162 int InsetMath::plaintext(odocstringstream &,
163         OutputParams const &, size_t) const
164 {
165         // all math plain text output shall take place in InsetMathHull
166         LATTEST(false);
167         return 0;
168 }
169
170
171 void InsetMath::normalize(NormalStream & os) const
172 {
173         os << '[' << name() << "] ";
174 }
175
176
177 void InsetMath::octave(OctaveStream & os) const
178 {
179         NormalStream ns(os.os());
180         normalize(ns);
181 }
182
183
184 void InsetMath::maple(MapleStream & os) const
185 {
186         NormalStream ns(os.os());
187         normalize(ns);
188 }
189
190
191 void InsetMath::maxima(MaximaStream & os) const
192 {
193         MapleStream ns(os.os());
194         maple(ns);
195 }
196
197
198 void InsetMath::mathematica(MathematicaStream & os) const
199 {
200         NormalStream ns(os.os());
201         normalize(ns);
202 }
203
204
205 void InsetMath::mathmlize(MathMLStream & ms) const
206 {
207         ms << "<!-- " << from_utf8(insetName(lyxCode())) << " -->";
208         ms << MTagInline("mi");
209         NormalStream ns(ms.os());
210         normalize(ns);
211         ms << ETagInline("mi");
212 }
213
214
215 void InsetMath::htmlize(HtmlStream & os) const
216 {
217         os << "<!-- " << from_utf8(insetName(lyxCode())) << " -->";
218         os << MTag("span", "style='color: red;'");
219         NormalStream ns(os.os());
220         normalize(ns);
221         os << ETag("span");
222 }
223
224
225 HullType InsetMath::getType() const
226 {
227         return hullNone;
228 }
229
230
231 ostream & operator<<(ostream & os, MathAtom const & at)
232 {
233         odocstringstream oss;
234         otexrowstream ots(oss);
235         TeXMathStream wi(ots, false, false, TeXMathStream::wsDefault);
236         at->write(wi);
237         return os << to_utf8(oss.str());
238 }
239
240
241 odocstream & operator<<(odocstream & os, MathAtom const & at)
242 {
243         otexrowstream ots(os);
244         TeXMathStream wi(ots, false, false, TeXMathStream::wsDefault);
245         at->write(wi);
246         return os;
247 }
248
249
250 } // namespace lyx