]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
Merge branch 'betterspacing'
[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         Dimension dim0;
292         Dimension dim1;
293         Dimension dim2;
294         cell(0).metrics(mi, dim0);
295         Changer dummy = mi.base.changeScript();
296         if (nargs() > 1)
297                 cell(1).metrics(mi, dim1);
298         if (nargs() > 2)
299                 cell(2).metrics(mi, dim2);
300
301         dim.wid = 0;
302         BufferView & bv = *mi.base.bv;
303         // FIXME: data copying... not very efficient.
304         Dimension dimup;
305         Dimension dimdown;
306         if (hasUp())
307                 dimup = up().dimension(bv);
308         if (hasDown())
309                 dimdown = down().dimension(bv);
310
311         if (hasLimits()) {
312                 dim.wid = nwid(bv);
313                 if (hasUp())
314                         dim.wid = max(dim.wid, dimup.width());
315                 if (hasDown())
316                         dim.wid = max(dim.wid, dimdown.width());
317         } else {
318                 if (hasUp())
319                         dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
320                 if (hasDown())
321                         dim.wid = max(dim.wid, dimdown.width());
322                 dim.wid += nwid(bv);
323         }
324         int na = nasc(bv);
325         if (hasUp()) {
326                 int asc = dy1(bv) + dimup.ascent();
327                 dim.asc = max(na, asc);
328         } else
329                 dim.asc = na;
330         int nd = ndes(bv);
331         if (hasDown()) {
332                 int des = dy0(bv) + dimdown.descent();
333                 dim.des = max(nd, des);
334         } else
335                 dim.des = nd;
336         metricsMarkers(mi, dim);
337 }
338
339
340 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
341 {
342         BufferView & bv = *pi.base.bv;
343         if (!nuc().empty())
344                 nuc().draw(pi, x + dxx(bv), y);
345         else {
346                 nuc().setXY(bv, x + dxx(bv), y);
347                 if (editing(&bv))
348                         pi.draw(x + dxx(bv), y, char_type('.'));
349         }
350         Changer dummy = pi.base.changeScript();
351         if (hasUp())
352                 up().draw(pi, x + dx1(bv), y - dy1(bv));
353         if (hasDown())
354                 down().draw(pi, x + dx0(bv), y + dy0(bv));
355         drawMarkers(pi, x, y);
356 }
357
358
359 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
360 {
361         if (hasUp())
362                 up().metricsT(mi, dim);
363         if (hasDown())
364                 down().metricsT(mi, dim);
365         nuc().metricsT(mi, dim);
366 }
367
368
369 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
370 {
371         // FIXME: BROKEN
372         if (!nuc().empty())
373                 nuc().drawT(pain, x + 1, y);
374         if (hasUp())
375                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
376         if (hasDown())
377                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
378 }
379
380
381
382 bool InsetMathScript::hasLimits() const
383 {
384         // obvious cases
385         if (limits_ == 1)
386                 return true;
387         if (limits_ == -1)
388                 return false;
389
390         // we can only display limits if the nucleus wants some
391         if (nuc().empty())
392                 return false;
393         if (!nuc().back()->isScriptable())
394                 return false;
395
396         if (nuc().back()->asSymbolInset()) {
397                 // \intop is an alias for \int\limits, \ointop == \oint\limits
398                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
399                         return true;
400                 // per default \int has limits beside the \int even in displayed formulas
401                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
402                         return false;
403         }
404
405         // assume "real" limits for everything else
406         return true;
407 }
408
409
410 void InsetMathScript::removeScript(bool up)
411 {
412         if (nargs() == 2) {
413                 if (up == cell_1_is_up_)
414                         cells_.pop_back();
415         } else if (nargs() == 3) {
416                 if (up == true) {
417                         swap(cells_[1], cells_[2]);
418                         cell_1_is_up_ = false;
419                 } else {
420                         cell_1_is_up_ = true;
421                 }
422                 cells_.pop_back();
423         }
424 }
425
426
427 bool InsetMathScript::has(bool up) const
428 {
429         return idxOfScript(up);
430 }
431
432
433 bool InsetMathScript::hasUp() const
434 {
435         //lyxerr << "1up: " << bool(cell_1_is_up_));
436         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
437         return idxOfScript(true);
438 }
439
440
441 bool InsetMathScript::hasDown() const
442 {
443         //LYXERR0("1up: " << bool(cell_1_is_up_));
444         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
445         return idxOfScript(false);
446 }
447
448
449 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
450 {
451         if (nargs() == 1)
452                 return 0;
453         if (nargs() == 2)
454                 return (cell_1_is_up_ == up) ? 1 : 0;
455         if (nargs() == 3)
456                 return up ? 1 : 2;
457         LASSERT(false, return 0);
458 }
459
460
461 bool InsetMathScript::idxForward(Cursor &) const
462 {
463         return false;
464 }
465
466
467 bool InsetMathScript::idxBackward(Cursor &) const
468 {
469         return false;
470 }
471
472
473 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
474 {
475         // in nucleus?
476         if (cur.idx() == 0) {
477                 // don't go up/down if there is no cell in this direction
478                 if (!has(up))
479                         return false;
480                 // go up/down only if in the last position
481                 // or in the first position of something with displayed limits
482                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
483                         cur.idx() = idxOfScript(up);
484                         cur.pos() = 0;
485                         return true;
486                 }
487                 return false;
488         }
489
490         // Are we 'up'?
491         if (cur.idx() == idxOfScript(true)) {
492                 // can't go further up
493                 if (up)
494                         return false;
495                 // otherwise go to last position in the nucleus
496                 cur.idx() = 0;
497                 cur.pos() = cur.lastpos();
498                 return true;
499         }
500
501         // Are we 'down'?
502         if (cur.idx() == idxOfScript(false)) {
503                 // can't go further down
504                 if (!up)
505                         return false;
506                 // otherwise go to last position in the nucleus
507                 cur.idx() = 0;
508                 cur.pos() = cur.lastpos();
509                 return true;
510         }
511
512         return false;
513 }
514
515
516 void InsetMathScript::write(WriteStream & os) const
517 {
518         MathEnsurer ensurer(os);
519
520         if (!nuc().empty()) {
521                 os << nuc();
522                 //if (nuc().back()->takesLimits()) {
523                         if (limits_ == -1)
524                                 os << "\\nolimits ";
525                         if (limits_ == 1)
526                                 os << "\\limits ";
527                 //}
528         } else {
529                 if (os.firstitem())
530                         LYXERR(Debug::MATHED, "suppressing {} when writing");
531                 else
532                         os << "{}";
533         }
534
535         if (hasDown() /*&& !down().empty()*/)
536                 os << "_{" << down() << '}';
537
538         if (hasUp() /*&& !up().empty()*/) {
539                 // insert space if up() is empty or an empty brace inset
540                 // (see bug 8305)
541                 if (os.latex() && (up().empty() ||
542                     (up().size() == 1 && up().back()->asBraceInset() &&
543                      up().back()->asBraceInset()->cell(0).empty())))
544                         os << "^ {}";
545                 else
546                         os << "^{" << up() << '}';
547         }
548
549         if (lock_ && !os.latex())
550                 os << "\\lyxlock ";
551 }
552
553
554 void InsetMathScript::normalize(NormalStream & os) const
555 {
556         bool d = hasDown() && !down().empty();
557         bool u = hasUp() && !up().empty();
558
559         if (u && d)
560                 os << "[subsup ";
561         else if (u)
562                 os << "[sup ";
563         else if (d)
564                 os << "[sub ";
565
566         if (!nuc().empty())
567                 os << nuc() << ' ';
568         else
569                 os << "[par]";
570
571         if (u && d)
572                 os << down() << ' ' << up() << ']';
573         else if (d)
574                 os << down() << ']';
575         else if (u)
576                 os << up() << ']';
577 }
578
579
580 void InsetMathScript::maple(MapleStream & os) const
581 {
582         if (!nuc().empty())
583                 os << nuc();
584         if (hasDown() && !down().empty())
585                 os << '[' << down() << ']';
586         if (hasUp() && !up().empty())
587                 os << "^(" << up() << ')';
588 }
589
590
591 void InsetMathScript::mathematica(MathematicaStream & os) const
592 {
593         bool d = hasDown() && !down().empty();
594         bool u = hasUp() && !up().empty();
595
596         if (!nuc().empty()) {
597                 if (d)
598                         os << "Subscript[" << nuc();
599                 else
600                         os << nuc();
601         }
602
603         if (u)
604                 os << "^(" << up() << ')';
605
606         if (!nuc().empty()) {
607                 if (d)
608                         os << ',' << down() << ']';
609         }
610 }
611
612
613 void InsetMathScript::mathmlize(MathStream & os) const
614 {
615         bool d = hasDown() && !down().empty();
616         bool u = hasUp() && !up().empty();
617         bool l = hasLimits();
618
619         if (u && d)
620                 os << MTag(l ? "munderover" : "msubsup");
621         else if (u)
622                 os << MTag(l ? "mover" : "msup");
623         else if (d)
624                 os << MTag(l ? "munder" : "msub");
625
626         if (!nuc().empty())
627                 os << MTag("mrow") << nuc() << ETag("mrow");
628         else
629                 os << "<mrow />";
630
631         if (u && d)
632                 os << MTag("mrow") << down() << ETag("mrow") 
633                    << MTag("mrow") << up() << ETag("mrow") 
634                    << ETag(l ? "munderover" : "msubsup");
635         else if (u)
636                 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
637         else if (d)
638                 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
639 }
640
641
642 void InsetMathScript::htmlize(HtmlStream & os) const
643 {
644         bool d = hasDown() && !down().empty();
645         bool u = hasUp() && !up().empty();
646
647         if (!nuc().empty())
648                 os << nuc();
649
650         if (u && d)
651                 os << MTag("span", "class='scripts'")
652                          << MTag("span") << up() << ETag("span")
653                          << MTag("span") << down() << ETag("span")
654                          << ETag("span");
655         else if (u)
656                 os << MTag("sup", "class='math'") << up() << ETag("sup");
657         else if (d)
658                 os << MTag("sub", "class='math'") << down() << ETag("sub");
659 }
660
661
662 void InsetMathScript::octave(OctaveStream & os) const
663 {
664         if (!nuc().empty())
665                 os << nuc();
666         if (hasDown() && !down().empty())
667                 os << '[' << down() << ']';
668         if (hasUp() && !up().empty())
669                 os << "^(" << up() << ')';
670 }
671
672
673 void InsetMathScript::infoize(odocstream & os) const
674 {
675         os << "Scripts";
676 }
677
678
679 void InsetMathScript::infoize2(odocstream & os) const
680 {
681         if (limits_)
682                 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
683 }
684
685
686 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
687 {
688         InsetMathNest::notifyCursorLeaves(old, cur);
689
690         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
691
692         // Remove empty scripts if possible:
693
694         // The case of two scripts, but only one got empty (1 = super, 2 = sub).
695         // We keep the script inset, but remove the empty script.
696         if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
697                 if (cell(2).empty()) {
698                         // must be a subscript...
699                         old.recordUndoInset();
700                         removeScript(false);
701                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
702                         return true;
703                 } else if (cell(1).empty()) {
704                         // must be a superscript...
705                         old.recordUndoInset();
706                         removeScript(true);
707                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
708                         return true;
709                 }
710         }
711         // Now the two suicide cases:
712         // * we have only one script which is empty
713         // * we have two scripts which are both empty.
714         // The script inset is removed completely.
715         if ((nargs() == 2 && cell(1).empty())
716             || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
717                 // Make undo step. We cannot use cur for this because
718                 // it does not necessarily point to us anymore. But we
719                 // should be on top of the cursor old.
720                 Cursor insetCur = old;
721                 int scriptSlice = insetCur.find(this);
722                 LASSERT(scriptSlice != -1, /**/);
723                 insetCur.cutOff(scriptSlice);
724                 insetCur.recordUndoInset();
725
726                 // Let the script inset commit suicide. This is
727                 // modelled on Cursor.pullArg(), but tries not to
728                 // invoke notifyCursorLeaves again and does not touch
729                 // cur (since the top slice will be deleted
730                 // afterwards)
731                 MathData ar = cell(0);
732                 insetCur.pop();
733                 insetCur.cell().erase(insetCur.pos());
734                 insetCur.cell().insert(insetCur.pos(), ar);
735
736                 // redraw
737                 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
738                 return true;
739         }
740
741         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
742         return false;
743 }
744
745
746 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
747 {
748         //LYXERR("InsetMathScript: request: " << cmd);
749
750         if (cmd.action() == LFUN_MATH_LIMITS) {
751                 cur.recordUndoInset();
752                 if (!cmd.argument().empty()) {
753                         if (cmd.argument() == "limits")
754                                 limits_ = 1;
755                         else if (cmd.argument() == "nolimits")
756                                 limits_ = -1;
757                         else
758                                 limits_ = 0;
759                 } else if (limits_ == 0)
760                         limits_ = hasLimits() ? -1 : 1;
761                 else
762                         limits_ = 0;
763                 return;
764         }
765
766         InsetMathNest::doDispatch(cur, cmd);
767 }
768
769
770 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
771                                 FuncStatus & flag) const
772 {
773         if (cmd.action() == LFUN_MATH_LIMITS) {
774                 if (!cmd.argument().empty()) {
775                         if (cmd.argument() == "limits")
776                                 flag.setOnOff(limits_ == 1);
777                         else if (cmd.argument() == "nolimits")
778                                 flag.setOnOff(limits_ == -1);
779                         else
780                                 flag.setOnOff(limits_ == 0);
781                 } 
782                 flag.setEnabled(true);
783                 return true;
784         }
785
786         return InsetMathNest::getStatus(cur, cmd, flag);
787 }
788
789
790 // the idea for dual scripts came from the eLyXer code
791 void InsetMathScript::validate(LaTeXFeatures & features) const
792 {
793         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
794                 features.addCSSSnippet(
795                         "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
796                         "span.scripts span {display: block;}\n"
797                         "sub.math{font-size: 75%;}\n"
798                         "sup.math{font-size: 75%;}");
799         InsetMathNest::validate(features);
800 }
801
802 } // namespace lyx