2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-10-25 21:53:06 +00:00
|
|
|
* \file InsetMathFracBase.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.
|
|
|
|
*
|
|
|
|
* \author Alejandro Aguilar Sierra
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2009-02-28 02:43:26 +00:00
|
|
|
* \author Uwe Stöhr
|
2003-08-19 13:00:56 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathFrac.h"
|
2007-10-25 21:53:06 +00:00
|
|
|
|
|
|
|
#include "Cursor.h"
|
|
|
|
#include "LaTeXFeatures.h"
|
2007-04-26 16:05:57 +00:00
|
|
|
#include "MathData.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
#include "MathStream.h"
|
2007-10-25 21:53:06 +00:00
|
|
|
#include "MathSupport.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "MetricsInfo.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "TextPainter.h"
|
2007-10-25 21:53:06 +00:00
|
|
|
|
2003-09-07 21:25:37 +00:00
|
|
|
#include "frontends/Painter.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
|
2016-05-23 21:30:23 +00:00
|
|
|
#include "support/lassert.h"
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2016-05-23 21:30:23 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2007-10-25 21:53:06 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// InsetMathFracBase
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
|
|
|
|
: InsetMathNest(buf, ncells)
|
2007-10-25 21:53:06 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
|
|
|
|
{
|
2009-11-26 01:56:50 +00:00
|
|
|
// If we only have one cell, target = 0, otherwise
|
|
|
|
// target = up ? 0 : 1, since upper cell has idx 0
|
|
|
|
InsetMath::idx_type target = nargs() > 1 ? !up : 0;
|
2007-10-25 21:53:06 +00:00
|
|
|
if (cur.idx() == target)
|
|
|
|
return false;
|
|
|
|
cur.idx() = target;
|
2007-12-24 10:52:58 +00:00
|
|
|
cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
|
2007-10-25 21:53:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// InsetMathFrac
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
|
|
|
|
: InsetMathFracBase(buf, ncells), kind_(kind)
|
2001-07-09 10:19:50 +00:00
|
|
|
{}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathFrac::clone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathFrac(*this);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathFrac * InsetMathFrac::asFracInset()
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
return kind_ == ATOP ? 0 : this;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathFrac const * InsetMathFrac::asFracInset() const
|
2002-08-08 16:08:11 +00:00
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
return kind_ == ATOP ? 0 : this;
|
2002-08-08 16:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-05 19:44:03 +00:00
|
|
|
bool InsetMathFrac::idxForward(Cursor & cur) const
|
2007-09-15 16:39:51 +00:00
|
|
|
{
|
2007-09-30 17:56:49 +00:00
|
|
|
InsetMath::idx_type target = 0;
|
2009-04-06 23:58:42 +00:00
|
|
|
if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
|
2007-09-18 15:04:58 +00:00
|
|
|
if (nargs() == 3)
|
|
|
|
target = 0;
|
|
|
|
else if (nargs() == 2)
|
|
|
|
target = 1;
|
|
|
|
} else
|
2007-09-30 17:56:49 +00:00
|
|
|
return false;
|
2007-09-15 16:39:51 +00:00
|
|
|
if (cur.idx() == target)
|
|
|
|
return false;
|
|
|
|
cur.idx() = target;
|
2007-12-24 10:52:58 +00:00
|
|
|
cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
|
2007-09-15 16:39:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-05 19:44:03 +00:00
|
|
|
bool InsetMathFrac::idxBackward(Cursor & cur) const
|
2007-09-15 16:39:51 +00:00
|
|
|
{
|
2007-09-30 17:56:49 +00:00
|
|
|
InsetMath::idx_type target = 0;
|
2009-04-06 23:58:42 +00:00
|
|
|
if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
|
2007-09-18 15:04:58 +00:00
|
|
|
if (nargs() == 3)
|
|
|
|
target = 2;
|
|
|
|
else if (nargs() == 2)
|
|
|
|
target = 0;
|
|
|
|
} else
|
2007-09-30 17:56:49 +00:00
|
|
|
return false;
|
2007-09-15 16:39:51 +00:00
|
|
|
if (cur.idx() == target)
|
|
|
|
return false;
|
|
|
|
cur.idx() = target;
|
2007-12-24 10:52:58 +00:00
|
|
|
cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
|
2007-09-15 16:39:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2007-09-24 13:52:04 +00:00
|
|
|
Dimension dim0, dim1, dim2;
|
|
|
|
|
2016-05-23 21:30:23 +00:00
|
|
|
// This could be simplified, including avoiding useless recalculation of
|
|
|
|
// cell metrics
|
2007-09-18 15:04:58 +00:00
|
|
|
if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
|
|
|
|
if (nargs() == 1) {
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = mi.base.font.changeShape(UP_SHAPE);
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
dim.wid = dim0.width()+ 3;
|
|
|
|
dim.asc = dim0.asc;
|
|
|
|
dim.des = dim0.des;
|
2007-09-18 15:04:58 +00:00
|
|
|
} else if (nargs() == 2) {
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(0).metrics(mi, dim0);
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = mi.base.font.changeShape(UP_SHAPE);
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(1).metrics(mi, dim1);
|
|
|
|
dim.wid = dim0.width() + dim1.wid + 5;
|
2007-12-12 19:28:07 +00:00
|
|
|
dim.asc = max(dim0.asc, dim1.asc);
|
|
|
|
dim.des = max(dim0.des, dim1.des);
|
2007-09-18 15:04:58 +00:00
|
|
|
} else {
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(2).metrics(mi, dim2);
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = mi.base.font.changeShape(UP_SHAPE);
|
|
|
|
Changer dummy2 = mi.base.changeFrac();
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
cell(1).metrics(mi, dim1);
|
|
|
|
dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
|
2007-12-12 19:28:07 +00:00
|
|
|
dim.asc = max(dim2.asc, dim0.height() + 5);
|
|
|
|
dim.des = max(dim2.des, dim1.height() - 5);
|
2007-09-18 15:04:58 +00:00
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
} else {
|
2009-04-08 00:19:57 +00:00
|
|
|
// general cell metrics used for \frac
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = mi.base.changeFrac();
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
cell(1).metrics(mi, dim1);
|
2007-10-01 10:23:56 +00:00
|
|
|
if (nargs() == 3)
|
|
|
|
cell(2).metrics(mi, dim2);
|
2009-04-08 00:19:57 +00:00
|
|
|
// metrics for special fraction types
|
2016-05-23 21:30:23 +00:00
|
|
|
if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
|
|
|
|
Changer dummy2 = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
|
2007-09-24 13:52:04 +00:00
|
|
|
dim.wid = dim0.width() + dim1.wid + 5;
|
|
|
|
dim.asc = dim0.height() + 5;
|
|
|
|
dim.des = dim1.height() - 5;
|
2007-09-15 16:39:51 +00:00
|
|
|
} else {
|
2016-05-23 21:30:23 +00:00
|
|
|
if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
|
|
|
|
|| kind_ == DFRAC || kind_ == TFRAC) {
|
2009-04-08 00:19:57 +00:00
|
|
|
// \cfrac and \dfrac are always in display size
|
2016-05-23 21:30:23 +00:00
|
|
|
// \tfrac is in always in text size
|
|
|
|
Changer dummy2 = mi.base.changeStyle((kind_ == TFRAC)
|
|
|
|
? LM_ST_SCRIPT
|
|
|
|
: LM_ST_DISPLAY);
|
2009-04-07 01:02:34 +00:00
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
cell(1).metrics(mi, dim1);
|
|
|
|
}
|
2009-04-05 19:52:53 +00:00
|
|
|
dim.wid = max(dim0.wid, dim1.wid) + 2;
|
2007-09-24 13:52:04 +00:00
|
|
|
dim.asc = dim0.height() + 2 + 5;
|
|
|
|
dim.des = dim1.height() + 2 - 5;
|
2007-09-15 16:39:51 +00:00
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
}
|
2004-04-07 16:54:15 +00:00
|
|
|
metricsMarkers(dim);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2004-01-30 11:41:12 +00:00
|
|
|
setPosCache(pi, x, y);
|
2007-09-23 22:39:49 +00:00
|
|
|
Dimension const dim = dimension(*pi.base.bv);
|
2007-09-24 13:52:04 +00:00
|
|
|
Dimension const dim0 = cell(0).dimension(*pi.base.bv);
|
2007-09-18 15:04:58 +00:00
|
|
|
if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
|
|
|
|
if (nargs() == 1) {
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = pi.base.font.changeShape(UP_SHAPE);
|
2007-09-18 15:04:58 +00:00
|
|
|
cell(0).draw(pi, x + 1, y);
|
|
|
|
} else if (nargs() == 2) {
|
|
|
|
cell(0).draw(pi, x + 1, y);
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = pi.base.font.changeShape(UP_SHAPE);
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(1).draw(pi, x + dim0.width() + 5, y);
|
2007-09-18 15:04:58 +00:00
|
|
|
} else {
|
|
|
|
cell(2).draw(pi, x + 1, y);
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = pi.base.font.changeShape(UP_SHAPE);
|
|
|
|
Changer dummy2 = pi.base.changeFrac();
|
2007-10-01 09:54:41 +00:00
|
|
|
Dimension const dim1 = cell(1).dimension(*pi.base.bv);
|
|
|
|
Dimension const dim2 = cell(2).dimension(*pi.base.bv);
|
2007-09-24 13:52:04 +00:00
|
|
|
int xx = x + dim2.wid + 5;
|
2007-09-18 15:04:58 +00:00
|
|
|
cell(0).draw(pi, xx + 2,
|
2007-09-24 13:52:04 +00:00
|
|
|
y - dim0.des - 5);
|
|
|
|
cell(1).draw(pi, xx + dim0.width() + 5,
|
|
|
|
y + dim1.asc / 2);
|
2007-09-18 15:04:58 +00:00
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
} else {
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy = pi.base.changeFrac();
|
2007-10-01 09:54:41 +00:00
|
|
|
Dimension const dim1 = cell(1).dimension(*pi.base.bv);
|
2009-04-07 01:02:34 +00:00
|
|
|
int m = x + dim.wid / 2;
|
2007-09-15 16:39:51 +00:00
|
|
|
if (kind_ == NICEFRAC) {
|
|
|
|
cell(0).draw(pi, x + 2,
|
2007-09-24 13:52:04 +00:00
|
|
|
y - dim0.des - 5);
|
|
|
|
cell(1).draw(pi, x + dim0.width() + 5,
|
|
|
|
y + dim1.asc / 2);
|
2007-09-15 16:39:51 +00:00
|
|
|
} else if (kind_ == UNITFRAC) {
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy2 = pi.base.font.changeShape(UP_SHAPE);
|
2009-04-07 01:02:34 +00:00
|
|
|
cell(0).draw(pi, x + 2, y - dim0.des - 5);
|
|
|
|
cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
|
2016-05-23 21:30:23 +00:00
|
|
|
} else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER
|
|
|
|
|| kind_ == TFRAC) {
|
2009-04-05 19:52:53 +00:00
|
|
|
// tfrac is in always in text size
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy2 = pi.base.changeStyle(LM_ST_SCRIPT, kind_ == TFRAC);
|
2009-04-05 19:52:53 +00:00
|
|
|
cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
|
|
|
|
cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
|
2007-09-15 16:39:51 +00:00
|
|
|
} else {
|
2009-04-07 01:02:34 +00:00
|
|
|
// \cfrac and \dfrac are always in display size
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy2 = pi.base.changeStyle(LM_ST_DISPLAY);
|
2009-04-07 01:02:34 +00:00
|
|
|
if (kind_ == CFRAC || kind_ == DFRAC)
|
|
|
|
cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
|
|
|
|
else if (kind_ == CFRACLEFT)
|
|
|
|
cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
|
|
|
|
else if (kind_ == CFRACRIGHT)
|
|
|
|
cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
|
|
|
|
y - dim0.des - 2 - 5);
|
2009-04-05 19:52:53 +00:00
|
|
|
cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
|
2007-09-15 16:39:51 +00:00
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
}
|
2007-09-18 15:04:58 +00:00
|
|
|
if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
|
2007-09-15 16:39:51 +00:00
|
|
|
// Diag line:
|
|
|
|
int xx = x;
|
2007-09-18 15:04:58 +00:00
|
|
|
if (nargs() == 3)
|
2007-10-01 10:23:56 +00:00
|
|
|
xx += cell(2).dimension(*pi.base.bv).wid + 5;
|
2007-09-24 13:52:04 +00:00
|
|
|
pi.pain.line(xx + dim0.wid,
|
2007-09-23 22:39:49 +00:00
|
|
|
y + dim.des - 2,
|
2007-09-24 13:52:04 +00:00
|
|
|
xx + dim0.wid + 5,
|
2010-04-28 13:19:09 +00:00
|
|
|
y - dim.asc + 2, pi.base.font.color());
|
2007-09-12 08:15:33 +00:00
|
|
|
}
|
2009-04-06 23:58:42 +00:00
|
|
|
if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
|
|
|
|
|| kind_ == CFRACRIGHT || kind_ == DFRAC
|
2009-04-05 19:52:53 +00:00
|
|
|
|| kind_ == TFRAC || kind_ == OVER)
|
2007-05-28 22:27:45 +00:00
|
|
|
pi.pain.line(x + 1, y - 5,
|
2010-04-28 13:19:09 +00:00
|
|
|
x + dim.wid - 2, y - 5, pi.base.font.color());
|
2004-04-07 16:54:15 +00:00
|
|
|
drawMarkers(pi, x, y);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
|
2002-03-19 16:55:58 +00:00
|
|
|
{
|
2007-09-24 13:52:04 +00:00
|
|
|
Dimension dim0, dim1;
|
|
|
|
cell(0).metricsT(mi, dim0);
|
|
|
|
cell(1).metricsT(mi, dim1);
|
2007-12-12 19:28:07 +00:00
|
|
|
dim.wid = max(dim0.width(), dim1.wid);
|
2007-09-24 13:52:04 +00:00
|
|
|
dim.asc = dim0.height() + 1;
|
|
|
|
dim.des = dim1.height();
|
2002-03-19 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-03 15:44:05 +00:00
|
|
|
void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
|
2002-03-19 16:55:58 +00:00
|
|
|
{
|
2007-09-23 22:39:49 +00:00
|
|
|
// FIXME: BROKEN!
|
2007-09-24 13:52:04 +00:00
|
|
|
/*
|
2007-09-23 22:39:49 +00:00
|
|
|
Dimension dim;
|
|
|
|
int m = x + dim.width() / 2;
|
2007-09-24 13:52:04 +00:00
|
|
|
cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
|
|
|
|
cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
|
2006-04-06 09:46:01 +00:00
|
|
|
// ASCII art: ignore niceties
|
2007-09-12 08:15:33 +00:00
|
|
|
if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
|
2007-09-23 22:39:49 +00:00
|
|
|
pain.horizontalLine(x, y, dim.width());
|
2007-09-24 13:52:04 +00:00
|
|
|
*/
|
2002-03-19 16:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::write(WriteStream & os) const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2008-06-17 11:10:43 +00:00
|
|
|
MathEnsurer ensurer(os);
|
2006-07-19 10:50:18 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case ATOP:
|
2009-04-06 23:58:42 +00:00
|
|
|
// \\atop is only for compatibility, \\binom is the
|
|
|
|
// LaTeX2e successor
|
2001-10-19 11:25:48 +00:00
|
|
|
os << '{' << cell(0) << "\\atop " << cell(1) << '}';
|
2006-07-19 10:50:18 +00:00
|
|
|
break;
|
|
|
|
case OVER:
|
|
|
|
// \\over is only for compatibility, normalize this to \\frac
|
|
|
|
os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
case FRAC:
|
2009-04-05 19:52:53 +00:00
|
|
|
case DFRAC:
|
|
|
|
case TFRAC:
|
2006-07-19 10:50:18 +00:00
|
|
|
case NICEFRAC:
|
2009-04-05 19:52:53 +00:00
|
|
|
case CFRAC:
|
2007-09-12 08:15:33 +00:00
|
|
|
case UNITFRAC:
|
2007-09-18 15:04:58 +00:00
|
|
|
if (nargs() == 2)
|
|
|
|
InsetMathNest::write(os);
|
|
|
|
else
|
|
|
|
os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
|
2006-07-19 10:50:18 +00:00
|
|
|
break;
|
2007-09-15 16:39:51 +00:00
|
|
|
case UNIT:
|
2007-09-18 15:04:58 +00:00
|
|
|
if (nargs() == 2)
|
|
|
|
os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
|
|
|
|
else
|
|
|
|
os << "\\unit{" << cell(0) << '}';
|
2007-09-15 16:39:51 +00:00
|
|
|
break;
|
2009-04-06 23:58:42 +00:00
|
|
|
case CFRACLEFT:
|
|
|
|
os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
case CFRACRIGHT:
|
|
|
|
os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
break;
|
2006-07-19 10:50:18 +00:00
|
|
|
}
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring InsetMathFrac::name() const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case FRAC:
|
2006-10-22 10:15:23 +00:00
|
|
|
return from_ascii("frac");
|
2009-04-05 19:52:53 +00:00
|
|
|
case CFRAC:
|
2009-04-06 23:58:42 +00:00
|
|
|
case CFRACLEFT:
|
|
|
|
case CFRACRIGHT:
|
2009-04-05 19:52:53 +00:00
|
|
|
return from_ascii("cfrac");
|
|
|
|
case DFRAC:
|
|
|
|
return from_ascii("dfrac");
|
|
|
|
case TFRAC:
|
|
|
|
return from_ascii("tfrac");
|
2006-07-19 10:50:18 +00:00
|
|
|
case OVER:
|
2006-10-22 10:15:23 +00:00
|
|
|
return from_ascii("over");
|
2006-04-06 09:46:01 +00:00
|
|
|
case NICEFRAC:
|
2006-10-22 10:15:23 +00:00
|
|
|
return from_ascii("nicefrac");
|
2007-09-12 08:15:33 +00:00
|
|
|
case UNITFRAC:
|
|
|
|
return from_ascii("unitfrac");
|
2007-09-15 16:39:51 +00:00
|
|
|
case UNIT:
|
|
|
|
return from_ascii("unit");
|
2006-04-06 09:46:01 +00:00
|
|
|
case ATOP:
|
2006-10-22 10:15:23 +00:00
|
|
|
return from_ascii("atop");
|
2006-04-06 09:46:01 +00:00
|
|
|
}
|
2006-07-31 17:44:50 +00:00
|
|
|
// shut up stupid compiler
|
2006-10-22 10:15:23 +00:00
|
|
|
return docstring();
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathFrac::extraBraces() const
|
2006-07-16 17:19:05 +00:00
|
|
|
{
|
2006-07-19 10:50:18 +00:00
|
|
|
return kind_ == ATOP || kind_ == OVER;
|
2006-07-16 17:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::maple(MapleStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2011-05-12 13:28:23 +00:00
|
|
|
if (nargs() != 2) {
|
|
|
|
// Someone who knows about maple should fix this.
|
|
|
|
LASSERT(false, return);
|
|
|
|
}
|
2001-11-13 16:27:06 +00:00
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
2001-11-07 17:30:26 +00:00
|
|
|
}
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
{
|
2011-05-12 13:28:23 +00:00
|
|
|
if (nargs() != 2) {
|
|
|
|
// Someone who knows about mathematica should fix this.
|
|
|
|
LASSERT(false, return);
|
|
|
|
}
|
2002-07-01 11:17:14 +00:00
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
|
|
|
}
|
2001-11-07 17:30:26 +00:00
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::octave(OctaveStream & os) const
|
2002-04-25 15:37:10 +00:00
|
|
|
{
|
2011-05-12 13:28:23 +00:00
|
|
|
if (nargs() != 2) {
|
|
|
|
// Someone who knows about octave should fix this.
|
|
|
|
LASSERT(false, return);
|
|
|
|
}
|
2002-04-25 15:37:10 +00:00
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathFrac::mathmlize(MathStream & os) const
|
2001-11-07 17:30:26 +00:00
|
|
|
{
|
2011-05-12 13:16:36 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case ATOP:
|
|
|
|
os << MTag("mfrac", "linethickeness='0'")
|
|
|
|
<< MTag("mrow") << cell(0) << ETag("mrow")
|
|
|
|
<< MTag("mrow") << cell(1) << ETag("mrow")
|
|
|
|
<< ETag("mfrac");
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we do not presently distinguish these
|
|
|
|
case OVER:
|
|
|
|
case FRAC:
|
|
|
|
case DFRAC:
|
|
|
|
case TFRAC:
|
|
|
|
case CFRAC:
|
|
|
|
case CFRACLEFT:
|
|
|
|
case CFRACRIGHT:
|
|
|
|
os << MTag("mfrac")
|
|
|
|
<< MTag("mrow") << cell(0) << ETag("mrow")
|
|
|
|
<< MTag("mrow") << cell(1) << ETag("mrow")
|
|
|
|
<< ETag("mfrac");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NICEFRAC:
|
|
|
|
os << MTag("mfrac", "bevelled='true'")
|
|
|
|
<< MTag("mrow") << cell(0) << ETag("mrow")
|
|
|
|
<< MTag("mrow") << cell(1) << ETag("mrow")
|
|
|
|
<< ETag("mfrac");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNITFRAC:
|
|
|
|
if (nargs() == 3)
|
|
|
|
os << cell(2);
|
|
|
|
os << MTag("mfrac", "bevelled='true'")
|
|
|
|
<< MTag("mrow") << cell(0) << ETag("mrow")
|
|
|
|
<< MTag("mrow") << cell(1) << ETag("mrow")
|
|
|
|
<< ETag("mfrac");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT:
|
|
|
|
// FIXME This is not right, because we still output mi, etc,
|
|
|
|
// when we output the cell. So we need to prevent that somehow.
|
|
|
|
if (nargs() == 2)
|
|
|
|
os << cell(0)
|
|
|
|
<< MTag("mstyle mathvariant='normal'")
|
|
|
|
<< cell(1)
|
|
|
|
<< ETag("mstyle");
|
|
|
|
else
|
|
|
|
os << MTag("mstyle mathvariant='normal'")
|
|
|
|
<< cell(0)
|
|
|
|
<< ETag("mstyle");
|
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
|
|
|
|
|
2010-03-30 02:03:45 +00:00
|
|
|
void InsetMathFrac::htmlize(HtmlStream & os) const
|
|
|
|
{
|
2011-05-12 13:16:36 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case ATOP:
|
|
|
|
os << MTag("span", "class='frac'")
|
|
|
|
<< MTag("span", "class='numer'") << cell(0) << ETag("span")
|
|
|
|
<< MTag("span", "class='numer'") << cell(1) << ETag("span")
|
|
|
|
<< ETag("span");
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we do not presently distinguish these
|
|
|
|
case OVER:
|
|
|
|
case FRAC:
|
|
|
|
case DFRAC:
|
|
|
|
case TFRAC:
|
|
|
|
case CFRAC:
|
|
|
|
case CFRACLEFT:
|
|
|
|
case CFRACRIGHT:
|
|
|
|
os << MTag("span", "class='frac'")
|
|
|
|
<< MTag("span", "class='numer'") << cell(0) << ETag("span")
|
|
|
|
<< MTag("span", "class='denom'") << cell(1) << ETag("span")
|
|
|
|
<< ETag("span");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NICEFRAC:
|
|
|
|
os << cell(0) << '/' << cell(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNITFRAC:
|
|
|
|
if (nargs() == 3)
|
|
|
|
os << cell(2) << ' ';
|
|
|
|
os << cell(0) << '/' << cell(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNIT:
|
|
|
|
// FIXME This is not right, because we still output i, etc,
|
|
|
|
// when we output the cell. So we need to prevent that somehow.
|
|
|
|
if (nargs() == 2)
|
|
|
|
os << cell(0)
|
|
|
|
<< MTag("span")
|
|
|
|
<< cell(1)
|
|
|
|
<< ETag("span");
|
|
|
|
else
|
|
|
|
os << MTag("span")
|
|
|
|
<< cell(0)
|
|
|
|
<< ETag("span");
|
|
|
|
}
|
2010-03-30 02:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathFrac::validate(LaTeXFeatures & features) const
|
2006-04-06 09:46:01 +00:00
|
|
|
{
|
2007-09-18 15:04:58 +00:00
|
|
|
if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
|
2007-09-12 08:15:33 +00:00
|
|
|
features.require("units");
|
2009-04-06 23:58:42 +00:00
|
|
|
if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
|
2010-11-25 16:53:56 +00:00
|
|
|
|| kind_ == DFRAC || kind_ == TFRAC)
|
2009-04-05 19:52:53 +00:00
|
|
|
features.require("amsmath");
|
2010-03-31 18:24:52 +00:00
|
|
|
if (features.runparams().math_flavor == OutputParams::MathAsHTML)
|
2010-03-30 02:03:45 +00:00
|
|
|
// CSS adapted from eLyXer
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet(
|
2010-03-30 02:03:45 +00:00
|
|
|
"span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
|
|
|
|
"span.numer{display: block;}\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"span.denom{display: block; border-top: thin solid #000040;}");
|
2009-02-28 02:43:26 +00:00
|
|
|
InsetMathNest::validate(features);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 21:53:06 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// InsetMathBinom
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
|
|
|
|
: InsetMathFracBase(buf), kind_(kind)
|
2007-10-25 21:53:06 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
Inset * InsetMathBinom::clone() const
|
|
|
|
{
|
|
|
|
return new InsetMathBinom(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InsetMathBinom::dw(int height) const
|
|
|
|
{
|
|
|
|
int w = height / 5;
|
|
|
|
if (w > 15)
|
|
|
|
w = 15;
|
|
|
|
if (w < 6)
|
|
|
|
w = 6;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
{
|
|
|
|
Dimension dim0, dim1;
|
2016-05-23 21:30:23 +00:00
|
|
|
Changer dummy =
|
|
|
|
(kind_ == DBINOM) ? mi.base.changeStyle(LM_ST_DISPLAY) :
|
|
|
|
(kind_ == TBINOM) ? mi.base.changeStyle(LM_ST_SCRIPT) :
|
|
|
|
mi.base.changeFrac();
|
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
cell(1).metrics(mi, dim1);
|
2007-10-25 21:53:06 +00:00
|
|
|
dim.asc = dim0.height() + 4 + 5;
|
|
|
|
dim.des = dim1.height() + 4 - 5;
|
2009-04-05 19:52:53 +00:00
|
|
|
dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
|
2007-10-25 21:53:06 +00:00
|
|
|
metricsMarkers2(dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
{
|
|
|
|
Dimension const dim = dimension(*pi.base.bv);
|
|
|
|
Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
|
|
|
|
Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
|
2009-04-05 19:52:53 +00:00
|
|
|
// define the binom brackets
|
2008-03-31 01:23:25 +00:00
|
|
|
docstring const bra = kind_ == BRACE ? from_ascii("{") :
|
2009-04-05 19:52:53 +00:00
|
|
|
kind_ == BRACK ? from_ascii("[") : from_ascii("(");
|
2008-03-31 01:23:25 +00:00
|
|
|
docstring const ket = kind_ == BRACE ? from_ascii("}") :
|
2009-04-05 19:52:53 +00:00
|
|
|
kind_ == BRACK ? from_ascii("]") : from_ascii(")");
|
|
|
|
|
2007-10-25 21:53:06 +00:00
|
|
|
int m = x + dim.width() / 2;
|
2016-05-23 21:30:23 +00:00
|
|
|
{
|
|
|
|
Changer dummy =
|
|
|
|
(kind_ == DBINOM) ? pi.base.changeStyle(LM_ST_DISPLAY) :
|
|
|
|
(kind_ == TBINOM) ? pi.base.changeStyle(LM_ST_SCRIPT) :
|
|
|
|
pi.base.changeFrac();
|
2009-04-05 19:52:53 +00:00
|
|
|
cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
|
|
|
|
cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
|
|
|
|
}
|
|
|
|
// draw the brackets and the marker
|
|
|
|
mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
|
|
|
|
dim.height(), bra);
|
|
|
|
mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
|
|
|
|
y - dim.ascent(), dw(dim.height()), dim.height(), ket);
|
2007-10-25 21:53:06 +00:00
|
|
|
drawMarkers2(pi, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathBinom::extraBraces() const
|
|
|
|
{
|
2008-03-31 01:23:25 +00:00
|
|
|
return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
|
2007-10-25 21:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathBinom::write(WriteStream & os) const
|
|
|
|
{
|
2008-06-17 11:10:43 +00:00
|
|
|
MathEnsurer ensurer(os);
|
2008-03-31 01:23:25 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case BINOM:
|
2007-10-25 21:53:06 +00:00
|
|
|
os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
|
2008-03-31 01:23:25 +00:00
|
|
|
break;
|
2009-04-05 19:52:53 +00:00
|
|
|
case DBINOM:
|
|
|
|
os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
case TBINOM:
|
|
|
|
os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
break;
|
2008-03-31 01:23:25 +00:00
|
|
|
case CHOOSE:
|
|
|
|
os << '{' << cell(0) << " \\choose " << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
case BRACE:
|
|
|
|
os << '{' << cell(0) << " \\brace " << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
case BRACK:
|
|
|
|
os << '{' << cell(0) << " \\brack " << cell(1) << '}';
|
|
|
|
break;
|
|
|
|
}
|
2007-10-25 21:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetMathBinom::normalize(NormalStream & os) const
|
|
|
|
{
|
|
|
|
os << "[binom " << cell(0) << ' ' << cell(1) << ']';
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2007-10-28 21:48:51 +00:00
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathBinom::mathmlize(MathStream & os) const
|
2007-10-28 21:48:51 +00:00
|
|
|
{
|
2009-12-31 21:22:04 +00:00
|
|
|
char ldelim = ' ';
|
|
|
|
char rdelim = ' ';
|
2009-04-05 19:52:53 +00:00
|
|
|
switch (kind_) {
|
|
|
|
case BINOM:
|
|
|
|
case TBINOM:
|
2009-04-16 09:15:24 +00:00
|
|
|
case DBINOM:
|
2009-12-31 21:22:04 +00:00
|
|
|
case CHOOSE:
|
|
|
|
ldelim = '(';
|
|
|
|
rdelim = ')';
|
|
|
|
break;
|
|
|
|
case BRACE:
|
|
|
|
ldelim = '{';
|
|
|
|
rdelim = '}';
|
|
|
|
break;
|
|
|
|
case BRACK:
|
|
|
|
ldelim = '[';
|
|
|
|
rdelim = ']';
|
2009-04-16 09:15:24 +00:00
|
|
|
break;
|
2009-04-05 19:52:53 +00:00
|
|
|
}
|
2009-12-31 21:22:04 +00:00
|
|
|
os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
|
|
|
|
<< "<mfrac linethickness='0'>"
|
|
|
|
<< cell(0) << cell(1)
|
|
|
|
<< "</mfrac>"
|
|
|
|
<< "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
|
2007-10-28 21:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-31 21:33:33 +00:00
|
|
|
void InsetMathBinom::htmlize(HtmlStream & os) const
|
|
|
|
{
|
|
|
|
char ldelim = ' ';
|
|
|
|
char rdelim = ' ';
|
|
|
|
switch (kind_) {
|
|
|
|
case BINOM:
|
|
|
|
case TBINOM:
|
|
|
|
case DBINOM:
|
|
|
|
case CHOOSE:
|
|
|
|
ldelim = '(';
|
|
|
|
rdelim = ')';
|
|
|
|
break;
|
|
|
|
case BRACE:
|
|
|
|
ldelim = '{';
|
|
|
|
rdelim = '}';
|
|
|
|
break;
|
|
|
|
case BRACK:
|
|
|
|
ldelim = '[';
|
|
|
|
rdelim = ']';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
|
|
|
|
<< MTag("span", "class='binom'") << '\n'
|
|
|
|
<< MTag("span") << cell(0) << ETag("span") << '\n'
|
|
|
|
<< MTag("span") << cell(1) << ETag("span") << '\n'
|
|
|
|
<< ETag("span") << '\n'
|
|
|
|
<< MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-05 19:52:53 +00:00
|
|
|
void InsetMathBinom::validate(LaTeXFeatures & features) const
|
2007-10-28 21:48:51 +00:00
|
|
|
{
|
2010-03-31 21:33:33 +00:00
|
|
|
if (features.runparams().isLaTeX()) {
|
|
|
|
if (kind_ == BINOM)
|
|
|
|
features.require("binom");
|
|
|
|
if (kind_ == DBINOM || kind_ == TBINOM)
|
|
|
|
features.require("amsmath");
|
|
|
|
} else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet(
|
2010-03-31 21:33:33 +00:00
|
|
|
"span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
|
|
|
|
"span.binom span{display: block;}\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"span.binomdelim{font-size: 2em;}");
|
2007-10-28 21:48:51 +00:00
|
|
|
InsetMathNest::validate(features);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|