]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
preview as preview can...
[lyx.git] / src / mathed / math_scriptinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
10 #include "debug.h"
11
12
13 using std::max;
14
15
16 MathScriptInset::MathScriptInset()
17         : MathNestInset(2), limits_(0)
18 {
19         script_[0] = false;
20         script_[1] = false;
21 }
22
23
24 MathScriptInset::MathScriptInset(bool up)
25         : MathNestInset(2), limits_(0)
26 {
27         script_[0] = !up;
28         script_[1] = up;
29 }
30
31
32 MathInset * MathScriptInset::clone() const
33 {
34         return new MathScriptInset(*this);
35 }
36
37
38 MathScriptInset const * MathScriptInset::asScriptInset() const
39 {
40         return this;
41 }
42
43
44 MathScriptInset * MathScriptInset::asScriptInset()
45 {
46         return this;
47 }
48
49
50 MathXArray const & MathScriptInset::up() const
51 {
52         return xcell(1);
53 }
54
55
56 MathXArray const & MathScriptInset::down() const
57 {
58         return xcell(0);
59 }
60
61
62 MathXArray & MathScriptInset::up()
63 {
64         return xcell(1);
65 }
66
67
68 MathXArray & MathScriptInset::down()
69 {
70         return xcell(0);
71 }
72
73
74 void MathScriptInset::ensure(bool up)
75 {
76         script_[up] = true;
77 }
78
79
80 int MathScriptInset::dy0(MathInset const * nuc) const
81 {
82         int nd = ndes(nuc);
83         if (!hasDown())
84                 return nd;
85         int des = down().ascent();
86         if (hasLimits(nuc))
87                 des += nd + 2;
88         else
89                 des = max(des, nd);
90         return des;
91 }
92
93
94 int MathScriptInset::dy1(MathInset const * nuc) const
95 {
96         int na = nasc(nuc);
97         if (!hasUp())
98                 return na;
99         int asc = up().descent();
100         if (hasLimits(nuc))
101                 asc += na + 2;
102         else
103                 asc = max(asc, na);
104         asc = max(asc, mathed_char_ascent(font_, 'I'));
105         return asc;
106 }
107
108
109 int MathScriptInset::dx0(MathInset const * nuc) const
110 {
111         lyx::Assert(hasDown());
112         return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
113 }
114
115
116 int MathScriptInset::dx1(MathInset const * nuc) const
117 {
118         lyx::Assert(hasUp());
119         return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
120 }
121
122
123 int MathScriptInset::dxx(MathInset const * nuc) const
124 {
125         //lyx::Assert(nuc());
126         return hasLimits(nuc)  ?  (width2(nuc) - nwid(nuc)) / 2  :  0;
127 }
128
129
130 int MathScriptInset::ascent2(MathInset const * nuc) const
131 {
132         return dy1(nuc) + (hasUp() ? up().ascent() : 0);
133 }
134
135
136 int MathScriptInset::descent2(MathInset const * nuc) const
137 {
138         return dy0(nuc) + (hasDown() ? down().descent() : 0);
139 }
140
141
142 int MathScriptInset::width2(MathInset const * nuc) const
143 {
144         int wid = 0;
145         if (hasLimits(nuc)) {
146                 wid = nwid(nuc);
147                 if (hasUp())
148                         wid = max(wid, up().width());
149                 if (hasDown())
150                         wid = max(wid, down().width());
151         } else {
152                 if (hasUp())
153                         wid = max(wid, up().width());
154                 if (hasDown())
155                         wid = max(wid, down().width());
156                 wid += nwid(nuc);
157         }
158         return wid;
159 }
160
161
162 int MathScriptInset::nwid(MathInset const * nuc) const
163 {
164         return nuc ?  nuc->width() : mathed_char_width(font_, '.');
165 }
166
167
168 int MathScriptInset::nasc(MathInset const * nuc) const
169 {
170         return nuc ? nuc->ascent() : mathed_char_ascent(font_, 'I');
171 }
172
173
174 int MathScriptInset::ndes(MathInset const * nuc) const
175 {
176         return nuc ? nuc->descent() : mathed_char_descent(font_, 'I');
177 }
178
179
180 void MathScriptInset::metrics(MathMetricsInfo & mi) const
181 {
182         metrics(0, mi);
183 }
184
185
186 void MathScriptInset::metrics(MathInset const * nuc, MathMetricsInfo & mi) const
187 {
188         if (nuc)
189                 nuc->metrics(mi);
190         MathNestInset::metrics(mi);
191         MathScriptChanger dummy(mi.base);
192         ascent_  = ascent2(nuc);
193         descent_ = descent2(nuc);
194         width_   = width2(nuc);
195 }
196
197
198 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
199 {
200         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
201         draw(0, pi, x, y);
202 }
203
204
205 void MathScriptInset::draw(MathInset const * nuc, MathPainterInfo & pi,
206         int x, int y) const
207 {
208         if (nuc)
209                 nuc->draw(pi, x + dxx(nuc), y);
210         else if (editing())
211                 drawStr(pi, font_, x + dxx(nuc), y, ".");
212
213         MathScriptChanger dummy(pi.base);
214         if (hasUp())
215                 up().draw(pi, x + dx1(nuc), y - dy1(nuc));
216         if (hasDown())
217                 down().draw(pi, x + dx0(nuc), y + dy0(nuc));
218 }
219
220
221 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
222 {
223         metricsT(0, mi);
224 }
225
226
227 void MathScriptInset::metricsT(MathInset const * nuc,
228         TextMetricsInfo const & mi) const
229 {
230         if (hasUp())
231                 up().metricsT(mi);
232         if (hasDown())
233                 down().metricsT(mi);
234         if (nuc)
235                 nuc->metricsT(mi);
236         //ascent_  = ascent2(nuc);
237         //descent_ = descent2(nuc);
238         //width_   = width2(nuc);
239 }
240
241
242 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
243 {
244         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
245         drawT(0, pain, x, y);
246 }
247
248
249 void MathScriptInset::drawT(MathInset const * nuc, TextPainter & pain,
250         int x, int y) const
251 {
252         if (nuc)
253                 nuc->drawT(pain, x + dxx(nuc), y);
254         if (hasUp())
255                 up().drawT(pain, x + dx1(nuc), y - dy1(nuc));
256         if (hasDown())
257                 down().drawT(pain, x + dx0(nuc), y + dy0(nuc));
258 }
259
260
261
262 bool MathScriptInset::hasLimits(MathInset const * nuc) const
263 {
264         // obviuos cases
265         if (limits_ == 1)
266                 return true;
267         if (limits_ == -1)
268                 return false;
269
270         // we can only display limits if the nucleus wants some
271         if (!nuc)
272                 return false;
273         if (!nuc->isScriptable())
274                 return false;
275
276         // per default \int has limits beside the \int even in displayed formulas
277         if (nuc->asSymbolInset())
278                 if (nuc->asSymbolInset()->name().find("int") != string::npos)
279                         return false;
280
281         // assume "real" limits for everything else
282         return true;
283 }
284
285
286 void MathScriptInset::removeEmptyScripts()
287 {
288         for (int i = 0; i <= 1; ++i)
289                 if (script_[i] && cell(i).size() == 0) {
290                         cell(i).clear();
291                         script_[i] = false;
292                 }
293 }
294
295
296 void MathScriptInset::removeScript(bool up)
297 {
298         cell(up).clear();
299         script_[up] = false;
300 }
301
302
303 bool MathScriptInset::has(bool up) const
304 {
305         return script_[up];
306 }
307
308
309 bool MathScriptInset::empty() const
310 {
311         return !script_[0] && !script_[1];
312 }
313
314
315 bool MathScriptInset::hasUp() const
316 {
317         return script_[1];
318 }
319
320
321 bool MathScriptInset::hasDown() const
322 {
323         return script_[0];
324 }
325
326
327 bool MathScriptInset::idxRight(MathInset::idx_type &,
328                                  MathInset::pos_type &) const
329 {
330         return false;
331 }
332
333
334 bool MathScriptInset::idxLeft(MathInset::idx_type &,
335                                 MathInset::pos_type &) const
336 {
337         return false;
338 }
339
340
341 void MathScriptInset::write(WriteStream & os) const
342 {
343         //lyxerr << "unexpected call to MathScriptInset::write()\n";
344         write2(0, os);
345 }
346
347
348 void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
349 {
350         if (nuc) {
351                 os << nuc;
352                 if (nuc->takesLimits()) {
353                         if (limits_ == -1)
354                                 os << "\\nolimits ";
355                         if (limits_ == 1)
356                                 os << "\\limits ";
357                 }
358         } else
359                         if (os.firstitem())
360                                 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
361                         else
362                                 os << "{}";
363
364         if (hasDown() && down().data_.size())
365                 os << "_{" << down().data_ << '}';
366
367         if (hasUp() && up().data_.size())
368                 os << "^{" << up().data_ << '}';
369 }
370
371
372 void MathScriptInset::normalize(NormalStream & os) const
373 {
374         //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
375         normalize2(0, os);
376 }
377
378
379 void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
380 {
381         bool d = hasDown() && down().data_.size();
382         bool u = hasUp() && up().data_.size();
383
384         if (u)
385                 os << "[sup ";
386         if (d)
387                 os << "[sub ";
388
389         if (nuc)
390                 os << nuc << ' ';
391         else
392                 os << "[par]";
393
394         if (d)
395                 os << down().data_ << ']';
396         if (u)
397                 os << up().data_ << ']';
398 }
399
400
401 void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
402 {
403         if (nuc)
404                 os << nuc;
405         if (hasDown() && down().data_.size())
406                 os << '[' << down().data_ << ']';
407         if (hasUp() && up().data_.size())
408                 os << "^(" << up().data_ << ')';
409 }
410
411
412 void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
413 {
414         bool d = hasDown() && down().data_.size();
415         bool u = hasUp() && up().data_.size();
416
417         if (u && d)
418                 os << MTag("msubsup");
419         else if (u)
420                 os << MTag("msup");
421         else if (d)
422                 os << MTag("msub");
423
424         if (nuc)
425                 os << nuc;
426         else
427                 os << "<mrow/>";
428
429         if (u && d)
430                 os << down().data_ << up().data_ << ETag("msubsup");
431         else if (u)
432                 os << up().data_ << ETag("msup");
433         else if (d)
434                 os << down().data_ << ETag("msub");
435 }
436
437
438 void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
439 {
440         if (nuc)
441                 os << nuc;
442         if (hasDown() && down().data_.size())
443                 os << '[' << down().data_ << ']';
444         if (hasUp() && up().data_.size())
445                 os << "^(" << up().data_ << ')';
446 }