]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
replace (ascent, descent, width) triples by a structure 'dimension'
[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 void MathScriptInset::dimensions2(MathInset const * nuc, Dimension & dim) const
131 {
132         dim.a = dy1(nuc) + (hasUp() ? up().ascent() : 0);
133         dim.d = dy0(nuc) + (hasDown() ? down().descent() : 0);
134         dim.w = width2(nuc);
135 }
136
137
138 int MathScriptInset::width2(MathInset const * nuc) const
139 {
140         int w = 0;
141         if (hasLimits(nuc)) {
142                 w = nwid(nuc);
143                 if (hasUp())
144                         w = max(w, up().width());
145                 if (hasDown())
146                         w = max(w, down().width());
147         } else {
148                 if (hasUp())
149                         w = max(w, up().width());
150                 if (hasDown())
151                         w = max(w, down().width());
152                 w += nwid(nuc);
153         }
154         return w;
155 }
156
157
158 int MathScriptInset::nwid(MathInset const * nuc) const
159 {
160         return nuc ?  nuc->width() : mathed_char_width(font_, '.');
161 }
162
163
164 int MathScriptInset::nasc(MathInset const * nuc) const
165 {
166         return nuc ? nuc->ascent() : mathed_char_ascent(font_, 'I');
167 }
168
169
170 int MathScriptInset::ndes(MathInset const * nuc) const
171 {
172         return nuc ? nuc->descent() : mathed_char_descent(font_, 'I');
173 }
174
175
176 void MathScriptInset::metrics(MathMetricsInfo & mi) const
177 {
178         metrics(0, mi);
179 }
180
181
182 void MathScriptInset::metrics(MathInset const * nuc, MathMetricsInfo & mi) const
183 {
184         if (nuc)
185                 nuc->metrics(mi);
186         MathNestInset::metrics(mi);
187         MathScriptChanger dummy(mi.base);
188         dimensions2(nuc, dim_);
189 }
190
191
192 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
193 {
194         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
195         draw(0, pi, x, y);
196 }
197
198
199 void MathScriptInset::draw(MathInset const * nuc, MathPainterInfo & pi,
200         int x, int y) const
201 {
202         if (nuc)
203                 nuc->draw(pi, x + dxx(nuc), y);
204         else if (editing())
205                 drawStr(pi, font_, x + dxx(nuc), y, ".");
206
207         MathScriptChanger dummy(pi.base);
208         if (hasUp())
209                 up().draw(pi, x + dx1(nuc), y - dy1(nuc));
210         if (hasDown())
211                 down().draw(pi, x + dx0(nuc), y + dy0(nuc));
212 }
213
214
215 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
216 {
217         metricsT(0, mi);
218 }
219
220
221 void MathScriptInset::metricsT(MathInset const * nuc,
222         TextMetricsInfo const & mi) const
223 {
224         if (hasUp())
225                 up().metricsT(mi);
226         if (hasDown())
227                 down().metricsT(mi);
228         if (nuc)
229                 nuc->metricsT(mi);
230         //ascent_  = ascent2(nuc);
231         //descent_ = descent2(nuc);
232         //width_   = width2(nuc);
233 }
234
235
236 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
237 {
238         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
239         drawT(0, pain, x, y);
240 }
241
242
243 void MathScriptInset::drawT(MathInset const * nuc, TextPainter & pain,
244         int x, int y) const
245 {
246         if (nuc)
247                 nuc->drawT(pain, x + dxx(nuc), y);
248         if (hasUp())
249                 up().drawT(pain, x + dx1(nuc), y - dy1(nuc));
250         if (hasDown())
251                 down().drawT(pain, x + dx0(nuc), y + dy0(nuc));
252 }
253
254
255
256 bool MathScriptInset::hasLimits(MathInset const * nuc) const
257 {
258         // obvious cases
259         if (limits_ == 1)
260                 return true;
261         if (limits_ == -1)
262                 return false;
263
264         // we can only display limits if the nucleus wants some
265         if (!nuc)
266                 return false;
267         if (!nuc->isScriptable())
268                 return false;
269
270         // per default \int has limits beside the \int even in displayed formulas
271         if (nuc->asSymbolInset())
272                 if (nuc->asSymbolInset()->name().find("int") != string::npos)
273                         return false;
274
275         // assume "real" limits for everything else
276         return true;
277 }
278
279
280 void MathScriptInset::removeEmptyScripts()
281 {
282         for (int i = 0; i <= 1; ++i)
283                 if (script_[i] && cell(i).size() == 0) {
284                         cell(i).clear();
285                         script_[i] = false;
286                 }
287 }
288
289
290 void MathScriptInset::removeScript(bool up)
291 {
292         cell(up).clear();
293         script_[up] = false;
294 }
295
296
297 bool MathScriptInset::has(bool up) const
298 {
299         return script_[up];
300 }
301
302
303 bool MathScriptInset::empty() const
304 {
305         return !script_[0] && !script_[1];
306 }
307
308
309 bool MathScriptInset::hasUp() const
310 {
311         return script_[1];
312 }
313
314
315 bool MathScriptInset::hasDown() const
316 {
317         return script_[0];
318 }
319
320
321 bool MathScriptInset::idxRight(MathInset::idx_type &,
322                                  MathInset::pos_type &) const
323 {
324         return false;
325 }
326
327
328 bool MathScriptInset::idxLeft(MathInset::idx_type &,
329                                 MathInset::pos_type &) const
330 {
331         return false;
332 }
333
334
335 void MathScriptInset::write(WriteStream & os) const
336 {
337         //lyxerr << "unexpected call to MathScriptInset::write()\n";
338         write2(0, os);
339 }
340
341
342 void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
343 {
344         if (nuc) {
345                 os << nuc;
346                 if (nuc->takesLimits()) {
347                         if (limits_ == -1)
348                                 os << "\\nolimits ";
349                         if (limits_ == 1)
350                                 os << "\\limits ";
351                 }
352         } else
353                         if (os.firstitem())
354                                 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
355                         else
356                                 os << "{}";
357
358         if (hasDown() && down().data().size())
359                 os << "_{" << down().data() << '}';
360
361         if (hasUp() && up().data().size())
362                 os << "^{" << up().data() << '}';
363 }
364
365
366 void MathScriptInset::normalize(NormalStream & os) const
367 {
368         //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
369         normalize2(0, os);
370 }
371
372
373 void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
374 {
375         bool d = hasDown() && down().data().size();
376         bool u = hasUp() && up().data().size();
377
378         if (u)
379                 os << "[sup ";
380         if (d)
381                 os << "[sub ";
382
383         if (nuc)
384                 os << nuc << ' ';
385         else
386                 os << "[par]";
387
388         if (d)
389                 os << down().data() << ']';
390         if (u)
391                 os << up().data() << ']';
392 }
393
394
395 void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
396 {
397         if (nuc)
398                 os << nuc;
399         if (hasDown() && down().data().size())
400                 os << '[' << down().data() << ']';
401         if (hasUp() && up().data().size())
402                 os << "^(" << up().data() << ')';
403 }
404
405
406 void MathScriptInset::mathematicize2(MathInset const * nuc, MathematicaStream & os) const
407 {
408         bool d = hasDown() && down().data().size();
409         bool u = hasUp() && up().data().size();
410
411         if (nuc)
412                 if (d)  //subscript only if nuc !
413                         os << "Subscript[" << nuc;
414                 else
415                         os << nuc;
416         if (u)
417                 os << "^(" << up().data() << ")";
418
419         if (nuc)
420                 if (d)
421                 os << "," << down().data() << "]"; 
422 }
423
424
425 void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
426 {
427         bool d = hasDown() && down().data().size();
428         bool u = hasUp() && up().data().size();
429
430         if (u && d)
431                 os << MTag("msubsup");
432         else if (u)
433                 os << MTag("msup");
434         else if (d)
435                 os << MTag("msub");
436
437         if (nuc)
438                 os << nuc;
439         else
440                 os << "<mrow/>";
441
442         if (u && d)
443                 os << down().data() << up().data() << ETag("msubsup");
444         else if (u)
445                 os << up().data() << ETag("msup");
446         else if (d)
447                 os << down().data() << ETag("msub");
448 }
449
450
451 void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
452 {
453         if (nuc)
454                 os << nuc;
455         if (hasDown() && down().data().size())
456                 os << '[' << down().data() << ']';
457         if (hasUp() && up().data().size())
458                 os << "^(" << up().data() << ')';
459 }
460
461
462 void MathScriptInset::infoize(std::ostream & os) const
463 {
464         if (limits_)
465                 os << (limits_ == 1 ? "Displayed limits" : "Inlined limits");
466 }