]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
* kerning may depend on the BufferView because the metrics of some insets (e.g. math...
[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 "support/debug.h"
22 #include "FuncRequest.h"
23
24 #include <boost/assert.hpp>
25
26 #include <ostream>
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         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(&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         // 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_));
444         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
445         return idxOfScript(true);
446 }
447
448
449 bool InsetMathScript::hasDown() const
450 {
451         //LYXERR0("1up: " << bool(cell_1_is_up_));
452         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
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                 else
540                         os << "{}";
541         }
542
543         if (hasDown() /*&& down().size()*/)
544                 os << "_{" << down() << '}';
545
546         if (hasUp() /*&& up().size()*/)
547                 os << "^{" << up() << '}';
548
549         if (lock_ && !os.latex())
550                 os << "\\lyxlock ";
551 }
552
553
554 void InsetMathScript::normalize(NormalStream & os) const
555 {
556         bool d = hasDown() && down().size();
557         bool u = hasUp() && up().size();
558
559         if (u && d)
560                 os << "[subsup ";
561         else if (u)
562                 os << "[sup ";
563         else if (d)
564                 os << "[sub ";
565
566         if (nuc().size())
567                 os << nuc() << ' ';
568         else
569                 os << "[par]";
570
571         if (u && d)
572                 os << down() << ' ' << up() << ']';
573         else if (d)
574                 os << down() << ']';
575         else if (u)
576                 os << up() << ']';
577 }
578
579
580 void InsetMathScript::maple(MapleStream & os) const
581 {
582         if (nuc().size())
583                 os << nuc();
584         if (hasDown() && down().size())
585                 os << '[' << down() << ']';
586         if (hasUp() && up().size())
587                 os << "^(" << up() << ')';
588 }
589
590
591 void InsetMathScript::mathematica(MathematicaStream & os) const
592 {
593         bool d = hasDown() && down().size();
594         bool u = hasUp() && up().size();
595
596         if (nuc().size()) {
597                 if (d)
598                         os << "Subscript[" << nuc();
599                 else
600                         os << nuc();
601         }
602
603         if (u)
604                 os << "^(" << up() << ')';
605
606         if (nuc().size()) {
607                 if (d)
608                         os << ',' << down() << ']';
609         }
610 }
611
612
613 void InsetMathScript::mathmlize(MathStream & os) const
614 {
615         bool d = hasDown() && down().size();
616         bool u = hasUp() && up().size();
617
618         if (u && d)
619                 os << MTag("msubsup");
620         else if (u)
621                 os << MTag("msup");
622         else if (d)
623                 os << MTag("msub");
624
625         if (nuc().size())
626                 os << nuc();
627         else
628                 os << "<mrow/>";
629
630         if (u && d)
631                 os << down() << up() << ETag("msubsup");
632         else if (u)
633                 os << up() << ETag("msup");
634         else if (d)
635                 os << down() << ETag("msub");
636 }
637
638
639 void InsetMathScript::octave(OctaveStream & os) const
640 {
641         if (nuc().size())
642                 os << nuc();
643         if (hasDown() && down().size())
644                 os << '[' << down() << ']';
645         if (hasUp() && up().size())
646                 os << "^(" << up() << ')';
647 }
648
649
650 void InsetMathScript::infoize(odocstream & os) const
651 {
652         os << "Scripts";
653 }
654
655
656 void InsetMathScript::infoize2(odocstream & os) const
657 {
658         if (limits_)
659                 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
660 }
661
662
663 bool InsetMathScript::notifyCursorLeaves(Cursor & cur)
664 {
665         InsetMathNest::notifyCursorLeaves(cur);
666
667         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
668
669         // remove empty scripts if possible
670         if (nargs() > 2) {
671                 // Case of two scripts. In this case, 1 = super, 2 = sub
672                 if (cur.idx() == 2 && cell(2).empty()) {
673                         // must be a subscript...
674                         cur.recordUndoInset();
675                         removeScript(false);
676                         return true;
677                 } else if (cur.idx() == 1 && cell(1).empty()) {
678                         // must be a superscript...
679                         cur.recordUndoInset();
680                         removeScript(true);
681                         return true;
682                 }
683         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
684                 // could be either subscript or super script
685                 cur.recordUndoInset();
686                 removeScript(cell_1_is_up_);
687                 // Let the script inset commit suicide. This is
688                 // modelled on Cursor.pullArg(), but tries not to
689                 // invoke notifyCursorLeaves again and does not touch
690                 // cur (since the top slice will be deleted
691                 // afterwards))
692                 MathData ar = cell(0);
693                 Cursor tmpcur = cur;
694                 tmpcur.pop();
695                 tmpcur.cell().erase(tmpcur.pos());
696                 tmpcur.cell().insert(tmpcur.pos(), ar);
697                 return true;
698         }
699
700         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
701         return false;
702 }
703
704
705 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
706 {
707         //LYXERR("InsetMathScript: request: " << cmd);
708
709         if (cmd.action == LFUN_MATH_LIMITS) {
710                 if (!cmd.argument().empty()) {
711                         if (cmd.argument() == "limits")
712                                 limits_ = 1;
713                         else if (cmd.argument() == "nolimits")
714                                 limits_ = -1;
715                         else
716                                 limits_ = 0;
717                 } else if (limits_ == 0)
718                         limits_ = hasLimits() ? -1 : 1;
719                 else
720                         limits_ = 0;
721                 return;
722         }
723
724         InsetMathNest::doDispatch(cur, cmd);
725 }
726
727
728 } // namespace lyx