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