5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "funcrequest.h"
10 #include "support/LAssert.h"
17 MathScriptInset::MathScriptInset()
18 : MathNestInset(3), limits_(0)
25 MathScriptInset::MathScriptInset(bool up)
26 : MathNestInset(3), limits_(0)
33 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
34 : MathNestInset(3), limits_(0)
38 cell(2).push_back(at);
43 MathInset * MathScriptInset::clone() const
45 return new MathScriptInset(*this);
49 MathScriptInset const * MathScriptInset::asScriptInset() const
55 MathScriptInset * MathScriptInset::asScriptInset()
61 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
69 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
77 MathArray const & MathScriptInset::down() const
83 MathArray & MathScriptInset::down()
89 MathArray const & MathScriptInset::up() const
95 MathArray & MathScriptInset::up()
101 void MathScriptInset::ensure(bool up)
107 MathArray const & MathScriptInset::nuc() const
113 MathArray & MathScriptInset::nuc()
119 int MathScriptInset::dy0() const
124 int des = down().ascent();
133 int MathScriptInset::dy1() const
138 int asc = up().descent();
148 int MathScriptInset::dx0() const
150 lyx::Assert(hasDown());
151 return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
155 int MathScriptInset::dx1() const
157 lyx::Assert(hasUp());
158 return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
162 int MathScriptInset::dxx() const
164 return hasLimits() ? (dim_.w - nwid()) / 2 : 0;
168 int MathScriptInset::nwid() const
170 return nuc().size() ? nuc().width() : 2;
174 int MathScriptInset::nasc() const
176 return nuc().size() ? nuc().ascent() : 5;
180 int MathScriptInset::ndes() const
182 return nuc().size() ? nuc().descent() : 0;
186 void MathScriptInset::metrics(MathMetricsInfo & mi) const
189 MathScriptChanger dummy(mi.base);
196 dim_.w = max(dim_.w, up().width());
198 dim_.w = max(dim_.w, down().width());
201 dim_.w = max(dim_.w, up().width());
203 dim_.w = max(dim_.w, down().width());
206 dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
207 dim_.d = dy0() + (hasDown() ? down().descent() : 0);
212 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
215 nuc().draw(pi, x + dxx(), y);
217 nuc().setXY(x + dxx(), y);
219 drawStr(pi, pi.base.font, x + dxx(), y, ".");
221 MathScriptChanger dummy(pi.base);
223 up().draw(pi, x + dx1(), y - dy1());
225 down().draw(pi, x + dx0(), y + dy0());
226 drawMarkers2(pi, x, y);
230 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
240 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
243 nuc().drawT(pain, x + dxx(), y);
245 up().drawT(pain, x + dx1(), y - dy1());
247 down().drawT(pain, x + dx0(), y + dy0());
252 bool MathScriptInset::hasLimits() const
260 // we can only display limits if the nucleus wants some
263 if (!nuc().back()->isScriptable())
266 // per default \int has limits beside the \int even in displayed formulas
267 if (nuc().back()->asSymbolInset())
268 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
271 // assume "real" limits for everything else
276 void MathScriptInset::removeEmptyScripts()
278 for (int i = 0; i <= 1; ++i)
279 if (script_[i] && cell(i).size() == 0) {
286 void MathScriptInset::removeScript(bool up)
293 bool MathScriptInset::has(bool up) const
299 bool MathScriptInset::hasUp() const
305 bool MathScriptInset::hasDown() const
311 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
317 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
323 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
327 // if we are 'up' we can't go further up
330 // otherwise go to last base position
332 pos = cell(2).size();
336 // if we are 'down' we can't go further down
340 pos = cell(2).size();
345 // don't go up/down unless in last position
346 if (pos != cell(2).size())
348 // don't go up/down if there is no cell.
351 // otherwise move into the first position
359 void MathScriptInset::write(WriteStream & os) const
363 if (nuc().back()->takesLimits()) {
371 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
376 if (hasDown() && down().size())
377 os << "_{" << down() << '}';
379 if (hasUp() && up().size())
380 os << "^{" << up() << '}';
382 if (lock_ && !os.latex())
387 void MathScriptInset::normalize(NormalStream & os) const
389 bool d = hasDown() && down().size();
390 bool u = hasUp() && up().size();
405 os << down() << ' ' << up() << ']';
413 void MathScriptInset::maplize(MapleStream & os) const
417 if (hasDown() && down().size())
418 os << '[' << down() << ']';
419 if (hasUp() && up().size())
420 os << "^(" << up() << ')';
424 void MathScriptInset::mathematicize(MathematicaStream & os) const
426 bool d = hasDown() && down().size();
427 bool u = hasUp() && up().size();
431 os << "Subscript[" << nuc();
437 os << "^(" << up() << ")";
441 os << "," << down() << "]";
445 void MathScriptInset::mathmlize( MathMLStream & os) const
447 bool d = hasDown() && down().size();
448 bool u = hasUp() && up().size();
451 os << MTag("msubsup");
463 os << down() << up() << ETag("msubsup");
465 os << up() << ETag("msup");
467 os << down() << ETag("msub");
471 void MathScriptInset::octavize(OctaveStream & os) const
475 if (hasDown() && down().size())
476 os << '[' << down() << ']';
477 if (hasUp() && up().size())
478 os << "^(" << up() << ')';
482 void MathScriptInset::infoize(std::ostream & os) const
486 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
490 MathInset::result_type MathScriptInset::dispatch
491 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
493 if (cmd.action == LFUN_MATH_LIMITS) {
494 limits_ = limits_ < 0 ? 1 : -1;
498 return MathNestInset::dispatch(cmd, idx, pos);