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