2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file InsetMathDecoration.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-12-05 08:04:20 +00:00
|
|
|
|
2003-08-19 13:00:56 +00:00
|
|
|
#include <config.h>
|
2001-02-26 12:53:35 +00:00
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathDecoration.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
#include "MathData.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathParser.h"
|
2006-12-17 15:05:18 +00:00
|
|
|
#include "MathSupport.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathStream.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "MetricsInfo.h"
|
2004-04-08 15:20:05 +00:00
|
|
|
|
2005-01-08 20:28:29 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2010-04-27 19:23:31 +00:00
|
|
|
#include "support/docstring.h"
|
2014-11-14 17:18:30 +00:00
|
|
|
#include "support/gettext.h"
|
2009-12-25 23:23:42 +00:00
|
|
|
#include "support/lassert.h"
|
2014-11-14 17:18:30 +00:00
|
|
|
#include "support/lstrings.h"
|
2004-08-14 14:03:42 +00:00
|
|
|
|
2008-05-06 10:36:32 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
2014-11-14 17:18:30 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
|
2009-12-25 23:23:42 +00:00
|
|
|
using namespace std;
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
|
2015-09-13 17:47:21 +00:00
|
|
|
: InsetMathNest(buf, 1), key_(key), dh_(0), dy_(0), dw_(0)
|
2004-08-14 14:03:42 +00:00
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
// lyxerr << " creating deco " << key->name << endl;
|
2004-08-14 14:03:42 +00:00
|
|
|
}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathDecoration::clone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathDecoration(*this);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDecoration::upper() const
|
2001-08-15 06:53:25 +00:00
|
|
|
{
|
2011-05-08 10:06:33 +00:00
|
|
|
return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
|
2001-08-15 06:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-05 15:29:43 +00:00
|
|
|
MathClass InsetMathDecoration::mathClass() const
|
|
|
|
{
|
|
|
|
if (key_->name == "overbrace" || key_->name == "underbrace")
|
|
|
|
return MC_OP;
|
|
|
|
return MC_ORD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDecoration::isScriptable() const
|
2001-09-11 10:58:17 +00:00
|
|
|
{
|
2018-09-05 15:29:43 +00:00
|
|
|
return mathClass() == MC_OP;
|
2001-09-11 10:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDecoration::protect() const
|
2001-08-15 06:53:25 +00:00
|
|
|
{
|
|
|
|
return
|
2002-07-17 10:25:33 +00:00
|
|
|
key_->name == "overbrace" ||
|
|
|
|
key_->name == "underbrace" ||
|
|
|
|
key_->name == "overleftarrow" ||
|
|
|
|
key_->name == "overrightarrow" ||
|
|
|
|
key_->name == "overleftrightarrow" ||
|
|
|
|
key_->name == "underleftarrow" ||
|
|
|
|
key_->name == "underrightarrow" ||
|
|
|
|
key_->name == "underleftrightarrow";
|
2001-08-15 06:53:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathDecoration::wide() const
|
2001-09-12 15:56:09 +00:00
|
|
|
{
|
|
|
|
return
|
2002-07-17 10:25:33 +00:00
|
|
|
key_->name == "overline" ||
|
|
|
|
key_->name == "underline" ||
|
|
|
|
key_->name == "overbrace" ||
|
|
|
|
key_->name == "underbrace" ||
|
|
|
|
key_->name == "overleftarrow" ||
|
|
|
|
key_->name == "overrightarrow" ||
|
|
|
|
key_->name == "overleftrightarrow" ||
|
|
|
|
key_->name == "widehat" ||
|
|
|
|
key_->name == "widetilde" ||
|
|
|
|
key_->name == "underleftarrow" ||
|
|
|
|
key_->name == "underrightarrow" ||
|
2011-05-08 10:06:33 +00:00
|
|
|
key_->name == "underleftrightarrow" ||
|
2011-05-08 10:23:56 +00:00
|
|
|
key_->name == "undertilde" ||
|
2011-05-08 10:06:33 +00:00
|
|
|
key_->name == "utilde";
|
2001-09-12 15:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-31 01:09:38 +00:00
|
|
|
InsetMath::mode_type InsetMathDecoration::currentMode() const
|
|
|
|
{
|
|
|
|
return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2016-12-12 00:17:41 +00:00
|
|
|
Changer dummy = mi.base.changeEnsureMath(currentMode());
|
2010-03-31 01:09:38 +00:00
|
|
|
|
2004-01-30 11:41:12 +00:00
|
|
|
cell(0).metrics(mi, dim);
|
2003-05-28 13:22:36 +00:00
|
|
|
|
2002-07-11 11:27:24 +00:00
|
|
|
dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
|
|
|
|
dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-08-15 06:53:25 +00:00
|
|
|
if (upper()) {
|
2004-01-30 11:41:12 +00:00
|
|
|
dy_ = -dim.asc - dh_;
|
|
|
|
dim.asc += dh_ + 1;
|
2001-02-13 13:28:32 +00:00
|
|
|
} else {
|
2004-01-30 11:41:12 +00:00
|
|
|
dy_ = dim.des + 1;
|
|
|
|
dim.des += dh_ + 2;
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-03 15:22:55 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2016-12-12 00:17:41 +00:00
|
|
|
Changer dummy = pi.base.changeEnsureMath(currentMode());
|
2010-03-31 01:09:38 +00:00
|
|
|
|
2017-01-06 08:52:10 +00:00
|
|
|
cell(0).draw(pi, x, y);
|
2007-09-24 13:52:04 +00:00
|
|
|
Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
|
2002-03-21 17:42:56 +00:00
|
|
|
if (wide())
|
2007-09-24 13:52:04 +00:00
|
|
|
mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
|
2002-03-19 16:55:58 +00:00
|
|
|
else
|
2007-09-24 13:52:04 +00:00
|
|
|
mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
|
2003-05-28 13:22:36 +00:00
|
|
|
y + dy_, dw_, dh_, key_->name);
|
2001-07-05 14:48:45 +00:00
|
|
|
}
|
|
|
|
|
2001-02-13 13:28:32 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDecoration::write(WriteStream & os) const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2008-06-17 11:10:43 +00:00
|
|
|
MathEnsurer ensurer(os);
|
2001-12-03 16:24:50 +00:00
|
|
|
if (os.fragile() && protect())
|
2001-02-13 13:28:32 +00:00
|
|
|
os << "\\protect";
|
2010-03-31 01:09:38 +00:00
|
|
|
os << '\\' << key_->name << '{';
|
|
|
|
ModeSpecifier specifier(os, currentMode());
|
|
|
|
os << cell(0) << '}';
|
2001-07-13 07:55:55 +00:00
|
|
|
}
|
|
|
|
|
2001-08-15 06:53:25 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathDecoration::normalize(NormalStream & os) const
|
2001-07-13 07:55:55 +00:00
|
|
|
{
|
2002-07-17 10:25:33 +00:00
|
|
|
os << "[deco " << key_->name << ' ' << cell(0) << ']';
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
2002-06-24 15:37:14 +00:00
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
void InsetMathDecoration::infoize(odocstream & os) const
|
2002-06-24 15:37:14 +00:00
|
|
|
{
|
2014-11-14 17:18:30 +00:00
|
|
|
os << bformat(_("Decoration: %1$s"), key_->name);
|
2002-06-24 15:37:14 +00:00
|
|
|
}
|
2005-01-08 20:28:29 +00:00
|
|
|
|
|
|
|
|
2009-12-25 23:23:42 +00:00
|
|
|
namespace {
|
|
|
|
struct Attributes {
|
2016-06-12 03:39:45 +00:00
|
|
|
Attributes() : over(false) {}
|
2009-12-25 23:23:42 +00:00
|
|
|
Attributes(bool o, string t)
|
|
|
|
: over(o), tag(t) {}
|
|
|
|
bool over;
|
|
|
|
string tag;
|
|
|
|
};
|
|
|
|
|
2010-04-23 09:10:35 +00:00
|
|
|
typedef map<string, Attributes> TranslationMap;
|
2009-12-25 23:23:42 +00:00
|
|
|
|
2010-04-23 09:10:35 +00:00
|
|
|
void buildTranslationMap(TranslationMap & t) {
|
2009-12-25 23:23:42 +00:00
|
|
|
// the decorations we need to support are listed in lib/symbols
|
|
|
|
t["acute"] = Attributes(true, "´");
|
|
|
|
t["bar"] = Attributes(true, "‾");
|
|
|
|
t["breve"] = Attributes(true, "˘");
|
|
|
|
t["check"] = Attributes(true, "ˇ");
|
|
|
|
t["ddddot"] = Attributes(true, "⃜");
|
|
|
|
t["dddot"] = Attributes(true, "⃛");
|
|
|
|
t["ddot"] = Attributes(true, "¨");
|
|
|
|
t["dot"] = Attributes(true, "˙");
|
|
|
|
t["grave"] = Attributes(true, "`");
|
|
|
|
t["hat"] = Attributes(true, "ˆ");
|
|
|
|
t["mathring"] = Attributes(true, "˚");
|
|
|
|
t["overbrace"] = Attributes(true, "⏞");
|
|
|
|
t["overleftarrow"] = Attributes(true, "⟵");
|
|
|
|
t["overleftrightarrow"] = Attributes(true, "⟷");
|
2010-03-30 21:56:52 +00:00
|
|
|
t["overline"] = Attributes(true, "¯");
|
2009-12-25 23:23:42 +00:00
|
|
|
t["overrightarrow"] = Attributes(true, "⟶");
|
|
|
|
t["tilde"] = Attributes(true, "˜");
|
|
|
|
t["underbar"] = Attributes(false, "_");
|
|
|
|
t["underbrace"] = Attributes(false, "⏟");
|
|
|
|
t["underleftarrow"] = Attributes(false, "⟵");
|
|
|
|
t["underleftrightarrow"] = Attributes(false, "⟷");
|
2010-01-03 17:46:02 +00:00
|
|
|
// this is the macron, again, but it works
|
|
|
|
t["underline"] = Attributes(false, "¯");
|
2009-12-25 23:23:42 +00:00
|
|
|
t["underrightarrow"] = Attributes(false, "⟶");
|
2011-05-08 10:23:56 +00:00
|
|
|
t["undertilde"] = Attributes(false, "∼");
|
2011-05-08 10:06:33 +00:00
|
|
|
t["utilde"] = Attributes(false, "∼");
|
2009-12-25 23:23:42 +00:00
|
|
|
t["vec"] = Attributes(true, "→");
|
|
|
|
t["widehat"] = Attributes(true, "^");
|
|
|
|
t["widetilde"] = Attributes(true, "∼");
|
|
|
|
}
|
|
|
|
|
2010-04-23 09:10:35 +00:00
|
|
|
TranslationMap const & translationMap() {
|
|
|
|
static TranslationMap t;
|
2009-12-25 23:23:42 +00:00
|
|
|
if (t.empty())
|
2010-04-23 09:10:35 +00:00
|
|
|
buildTranslationMap(t);
|
2009-12-25 23:23:42 +00:00
|
|
|
return t;
|
|
|
|
}
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2009-12-25 23:23:42 +00:00
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathDecoration::mathmlize(MathStream & os) const
|
2009-12-25 23:23:42 +00:00
|
|
|
{
|
2010-04-23 09:10:35 +00:00
|
|
|
TranslationMap const & t = translationMap();
|
|
|
|
TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
|
2009-12-31 15:46:39 +00:00
|
|
|
LASSERT(cur != t.end(), return);
|
2009-12-25 23:23:42 +00:00
|
|
|
char const * const outag = cur->second.over ? "mover" : "munder";
|
2009-12-31 15:46:39 +00:00
|
|
|
os << MTag(outag)
|
|
|
|
<< MTag("mrow") << cell(0) << ETag("mrow")
|
2009-12-25 23:23:42 +00:00
|
|
|
<< from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
|
|
|
|
<< ETag(outag);
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2010-03-30 21:56:52 +00:00
|
|
|
void InsetMathDecoration::htmlize(HtmlStream & os) const
|
|
|
|
{
|
2010-03-30 22:05:28 +00:00
|
|
|
string const name = to_utf8(key_->name);
|
|
|
|
if (name == "bar") {
|
|
|
|
os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-29 20:46:30 +00:00
|
|
|
|
2010-03-30 22:05:28 +00:00
|
|
|
if (name == "underbar" || name == "underline") {
|
|
|
|
os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-23 09:10:35 +00:00
|
|
|
TranslationMap const & t = translationMap();
|
|
|
|
TranslationMap::const_iterator cur = t.find(name);
|
2010-03-30 21:56:52 +00:00
|
|
|
LASSERT(cur != t.end(), return);
|
2014-12-29 20:46:30 +00:00
|
|
|
|
2010-03-30 21:56:52 +00:00
|
|
|
bool symontop = cur->second.over;
|
|
|
|
string const symclass = symontop ? "symontop" : "symonbot";
|
|
|
|
os << MTag("span", "class='symbolpair " + symclass + "'")
|
|
|
|
<< '\n';
|
2014-12-29 20:46:30 +00:00
|
|
|
|
2010-03-30 21:56:52 +00:00
|
|
|
if (symontop)
|
|
|
|
os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
|
|
|
|
else
|
|
|
|
os << MTag("span", "class='base'") << cell(0);
|
|
|
|
os << ETag("span") << '\n';
|
|
|
|
if (symontop)
|
|
|
|
os << MTag("span", "class='base'") << cell(0);
|
|
|
|
else
|
|
|
|
os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
|
|
|
|
os << ETag("span") << '\n' << ETag("span") << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ideas borrowed from the eLyXer code
|
|
|
|
void InsetMathDecoration::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2010-03-31 18:24:52 +00:00
|
|
|
if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
|
2010-03-30 22:05:28 +00:00
|
|
|
string const name = to_utf8(key_->name);
|
|
|
|
if (name == "bar") {
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
|
2010-03-30 22:05:28 +00:00
|
|
|
} else if (name == "underbar" || name == "underline") {
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
|
2010-03-30 22:05:28 +00:00
|
|
|
} else {
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet(
|
2010-03-30 22:05:28 +00:00
|
|
|
"span.symbolpair{display: inline-block; text-align:center;}\n"
|
|
|
|
"span.symontop{vertical-align: top;}\n"
|
|
|
|
"span.symonbot{vertical-align: bottom;}\n"
|
2014-12-29 20:46:30 +00:00
|
|
|
"span.symbolpair span{display: block;}\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"span.symbol{height: 0.5ex;}");
|
2010-03-30 22:05:28 +00:00
|
|
|
}
|
2010-03-30 21:56:52 +00:00
|
|
|
} else {
|
|
|
|
if (!key_->requires.empty())
|
2014-12-29 20:46:30 +00:00
|
|
|
features.require(key_->requires);
|
2010-03-30 21:56:52 +00:00
|
|
|
}
|
|
|
|
InsetMathNest::validate(features);
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|