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