2 * \file InsetMathSideset.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathSideset.h"
16 #include "InsetMathSymbol.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
21 #include "BufferView.h"
23 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "MetricsInfo.h"
29 #include "support/debug.h"
30 #include "support/lassert.h"
37 /// x spacing between the nucleus and the scripts
44 InsetMathSideset::InsetMathSideset(Buffer * buf, bool scriptl, bool scriptr)
45 : InsetMathNest(buf, 3 + scriptl + scriptr), scriptl_(scriptl),
50 InsetMathSideset::InsetMathSideset(Buffer * buf, bool scriptl, bool scriptr,
52 : InsetMathNest(buf, 3 + scriptl + scriptr), scriptl_(scriptl),
59 Inset * InsetMathSideset::clone() const
61 return new InsetMathSideset(*this);
65 bool InsetMathSideset::idxFirst(Cursor & cur) const
73 bool InsetMathSideset::idxLast(Cursor & cur) const
76 cur.pos() = nuc().size();
81 int InsetMathSideset::dybt(BufferView const & bv, int asc, int des, bool top) const
83 bool isCharBox = nuc().empty() ? false : isAlphaSymbol(nuc().back());
85 if (scriptl_ && scriptr_)
86 dasc = max(bl().dimension(bv).ascent(), br().dimension(bv).ascent());
88 dasc = bl().dimension(bv).ascent();
90 dasc = br().dimension(bv).ascent();
91 int slevel = nuc().slevel();
92 int ascdrop = dasc - slevel;
93 int desdrop = isCharBox ? 0 : des + nuc().sshift();
94 int mindes = nuc().mindes();
95 des = max(desdrop, ascdrop);
96 des = max(mindes, des);
97 int minasc = nuc().minasc();
99 if (!isCharBox && (scriptl_ || scriptr_)) {
100 if (scriptl_ && scriptr_)
101 ascdrop = asc - min(tl().mindes(), tr().mindes());
103 ascdrop = asc - tl().mindes();
105 ascdrop = asc - tr().mindes();
109 udes = bl().dimension(bv).descent();
111 udes = max(udes, br().dimension(bv).descent());
112 asc = udes + nuc().sshift();
113 asc = max(ascdrop, asc);
114 asc = max(minasc, asc);
115 int del = asc - udes - dasc;
116 if (del + des <= 2) {
117 int newdes = 2 - del;
118 del = slevel - asc + udes;
123 des = max(des, newdes);
125 return top ? asc : des;
129 int InsetMathSideset::dyb(BufferView const & bv) const
133 int des = dybt(bv, na, nd, false);
138 int InsetMathSideset::dyt(BufferView const & bv) const
142 int asc = dybt(bv, na, nd, true);
147 int InsetMathSideset::dxr(BufferView const & bv) const
149 return dxn(bv) + nwid(bv) + dx;
153 int InsetMathSideset::dxn(BufferView const & bv) const
155 Dimension const dimb = bl().dimension(bv);
156 Dimension const dimt = tl().dimension(bv);
157 return max(dimb.width(), dimt.width()) + dx;
161 int InsetMathSideset::nwid(BufferView const & bv) const
163 return nuc().dimension(bv).width();
167 int InsetMathSideset::nasc(BufferView const & bv) const
169 return nuc().dimension(bv).ascent();
173 int InsetMathSideset::ndes(BufferView const & bv) const
175 return nuc().dimension(bv).descent();
179 int InsetMathSideset::nker(BufferView const * bv) const
181 int const kerning = nuc().kerning(bv);
182 return max(kerning, 0);
186 void InsetMathSideset::metrics(MetricsInfo & mi, Dimension & dim) const
188 Changer dummy2 = mi.base.changeEnsureMath();
194 nuc().metrics(mi, dimn);
196 bl().metrics(mi, dimbl);
200 br().metrics(mi, dimbr);
203 Changer dummy = mi.base.changeScript();
205 bl().metrics(mi, dimbl);
206 tl().metrics(mi, dimtl);
209 br().metrics(mi, dimbr);
210 tr().metrics(mi, dimtr);
213 BufferView & bv = *mi.base.bv;
214 // FIXME: data copying... not very efficient.
216 dim.wid = nwid(bv) + nker(mi.base.bv) + 2 * dx;
217 dim.wid += max(dimbl.width(), dimtl.width());
218 dim.wid += max(dimbr.width(), dimtr.width());
220 int asc = dyt(bv) + max(dimtl.ascent(), dimtr.ascent());
221 dim.asc = max(na, asc);
223 int des = dyb(bv) + max(dimbl.descent(), dimbr.descent());
224 dim.des = max(nd, des);
228 void InsetMathSideset::draw(PainterInfo & pi, int x, int y) const
230 Changer dummy2 = pi.base.changeEnsureMath();
231 BufferView & bv = *pi.base.bv;
232 nuc().draw(pi, x + dxn(bv), y);
234 bl().draw(pi, x , y);
236 br().draw(pi, x + dxr(bv), y);
237 Changer dummy = pi.base.changeScript();
239 bl().draw(pi, x , y + dyb(bv));
240 tl().draw(pi, x , y - dyt(bv));
243 br().draw(pi, x + dxr(bv), y + dyb(bv));
244 tr().draw(pi, x + dxr(bv), y - dyt(bv));
249 void InsetMathSideset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
251 bl().metricsT(mi, dim);
252 tl().metricsT(mi, dim);
253 br().metricsT(mi, dim);
254 tr().metricsT(mi, dim);
255 nuc().metricsT(mi, dim);
259 void InsetMathSideset::drawT(TextPainter & pain, int x, int y) const
262 nuc().drawT(pain, x + 1, y);
263 bl().drawT(pain, x + 1, y + 1 /*dy0()*/);
264 tl().drawT(pain, x + 1, y - 1 /*dy1()*/);
265 br().drawT(pain, x + 1, y + 1 /*dy0()*/);
266 tr().drawT(pain, x + 1, y - 1 /*dy1()*/);
271 bool InsetMathSideset::idxForward(Cursor & cur) const
273 if (!scriptl_ && cur.idx() == 1) {
277 } else if (!scriptr_ && cur.idx() == 0) {
279 cur.idx() = 2 + scriptl_;
286 bool InsetMathSideset::idxBackward(Cursor & cur) const
288 if (!scriptr_ && cur.idx() == (scriptl_ ? 3 : 2)) {
292 } else if (!scriptl_ && cur.idx() == 0) {
301 bool InsetMathSideset::idxUpDown(Cursor & cur, bool up) const
304 if (cur.idx() == 0) {
305 // go up/down only if in the last position
306 // or in the first position
307 if ((scriptr_ && cur.pos() == cur.lastpos()) ||
308 (scriptl_ && cur.pos() == 0)) {
310 cur.idx() = up ? 2 : 1;
312 cur.idx() = (up ? 3 : 2) + scriptl_;
320 if ((scriptl_ && cur.idx() == 2) ||
321 (scriptr_ && cur.idx() == (scriptl_ ? 4 : 3))) {
322 // can't go further up
325 // otherwise go to first or last position in the nucleus
330 cur.pos() = cur.lastpos();
335 if ((scriptl_ && cur.idx() == 1) ||
336 (scriptr_ && cur.idx() == (scriptl_ ? 3 : 2))) {
337 // can't go further down
340 // otherwise go to first or last position in the nucleus
345 cur.pos() = cur.lastpos();
353 void InsetMathSideset::write(WriteStream & os) const
355 MathEnsurer ensurer(os);
361 os << "_{" << bl() << '}';
363 os << "^{" << tl() << '}';
369 os << "_{" << br() << '}';
371 os << "^{" << tr() << '}';
377 if (lock_ && !os.latex())
382 void InsetMathSideset::normalize(NormalStream & os) const
388 if (scriptl_ && !tl().empty())
398 if (scriptr_ && !tr().empty())
404 void InsetMathSideset::mathmlize(MathStream & os) const
406 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
408 os << MTag("mrow") << bl() << ETag("mrow");
409 if (scriptl_ || scriptr_) {
410 os << MTag("mmultiscripts");
415 os << MTag("mrow") << nuc() << ETag("mrow");
417 if (br().empty() || !scriptr_)
420 os << MTag("mrow") << br() << ETag("mrow");
421 if (tr().empty() || !scriptr_)
424 os << MTag("mrow") << tr() << ETag("mrow");
426 if (bl().empty() || !scriptl_)
429 os << MTag("mrow") << bl() << ETag("mrow");
430 if (tl().empty() || !scriptl_)
433 os << MTag("mrow") << tl() << ETag("mrow");
435 os << ETag("mmultiscripts");
438 os << MTag("mrow") << br() << ETag("mrow");
442 void InsetMathSideset::htmlize(HtmlStream & os) const
444 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
445 bool const havebl = scriptl_ && !bl().empty();
446 bool const havetl = scriptl_ && !tl().empty();
447 bool const havebr = scriptr_ && !br().empty();
448 bool const havetr = scriptr_ && !tr().empty();
450 if (!scriptl_ && !bl().empty())
453 if (havebl && havetl)
454 os << MTag("span", "class='scripts'")
455 << MTag("span") << tl() << ETag("span")
456 << MTag("span") << bl() << ETag("span")
459 os << MTag("sub", "class='math'") << bl() << ETag("sub");
461 os << MTag("sup", "class='math'") << tl() << ETag("sup");
466 if (havebr && havetr)
467 os << MTag("span", "class='scripts'")
468 << MTag("span") << tr() << ETag("span")
469 << MTag("span") << br() << ETag("span")
472 os << MTag("sub", "class='math'") << br() << ETag("sub");
474 os << MTag("sup", "class='math'") << tr() << ETag("sup");
476 if (!scriptr_ && !br().empty())
481 void InsetMathSideset::infoize(odocstream & os) const
487 // the idea for dual scripts came from the eLyXer code
488 void InsetMathSideset::validate(LaTeXFeatures & features) const
490 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
491 features.addCSSSnippet(
492 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
493 "span.scripts span {display: block;}\n"
494 "sub.math{font-size: 75%;}\n"
495 "sup.math{font-size: 75%;}");
496 features.require("amsmath");
497 InsetMathNest::validate(features);