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