]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
b91fbf64019d24008d1bd05a53db1cae32c67c8f
[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 #include "MathData.h"
15 #include "MathStream.h"
16 #include "MathSupport.h"
17 #include "InsetMathSymbol.h"
18 #include "InsetMathFont.h"
19 #include "DispatchResult.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "FuncRequest.h"
23
24 #include <boost/assert.hpp>
25
26
27 namespace lyx {
28
29 using std::string;
30 using std::max;
31 using std::endl;
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         BOOST_ASSERT(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         BOOST_ASSERT(nargs() > 1);
90         return cell(1);
91 }
92
93
94 MathData & InsetMathScript::down()
95 {
96         if (nargs() == 3)
97                 return cell(2);
98         BOOST_ASSERT(nargs() > 1);
99         return cell(1);
100 }
101
102
103 MathData const & InsetMathScript::up() const
104 {
105         BOOST_ASSERT(nargs() > 1);
106         return cell(1);
107 }
108
109
110 MathData & InsetMathScript::up()
111 {
112         BOOST_ASSERT(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         BOOST_ASSERT(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         BOOST_ASSERT(hasUp());
255         Dimension const dim = dimension(bv);
256         return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker();
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() const
286 {
287         if (nuc().size()) {
288                 int kerning = nuc().kerning();
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() + 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         // Cache the inset dimension. 
344         setDimCache(mi, dim);
345 }
346
347
348 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
349 {
350         BufferView & bv = *pi.base.bv;
351         if (nuc().size())
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         ScriptChanger dummy(pi.base);
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         drawMarkers(pi, x, y);
364 }
365
366
367 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
368 {
369         if (hasUp())
370                 up().metricsT(mi, dim);
371         if (hasDown())
372                 down().metricsT(mi, dim);
373         nuc().metricsT(mi, dim);
374 }
375
376
377 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
378 {
379         // FIXME: BROKEN
380         if (nuc().size())
381                 nuc().drawT(pain, x + 1, y);
382         if (hasUp())
383                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
384         if (hasDown())
385                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
386 }
387
388
389
390 bool InsetMathScript::hasLimits() const
391 {
392         // obvious cases
393         if (limits_ == 1)
394                 return true;
395         if (limits_ == -1)
396                 return false;
397
398         // we can only display limits if the nucleus wants some
399         if (!nuc().size())
400                 return false;
401         if (!nuc().back()->isScriptable())
402                 return false;
403
404         if (nuc().back()->asSymbolInset()) {
405                 // \intop is an alias for \int\limits, \ointop == \oint\limits
406                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
407                         return true;
408                 // per default \int has limits beside the \int even in displayed formulas
409                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
410                         return false;
411         }
412
413         // assume "real" limits for everything else
414         return true;
415 }
416
417
418 void InsetMathScript::removeScript(bool up)
419 {
420         if (nargs() == 2) {
421                 if (up == cell_1_is_up_)
422                         cells_.pop_back();
423         } else if (nargs() == 3) {
424                 if (up == true) {
425                         swap(cells_[1], cells_[2]);
426                         cell_1_is_up_ = false;
427                 } else {
428                         cell_1_is_up_ = true;
429                 }
430                 cells_.pop_back();
431         }
432 }
433
434
435 bool InsetMathScript::has(bool up) const
436 {
437         return idxOfScript(up);
438 }
439
440
441 bool InsetMathScript::hasUp() const
442 {
443         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
444         //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
445         return idxOfScript(true);
446 }
447
448
449 bool InsetMathScript::hasDown() const
450 {
451         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
452         //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
453         return idxOfScript(false);
454 }
455
456
457 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
458 {
459         if (nargs() == 1)
460                 return 0;
461         if (nargs() == 2)
462                 return (cell_1_is_up_ == up) ? 1 : 0;
463         if (nargs() == 3)
464                 return up ? 1 : 2;
465         BOOST_ASSERT(false);
466         // Silence compiler
467         return 0;
468 }
469
470
471 bool InsetMathScript::idxForward(Cursor &) const
472 {
473         return false;
474 }
475
476
477 bool InsetMathScript::idxBackward(Cursor &) const
478 {
479         return false;
480 }
481
482
483 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
484 {
485         // in nucleus?
486         if (cur.idx() == 0) {
487                 // don't go up/down if there is no cell in this direction
488                 if (!has(up))
489                         return false;
490                 // go up/down only if in the last position
491                 // or in the first position of something with displayed limits
492                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
493                         cur.idx() = idxOfScript(up);
494                         cur.pos() = 0;
495                         return true;
496                 }
497                 return false;
498         }
499
500         // Are we 'up'?
501         if (cur.idx() == idxOfScript(true)) {
502                 // can't go further up
503                 if (up)
504                         return false;
505                 // otherwise go to last position in the nucleus
506                 cur.idx() = 0;
507                 cur.pos() = cur.lastpos();
508                 return true;
509         }
510
511         // Are we 'down'?
512         if (cur.idx() == idxOfScript(false)) {
513                 // can't go further down
514                 if (!up)
515                         return false;
516                 // otherwise go to last position in the nucleus
517                 cur.idx() = 0;
518                 cur.pos() = cur.lastpos();
519                 return true;
520         }
521
522         return false;
523 }
524
525
526 void InsetMathScript::write(WriteStream & os) const
527 {
528         if (nuc().size()) {
529                 os << nuc();
530                 //if (nuc().back()->takesLimits()) {
531                         if (limits_ == -1)
532                                 os << "\\nolimits ";
533                         if (limits_ == 1)
534                                 os << "\\limits ";
535                 //}
536         } else {
537                 if (os.firstitem())
538                         LYXERR(Debug::MATHED) << "suppressing {} when writing"
539                                               << endl;
540                 else
541                         os << "{}";
542         }
543
544         if (hasDown() /*&& down().size()*/)
545                 os << "_{" << down() << '}';
546
547         if (hasUp() /*&& up().size()*/)
548                 os << "^{" << up() << '}';
549
550         if (lock_ && !os.latex())
551                 os << "\\lyxlock ";
552 }
553
554
555 void InsetMathScript::normalize(NormalStream & os) const
556 {
557         bool d = hasDown() && down().size();
558         bool u = hasUp() && up().size();
559
560         if (u && d)
561                 os << "[subsup ";
562         else if (u)
563                 os << "[sup ";
564         else if (d)
565                 os << "[sub ";
566
567         if (nuc().size())
568                 os << nuc() << ' ';
569         else
570                 os << "[par]";
571
572         if (u && d)
573                 os << down() << ' ' << up() << ']';
574         else if (d)
575                 os << down() << ']';
576         else if (u)
577                 os << up() << ']';
578 }
579
580
581 void InsetMathScript::maple(MapleStream & os) const
582 {
583         if (nuc().size())
584                 os << nuc();
585         if (hasDown() && down().size())
586                 os << '[' << down() << ']';
587         if (hasUp() && up().size())
588                 os << "^(" << up() << ')';
589 }
590
591
592 void InsetMathScript::mathematica(MathematicaStream & os) const
593 {
594         bool d = hasDown() && down().size();
595         bool u = hasUp() && up().size();
596
597         if (nuc().size()) {
598                 if (d)
599                         os << "Subscript[" << nuc();
600                 else
601                         os << nuc();
602         }
603
604         if (u)
605                 os << "^(" << up() << ')';
606
607         if (nuc().size()) {
608                 if (d)
609                         os << ',' << down() << ']';
610         }
611 }
612
613
614 void InsetMathScript::mathmlize(MathStream & os) const
615 {
616         bool d = hasDown() && down().size();
617         bool u = hasUp() && up().size();
618
619         if (u && d)
620                 os << MTag("msubsup");
621         else if (u)
622                 os << MTag("msup");
623         else if (d)
624                 os << MTag("msub");
625
626         if (nuc().size())
627                 os << nuc();
628         else
629                 os << "<mrow/>";
630
631         if (u && d)
632                 os << down() << up() << ETag("msubsup");
633         else if (u)
634                 os << up() << ETag("msup");
635         else if (d)
636                 os << down() << ETag("msub");
637 }
638
639
640 void InsetMathScript::octave(OctaveStream & os) const
641 {
642         if (nuc().size())
643                 os << nuc();
644         if (hasDown() && down().size())
645                 os << '[' << down() << ']';
646         if (hasUp() && up().size())
647                 os << "^(" << up() << ')';
648 }
649
650
651 void InsetMathScript::infoize(odocstream & os) const
652 {
653         os << "Scripts";
654 }
655
656
657 void InsetMathScript::infoize2(odocstream & os) const
658 {
659         if (limits_)
660                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
661 }
662
663
664 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
665 {
666         InsetMathNest::notifyCursorLeaves(cur);
667
668         //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
669
670         // remove empty scripts if possible
671         if (nargs() > 2) {
672                 // Case of two scripts. In this case, 1 = super, 2 = sub
673                 if (cur.idx() == 2 && cell(2).empty()) {
674                         // must be a subscript...
675                         cur.recordUndoInset();
676                         removeScript(false);
677                         return true;
678                 } else if (cur.idx() == 1 && cell(1).empty()) {
679                         // must be a superscript...
680                         cur.recordUndoInset();
681                         removeScript(true);
682                         return true;
683                 }
684         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
685                 // could be either subscript or super script
686                 cur.recordUndoInset();
687                 removeScript(cell_1_is_up_);
688                 // Let the script inset commit suicide. This is
689                 // modelled on Cursor.pullArg(), but tries not to
690                 // invoke notifyCursorLeaves again and does not touch
691                 // cur (since the top slice will be deleted
692                 // afterwards))
693                 MathData ar = cell(0);
694                 Cursor tmpcur = cur;
695                 tmpcur.pop();
696                 tmpcur.cell().erase(tmpcur.pos());
697                 tmpcur.cell().insert(tmpcur.pos(), ar);
698                 return true;
699         }
700
701         //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
702         return false;
703 }
704
705
706 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
707 {
708         //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
709
710         if (cmd.action == LFUN_MATH_LIMITS) {
711                 if (!cmd.argument().empty()) {
712                         if (cmd.argument() == "limits")
713                                 limits_ = 1;
714                         else if (cmd.argument() == "nolimits")
715                                 limits_ = -1;
716                         else
717                                 limits_ = 0;
718                 } else if (limits_ == 0)
719                         limits_ = hasLimits() ? -1 : 1;
720                 else
721                         limits_ = 0;
722                 return;
723         }
724
725         InsetMathNest::doDispatch(cur, cmd);
726 }
727
728
729 } // namespace lyx