]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
Revert "Fix a number of signedness warnings"
[lyx.git] / src / mathed / InsetMathScript.cpp
1 /**
2  * \file InsetMathScript.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 "InsetMathScript.h"
14
15 #include "InsetMathBrace.h"
16 #include "MathData.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19
20 #include "BufferView.h"
21 #include "Cursor.h"
22 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24
25 #include "support/debug.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28
29
30 using namespace std;
31
32 namespace lyx {
33
34
35 InsetMathScript::InsetMathScript(Buffer * buf)
36         : InsetMathNest(buf, 1), cell_1_is_up_(false)
37 {}
38
39
40 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
41         : InsetMathNest(buf, 2), cell_1_is_up_(up)
42 {}
43
44
45 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
46         : InsetMathNest(buf, 2), cell_1_is_up_(up)
47 {
48         LATTEST(nargs() >= 1);
49         cell(0).push_back(at);
50 }
51
52
53 Inset * InsetMathScript::clone() const
54 {
55         return new InsetMathScript(*this);
56 }
57
58
59 InsetMathScript const * InsetMathScript::asScriptInset() const
60 {
61         return this;
62 }
63
64
65 InsetMathScript * InsetMathScript::asScriptInset()
66 {
67         return this;
68 }
69
70
71 MathData const & InsetMathScript::down() const
72 {
73         if (nargs() == 3)
74                 return cell(2);
75         LBUFERR(nargs() > 1);
76         return cell(1);
77 }
78
79
80 MathData & InsetMathScript::down()
81 {
82         if (nargs() == 3)
83                 return cell(2);
84         LBUFERR(nargs() > 1);
85         return cell(1);
86 }
87
88
89 MathData const & InsetMathScript::up() const
90 {
91         LBUFERR(nargs() > 1);
92         return cell(1);
93 }
94
95
96 MathData & InsetMathScript::up()
97 {
98         LBUFERR(nargs() > 1);
99         return cell(1);
100 }
101
102
103 void InsetMathScript::ensure(bool up)
104 {
105         if (nargs() == 1) {
106                 // just nucleus so far
107                 cells_.push_back(MathData());
108                 cell_1_is_up_ = up;
109         } else if (nargs() == 2 && !has(up)) {
110                 if (up) {
111                         cells_.push_back(cell(1));
112                         cell(1).clear();
113                 } else {
114                         cells_.push_back(MathData());
115                 }
116         }
117 }
118
119
120 MathData const & InsetMathScript::nuc() const
121 {
122         return cell(0);
123 }
124
125
126 MathData & InsetMathScript::nuc()
127 {
128         return cell(0);
129 }
130
131
132 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
133 {
134         int dasc = 0;
135         int slevel = 0;
136         bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
137         if (hasDown()) {
138                 Dimension const & dimdown = down().dimension(bv);
139                 dasc = dimdown.ascent();
140                 slevel = nuc().slevel();
141                 int ascdrop = dasc - slevel;
142                 int desdrop = isCharBox ? 0 : des + nuc().sshift();
143                 int mindes = nuc().mindes();
144                 des = max(desdrop, ascdrop);
145                 des = max(mindes, des);
146         }
147         if (hasUp()) {
148                 Dimension const & dimup = up().dimension(bv);
149                 int minasc = nuc().minasc();
150                 int ascdrop = isCharBox ? 0 : asc - up().mindes();
151                 int udes = dimup.descent();
152                 asc = udes + nuc().sshift();
153                 asc = max(ascdrop, asc);
154                 asc = max(minasc, asc);
155                 if (hasDown()) {
156                         int del = asc - udes - dasc;
157                         if (del + des <= 2) {
158                                 int newdes = 2 - del;
159                                 del = slevel - asc + udes;
160                                 if (del > 0) {
161                                         asc += del;
162                                         newdes -= del;
163                                 }
164                                 des = max(des, newdes);
165                         }
166                 }
167         }
168         return what ? asc : des;
169 }
170
171
172 int InsetMathScript::dy0(BufferView const & bv) const
173 {
174         int nd = ndes(bv);
175         if (!hasDown())
176                 return nd;
177         int des = down().dimension(bv).ascent();
178         if (has_limits_)
179                 des += nd + 2;
180         else {
181                 int na = nasc(bv);
182                 des = dy01(bv, na, nd, 0);
183         }
184         return des;
185 }
186
187
188 int InsetMathScript::dy1(BufferView const & bv) const
189 {
190         int na = nasc(bv);
191         if (!hasUp())
192                 return na;
193         int asc = up().dimension(bv).descent();
194         if (has_limits_)
195                 asc += na + 2;
196         else {
197                 int nd = ndes(bv);
198                 asc = dy01(bv, na, nd, 1);
199         }
200         asc = max(asc, 5);
201         return asc;
202 }
203
204
205 int InsetMathScript::dx0(BufferView const & bv) const
206 {
207         LASSERT(hasDown(), return 0);
208         Dimension const dim = dimension(bv);
209         return has_limits_ ? (dim.wid - down().dimension(bv).width()) / 2
210                 : nwid(bv) + min(nker(&bv), 0);
211 }
212
213
214 int InsetMathScript::dx1(BufferView const & bv) const
215 {
216         LASSERT(hasUp(), return 0);
217         Dimension const dim = dimension(bv);
218         return has_limits_ ? (dim.wid - up().dimension(bv).width()) / 2
219                 : nwid(bv) + max(nker(&bv), 0);
220 }
221
222
223 int InsetMathScript::dxx(BufferView const & bv) const
224 {
225         Dimension const dim = dimension(bv);
226         return has_limits_ ? (dim.wid - nwid(bv)) / 2  :  0;
227 }
228
229
230 int InsetMathScript::nwid(BufferView const & bv) const
231 {
232         return !nuc().empty() ? nuc().dimension(bv).width() : 2;
233 }
234
235
236 int InsetMathScript::nasc(BufferView const & bv) const
237 {
238         return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
239 }
240
241
242 int InsetMathScript::ndes(BufferView const & bv) const
243 {
244         return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
245 }
246
247
248 int InsetMathScript::nker(BufferView const * bv) const
249 {
250         if (!nuc().empty())
251                 return nuc().kerning(bv);
252         return 0;
253 }
254
255
256 MathClass InsetMathScript::mathClass() const
257 {
258         // FIXME: this is a hack, since the class will not be correct if
259         // the nucleus has several elements or if the last element is a math macro
260         // or a macro argument proxy.
261         // The correct implementation would require to linearize the nucleus.
262         if (nuc().empty())
263                 return MC_ORD;
264         else {
265                 // return the class of last element since this is the one that counts.
266                 MathClass mc = nuc().back()->mathClass();
267                 return (mc == MC_UNKNOWN) ? MC_ORD : mc;
268         }
269 }
270
271
272 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
273 {
274         // we store this, because it is much easier
275         has_limits_ = hasLimits(mi.base.font);
276
277         // Compute metrics of the available cells
278         Dimension dim0;
279         Dimension dim1;
280         Dimension dim2;
281         {
282                 // Keeps the changers local
283                 Changer dummy2 = mi.base.changeEnsureMath();
284                 cell(0).metrics(mi, dim0);
285                 Changer dummy = mi.base.changeScript();
286                 if (nargs() > 1)
287                         cell(1).metrics(mi, dim1, !has_limits_);
288                 if (nargs() > 2)
289                         cell(2).metrics(mi, dim2, !has_limits_);
290         }
291
292         dim.wid = 0;
293         BufferView & bv = *mi.base.bv;
294         // FIXME: data copying... not very efficient.
295         Dimension dimup;
296         Dimension dimdown;
297         if (hasUp())
298                 dimup = up().dimension(bv);
299         if (hasDown())
300                 dimdown = down().dimension(bv);
301
302         if (has_limits_) {
303                 dim.wid = nwid(bv);
304                 if (hasUp())
305                         dim.wid = max(dim.wid, dimup.width());
306                 if (hasDown())
307                         dim.wid = max(dim.wid, dimdown.width());
308         } else {
309                 if (hasUp())
310                         dim.wid = max(dim.wid, max(nker(mi.base.bv), 0) + dimup.width());
311                 if (hasDown())
312                         dim.wid = max(dim.wid, min(nker(mi.base.bv), 0) + dimdown.width());
313                 dim.wid += nwid(bv);
314         }
315         int na = nasc(bv);
316         if (hasUp()) {
317                 int asc = dy1(bv) + dimup.ascent();
318                 dim.asc = max(na, asc);
319         } else
320                 dim.asc = na;
321         int nd = ndes(bv);
322         if (hasDown()) {
323                 int des = dy0(bv) + dimdown.descent();
324                 dim.des = max(nd, des);
325         } else
326                 dim.des = nd;
327 }
328
329
330 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
331 {
332         Changer dummy2 = pi.base.changeEnsureMath();
333         BufferView & bv = *pi.base.bv;
334         if (!nuc().empty())
335                 nuc().draw(pi, x + dxx(bv), y);
336         else {
337                 nuc().setXY(bv, x + dxx(bv), y);
338                 if (editing(&bv))
339                         pi.draw(x + dxx(bv), y, char_type('.'));
340         }
341         Changer dummy = pi.base.changeScript();
342         if (hasUp())
343                 up().draw(pi, x + dx1(bv), y - dy1(bv));
344         if (hasDown())
345                 down().draw(pi, x + dx0(bv), y + dy0(bv));
346 }
347
348
349 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
350 {
351         if (hasUp())
352                 up().metricsT(mi, dim);
353         if (hasDown())
354                 down().metricsT(mi, dim);
355         nuc().metricsT(mi, dim);
356 }
357
358
359 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
360 {
361         // FIXME: BROKEN
362         if (!nuc().empty())
363                 nuc().drawT(pain, x + 1, y);
364         if (hasUp())
365                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
366         if (hasDown())
367                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
368 }
369
370
371 bool InsetMathScript::hasLimits(FontInfo const & font) const
372 {
373         if (font.style() != DISPLAY_STYLE)
374                 return false;
375         if (nuc().empty())
376                 return false;
377
378         Limits const lim = nuc().back()->limits() == AUTO_LIMITS
379                 ? nuc().back()->defaultLimits() : nuc().back()->limits();
380         LASSERT(lim != AUTO_LIMITS, return false);
381         return lim == LIMITS;
382 }
383
384
385 void InsetMathScript::removeScript(bool up)
386 {
387         if (nargs() == 2) {
388                 if (up == cell_1_is_up_)
389                         cells_.pop_back();
390         } else if (nargs() == 3) {
391                 if (up) {
392                         swap(cells_[1], cells_[2]);
393                         cell_1_is_up_ = false;
394                 } else {
395                         cell_1_is_up_ = true;
396                 }
397                 cells_.pop_back();
398         }
399 }
400
401
402 bool InsetMathScript::has(bool up) const
403 {
404         return idxOfScript(up);
405 }
406
407
408 bool InsetMathScript::hasUp() const
409 {
410         //lyxerr << "1up: " << bool(cell_1_is_up_));
411         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
412         return idxOfScript(true);
413 }
414
415
416 bool InsetMathScript::hasDown() const
417 {
418         //LYXERR0("1up: " << bool(cell_1_is_up_));
419         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
420         return idxOfScript(false);
421 }
422
423
424 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
425 {
426         if (nargs() == 1)
427                 return 0;
428         if (nargs() == 2)
429                 return (cell_1_is_up_ == up) ? 1 : 0;
430         if (nargs() == 3)
431                 return up ? 1 : 2;
432         LASSERT(false, return 0);
433 }
434
435
436 bool InsetMathScript::idxForward(Cursor &) const
437 {
438         return false;
439 }
440
441
442 bool InsetMathScript::idxBackward(Cursor &) const
443 {
444         return false;
445 }
446
447
448 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
449 {
450         // in nucleus?
451         if (cur.idx() == 0) {
452                 // don't go up/down if there is no cell in this direction
453                 if (!has(up))
454                         return false;
455                 // go up/down only if in the last position
456                 // or in the first position of something with displayed limits
457                 if (cur.pos() == cur.lastpos()
458                                  || (cur.pos() == 0 && has_limits_)) {
459                         cur.idx() = idxOfScript(up);
460                         cur.pos() = 0;
461                         return true;
462                 }
463                 return false;
464         }
465
466         // Are we 'up'?
467         if (cur.idx() == idxOfScript(true)) {
468                 // can't go further up
469                 if (up)
470                         return false;
471                 // otherwise go to last position in the nucleus
472                 cur.idx() = 0;
473                 cur.pos() = cur.lastpos();
474                 return true;
475         }
476
477         // Are we 'down'?
478         if (cur.idx() == idxOfScript(false)) {
479                 // can't go further down
480                 if (!up)
481                         return false;
482                 // otherwise go to last position in the nucleus
483                 cur.idx() = 0;
484                 cur.pos() = cur.lastpos();
485                 return true;
486         }
487
488         return false;
489 }
490
491
492 void InsetMathScript::write(WriteStream & os) const
493 {
494         MathEnsurer ensurer(os);
495
496         if (!nuc().empty())
497                 os << nuc();
498         else if (os.firstitem())
499                 LYXERR(Debug::MATHED, "suppressing {} when writing");
500         else
501                 os << "{}";
502
503         if (hasDown() /*&& !down().empty()*/)
504                 os << "_{" << down() << '}';
505
506         if (hasUp() /*&& !up().empty()*/) {
507                 // insert space if up() is empty or an empty brace inset
508                 // (see bug 8305)
509                 if (os.latex() && (up().empty() ||
510                     (up().size() == 1 && up().back()->asBraceInset() &&
511                      up().back()->asBraceInset()->cell(0).empty())))
512                         os << "^ {}";
513                 else
514                         os << "^{" << up() << '}';
515         }
516
517         if (lock_ && !os.latex())
518                 os << "\\lyxlock ";
519 }
520
521
522 void InsetMathScript::normalize(NormalStream & os) const
523 {
524         bool d = hasDown() && !down().empty();
525         bool u = hasUp() && !up().empty();
526
527         if (u && d)
528                 os << "[subsup ";
529         else if (u)
530                 os << "[sup ";
531         else if (d)
532                 os << "[sub ";
533
534         if (!nuc().empty())
535                 os << nuc() << ' ';
536         else
537                 os << "[par]";
538
539         if (u && d)
540                 os << down() << ' ' << up() << ']';
541         else if (d)
542                 os << down() << ']';
543         else if (u)
544                 os << up() << ']';
545 }
546
547
548 void InsetMathScript::maple(MapleStream & os) const
549 {
550         if (!nuc().empty())
551                 os << nuc();
552         if (hasDown() && !down().empty())
553                 os << '[' << down() << ']';
554         if (hasUp() && !up().empty())
555                 os << "^(" << up() << ')';
556 }
557
558
559 void InsetMathScript::mathematica(MathematicaStream & os) const
560 {
561         bool d = hasDown() && !down().empty();
562         bool u = hasUp() && !up().empty();
563
564         if (!nuc().empty()) {
565                 if (d)
566                         os << "Subscript[" << nuc();
567                 else
568                         os << nuc();
569         }
570
571         if (u)
572                 os << "^(" << up() << ')';
573
574         if (!nuc().empty()) {
575                 if (d)
576                         os << ',' << down() << ']';
577         }
578 }
579
580
581 void InsetMathScript::mathmlize(MathStream & ms) const
582 {
583         bool d = hasDown() && !down().empty();
584         bool u = hasUp() && !up().empty();
585         // FIXME: the MathStream should be able to give us this information
586         bool l = has_limits_;
587
588         if (u && d)
589                 ms << MTag(l ? "munderover" : "msubsup");
590         else if (u)
591                 ms << MTag(l ? "mover" : "msup");
592         else if (d)
593                 ms << MTag(l ? "munder" : "msub");
594
595         if (!nuc().empty())
596                 ms << MTag("mrow") << nuc() << ETag("mrow");
597         else
598                 ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
599
600         if (u && d)
601                 ms << MTag("mrow") << down() << ETag("mrow")
602                    << MTag("mrow") << up() << ETag("mrow")
603                    << ETag(l ? "munderover" : "msubsup");
604         else if (u)
605                 ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
606         else if (d)
607                 ms << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
608 }
609
610
611 void InsetMathScript::htmlize(HtmlStream & os) const
612 {
613         bool d = hasDown() && !down().empty();
614         bool u = hasUp() && !up().empty();
615
616         if (!nuc().empty())
617                 os << nuc();
618
619         if (u && d)
620                 os << MTag("span", "class='scripts'")
621                          << MTag("span") << up() << ETag("span")
622                          << MTag("span") << down() << ETag("span")
623                          << ETag("span");
624         else if (u)
625                 os << MTag("sup", "class='math'") << up() << ETag("sup");
626         else if (d)
627                 os << MTag("sub", "class='math'") << down() << ETag("sub");
628 }
629
630
631 void InsetMathScript::octave(OctaveStream & os) const
632 {
633         if (!nuc().empty())
634                 os << nuc();
635         if (hasDown() && !down().empty())
636                 os << '[' << down() << ']';
637         if (hasUp() && !up().empty())
638                 os << "^(" << up() << ')';
639 }
640
641
642 void InsetMathScript::infoize(odocstream & os) const
643 {
644         os << "Scripts";
645 }
646
647
648 void InsetMathScript::infoize2(odocstream & os) const
649 {
650         os << from_ascii(has_limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
651 }
652
653
654 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
655 {
656         InsetMathNest::notifyCursorLeaves(old, cur);
657
658         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
659
660         // Remove empty scripts if possible:
661
662         // The case of two scripts, but only one got empty (1 = super, 2 = sub).
663         // We keep the script inset, but remove the empty script.
664         if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
665                 if (cell(2).empty()) {
666                         // must be a subscript...
667                         old.recordUndoInset();
668                         removeScript(false);
669                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
670                         return true;
671                 } else if (cell(1).empty()) {
672                         // must be a superscript...
673                         old.recordUndoInset();
674                         removeScript(true);
675                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
676                         return true;
677                 }
678         }
679         // Now the two suicide cases:
680         // * we have only one script which is empty
681         // * we have two scripts which are both empty.
682         // The script inset is removed completely.
683         if ((nargs() == 2 && cell(1).empty())
684             || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
685                 // Make undo step. We cannot use cur for this because
686                 // it does not necessarily point to us anymore. But we
687                 // should be on top of the cursor old.
688                 Cursor insetCur = old;
689                 int scriptSlice = insetCur.find(this);
690                 LASSERT(scriptSlice != -1, /**/);
691                 insetCur.cutOff(scriptSlice);
692                 insetCur.recordUndoInset();
693
694                 // Let the script inset commit suicide. This is
695                 // modelled on Cursor.pullArg(), but tries not to
696                 // invoke notifyCursorLeaves again and does not touch
697                 // cur (since the top slice will be deleted
698                 // afterwards)
699                 MathData ar = cell(0);
700                 insetCur.pop();
701                 insetCur.cell().erase(insetCur.pos());
702                 insetCur.cell().insert(insetCur.pos(), ar);
703
704                 // redraw
705                 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
706                 return true;
707         }
708
709         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
710         return false;
711 }
712
713
714 // the idea for dual scripts came from the eLyXer code
715 void InsetMathScript::validate(LaTeXFeatures & features) const
716 {
717         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
718                 features.addCSSSnippet(
719                         "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
720                         "span.scripts span {display: block;}\n"
721                         "sub.math{font-size: 75%;}\n"
722                         "sup.math{font-size: 75%;}");
723         InsetMathNest::validate(features);
724 }
725
726 } // namespace lyx