5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
16 MathScriptInset::MathScriptInset()
17 : MathNestInset(3), limits_(0)
24 MathScriptInset::MathScriptInset(bool up)
25 : MathNestInset(3), limits_(0)
32 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
33 : MathNestInset(3), limits_(0)
37 cell(2).push_back(at);
42 MathInset * MathScriptInset::clone() const
44 return new MathScriptInset(*this);
48 MathScriptInset const * MathScriptInset::asScriptInset() const
54 MathScriptInset * MathScriptInset::asScriptInset()
60 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
68 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
76 MathXArray const & MathScriptInset::down() const
82 MathXArray & MathScriptInset::down()
88 MathXArray const & MathScriptInset::up() const
94 MathXArray & MathScriptInset::up()
100 void MathScriptInset::ensure(bool up)
106 MathXArray const & MathScriptInset::nuc() const
112 MathXArray & MathScriptInset::nuc()
118 int MathScriptInset::dy0() const
123 int des = down().ascent();
132 int MathScriptInset::dy1() const
137 int asc = up().descent();
142 asc = max(asc, mathed_char_ascent(font_, 'I'));
147 int MathScriptInset::dx0() const
149 lyx::Assert(hasDown());
150 return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
154 int MathScriptInset::dx1() const
156 lyx::Assert(hasUp());
157 return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
161 int MathScriptInset::dxx() const
163 return hasLimits() ? (dim_.w - nwid()) / 2 : 0;
167 int MathScriptInset::nwid() const
169 return nuc().size() ? nuc().width() : mathed_char_width(font_, '.');
173 int MathScriptInset::nasc() const
175 return nuc().size() ? nuc().ascent() : mathed_char_ascent(font_, 'I');
179 int MathScriptInset::ndes() const
181 return nuc().size() ? nuc().descent() : mathed_char_descent(font_, 'I');
185 void MathScriptInset::metrics(MathMetricsInfo & mi) const
187 MathNestInset::metrics(mi);
188 MathScriptChanger dummy(mi.base);
193 dim_.w = max(dim_.w, up().width());
195 dim_.w = max(dim_.w, down().width());
198 dim_.w = max(dim_.w, up().width());
200 dim_.w = max(dim_.w, down().width());
203 dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
204 dim_.d = dy0() + (hasDown() ? down().descent() : 0);
209 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
212 nuc().draw(pi, x + dxx(), y);
214 drawStr(pi, font_, x + dxx(), y, ".");
215 MathScriptChanger dummy(pi.base);
217 up().draw(pi, x + dx1(), y - dy1());
219 down().draw(pi, x + dx0(), y + dy0());
220 drawMarkers2(pi, x, y);
224 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
234 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
237 nuc().drawT(pain, x + dxx(), y);
239 up().drawT(pain, x + dx1(), y - dy1());
241 down().drawT(pain, x + dx0(), y + dy0());
246 bool MathScriptInset::hasLimits() const
254 // we can only display limits if the nucleus wants some
257 if (!nuc().back()->isScriptable())
260 // per default \int has limits beside the \int even in displayed formulas
261 if (nuc().back()->asSymbolInset())
262 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
265 // assume "real" limits for everything else
270 void MathScriptInset::removeEmptyScripts()
272 for (int i = 0; i <= 1; ++i)
273 if (script_[i] && cell(i).size() == 0) {
280 void MathScriptInset::removeScript(bool up)
287 bool MathScriptInset::has(bool up) const
293 bool MathScriptInset::empty() const
295 return !script_[0] && !script_[1] && cell(2).empty();
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,
326 if ((idx == 1 && up) || (idx == 0 && !up))
335 pos = cell(2).size();
341 void MathScriptInset::write(WriteStream & os) const
345 if (nuc().back()->takesLimits()) {
353 lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
358 if (hasDown() && down().size())
359 os << "_{" << down().data() << '}';
361 if (hasUp() && up().size())
362 os << "^{" << up().data() << '}';
364 if (lock_ && !os.latex())
369 void MathScriptInset::normalize(NormalStream & os) const
371 bool d = hasDown() && down().size();
372 bool u = hasUp() && up().size();
382 os << nuc().data() << ' ';
387 os << down().data() << ' ' << up().data() << ']';
389 os << down().data() << ']';
391 os << up().data() << ']';
395 void MathScriptInset::maplize(MapleStream & os) const
399 if (hasDown() && down().size())
400 os << '[' << down().data() << ']';
401 if (hasUp() && up().size())
402 os << "^(" << up().data() << ')';
406 void MathScriptInset::mathematicize(MathematicaStream & os) const
408 bool d = hasDown() && down().size();
409 bool u = hasUp() && up().size();
413 os << "Subscript[" << nuc().data();
419 os << "^(" << up().data() << ")";
423 os << "," << down().data() << "]";
427 void MathScriptInset::mathmlize( MathMLStream & os) const
429 bool d = hasDown() && down().size();
430 bool u = hasUp() && up().size();
433 os << MTag("msubsup");
445 os << down().data() << up().data() << ETag("msubsup");
447 os << up().data() << ETag("msup");
449 os << down().data() << ETag("msub");
453 void MathScriptInset::octavize(OctaveStream & os) const
457 if (hasDown() && down().size())
458 os << '[' << down().data() << ']';
459 if (hasUp() && up().size())
460 os << "^(" << up().data() << ')';
464 void MathScriptInset::infoize(std::ostream & os) const
468 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");