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