2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file InsetMathString.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
|
|
|
|
2003-08-19 13:00:56 +00:00
|
|
|
#include <config.h>
|
2001-11-05 17:08:45 +00:00
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathString.h"
|
2009-08-07 05:25:22 +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"
|
2001-11-05 17:08:45 +00:00
|
|
|
|
2008-05-31 20:54:50 +00:00
|
|
|
#include "Encoding.h"
|
|
|
|
|
2009-08-07 05:25:22 +00:00
|
|
|
#include "support/debug.h"
|
2008-05-31 20:54:50 +00:00
|
|
|
#include "support/gettext.h"
|
2009-11-14 20:20:17 +00:00
|
|
|
#include "support/lassert.h"
|
2008-06-03 00:09:20 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/textutils.h"
|
2008-05-31 20:54:50 +00:00
|
|
|
|
2010-02-04 19:08:17 +00:00
|
|
|
using lyx::support::escape;
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
InsetMathString::InsetMathString(docstring const & s)
|
2002-05-30 07:09:54 +00:00
|
|
|
: str_(s)
|
2001-11-05 17:08:45 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
Inset * InsetMathString::clone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new InsetMathString(*this);
|
2001-11-05 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void InsetMathString::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-11-05 17:08:45 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
mathed_string_dim(mi.base.font, str_, dim);
|
2001-11-05 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::draw(PainterInfo & pi, int x, int y) const
|
2002-03-21 17:42:56 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
pi.draw(x, y, str_);
|
2001-11-05 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::normalize(NormalStream & os) const
|
2001-11-05 17:08:45 +00:00
|
|
|
{
|
2002-11-27 10:30:28 +00:00
|
|
|
os << "[string " << str_ << ' ' << "mathalpha" << ']';
|
2001-11-05 17:08:45 +00:00
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::maple(MapleStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2002-07-01 11:17:14 +00:00
|
|
|
if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
|
2001-12-05 08:04:20 +00:00
|
|
|
os << ' ' << str_ << ' ';
|
2001-11-07 17:30:26 +00:00
|
|
|
return;
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-07 17:30:26 +00:00
|
|
|
|
|
|
|
// insert '*' between adjacent chars if type is LM_TC_VAR
|
|
|
|
os << str_[0];
|
2006-10-22 10:15:23 +00:00
|
|
|
for (size_t i = 1; i < str_.size(); ++i)
|
2002-06-25 13:19:50 +00:00
|
|
|
os << str_[i];
|
2001-11-07 08:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
{
|
|
|
|
os << ' ' << str_ << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::octave(OctaveStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
if (/*code_ != LM_TC_VAR ||*/ str_.size() <= 1) {
|
2001-12-05 08:04:20 +00:00
|
|
|
os << ' ' << str_ << ' ';
|
2001-11-07 17:30:26 +00:00
|
|
|
return;
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
|
|
// insert '*' between adjacent chars if type is LM_TC_VAR
|
2001-11-07 17:30:26 +00:00
|
|
|
os << str_[0];
|
2006-10-22 10:15:23 +00:00
|
|
|
for (size_t i = 1; i < str_.size(); ++i)
|
2002-06-25 13:19:50 +00:00
|
|
|
os << str_[i];
|
2001-11-07 17:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void InsetMathString::mathmlize(MathStream & /*os*/) const
|
|
|
|
{
|
|
|
|
// useless, no doubt, but we should not be here
|
|
|
|
LASSERT(false, /* */);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
void InsetMathString::write(WriteStream & os) const
|
2001-11-15 15:14:27 +00:00
|
|
|
{
|
2009-07-03 00:05:58 +00:00
|
|
|
if (!os.latex() || os.lockedMode()) {
|
2010-02-04 19:08:17 +00:00
|
|
|
os << (os.asciiOnly() ? escape(str_) : str_);
|
2008-05-31 20:54:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring::const_iterator cit = str_.begin();
|
|
|
|
docstring::const_iterator end = str_.end();
|
|
|
|
|
2008-06-16 01:21:17 +00:00
|
|
|
// We may already be inside an \ensuremath command.
|
|
|
|
bool in_forced_mode = os.pendingBrace();
|
|
|
|
|
|
|
|
// We will take care of matching braces.
|
|
|
|
os.pendingBrace(false);
|
|
|
|
|
2008-05-31 20:54:50 +00:00
|
|
|
while (cit != end) {
|
2008-09-25 18:38:25 +00:00
|
|
|
bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
|
2008-05-31 20:54:50 +00:00
|
|
|
char_type const c = *cit;
|
2008-09-25 18:38:25 +00:00
|
|
|
docstring command(1, c);
|
2008-05-31 20:54:50 +00:00
|
|
|
try {
|
2012-03-25 13:36:00 +00:00
|
|
|
bool termination = false;
|
2012-12-30 10:58:21 +00:00
|
|
|
if (isASCII(c) ||
|
2012-03-25 13:36:00 +00:00
|
|
|
Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
|
2008-06-04 00:36:04 +00:00
|
|
|
if (os.textMode()) {
|
2008-06-16 01:21:17 +00:00
|
|
|
if (in_forced_mode) {
|
|
|
|
// we were inside \lyxmathsym
|
2008-06-04 00:36:04 +00:00
|
|
|
os << '}';
|
2008-06-16 01:21:17 +00:00
|
|
|
os.textMode(false);
|
2008-06-04 00:36:04 +00:00
|
|
|
in_forced_mode = false;
|
|
|
|
}
|
2012-12-30 10:58:21 +00:00
|
|
|
if (!isASCII(c) && os.textMode()) {
|
2008-06-04 00:36:04 +00:00
|
|
|
os << "\\ensuremath{";
|
2008-06-16 01:21:17 +00:00
|
|
|
os.textMode(false);
|
2008-06-04 00:36:04 +00:00
|
|
|
in_forced_mode = true;
|
|
|
|
}
|
2012-12-30 10:58:21 +00:00
|
|
|
} else if (isASCII(c) && in_forced_mode) {
|
2008-06-16 01:21:17 +00:00
|
|
|
// we were inside \ensuremath
|
2008-05-31 20:54:50 +00:00
|
|
|
os << '}';
|
2008-06-16 01:21:17 +00:00
|
|
|
os.textMode(true);
|
2008-06-04 00:36:04 +00:00
|
|
|
in_forced_mode = false;
|
2008-05-31 20:54:50 +00:00
|
|
|
}
|
2008-06-16 01:21:17 +00:00
|
|
|
} else if (!os.textMode()) {
|
2008-06-04 00:36:04 +00:00
|
|
|
if (in_forced_mode) {
|
2008-06-16 01:21:17 +00:00
|
|
|
// we were inside \ensuremath
|
2008-06-04 00:36:04 +00:00
|
|
|
os << '}';
|
|
|
|
in_forced_mode = false;
|
2008-06-16 01:21:17 +00:00
|
|
|
} else {
|
2009-03-30 22:40:27 +00:00
|
|
|
os << "\\lyxmathsym{";
|
2008-06-16 01:21:17 +00:00
|
|
|
in_forced_mode = true;
|
2008-06-04 00:36:04 +00:00
|
|
|
}
|
2008-06-16 01:21:17 +00:00
|
|
|
os.textMode(true);
|
2008-05-31 20:54:50 +00:00
|
|
|
}
|
2008-06-04 00:36:04 +00:00
|
|
|
os << command;
|
2008-06-03 00:09:20 +00:00
|
|
|
// We may need a space if the command contains a macro
|
|
|
|
// and the last char is ASCII.
|
2012-03-25 13:36:00 +00:00
|
|
|
if (termination)
|
2008-06-03 00:09:20 +00:00
|
|
|
os.pendingSpace(true);
|
2012-09-17 08:01:26 +00:00
|
|
|
} catch (EncodingException const & e) {
|
2009-08-07 05:25:22 +00:00
|
|
|
switch (os.output()) {
|
|
|
|
case WriteStream::wsDryrun: {
|
2008-05-31 20:54:50 +00:00
|
|
|
os << "<" << _("LyX Warning: ")
|
|
|
|
<< _("uncodable character") << " '";
|
|
|
|
os << docstring(1, e.failed_char);
|
|
|
|
os << "'>";
|
2009-08-07 05:25:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WriteStream::wsPreview: {
|
|
|
|
// indicate the encoding error by a boxed '?'
|
2010-12-21 15:28:55 +00:00
|
|
|
os << "{\\fboxsep=1pt\\fbox{?}}";
|
2009-08-07 05:25:22 +00:00
|
|
|
LYXERR0("Uncodable character" << " '"
|
|
|
|
<< docstring(1, e.failed_char)
|
|
|
|
<< "'");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WriteStream::wsDefault:
|
|
|
|
default:
|
2008-05-31 20:54:50 +00:00
|
|
|
// throw again
|
|
|
|
throw(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++cit;
|
|
|
|
}
|
2008-06-16 01:21:17 +00:00
|
|
|
|
|
|
|
if (in_forced_mode && os.textMode()) {
|
|
|
|
// We have to care for closing \lyxmathsym
|
2008-05-31 20:54:50 +00:00
|
|
|
os << '}';
|
2008-06-16 01:21:17 +00:00
|
|
|
os.textMode(false);
|
|
|
|
} else {
|
|
|
|
os.pendingBrace(in_forced_mode);
|
|
|
|
}
|
2001-11-15 15:14:27 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|