2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file InsetMathRoot.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
2003-08-19 13:00:56 +00:00
|
|
|
* \author Alejandro Aguilar Sierra
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
2003-08-19 13:00:56 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
1999-09-27 18:44:28 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-19 13:00:56 +00:00
|
|
|
#include <config.h>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathRoot.h"
|
2010-03-31 20:31:04 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
#include "MathStream.h"
|
2017-03-31 16:50:14 +00:00
|
|
|
#include "MathSupport.h"
|
2016-06-04 08:41:13 +00:00
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
#include "Cursor.h"
|
2016-06-04 08:41:13 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
|
|
|
#include "MetricsInfo.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2004-01-20 14:25:24 +00:00
|
|
|
#include "frontends/Painter.h"
|
2001-10-22 15:37:49 +00:00
|
|
|
|
2018-11-06 07:29:47 +00:00
|
|
|
#include "support/lassert.h"
|
2016-06-04 08:41:13 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2017-12-04 09:44:49 +00:00
|
|
|
using namespace frontend;
|
2004-01-20 14:25:24 +00:00
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathRoot::InsetMathRoot(Buffer * buf)
|
|
|
|
: InsetMathNest(buf, 2)
|
2001-02-28 11:56:36 +00:00
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathRoot::clone() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathRoot(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 09:44:49 +00:00
|
|
|
void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
|
|
|
|
MathData const * root, Dimension & dim)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2016-11-22 10:26:15 +00:00
|
|
|
Changer dummy = mi.base.changeEnsureMath();
|
2017-12-04 09:44:49 +00:00
|
|
|
Dimension dimr;
|
|
|
|
if (root) {
|
2019-06-14 14:42:02 +00:00
|
|
|
Changer script = mi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
|
2017-03-31 16:50:14 +00:00
|
|
|
// make sure that the dim is high enough for any character
|
2018-04-19 11:15:43 +00:00
|
|
|
root->metrics(mi, dimr, false);
|
2017-03-31 16:50:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 09:44:49 +00:00
|
|
|
Dimension dimn;
|
|
|
|
nucleus.metrics(mi, dimn);
|
|
|
|
|
|
|
|
// Some room for the decoration
|
|
|
|
// The width of left decoration was 9 pixels with a 10em font
|
|
|
|
int const w = 9 * mathed_font_em(mi.base.font) / 10;
|
|
|
|
/* See rule 11 in Appendix G of Rhe TeXbook for the computation of the spacing
|
|
|
|
* above nucleus.
|
|
|
|
* FIXME more work is needed to implement properly rule 11.
|
|
|
|
* * Ideally, we should use sqrt glyphs from the math fonts. Note
|
|
|
|
that then we would get rule thickness from there.
|
|
|
|
* * The positioning of the root MathData is arbitrary. It should
|
|
|
|
* follow the definition of \root...\of... in The Texbook in
|
|
|
|
* Apprendix B page 360.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int const t = mi.base.solidLineThickness();
|
|
|
|
int const x_height = mathed_font_x_height(mi.base.font);
|
2019-06-14 14:42:02 +00:00
|
|
|
int const phi = (mi.base.font.style() == DISPLAY_STYLE) ? x_height : t;
|
2017-12-04 09:44:49 +00:00
|
|
|
// first part is the spacing, second part is the line width
|
|
|
|
// itself, and last one is the spacing above.
|
|
|
|
int const space_above = (t + phi / 4) + t + t;
|
|
|
|
int const a = dimn.ascent();
|
|
|
|
int const d = dimn.descent();
|
|
|
|
// Not sure what the 1 stands for, it is needed to have some spacing at small sizes.
|
|
|
|
dim.asc = max(dimr.ascent() + (d - a) / 2, a + space_above) + 1;
|
|
|
|
dim.des = max(dimr.descent() - (d - a) / 2, d);
|
|
|
|
dim.wid = max(dimr.width() + 3 * w / 8, w) + dimn.width();
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:50:14 +00:00
|
|
|
|
2017-12-04 09:44:49 +00:00
|
|
|
void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
mathed_root_metrics(mi, cell(0), &cell(1), dim);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 09:44:49 +00:00
|
|
|
void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
|
|
|
|
MathData const * root, Dimension const & dim)
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2016-11-22 10:26:15 +00:00
|
|
|
Changer dummy = pi.base.changeEnsureMath();
|
2017-12-04 09:44:49 +00:00
|
|
|
// The width of left decoration was 9 pixels with a 10em font
|
|
|
|
int const w = 9 * mathed_font_em(pi.base.font) / 10;
|
|
|
|
// the height of the hook was 5 with a 10em font
|
|
|
|
int const h = 5 * mathed_font_em(pi.base.font) / 10;
|
2017-03-31 16:50:14 +00:00
|
|
|
int const a = dim.ascent();
|
|
|
|
int const d = dim.descent();
|
2017-12-04 09:44:49 +00:00
|
|
|
int const t = pi.base.solidLineThickness();
|
2020-10-31 13:09:46 +00:00
|
|
|
Dimension const & dimn = nucleus.dimension(*pi.base.bv);
|
2017-12-04 09:44:49 +00:00
|
|
|
// the width of the left part of the root
|
|
|
|
int const wl = dim.width() - dimn.width();
|
2002-02-16 15:59:55 +00:00
|
|
|
// the "exponent"
|
2017-12-04 09:44:49 +00:00
|
|
|
if (root) {
|
2019-06-14 14:42:02 +00:00
|
|
|
Changer script = pi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
|
2020-10-31 13:09:46 +00:00
|
|
|
Dimension const & dimr = root->dimension(*pi.base.bv);
|
2017-12-04 09:44:49 +00:00
|
|
|
int const root_offset = wl - 3 * w / 8 - dimr.width();
|
|
|
|
root->draw(pi, x + root_offset, y + (d - a)/2);
|
2017-03-31 16:50:14 +00:00
|
|
|
}
|
2002-02-16 15:59:55 +00:00
|
|
|
// the "base"
|
2017-12-04 09:44:49 +00:00
|
|
|
nucleus.draw(pi, x + wl, y);
|
2007-05-19 10:52:47 +00:00
|
|
|
int xp[4];
|
|
|
|
int yp[4];
|
2017-12-04 09:44:49 +00:00
|
|
|
pi.pain.line(x + dim.width(), y - a + 2 * t,
|
|
|
|
x + wl, y - a + 2 * t, pi.base.font.color(),
|
|
|
|
Painter::line_solid, t);
|
2017-12-04 10:20:13 +00:00
|
|
|
xp[0] = x + wl; yp[0] = y - a + 2 * t + 1;
|
2017-12-04 09:44:49 +00:00
|
|
|
xp[1] = x + wl - w / 2; yp[1] = y + d;
|
|
|
|
xp[2] = x + wl - w + h / 4; yp[2] = y + d - h;
|
|
|
|
xp[3] = x + wl - w; yp[3] = y + d - h + h / 4;
|
|
|
|
pi.pain.lines(xp, yp, 4, pi.base.font.color(),
|
|
|
|
Painter::fill_none, Painter::line_solid, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathRoot::write(WriteStream & os) const
|
2000-03-07 01:14:37 +00:00
|
|
|
{
|
2008-06-17 11:10:43 +00:00
|
|
|
MathEnsurer ensurer(os);
|
2018-11-06 07:29:47 +00:00
|
|
|
os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2001-04-25 15:43:57 +00:00
|
|
|
|
2001-04-27 12:35:55 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathRoot::normalize(NormalStream & os) const
|
2001-04-25 15:43:57 +00:00
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
os << "[root " << cell(1) << ' ' << cell(0) << ']';
|
2001-04-25 15:43:57 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
|
2001-06-25 00:06:33 +00:00
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
Cursor::idx_type const target = up; //up ? 1 : 0;
|
2004-01-15 17:34:44 +00:00
|
|
|
if (cur.idx() == target)
|
2001-06-25 00:06:33 +00:00
|
|
|
return false;
|
2004-01-15 17:34:44 +00:00
|
|
|
cur.idx() = target;
|
|
|
|
cur.pos() = up ? cur.lastpos() : 0;
|
2001-06-25 00:06:33 +00:00
|
|
|
return true;
|
2018-11-07 21:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathRoot::idxForward(Cursor & cur) const
|
|
|
|
{
|
|
|
|
// nucleus is 0 and is on the right
|
|
|
|
if (cur.idx() == 0)
|
|
|
|
return false;
|
|
|
|
|
2018-11-09 05:22:04 +00:00
|
|
|
cur.idx() = 0;
|
|
|
|
cur.pos() = 0;
|
2018-11-07 21:53:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathRoot::idxBackward(Cursor & cur) const
|
|
|
|
{
|
|
|
|
// nucleus is 0 and is on the right
|
|
|
|
if (cur.idx() == 1)
|
|
|
|
return false;
|
|
|
|
|
2018-11-09 05:22:04 +00:00
|
|
|
cur.idx() = 1;
|
|
|
|
cur.pos() = cur.lastpos();
|
2018-11-07 21:53:00 +00:00
|
|
|
return true;
|
2018-11-10 13:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathRoot::idxFirst(Cursor & cur) const
|
|
|
|
{
|
|
|
|
LASSERT(&cur.inset() == this, return false);
|
|
|
|
cur.idx() = 1;
|
|
|
|
cur.pos() = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathRoot::idxLast(Cursor & cur) const
|
|
|
|
{
|
|
|
|
LASSERT(&cur.inset() == this, return false);
|
|
|
|
cur.idx() = 0;
|
|
|
|
cur.pos() = cur.lastpos();
|
|
|
|
return true;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
2001-11-07 13:15:59 +00:00
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathRoot::maple(MapleStream & os) const
|
2002-10-28 17:15:19 +00:00
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
|
2002-10-28 17:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 03:28:53 +00:00
|
|
|
void InsetMathRoot::mathematica(MathematicaStream & os) const
|
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
|
2007-01-07 03:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathRoot::octave(OctaveStream & os) const
|
2001-11-07 13:15:59 +00:00
|
|
|
{
|
2018-11-06 07:29:47 +00:00
|
|
|
os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
|
2001-11-07 17:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-09 23:52:07 +00:00
|
|
|
void InsetMathRoot::mathmlize(MathStream & ms) const
|
2001-11-07 17:30:26 +00:00
|
|
|
{
|
2019-05-09 23:52:07 +00:00
|
|
|
ms << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
|
2001-11-07 13:15:59 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2010-03-31 20:31:04 +00:00
|
|
|
void InsetMathRoot::htmlize(HtmlStream & os) const
|
|
|
|
{
|
|
|
|
os << MTag("span", "class='root'")
|
2018-11-06 07:29:47 +00:00
|
|
|
<< MTag("sup") << cell(1) << ETag("sup")
|
2017-07-03 17:53:14 +00:00
|
|
|
<< from_ascii("√")
|
2018-11-06 07:29:47 +00:00
|
|
|
<< MTag("span", "class='rootof'") << cell(0) << ETag("span")
|
2010-03-31 20:31:04 +00:00
|
|
|
<< ETag("span");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathRoot::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
|
|
|
if (features.runparams().math_flavor == OutputParams::MathAsHTML)
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet(
|
2010-03-31 20:31:04 +00:00
|
|
|
"span.rootof{border-top: thin solid black;}\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"span.root sup{font-size: 75%;}");
|
2010-03-31 21:24:16 +00:00
|
|
|
InsetMathNest::validate(features);
|
2010-03-31 20:31:04 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|