2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file InsetMathSymbol.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.
|
|
|
|
*
|
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
|
|
|
#include <config.h>
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathSymbol.h"
|
2007-11-29 17:57:06 +00:00
|
|
|
|
|
|
|
#include "MathAtom.h"
|
|
|
|
#include "MathParser.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathStream.h"
|
|
|
|
#include "MathSupport.h"
|
2007-05-11 14:17:53 +00:00
|
|
|
|
2016-06-04 08:41:13 +00:00
|
|
|
#include "Dimension.h"
|
|
|
|
#include "LaTeXFeatures.h"
|
|
|
|
#include "MetricsInfo.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-29 17:57:06 +00:00
|
|
|
#include "support/docstream.h"
|
2016-05-23 21:30:23 +00:00
|
|
|
#include "support/lyxlib.h"
|
2007-04-02 15:21:36 +00:00
|
|
|
#include "support/textutils.h"
|
2016-06-02 22:49:36 +00:00
|
|
|
#include "support/unique_ptr.h"
|
2013-01-09 21:23:07 +00:00
|
|
|
|
2003-07-25 17:11:25 +00:00
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
namespace lyx {
|
2003-05-28 13:22:36 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
|
2014-04-04 20:52:21 +00:00
|
|
|
: sym_(l), h_(0), kerning_(0), scriptable_(false)
|
2001-10-31 10:54:34 +00:00
|
|
|
{}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathSymbol::InsetMathSymbol(char const * name)
|
2014-04-04 19:54:32 +00:00
|
|
|
: sym_(in_word_set(from_ascii(name))), h_(0),
|
2014-04-04 20:52:21 +00:00
|
|
|
kerning_(0), scriptable_(false)
|
2001-11-09 16:27:44 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
InsetMathSymbol::InsetMathSymbol(docstring const & name)
|
2014-04-04 19:54:32 +00:00
|
|
|
: sym_(in_word_set(name)), h_(0), kerning_(0), scriptable_(false)
|
2001-12-05 08:04:20 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathSymbol::clone() const
|
2001-02-13 13:28:32 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathSymbol(*this);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring InsetMathSymbol::name() const
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
|
|
|
return sym_->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
|
2002-05-30 07:09:54 +00:00
|
|
|
{
|
|
|
|
//lyxerr << "metrics: symbol: '" << sym_->name
|
|
|
|
// << "' in font: '" << sym_->inset
|
|
|
|
// << "' drawn as: '" << sym_->draw
|
2007-12-12 19:28:07 +00:00
|
|
|
// << "'" << endl;
|
2002-10-17 13:15:25 +00:00
|
|
|
|
2011-01-01 19:24:16 +00:00
|
|
|
bool const italic_upcase_greek = sym_->inset == "cmr" &&
|
|
|
|
sym_->extra == "mathalpha" &&
|
|
|
|
mi.base.fontname == "mathit";
|
2014-12-29 20:46:30 +00:00
|
|
|
std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
|
Remove a conversion to_utf8() inside FontSetChanger
This requires to change many docstrings into std::strings. The logic behind that
is that they represent a fixed set of math fonts, and therefore “string” means
here “poor man's enum” rather than text (this is consistent with MetricsBase).
Profiling of scrolling inside a document over macro-instensive areas:
Before the patch:
44,1% BufferView::updateMetrics()
-> 34,8% InsetMathHull::metrics()
-> 9,8% FontSetChanger::FontSetChanger()
28,4% BufferView::draw()
After the patch:
35,3% BufferView::updateMetrics()
-> 27,2% InsetMathHull::metrics
-> 0,4% FontSetChanger::FontSetChanger()
47,5% BufferView::draw()
FontSetChanger::FontSetChanger() is made 41x less expensive (with reference
BV::draw()) just by removing this conversion. The remaining 0,4% could be
squished by replacing the strings with a proper enum, but this is premature. Of
course, this only treats the symptoms: there is no good reason that this
function is called 45500 times over the time of 40 repaints.
2016-06-07 21:58:55 +00:00
|
|
|
Changer dummy = mi.base.changeFontSet(font);
|
2007-05-11 14:17:53 +00:00
|
|
|
mathed_string_dim(mi.base.font, sym_->draw, dim);
|
|
|
|
docstring::const_reverse_iterator rit = sym_->draw.rbegin();
|
|
|
|
kerning_ = mathed_char_kerning(mi.base.font, *rit);
|
|
|
|
// correct height for broken cmex and wasy font
|
|
|
|
if (sym_->inset == "cmex" || sym_->inset == "wasy") {
|
|
|
|
h_ = 4 * dim.des / 5;
|
|
|
|
dim.asc += h_;
|
|
|
|
dim.des -= h_;
|
2001-08-30 22:42:26 +00:00
|
|
|
}
|
2007-05-11 14:17:53 +00:00
|
|
|
// seperate things a bit
|
2016-05-24 10:08:24 +00:00
|
|
|
if (isMathBin())
|
|
|
|
dim.wid += 2 * mathed_medmuskip(mi.base.font);
|
|
|
|
else if (isMathRel())
|
|
|
|
dim.wid += 2 * mathed_thickmuskip(mi.base.font);
|
|
|
|
else if (isMathPunct())
|
|
|
|
dim.wid += mathed_thinmuskip(mi.base.font);
|
|
|
|
// FIXME: I see no reason for this
|
|
|
|
//else
|
|
|
|
// dim.wid += support::iround(0.1667 * em);
|
2006-11-09 14:47:56 +00:00
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
scriptable_ = false;
|
|
|
|
if (mi.base.style == LM_ST_DISPLAY)
|
2006-11-13 17:35:18 +00:00
|
|
|
if (sym_->inset == "cmex" || sym_->inset == "esint" ||
|
2015-03-13 17:34:39 +00:00
|
|
|
sym_->extra == "funclim" ||
|
|
|
|
(sym_->inset == "stmry" && sym_->extra == "mathop"))
|
2002-06-18 15:44:30 +00:00
|
|
|
scriptable_ = true;
|
2001-06-25 00:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
//lyxerr << "metrics: symbol: '" << sym_->name
|
|
|
|
// << "' in font: '" << sym_->inset
|
|
|
|
// << "' drawn as: '" << sym_->draw
|
2007-12-12 19:28:07 +00:00
|
|
|
// << "'" << endl;
|
2011-01-01 19:24:16 +00:00
|
|
|
|
|
|
|
bool const italic_upcase_greek = sym_->inset == "cmr" &&
|
|
|
|
sym_->extra == "mathalpha" &&
|
|
|
|
pi.base.fontname == "mathit";
|
2014-12-29 20:46:30 +00:00
|
|
|
std::string const font = italic_upcase_greek ? "cmm" : sym_->inset;
|
2016-05-24 10:08:24 +00:00
|
|
|
if (isMathBin())
|
|
|
|
x += mathed_medmuskip(pi.base.font);
|
|
|
|
else if (isMathRel())
|
|
|
|
x += mathed_thickmuskip(pi.base.font);
|
|
|
|
// FIXME: I see no reason for this
|
|
|
|
//else
|
|
|
|
// x += support::iround(0.0833 * em);
|
2002-10-17 13:15:25 +00:00
|
|
|
|
Remove a conversion to_utf8() inside FontSetChanger
This requires to change many docstrings into std::strings. The logic behind that
is that they represent a fixed set of math fonts, and therefore “string” means
here “poor man's enum” rather than text (this is consistent with MetricsBase).
Profiling of scrolling inside a document over macro-instensive areas:
Before the patch:
44,1% BufferView::updateMetrics()
-> 34,8% InsetMathHull::metrics()
-> 9,8% FontSetChanger::FontSetChanger()
28,4% BufferView::draw()
After the patch:
35,3% BufferView::updateMetrics()
-> 27,2% InsetMathHull::metrics
-> 0,4% FontSetChanger::FontSetChanger()
47,5% BufferView::draw()
FontSetChanger::FontSetChanger() is made 41x less expensive (with reference
BV::draw()) just by removing this conversion. The remaining 0,4% could be
squished by replacing the strings with a proper enum, but this is premature. Of
course, this only treats the symptoms: there is no good reason that this
function is called 45500 times over the time of 40 repaints.
2016-06-07 21:58:55 +00:00
|
|
|
Changer dummy = pi.base.changeFontSet(font);
|
2006-11-07 19:29:01 +00:00
|
|
|
pi.draw(x, y - h_, sym_->draw);
|
2001-02-13 13:28:32 +00:00
|
|
|
}
|
2001-08-09 08:53:16 +00:00
|
|
|
|
|
|
|
|
2013-01-09 21:23:07 +00:00
|
|
|
InsetMath::mode_type InsetMathSymbol::currentMode() const
|
|
|
|
{
|
|
|
|
return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-24 10:08:24 +00:00
|
|
|
bool InsetMathSymbol::isMathBin() const
|
|
|
|
{
|
|
|
|
return sym_->extra == "mathbin";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetMathSymbol::isMathRel() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
return sym_->extra == "mathrel";
|
2001-08-30 22:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-24 10:08:24 +00:00
|
|
|
bool InsetMathSymbol::isMathPunct() const
|
|
|
|
{
|
|
|
|
return sym_->extra == "mathpunct";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-23 02:12:48 +00:00
|
|
|
bool InsetMathSymbol::isOrdAlpha() const
|
|
|
|
{
|
|
|
|
return sym_->extra == "mathord" || sym_->extra == "mathalpha";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathSymbol::isScriptable() const
|
2001-09-11 10:58:17 +00:00
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
return scriptable_;
|
2001-09-11 10:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
bool InsetMathSymbol::takesLimits() const
|
2001-08-30 22:42:26 +00:00
|
|
|
{
|
2002-06-18 15:44:30 +00:00
|
|
|
return
|
|
|
|
sym_->inset == "cmex" ||
|
|
|
|
sym_->inset == "lyxboldsymb" ||
|
2006-11-13 17:35:18 +00:00
|
|
|
sym_->inset == "esint" ||
|
2015-03-13 17:34:39 +00:00
|
|
|
sym_->extra == "funclim" ||
|
|
|
|
(sym_->inset == "stmry" && sym_->extra == "mathop");
|
2001-08-09 08:53:16 +00:00
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2002-05-30 07:09:54 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::normalize(NormalStream & os) const
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "[symbol " << name() << ']';
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::maple(MapleStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2001-11-09 14:23:44 +00:00
|
|
|
if (name() == "cdot")
|
2001-11-07 17:30:26 +00:00
|
|
|
os << '*';
|
2002-06-25 13:19:50 +00:00
|
|
|
else if (name() == "infty")
|
|
|
|
os << "infinity";
|
2001-11-07 17:30:26 +00:00
|
|
|
else
|
2001-12-05 08:04:20 +00:00
|
|
|
os << name();
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::maxima(MaximaStream & os) const
|
2002-10-28 17:15:19 +00:00
|
|
|
{
|
|
|
|
if (name() == "cdot")
|
|
|
|
os << '*';
|
|
|
|
else if (name() == "infty")
|
2006-09-02 01:57:36 +00:00
|
|
|
os << "inf";
|
|
|
|
else if (name() == "pi")
|
|
|
|
os << "%pi";
|
2002-10-28 17:15:19 +00:00
|
|
|
else
|
|
|
|
os << name();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
{
|
|
|
|
if ( name() == "pi") { os << "Pi"; return;}
|
|
|
|
if ( name() == "infty") { os << "Infinity"; return;}
|
2006-09-02 01:57:36 +00:00
|
|
|
if ( name() == "cdot") { os << '*'; return;}
|
2002-07-01 11:17:14 +00:00
|
|
|
os << name();
|
|
|
|
}
|
|
|
|
|
2001-11-07 08:51:35 +00:00
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathSymbol::mathmlize(MathStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2014-12-29 20:13:42 +00:00
|
|
|
// FIXME We may need to do more interesting things
|
2010-03-30 00:24:54 +00:00
|
|
|
// with MathMLtype.
|
2014-12-29 20:13:42 +00:00
|
|
|
char const * type = sym_->MathMLtype();
|
2001-11-09 18:55:03 +00:00
|
|
|
os << '<' << type << "> ";
|
2014-12-29 20:13:42 +00:00
|
|
|
if (sym_->xmlname == "x")
|
2009-11-14 16:21:33 +00:00
|
|
|
// unknown so far
|
2001-12-05 08:04:20 +00:00
|
|
|
os << name();
|
2001-11-09 18:55:03 +00:00
|
|
|
else
|
2001-12-05 08:04:20 +00:00
|
|
|
os << sym_->xmlname;
|
2001-11-09 18:55:03 +00:00
|
|
|
os << " </" << type << '>';
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 22:51:02 +00:00
|
|
|
void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
|
2010-03-30 00:24:54 +00:00
|
|
|
{
|
2014-12-29 20:13:42 +00:00
|
|
|
// FIXME We may need to do more interesting things
|
2010-03-30 00:24:54 +00:00
|
|
|
// with MathMLtype.
|
2014-12-29 20:13:42 +00:00
|
|
|
char const * type = sym_->MathMLtype();
|
2010-03-30 00:24:54 +00:00
|
|
|
bool op = (std::string(type) == "mo");
|
2014-12-29 20:13:42 +00:00
|
|
|
|
|
|
|
if (sym_->xmlname == "x")
|
2010-03-30 00:24:54 +00:00
|
|
|
// unknown so far
|
|
|
|
os << ' ' << name() << ' ';
|
2014-12-29 20:13:42 +00:00
|
|
|
else if (op && spacing)
|
2010-03-30 00:24:54 +00:00
|
|
|
os << ' ' << sym_->xmlname << ' ';
|
|
|
|
else
|
|
|
|
os << sym_->xmlname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 22:51:02 +00:00
|
|
|
void InsetMathSymbol::htmlize(HtmlStream & os) const
|
|
|
|
{
|
|
|
|
htmlize(os, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::octave(OctaveStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2001-11-09 14:23:44 +00:00
|
|
|
if (name() == "cdot")
|
2001-11-07 17:30:26 +00:00
|
|
|
os << '*';
|
|
|
|
else
|
2001-12-05 08:04:20 +00:00
|
|
|
os << name();
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathSymbol::write(WriteStream & os) const
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2016-06-02 22:49:36 +00:00
|
|
|
unique_ptr<MathEnsurer> ensurer;
|
2013-01-09 21:23:07 +00:00
|
|
|
if (currentMode() != TEXT_MODE)
|
2016-06-02 22:49:36 +00:00
|
|
|
ensurer = make_unique<MathEnsurer>(os);
|
2016-09-10 16:32:44 +00:00
|
|
|
else
|
|
|
|
ensurer = make_unique<MathEnsurer>(os, false, true, true);
|
2002-08-22 10:04:11 +00:00
|
|
|
os << '\\' << name();
|
2007-04-01 13:06:41 +00:00
|
|
|
|
|
|
|
// $,#, etc. In theory the restriction based on catcodes, but then
|
|
|
|
// we do not handle catcodes very well, let alone cat code changes,
|
|
|
|
// so being outside the alpha range is good enough.
|
2007-04-02 15:21:36 +00:00
|
|
|
if (name().size() == 1 && !isAlphaASCII(name()[0]))
|
2007-03-30 19:28:03 +00:00
|
|
|
return;
|
2007-04-01 13:06:41 +00:00
|
|
|
|
2002-08-22 10:04:11 +00:00
|
|
|
os.pendingSpace(true);
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
2001-11-07 17:30:26 +00:00
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
void InsetMathSymbol::infoize2(odocstream & os) const
|
2002-01-16 14:24:38 +00:00
|
|
|
{
|
2007-11-28 22:12:03 +00:00
|
|
|
os << from_ascii("Symbol: ") << name();
|
2002-01-16 14:24:38 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2010-03-30 22:51:02 +00:00
|
|
|
void InsetMathSymbol::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
|
|
|
// this is not really the ideal place to do this, but we can't
|
|
|
|
// validate in InsetMathExInt.
|
2010-03-31 18:24:52 +00:00
|
|
|
if (features.runparams().math_flavor == OutputParams::MathAsHTML
|
2010-03-30 22:51:02 +00:00
|
|
|
&& sym_->name == from_ascii("int")) {
|
2011-12-06 22:17:13 +00:00
|
|
|
features.addCSSSnippet(
|
2010-03-30 22:51:02 +00:00
|
|
|
"span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
|
|
|
|
"span.limits span{display: block;}\n"
|
|
|
|
"span.intsym{font-size: 150%;}\n"
|
|
|
|
"sub.limit{font-size: 75%;}\n"
|
2011-12-06 22:17:13 +00:00
|
|
|
"sup.limit{font-size: 75%;}");
|
2010-03-30 22:51:02 +00:00
|
|
|
} else {
|
|
|
|
if (!sym_->requires.empty())
|
2014-12-29 20:46:30 +00:00
|
|
|
features.require(sym_->requires);
|
2010-03-30 22:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|