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