change output to uses streams instead of strings

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2977 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-11-07 17:30:26 +00:00
parent f07a117fc3
commit 7cccd80619
26 changed files with 366 additions and 136 deletions

View File

@ -72,6 +72,8 @@ libmathed_la_SOURCES = \
math_macrotemplate.h \
math_macrotable.C \
math_macrotable.h \
math_mathmlstream.C \
math_mathmlstream.h \
math_matrixinset.C \
math_matrixinset.h \
math_metricsinfo.h \

View File

@ -194,7 +194,7 @@ MathArray MathArray::glueChars() const
}
bool needAsterisk(MathAtom const & a, MathAtom const & b)
bool needAsterisk(MathAtom const &, MathAtom const &)
{
return false;
}
@ -245,51 +245,47 @@ void MathArray::writeNormal(ostream & os) const
}
string MathArray::octavize() const
void MathArray::octavize(OctaveStream & os) const
{
MathArray ar = glueChars();
string res;
for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) {
res += q->octavize(p);
q->octavize(p, os);
++it;
} else
res += p->octavize();
p->octavize(os);
}
return res;
}
string MathArray::maplize() const
void MathArray::maplize(MapleStream & os) const
{
MathArray ar = glueChars();
string res;
for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) {
res += q->maplize(p);
q->maplize(p, os);
++it;
} else
res += p->maplize();
p->maplize(os);
}
return res;
}
string MathArray::mathmlize() const
void MathArray::mathmlize(MathMLStream & os) const
{
MathArray ar = glueChars();
string res;
os << "<mrow>";
for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) {
res += q->mathmlize(p);
q->mathmlize(p, os);
++it;
} else
res += p->mathmlize();
p->mathmlize(os);
}
return res;
os << "</mrow>";
}

View File

@ -26,6 +26,9 @@ class MathScriptInset;
class MathMacro;
class MathWriteInfo;
class MathMetricsInfo;
class MathMLStream;
class MapleStream;
class OctaveStream;
class LaTeXFeatures;
#ifdef __GNUG__
@ -122,11 +125,11 @@ public:
MathArray guessAsterisks() const;
/// interface to Octave
string octavize() const;
void octavize(OctaveStream &) const;
/// interface to Maple
string maplize() const;
void maplize(MapleStream &) const;
/// interface to MathML
string mathmlize() const;
void mathmlize(MathMLStream &) const;
///
bool isMatrix() const;
@ -139,4 +142,5 @@ private:
std::ostream & operator<<(std::ostream & os, MathArray const & ar);
#endif

View File

@ -67,16 +67,33 @@ namespace {
MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
{
string header =
"readlib(latex):\n"
"`latex/csname_font` := ``:\n"
"`latex/latex/*` := subs(`\\,`=`\\cdot `,eval(`latex/latex/*`)):\n";
string header = "readlib(latex):\n";
// remove the \\it for variable names
//"#`latex/csname_font` := `\\it `:"
header +=
"`latex/csname_font` := ``:\n";
// export matrices in (...) instead of [...]
header +=
"`latex/latex/matrix` := "
"subs(`[`=`(`, `]`=`)`,"
"eval(`latex/latex/matrix`)):\n";
// replace \\cdots with proper '*'
header +=
"`latex/latex/*` := "
"subs(`\\,`=`\\cdot `,"
"eval(`latex/latex/*`)):\n";
//"#`latex/latex/symbol` "
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
string trailer = "quit;";
string expr = ar.maplize();
ostringstream os;
MapleStream ms(os);
ms << ar;
string expr = os.str();
for (int i = 0; i < 100; ++i) { // at most 100 attempts
// try to fix missing '*' the hard way by using mint
@ -119,7 +136,10 @@ namespace {
MathArray pipeThroughOctave(string const &, MathArray const & ar)
{
string expr = ar.octavize();
ostringstream os;
OctaveStream vs(os);
vs << ar;
string expr = os.str();
string out;
for (int i = 0; i < 100; ++i) { // at most 100 attempts
@ -278,13 +298,17 @@ int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
{
return ascii(buf, os, 0);
return docbook(buf, os);
}
int InsetFormula::docbook(Buffer const * buf, ostream & os) const
{
return ascii(buf, os, 0);
MathMLStream ms(os);
ms << "<equation><alt>";
int res = ascii(buf, ms.os_, 0);
ms << "</alt>\n<mml>" << par_.nucleus() << "<mml></equation>";
return res + 1;
}

View File

@ -100,22 +100,24 @@ bool MathDelimInset::isMatrix() const
}
string MathDelimInset::octavize() const
void MathDelimInset::octavize(OctaveStream & os) const
{
if (left_ == "|" && right_ == "|")
return "det(" + cell(0).octavize() + ")";
return left_ + cell(0).octavize() + right_;
os << "det(" << cell(0) << ")";
else
os << left_.c_str() << cell(0) << right_.c_str();
}
string MathDelimInset::maplize() const
void MathDelimInset::maplize(MapleStream & os) const
{
if (left_ == "|" && right_ == "|") {
if (cell(0).isMatrix())
return "linalg[det](" + cell(0).maplize() + ")";
os << "linalg[det](" << cell(0) << ")";
else
return "abs(" + cell(0).maplize() + ")";
os << "abs(" << cell(0) << ")";
}
return left_ + cell(0).maplize() + right_;
else
os << left_.c_str() << cell(0) << right_.c_str();
}

View File

@ -30,9 +30,9 @@ public:
///
bool isMatrix() const;
///
string octavize() const;
void octavize(OctaveStream &) const;
///
string maplize() const;
void maplize(MapleStream &) const;
private:
///
int dw() const;

View File

@ -49,7 +49,7 @@ void MathExFuncInset::draw(Painter & pain, int x, int y) const
}
string MathExFuncInset::octavize() const
void MathExFuncInset::octavize(OctaveStream & os) const
{
return name_ + '(' + cell(0).octavize() + ')';
os << name_.c_str() << '(' << cell(0) << ')';
}

View File

@ -22,7 +22,7 @@ public:
///
void draw(Painter &, int x, int y) const;
///
string octavize() const;
void octavize(OctaveStream &) const;
private:
///

View File

@ -63,7 +63,13 @@ void MathFracInset::writeNormal(std::ostream & os) const
}
string MathFracInset::maplize() const
void MathFracInset::maplize(MapleStream & os) const
{
return '(' + cell(0).maplize() + '/' + cell(1).maplize() + ')';
os << '(' << cell(0) << '/' << cell(1) << ')';
}
void MathFracInset::mathmlize(MathMLStream & os) const
{
os << "<mfrac>" << cell(0) << cell(1) << "</mfrac>";
}

View File

@ -26,7 +26,9 @@ public:
///
void draw(Painter &, int x, int y) const;
///
string maplize() const;
void maplize(MapleStream &) const;
///
void mathmlize(MathMLStream &) const;
public:
///
const bool atop_;

View File

@ -587,40 +587,35 @@ std::vector<MathInset::idx_type>
}
string MathGridInset::octavize() const
void MathGridInset::octavize(OctaveStream & os) const
{
string res;
res += '[';
os << '[';
for (row_type row = 0; row < nrows(); ++row) {
if (row)
res += ';';
res += '[';
for (col_type col = 0; col < ncols(); ++col) {
res += cell(index(row, col)).octavize();
res += ' ';
}
res += ']';
os << ';';
os << '[';
for (col_type col = 0; col < ncols(); ++col)
os << cell(index(row, col)) << ' ';
os <<']';
}
res += ']';
return res;
os <<']';
}
string MathGridInset::maplize() const
void MathGridInset::maplize(MapleStream & os) const
{
string res = "array([";
os << "array([";
for (row_type row = 0; row < nrows(); ++row) {
if (row)
res += ',';
res += '[';
os << ',';
os << '[';
for (col_type col = 0; col < ncols(); ++col) {
if (col)
res += ',';
res += cell(index(row, col)).maplize();
os << ',';
os << cell(index(row, col));
}
res += ']';
os << ']';
}
res += "])";
return res;
os << "])";
}

View File

@ -150,9 +150,9 @@ public:
void setDefaults();
///
string octavize() const;
void octavize(OctaveStream &) const;
///
string maplize() const;
void maplize(MapleStream &) const;
protected:
/// returns proper 'end of line' code for LaTeX

View File

@ -217,22 +217,19 @@ void MathInset::write(MathWriteInfo &) const
}
string MathInset::octavize() const
void MathInset::octavize(OctaveStream & os) const
{
ostringstream os;
writeNormal(os);
return os.str();
return string();
writeNormal(os.os_);
}
string MathInset::maplize() const
void MathInset::maplize(MapleStream & os) const
{
return octavize();
writeNormal(os.os_);
}
string MathInset::mathmlize() const
void MathInset::mathmlize(MathMLStream & os) const
{
return string();
writeNormal(os.os_);
}

View File

@ -30,7 +30,7 @@
#include "xarray.h"
#include "math_defs.h"
#include "LString.h"
#include "math_mathmlstream.h"
/** Abstract base class for all math objects.
A math insets is for use of the math editor only, it isn't a
@ -255,11 +255,11 @@ public:
virtual void handleFont(MathTextCodes) {}
///
virtual string octavize() const;
virtual void octavize(OctaveStream &) const;
///
virtual string maplize() const;
virtual void maplize(MapleStream &) const;
///
virtual string mathmlize() const;
virtual void mathmlize(MathMLStream &) const;
};
std::ostream & operator<<(std::ostream &, MathInset const &);

View File

@ -0,0 +1,92 @@
#include "math_inset.h"
#include "math_mathmlstream.h"
MathMLStream & MathMLStream::operator<<(MathInset const * p)
{
p->mathmlize(*this);
return *this;
}
MathMLStream & MathMLStream::operator<<(MathArray const & ar)
{
ar.mathmlize(*this);
return *this;
}
MathMLStream & MathMLStream::operator<<(char const * s)
{
os_ << s;
return *this;
}
MathMLStream & MathMLStream::operator<<(char c)
{
os_ << c;
return *this;
}
MapleStream & MapleStream::operator<<(MathInset const * p)
{
p->maplize(*this);
return *this;
}
MapleStream & MapleStream::operator<<(MathArray const & ar)
{
ar.maplize(*this);
return *this;
}
MapleStream & MapleStream::operator<<(char const * s)
{
os_ << s;
return *this;
}
MapleStream & MapleStream::operator<<(char c)
{
os_ << c;
return *this;
}
OctaveStream & OctaveStream::operator<<(MathInset const * p)
{
p->octavize(*this);
return *this;
}
OctaveStream & OctaveStream::operator<<(MathArray const & ar)
{
ar.octavize(*this);
return *this;
}
OctaveStream & OctaveStream::operator<<(char const * s)
{
os_ << s;
return *this;
}
OctaveStream & OctaveStream::operator<<(char c)
{
os_ << c;
return *this;
}

View File

@ -0,0 +1,54 @@
#ifndef MATH_MATH_MLSTREAM
#define MATH_MATH_MLSTREAM
#include <iosfwd>
struct MathMLStream {
///
explicit MathMLStream(std::ostream & os) : os_(os) {}
///
std::ostream & os_;
///
MathMLStream & operator<<(MathInset const *);
///
MathMLStream & operator<<(MathArray const &);
///
MathMLStream & operator<<(char const *);
///
MathMLStream & operator<<(char);
};
struct MapleStream {
///
explicit MapleStream(std::ostream & os) : os_(os) {}
///
std::ostream & os_;
///
MapleStream & operator<<(MathInset const *);
///
MapleStream & operator<<(MathArray const &);
///
MapleStream & operator<<(char const *);
///
MapleStream & operator<<(char);
};
struct OctaveStream {
///
explicit OctaveStream(std::ostream & os) : os_(os) {}
///
std::ostream & os_;
///
OctaveStream & operator<<(MathInset const *);
///
OctaveStream & operator<<(MathArray const &);
///
OctaveStream & operator<<(char const *);
///
OctaveStream & operator<<(char);
};
#endif

View File

@ -94,7 +94,13 @@ bool MathRootInset::idxDown(int & idx, int & pos) const
}
string MathRootInset::octavize() const
void MathRootInset::octavize(OctaveStream & os) const
{
return "root(" + cell(1).octavize() + ',' + cell(0).octavize() + ')';
os << "root(" << cell(1) << ',' << cell(0) <<')';
}
void MathRootInset::mathmlize(MathMLStream & os) const
{
os << "<mroot>" << cell(1) << cell(0) << "</mroot>";
}

View File

@ -44,7 +44,9 @@ public:
///
bool idxDown(int & idx, int & pos) const;
///
string octavize() const;
void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;
};
#endif

View File

@ -346,28 +346,47 @@ bool MathScriptInset::idxLeft(MathInset::idx_type &,
}
string MathScriptInset::maplize(MathInset const * nuc) const
void MathScriptInset::maplize(MathInset const * nuc, MapleStream & os) const
{
string res;
if (nuc)
res += nuc->maplize();
os << nuc;
if (hasDown() && down().data_.size())
res += "[" + down().data_.maplize() + "]";
os << '[' << down().data_ << ']';
if (hasUp() && up().data_.size())
res += "^(" + up().data_.maplize() + ")";
return res;
os << "^(" << up().data_ << ')';
}
string MathScriptInset::octavize(MathInset const * nuc) const
{
return maplize(nuc);
}
string MathScriptInset::mathmlize(MathInset const * nuc) const
void MathScriptInset::octavize(MathInset const * nuc, OctaveStream & os) const
{
if (nuc)
return nuc->mathmlize();
return string();
os << nuc;
if (hasDown() && down().data_.size())
os << '[' << down().data_ << ']';
if (hasUp() && up().data_.size())
os << "^(" << up().data_ << ')';
}
void MathScriptInset::mathmlize(MathInset const * nuc, MathMLStream & os) const
{
bool d = hasDown() && down().data_.size();
bool u = hasUp() && up().data_.size();
if (u)
os << "<sup>";
if (d)
os << "<sub>";
if (nuc)
os << nuc;
else
os << "<mrow/>";
if (d)
os << down().data_ << "</sub>";
if (u)
os << up().data_ << "</sup>";
}

View File

@ -82,11 +82,11 @@ public:
void ensure(bool up);
///
virtual string octavize(MathInset const * nuc) const;
void octavize(MathInset const * nuc, OctaveStream & os) const;
///
virtual string maplize(MathInset const * nuc) const;
void maplize(MathInset const * nuc, MapleStream & os) const;
///
virtual string mathmlize(MathInset const * nuc) const;
void mathmlize(MathInset const * nuc, MathMLStream & os) const;
public:
/// returns x offset for main part

View File

@ -57,7 +57,13 @@ void MathSqrtInset::writeNormal(std::ostream & os) const
}
string MathSqrtInset::maplize() const
void MathSqrtInset::maplize(MapleStream & os) const
{
return "sqrt(" + cell(0).maplize() + ')';
os << "sqrt(" << cell(0) << ')';
}
void MathSqrtInset::mathmlize(MathMLStream & os) const
{
os << "<msqrt>" << cell(0) << "</msqrt>";
}

View File

@ -26,6 +26,8 @@ public:
///
void metrics(MathMetricsInfo const & st) const;
///
string maplize() const;
void maplize(MapleStream &) const;
///
void mathmlize(MathMLStream &) const;
};
#endif

View File

@ -70,25 +70,42 @@ void MathStringInset::writeNormal(std::ostream & os) const
}
string MathStringInset::octavize() const
void MathStringInset::maplize(MapleStream & os) const
{
return maplize();
}
string MathStringInset::maplize() const
{
if (code_ != LM_TC_VAR)
return str_;
if (str_.size() <= 1)
return str_;
string res;
if (code_ != LM_TC_VAR || str_.size() <= 1) {
os << str_.c_str();
return;
}
// insert '*' between adjacent chars if type is LM_TC_VAR
res += str_[0];
for (string::size_type i = 1; i < str_.size(); ++i) {
res += '*';
res += str_[i];
}
return res;
os << str_[0];
for (string::size_type i = 1; i < str_.size(); ++i)
os << '*' << str_[i];
}
void MathStringInset::octavize(OctaveStream & os) const
{
if (code_ != LM_TC_VAR || str_.size() <= 1) {
os << str_.c_str();
return;
}
// insert '*' between adjacent chars if type is LM_TC_VAR
os << str_[0];
for (string::size_type i = 1; i < str_.size(); ++i)
os << '*' << str_[i];
}
void MathStringInset::mathmlize(MathMLStream & os) const
{
if (code_ == LM_TC_VAR)
os << "<mi>" << str_.c_str() << "</mi>";
else if (code_ == LM_TC_CONST)
os << "<mn>" << str_.c_str() << "</mn>";
else if (code_ == LM_TC_RM || code_ == LM_TC_TEXTRM)
os << "<mtext>" << str_.c_str() << "</mtext>";
else
os << str_.c_str();
}

View File

@ -33,11 +33,11 @@ public:
///
int width() const;
///
string & str();
void octavize(OctaveStream &) const;
///
string octavize() const;
void maplize(MapleStream &) const;
///
string maplize() const;
void mathmlize(MathMLStream &) const;
private:
/// the string

View File

@ -114,23 +114,27 @@ bool MathSymbolInset::takesLimits() const
}
string MathSymbolInset::octavize() const
void MathSymbolInset::maplize(MapleStream & os) const
{
if (sym_->name == "cdot")
return "*";
return sym_->name;
os << '*';
else
os << sym_->name.c_str();
}
string MathSymbolInset::maplize() const
void MathSymbolInset::mathmlize(MathMLStream & os) const
{
os << sym_->name.c_str();
}
void MathSymbolInset::octavize(OctaveStream & os) const
{
if (sym_->name == "cdot")
return "*";
return sym_->name;
os << '*';
else
os << sym_->name.c_str();
}
string MathSymbolInset::mathmlize() const
{
return sym_->name;
}

View File

@ -31,11 +31,11 @@ public:
bool takesLimits() const;
///
string octavize() const;
void maplize(MapleStream &) const;
///
string maplize() const;
void mathmlize(MathMLStream &) const;
///
string mathmlize() const;
void octavize(OctaveStream &) const;
private:
///
MathTextCodes code() const;