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