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);
225 metricsMarkers(mi, dim);
229 void InsetMathSideset::draw(PainterInfo & pi, int x, int y) const
231 Changer dummy2 = pi.base.changeEnsureMath();
232 BufferView & bv = *pi.base.bv;
233 nuc().draw(pi, x + dxn(bv), y);
235 bl().draw(pi, x , y);
237 br().draw(pi, x + dxr(bv), y);
238 Changer dummy = pi.base.changeScript();
240 bl().draw(pi, x , y + dyb(bv));
241 tl().draw(pi, x , y - dyt(bv));
244 br().draw(pi, x + dxr(bv), y + dyb(bv));
245 tr().draw(pi, x + dxr(bv), y - dyt(bv));
247 drawMarkers(pi, x, y);
251 void InsetMathSideset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
253 bl().metricsT(mi, dim);
254 tl().metricsT(mi, dim);
255 br().metricsT(mi, dim);
256 tr().metricsT(mi, dim);
257 nuc().metricsT(mi, dim);
261 void InsetMathSideset::drawT(TextPainter & pain, int x, int y) const
264 nuc().drawT(pain, x + 1, y);
265 bl().drawT(pain, x + 1, y + 1 /*dy0()*/);
266 tl().drawT(pain, x + 1, y - 1 /*dy1()*/);
267 br().drawT(pain, x + 1, y + 1 /*dy0()*/);
268 tr().drawT(pain, x + 1, y - 1 /*dy1()*/);
273 bool InsetMathSideset::idxForward(Cursor & cur) const
275 if (!scriptl_ && cur.idx() == 1) {
279 } else if (!scriptr_ && cur.idx() == 0) {
281 cur.idx() = 2 + scriptl_;
288 bool InsetMathSideset::idxBackward(Cursor & cur) const
290 if (!scriptr_ && cur.idx() == (scriptl_ ? 3 : 2)) {
294 } else if (!scriptl_ && cur.idx() == 0) {
303 bool InsetMathSideset::idxUpDown(Cursor & cur, bool up) const
306 if (cur.idx() == 0) {
307 // go up/down only if in the last position
308 // or in the first position
309 if ((scriptr_ && cur.pos() == cur.lastpos()) ||
310 (scriptl_ && cur.pos() == 0)) {
312 cur.idx() = up ? 2 : 1;
314 cur.idx() = (up ? 3 : 2) + scriptl_;
322 if ((scriptl_ && cur.idx() == 2) ||
323 (scriptr_ && cur.idx() == (scriptl_ ? 4 : 3))) {
324 // can't go further up
327 // otherwise go to first or last position in the nucleus
332 cur.pos() = cur.lastpos();
337 if ((scriptl_ && cur.idx() == 1) ||
338 (scriptr_ && cur.idx() == (scriptl_ ? 3 : 2))) {
339 // can't go further down
342 // otherwise go to first or last position in the nucleus
347 cur.pos() = cur.lastpos();
355 void InsetMathSideset::write(WriteStream & os) const
357 MathEnsurer ensurer(os);
363 os << "_{" << bl() << '}';
365 os << "^{" << tl() << '}';
371 os << "_{" << br() << '}';
373 os << "^{" << tr() << '}';
379 if (lock_ && !os.latex())
384 void InsetMathSideset::normalize(NormalStream & os) const
390 if (scriptl_ && !tl().empty())
400 if (scriptr_ && !tr().empty())
406 void InsetMathSideset::mathmlize(MathStream & os) const
408 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
410 os << MTag("mrow") << bl() << ETag("mrow");
411 if (scriptl_ || scriptr_) {
412 os << MTag("mmultiscripts");
417 os << MTag("mrow") << nuc() << ETag("mrow");
419 if (br().empty() || !scriptr_)
422 os << MTag("mrow") << br() << ETag("mrow");
423 if (tr().empty() || !scriptr_)
426 os << MTag("mrow") << tr() << ETag("mrow");
428 if (bl().empty() || !scriptl_)
431 os << MTag("mrow") << bl() << ETag("mrow");
432 if (tl().empty() || !scriptl_)
435 os << MTag("mrow") << tl() << ETag("mrow");
437 os << ETag("mmultiscripts");
440 os << MTag("mrow") << br() << ETag("mrow");
444 void InsetMathSideset::htmlize(HtmlStream & os) const
446 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
447 bool const havebl = scriptl_ && !bl().empty();
448 bool const havetl = scriptl_ && !tl().empty();
449 bool const havebr = scriptr_ && !br().empty();
450 bool const havetr = scriptr_ && !tr().empty();
452 if (!scriptl_ && !bl().empty())
455 if (havebl && havetl)
456 os << MTag("span", "class='scripts'")
457 << MTag("span") << tl() << ETag("span")
458 << MTag("span") << bl() << ETag("span")
461 os << MTag("sub", "class='math'") << bl() << ETag("sub");
463 os << MTag("sup", "class='math'") << tl() << ETag("sup");
468 if (havebr && havetr)
469 os << MTag("span", "class='scripts'")
470 << MTag("span") << tr() << ETag("span")
471 << MTag("span") << br() << ETag("span")
474 os << MTag("sub", "class='math'") << br() << ETag("sub");
476 os << MTag("sup", "class='math'") << tr() << ETag("sup");
478 if (!scriptr_ && !br().empty())
483 void InsetMathSideset::infoize(odocstream & os) const
489 // the idea for dual scripts came from the eLyXer code
490 void InsetMathSideset::validate(LaTeXFeatures & features) const
492 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
493 features.addCSSSnippet(
494 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
495 "span.scripts span {display: block;}\n"
496 "sub.math{font-size: 75%;}\n"
497 "sup.math{font-size: 75%;}");
498 features.require("amsmath");
499 InsetMathNest::validate(features);