2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file InsetMathDelim.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
|
2003-08-19 13:00:56 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2001-08-17 11:08:55 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathDelim.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
#include "MathData.h"
|
2012-12-16 14:49:42 +00:00
|
|
|
#include "MathFactory.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
#include "MathStream.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathSupport.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "MetricsInfo.h"
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2012-12-16 14:49:42 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
|
|
|
|
2007-11-27 20:51:20 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
|
2006-12-04 10:45:43 +00:00
|
|
|
#include "frontends/FontMetrics.h"
|
2001-10-29 15:45:24 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
namespace lyx {
|
2002-06-25 13:19:50 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
static docstring convertDelimToLatexName(docstring const & name)
|
2002-06-25 13:19:50 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
if (name.size() == 1) {
|
|
|
|
char_type const c = name[0];
|
2007-05-28 22:27:45 +00:00
|
|
|
if (c == '<' || c == '(' || c == '[' || c == '.'
|
2006-10-22 10:15:23 +00:00
|
|
|
|| c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
|
|
|
|
return name;
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
return '\\' + name + ' ';
|
2002-06-25 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
|
|
|
|
docstring const & r)
|
2015-10-10 19:04:28 +00:00
|
|
|
: InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
|
2001-06-25 00:06:33 +00:00
|
|
|
{}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
|
|
|
|
MathData const & ar)
|
2015-10-10 19:04:28 +00:00
|
|
|
: InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
|
2002-06-25 13:19:50 +00:00
|
|
|
{
|
|
|
|
cell(0) = ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathDelim::clone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathDelim(*this);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-16 14:49:42 +00:00
|
|
|
void InsetMathDelim::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
|
|
|
InsetMathNest::validate(features);
|
|
|
|
// The delimiters may be used without \left or \right as well.
|
|
|
|
// Therefore they are listed in lib/symbols, and if they have
|
|
|
|
// requirements, we need to add them here.
|
|
|
|
MathWordList const & words = mathedWordList();
|
|
|
|
MathWordList::const_iterator it = words.find(left_);
|
|
|
|
if (it != words.end())
|
|
|
|
{
|
2014-12-29 20:46:30 +00:00
|
|
|
string const req = it->second.requires;
|
2012-12-16 14:49:42 +00:00
|
|
|
if (!req.empty())
|
2014-12-29 20:46:30 +00:00
|
|
|
features.require(req);
|
2012-12-16 14:49:42 +00:00
|
|
|
}
|
|
|
|
it = words.find(right_);
|
|
|
|
if (it != words.end())
|
|
|
|
{
|
2014-12-29 20:46:30 +00:00
|
|
|
string const req = it->second.requires;
|
2012-12-16 14:49:42 +00:00
|
|
|
if (!req.empty())
|
2014-12-29 20:46:30 +00:00
|
|
|
features.require(req);
|
2012-12-16 14:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::write(WriteStream & os) const
|
2001-08-09 15:19:31 +00:00
|
|
|
{
|
2008-06-17 11:10:43 +00:00
|
|
|
MathEnsurer ensurer(os);
|
2002-02-14 12:38:02 +00:00
|
|
|
os << "\\left" << convertDelimToLatexName(left_) << cell(0)
|
|
|
|
<< "\\right" << convertDelimToLatexName(right_);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::normalize(NormalStream & os) const
|
2001-10-24 16:10:38 +00:00
|
|
|
{
|
2002-02-14 12:38:02 +00:00
|
|
|
os << "[delim " << convertDelimToLatexName(left_) << ' '
|
|
|
|
<< convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
|
2001-10-24 16:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-08-15 06:53:25 +00:00
|
|
|
{
|
2007-09-24 13:52:04 +00:00
|
|
|
Dimension dim0;
|
|
|
|
cell(0).metrics(mi, dim0);
|
2006-12-04 10:45:43 +00:00
|
|
|
Dimension t = theFontMetrics(mi.base.font).dimension('I');
|
2003-05-27 13:55:03 +00:00
|
|
|
int h0 = (t.asc + t.des) / 2;
|
2007-09-24 13:52:04 +00:00
|
|
|
int a0 = max(dim0.asc, t.asc) - h0;
|
|
|
|
int d0 = max(dim0.des, t.des) + h0;
|
|
|
|
dw_ = dim0.height() / 5;
|
2003-05-28 13:22:36 +00:00
|
|
|
if (dw_ > 8)
|
|
|
|
dw_ = 8;
|
|
|
|
if (dw_ < 4)
|
|
|
|
dw_ = 4;
|
2007-09-24 13:52:04 +00:00
|
|
|
dim.wid = dim0.width() + 2 * dw_ + 8;
|
2006-11-28 15:15:49 +00:00
|
|
|
dim.asc = max(a0, d0) + h0;
|
|
|
|
dim.des = max(a0, d0) - h0;
|
2001-08-15 06:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2007-09-23 22:39:49 +00:00
|
|
|
Dimension const dim = dimension(*pi.base.bv);
|
|
|
|
int const b = y - dim.asc;
|
2003-05-28 13:22:36 +00:00
|
|
|
cell(0).draw(pi, x + dw_ + 4, y);
|
2007-09-23 22:39:49 +00:00
|
|
|
mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
|
|
|
|
mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
|
|
|
|
b, dw_, dim.height(), right_);
|
2005-04-04 22:11:53 +00:00
|
|
|
setPosCache(pi, x, y);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDelim::isParenthesis() const
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
|
|
|
return left_ == "(" && right_ == ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDelim::isBrackets() const
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
|
|
|
return left_ == "[" && right_ == "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDelim::isAbs() const
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
|
|
|
return left_ == "|" && right_ == "|";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::maple(MapleStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2001-11-09 10:44:24 +00:00
|
|
|
if (isAbs()) {
|
2002-08-08 17:11:30 +00:00
|
|
|
if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "linalg[det](" << cell(0) << ')';
|
2001-11-07 10:21:51 +00:00
|
|
|
else
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "abs(" << cell(0) << ')';
|
2001-11-07 10:21:51 +00:00
|
|
|
}
|
2001-11-07 17:30:26 +00:00
|
|
|
else
|
2001-12-05 08:04:20 +00:00
|
|
|
os << left_ << cell(0) << right_;
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
2001-11-07 10:21:51 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::maxima(MaximaStream & os) const
|
2002-10-28 17:15:19 +00:00
|
|
|
{
|
|
|
|
if (isAbs()) {
|
|
|
|
if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "determinant(" << cell(0) << ')';
|
2002-10-28 17:15:19 +00:00
|
|
|
else
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "abs(" << cell(0) << ')';
|
2002-10-28 17:15:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
os << left_ << cell(0) << right_;
|
|
|
|
}
|
|
|
|
|
2002-10-28 13:10:12 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
{
|
|
|
|
if (isAbs()) {
|
2002-08-08 17:11:30 +00:00
|
|
|
if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
|
2002-07-01 11:17:14 +00:00
|
|
|
os << "Det" << cell(0) << ']';
|
|
|
|
else
|
|
|
|
os << "Abs[" << cell(0) << ']';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
os << left_ << cell(0) << right_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathDelim::mathmlize(MathStream & os) const
|
2001-11-07 18:15:24 +00:00
|
|
|
{
|
2009-12-31 15:46:39 +00:00
|
|
|
os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>" << left_ << "</mo>"
|
|
|
|
<< cell(0) << "<mo form='postfix' fence='true' stretchy='true' symmetric='true'>" << right_ << "</mo>";
|
2001-11-07 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 22:07:05 +00:00
|
|
|
void InsetMathDelim::htmlize(HtmlStream & os) const
|
|
|
|
{
|
|
|
|
os << left_ << cell(0) << right_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDelim::octave(OctaveStream & os) const
|
2001-11-07 18:15:24 +00:00
|
|
|
{
|
2001-11-09 10:44:24 +00:00
|
|
|
if (isAbs())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "det(" << cell(0) << ')';
|
2001-11-07 18:15:24 +00:00
|
|
|
else
|
2001-12-05 08:04:20 +00:00
|
|
|
os << left_ << cell(0) << right_;
|
2001-11-07 18:15:24 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|