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 drawMarkers(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() << '}';
366 void MathScriptInset::normalize(NormalStream & os) const
368 bool d = hasDown() && down().size();
369 bool u = hasUp() && up().size();
379 os << nuc().data() << ' ';
384 os << down().data() << ' ' << up().data() << ']';
386 os << down().data() << ']';
388 os << up().data() << ']';
392 void MathScriptInset::maplize(MapleStream & os) const
396 if (hasDown() && down().size())
397 os << '[' << down().data() << ']';
398 if (hasUp() && up().size())
399 os << "^(" << up().data() << ')';
403 void MathScriptInset::mathematicize(MathematicaStream & os) const
405 bool d = hasDown() && down().size();
406 bool u = hasUp() && up().size();
410 os << "Subscript[" << nuc().data();
416 os << "^(" << up().data() << ")";
420 os << "," << down().data() << "]";
424 void MathScriptInset::mathmlize( MathMLStream & os) const
426 bool d = hasDown() && down().size();
427 bool u = hasUp() && up().size();
430 os << MTag("msubsup");
442 os << down().data() << up().data() << ETag("msubsup");
444 os << up().data() << ETag("msup");
446 os << down().data() << ETag("msub");
450 void MathScriptInset::octavize(OctaveStream & os) const
454 if (hasDown() && down().size())
455 os << '[' << down().data() << ']';
456 if (hasUp() && up().size())
457 os << "^(" << up().data() << ')';
461 void MathScriptInset::infoize(std::ostream & os) const
464 os << (limits_ == 1 ? "Displayed limits" : "Inlined limits");