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