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 "BufferView.h"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "InsetMathSideset.h"
20 #include "InsetMathSymbol.h"
21 #include "LaTeXFeatures.h"
23 #include "MathStream.h"
24 #include "MathSupport.h"
26 #include "support/debug.h"
28 #include "support/lassert.h"
35 /// x spacing between the nucleus and the scripts
42 InsetMathSideset::InsetMathSideset(Buffer * buf, bool scriptl, bool scriptr)
43 : InsetMathNest(buf, 3 + scriptl + scriptr), scriptl_(scriptl),
48 InsetMathSideset::InsetMathSideset(Buffer * buf, bool scriptl, bool scriptr,
50 : InsetMathNest(buf, 3 + scriptl + scriptr), scriptl_(scriptl),
57 Inset * InsetMathSideset::clone() const
59 return new InsetMathSideset(*this);
63 bool InsetMathSideset::idxFirst(Cursor & cur) const
71 bool InsetMathSideset::idxLast(Cursor & cur) const
74 cur.pos() = nuc().size();
79 int InsetMathSideset::dybt(BufferView const & bv, int asc, int des, bool top) const
81 bool isCharBox = nuc().empty() ? false : isAlphaSymbol(nuc().back());
83 if (scriptl_ && scriptr_)
84 dasc = max(bl().dimension(bv).ascent(), br().dimension(bv).ascent());
86 dasc = bl().dimension(bv).ascent();
88 dasc = br().dimension(bv).ascent();
89 int slevel = nuc().slevel();
90 int ascdrop = dasc - slevel;
91 int desdrop = isCharBox ? 0 : des + nuc().sshift();
92 int mindes = nuc().mindes();
93 des = max(desdrop, ascdrop);
94 des = max(mindes, des);
95 int minasc = nuc().minasc();
97 if (!isCharBox && (scriptl_ || scriptr_)) {
98 if (scriptl_ && scriptr_)
99 ascdrop = asc - min(tl().mindes(), tr().mindes());
101 ascdrop = asc - tl().mindes();
103 ascdrop = asc - tr().mindes();
107 udes = bl().dimension(bv).descent();
109 udes = max(udes, br().dimension(bv).descent());
110 asc = udes + nuc().sshift();
111 asc = max(ascdrop, asc);
112 asc = max(minasc, asc);
113 int del = asc - udes - dasc;
114 if (del + des <= 2) {
115 int newdes = 2 - del;
116 del = slevel - asc + udes;
121 des = max(des, newdes);
123 return top ? asc : des;
127 int InsetMathSideset::dyb(BufferView const & bv) const
131 int des = dybt(bv, na, nd, false);
136 int InsetMathSideset::dyt(BufferView const & bv) const
140 int asc = dybt(bv, na, nd, true);
145 int InsetMathSideset::dxr(BufferView const & bv) const
147 return dxn(bv) + nwid(bv) + dx;
151 int InsetMathSideset::dxn(BufferView const & bv) const
153 Dimension const dimb = bl().dimension(bv);
154 Dimension const dimt = tl().dimension(bv);
155 return max(dimb.width(), dimt.width()) + dx;
159 int InsetMathSideset::nwid(BufferView const & bv) const
161 return nuc().dimension(bv).width();
165 int InsetMathSideset::nasc(BufferView const & bv) const
167 return nuc().dimension(bv).ascent();
171 int InsetMathSideset::ndes(BufferView const & bv) const
173 return nuc().dimension(bv).descent();
177 int InsetMathSideset::nker(BufferView const * bv) const
179 int const kerning = nuc().kerning(bv);
180 return max(kerning, 0);
184 void InsetMathSideset::metrics(MetricsInfo & mi, Dimension & dim) const
191 nuc().metrics(mi, dimn);
193 bl().metrics(mi, dimbl);
197 br().metrics(mi, dimbr);
200 ScriptChanger dummy(mi.base);
202 bl().metrics(mi, dimbl);
203 tl().metrics(mi, dimtl);
206 br().metrics(mi, dimbr);
207 tr().metrics(mi, dimtr);
210 BufferView & bv = *mi.base.bv;
211 // FIXME: data copying... not very efficient.
213 dim.wid = nwid(bv) + nker(mi.base.bv) + 2 * dx;
214 dim.wid += max(dimbl.width(), dimtl.width());
215 dim.wid += max(dimbr.width(), dimtr.width());
217 int asc = dyt(bv) + max(dimtl.ascent(), dimtr.ascent());
218 dim.asc = max(na, asc);
220 int des = dyb(bv) + max(dimbl.descent(), dimbr.descent());
221 dim.des = max(nd, des);
226 void InsetMathSideset::draw(PainterInfo & pi, int x, int y) const
228 BufferView & bv = *pi.base.bv;
229 nuc().draw(pi, x + dxn(bv), y);
231 bl().draw(pi, x , y);
233 br().draw(pi, x + dxr(bv), y);
234 ScriptChanger dummy(pi.base);
236 bl().draw(pi, x , y + dyb(bv));
237 tl().draw(pi, x , y - dyt(bv));
240 br().draw(pi, x + dxr(bv), y + dyb(bv));
241 tr().draw(pi, x + dxr(bv), y - dyt(bv));
243 drawMarkers(pi, x, y);
247 void InsetMathSideset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
249 bl().metricsT(mi, dim);
250 tl().metricsT(mi, dim);
251 br().metricsT(mi, dim);
252 tr().metricsT(mi, dim);
253 nuc().metricsT(mi, dim);
257 void InsetMathSideset::drawT(TextPainter & pain, int x, int y) const
260 nuc().drawT(pain, x + 1, y);
261 bl().drawT(pain, x + 1, y + 1 /*dy0()*/);
262 tl().drawT(pain, x + 1, y - 1 /*dy1()*/);
263 br().drawT(pain, x + 1, y + 1 /*dy0()*/);
264 tr().drawT(pain, x + 1, y - 1 /*dy1()*/);
269 bool InsetMathSideset::idxForward(Cursor & cur) const
271 if (!scriptl_ && cur.idx() == 1) {
275 } else if (!scriptr_ && cur.idx() == 0) {
277 cur.idx() = 2 + scriptl_;
284 bool InsetMathSideset::idxBackward(Cursor & cur) const
286 if (!scriptr_ && cur.idx() == (scriptl_ ? 3 : 2)) {
290 } else if (!scriptl_ && cur.idx() == 0) {
299 bool InsetMathSideset::idxUpDown(Cursor & cur, bool up) const
302 if (cur.idx() == 0) {
303 // go up/down only if in the last position
304 // or in the first position
305 if ((scriptr_ && cur.pos() == cur.lastpos()) ||
306 (scriptl_ && cur.pos() == 0)) {
308 cur.idx() = up ? 2 : 1;
310 cur.idx() = (up ? 3 : 2) + scriptl_;
318 if ((scriptl_ && cur.idx() == 2) ||
319 (scriptr_ && cur.idx() == (scriptl_ ? 4 : 3))) {
320 // can't go further up
323 // otherwise go to first or last position in the nucleus
328 cur.pos() = cur.lastpos();
333 if ((scriptl_ && cur.idx() == 1) ||
334 (scriptr_ && cur.idx() == (scriptl_ ? 3 : 2))) {
335 // can't go further down
338 // otherwise go to first or last position in the nucleus
343 cur.pos() = cur.lastpos();
351 void InsetMathSideset::write(WriteStream & os) const
353 MathEnsurer ensurer(os);
359 os << "_{" << bl() << '}';
361 os << "^{" << tl() << '}';
367 os << "_{" << br() << '}';
369 os << "^{" << tr() << '}';
375 if (lock_ && !os.latex())
380 void InsetMathSideset::normalize(NormalStream & os) const
386 if (scriptl_ && !tl().empty())
396 if (scriptr_ && !tr().empty())
402 void InsetMathSideset::mathmlize(MathStream & os) const
404 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
406 os << MTag("mrow") << bl() << ETag("mrow");
407 if (scriptl_ || scriptr_) {
408 os << MTag("mmultiscripts");
413 os << MTag("mrow") << nuc() << ETag("mrow");
415 if (br().empty() || !scriptr_)
418 os << MTag("mrow") << br() << ETag("mrow");
419 if (tr().empty() || !scriptr_)
422 os << MTag("mrow") << tr() << ETag("mrow");
424 if (bl().empty() || !scriptl_)
427 os << MTag("mrow") << bl() << ETag("mrow");
428 if (tl().empty() || !scriptl_)
431 os << MTag("mrow") << tl() << ETag("mrow");
433 os << ETag("mmultiscripts");
436 os << MTag("mrow") << br() << ETag("mrow");
440 void InsetMathSideset::htmlize(HtmlStream & os) const
442 // FIXME This is only accurate if both scriptl_ and scriptr_ are true
443 bool const havebl = scriptl_ && !bl().empty();
444 bool const havetl = scriptl_ && !tl().empty();
445 bool const havebr = scriptr_ && !br().empty();
446 bool const havetr = scriptr_ && !tr().empty();
448 if (!scriptl_ && !bl().empty())
451 if (havebl && havetl)
452 os << MTag("span", "class='scripts'")
453 << MTag("span") << tl() << ETag("span")
454 << MTag("span") << bl() << ETag("span")
457 os << MTag("sub", "class='math'") << bl() << ETag("sub");
459 os << MTag("sup", "class='math'") << tl() << ETag("sup");
464 if (havebr && havetr)
465 os << MTag("span", "class='scripts'")
466 << MTag("span") << tr() << ETag("span")
467 << MTag("span") << br() << ETag("span")
470 os << MTag("sub", "class='math'") << br() << ETag("sub");
472 os << MTag("sup", "class='math'") << tr() << ETag("sup");
474 if (!scriptr_ && !br().empty())
479 void InsetMathSideset::infoize(odocstream & os) const
485 // the idea for dual scripts came from the eLyXer code
486 void InsetMathSideset::validate(LaTeXFeatures & features) const
488 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
489 features.addCSSSnippet(
490 "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
491 "span.scripts span {display: block;}\n"
492 "sub.math{font-size: 75%;}\n"
493 "sup.math{font-size: 75%;}");
494 features.require("amsmath");
495 InsetMathNest::validate(features);