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 drawStr(pi, pi.base.font, x + dxx(), y, ".");
218 MathScriptChanger dummy(pi.base);
220 up().draw(pi, x + dx1(), y - dy1());
222 down().draw(pi, x + dx0(), y + dy0());
223 drawMarkers2(pi, x, y);
227 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
237 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
240 nuc().drawT(pain, x + dxx(), y);
242 up().drawT(pain, x + dx1(), y - dy1());
244 down().drawT(pain, x + dx0(), y + dy0());
249 bool MathScriptInset::hasLimits() const
257 // we can only display limits if the nucleus wants some
260 if (!nuc().back()->isScriptable())
263 // per default \int has limits beside the \int even in displayed formulas
264 if (nuc().back()->asSymbolInset())
265 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
268 // assume "real" limits for everything else
273 void MathScriptInset::removeEmptyScripts()
275 for (int i = 0; i <= 1; ++i)
276 if (script_[i] && cell(i).size() == 0) {
283 void MathScriptInset::removeScript(bool up)
290 bool MathScriptInset::has(bool up) const
296 bool MathScriptInset::hasUp() const
302 bool MathScriptInset::hasDown() const
308 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
314 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
320 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
324 // if we are 'up' we can't go further up
327 // otherwise go to last base position
329 pos = cell(2).size();
333 // if we are 'down' we can't go further down
337 pos = cell(2).size();
342 // don't go up/down unless in last position
343 if (pos != cell(2).size())
345 // don't go up/down if there is no cell.
348 // otherwise move into the first position
356 void MathScriptInset::write(WriteStream & os) const
360 if (nuc().back()->takesLimits()) {
368 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
373 if (hasDown() && down().size())
374 os << "_{" << down() << '}';
376 if (hasUp() && up().size())
377 os << "^{" << up() << '}';
379 if (lock_ && !os.latex())
384 void MathScriptInset::normalize(NormalStream & os) const
386 bool d = hasDown() && down().size();
387 bool u = hasUp() && up().size();
402 os << down() << ' ' << up() << ']';
410 void MathScriptInset::maplize(MapleStream & os) const
414 if (hasDown() && down().size())
415 os << '[' << down() << ']';
416 if (hasUp() && up().size())
417 os << "^(" << up() << ')';
421 void MathScriptInset::mathematicize(MathematicaStream & os) const
423 bool d = hasDown() && down().size();
424 bool u = hasUp() && up().size();
428 os << "Subscript[" << nuc();
434 os << "^(" << up() << ")";
438 os << "," << down() << "]";
442 void MathScriptInset::mathmlize( MathMLStream & os) const
444 bool d = hasDown() && down().size();
445 bool u = hasUp() && up().size();
448 os << MTag("msubsup");
460 os << down() << up() << ETag("msubsup");
462 os << up() << ETag("msup");
464 os << down() << ETag("msub");
468 void MathScriptInset::octavize(OctaveStream & os) const
472 if (hasDown() && down().size())
473 os << '[' << down() << ']';
474 if (hasUp() && up().size())
475 os << "^(" << up() << ')';
479 void MathScriptInset::infoize(std::ostream & os) const
483 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
487 MathInset::result_type MathScriptInset::dispatch
488 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
490 if (cmd.action == LFUN_MATH_LIMITS) {
491 limits_ = limits_ < 0 ? 1 : -1;
495 return MathNestInset::dispatch(cmd, idx, pos);