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_macrotemplate.h \
math_macrotable.C \ math_macrotable.C \
math_macrotable.h \ math_macrotable.h \
math_mathmlstream.C \
math_mathmlstream.h \
math_matrixinset.C \ math_matrixinset.C \
math_matrixinset.h \ math_matrixinset.h \
math_metricsinfo.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; 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(); MathArray ar = glueChars();
string res;
for (const_iterator it = ar.begin(); it != ar.end(); ++it) { for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus(); MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) { if (MathScriptInset const * q = ar.asScript(it)) {
res += q->octavize(p); q->octavize(p, os);
++it; ++it;
} else } else
res += p->octavize(); p->octavize(os);
} }
return res;
} }
string MathArray::maplize() const void MathArray::maplize(MapleStream & os) const
{ {
MathArray ar = glueChars(); MathArray ar = glueChars();
string res;
for (const_iterator it = ar.begin(); it != ar.end(); ++it) { for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus(); MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) { if (MathScriptInset const * q = ar.asScript(it)) {
res += q->maplize(p); q->maplize(p, os);
++it; ++it;
} else } else
res += p->maplize(); p->maplize(os);
} }
return res;
} }
string MathArray::mathmlize() const void MathArray::mathmlize(MathMLStream & os) const
{ {
MathArray ar = glueChars(); MathArray ar = glueChars();
string res; os << "<mrow>";
for (const_iterator it = ar.begin(); it != ar.end(); ++it) { for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
MathInset const * p = it->nucleus(); MathInset const * p = it->nucleus();
if (MathScriptInset const * q = ar.asScript(it)) { if (MathScriptInset const * q = ar.asScript(it)) {
res += q->mathmlize(p); q->mathmlize(p, os);
++it; ++it;
} else } else
res += p->mathmlize(); p->mathmlize(os);
} }
return res; os << "</mrow>";
} }

View File

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

View File

@ -67,16 +67,33 @@ namespace {
MathArray pipeThroughMaple(string const & extra, MathArray const & ar) MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
{ {
string header = string header = "readlib(latex):\n";
"readlib(latex):\n"
"`latex/csname_font` := ``:\n" // remove the \\it for variable names
"`latex/latex/*` := subs(`\\,`=`\\cdot `,eval(`latex/latex/*`)):\n";
//"#`latex/csname_font` := `\\it `:" //"#`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` " //"#`latex/latex/symbol` "
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): "; // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
string trailer = "quit;"; 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 for (int i = 0; i < 100; ++i) { // at most 100 attempts
// try to fix missing '*' the hard way by using mint // try to fix missing '*' the hard way by using mint
@ -119,7 +136,10 @@ namespace {
MathArray pipeThroughOctave(string const &, MathArray const & ar) MathArray pipeThroughOctave(string const &, MathArray const & ar)
{ {
string expr = ar.octavize(); ostringstream os;
OctaveStream vs(os);
vs << ar;
string expr = os.str();
string out; string out;
for (int i = 0; i < 100; ++i) { // at most 100 attempts 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 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 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_ == "|") if (left_ == "|" && right_ == "|")
return "det(" + cell(0).octavize() + ")"; os << "det(" << cell(0) << ")";
return left_ + cell(0).octavize() + right_; else
os << left_.c_str() << cell(0) << right_.c_str();
} }
string MathDelimInset::maplize() const void MathDelimInset::maplize(MapleStream & os) const
{ {
if (left_ == "|" && right_ == "|") { if (left_ == "|" && right_ == "|") {
if (cell(0).isMatrix()) if (cell(0).isMatrix())
return "linalg[det](" + cell(0).maplize() + ")"; os << "linalg[det](" << cell(0) << ")";
else 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; bool isMatrix() const;
/// ///
string octavize() const; void octavize(OctaveStream &) const;
/// ///
string maplize() const; void maplize(MapleStream &) const;
private: private:
/// ///
int dw() const; 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; void draw(Painter &, int x, int y) const;
/// ///
string octavize() const; void octavize(OctaveStream &) const;
private: 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; void draw(Painter &, int x, int y) const;
/// ///
string maplize() const; void maplize(MapleStream &) const;
///
void mathmlize(MathMLStream &) const;
public: public:
/// ///
const bool atop_; 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; os << '[';
res += '[';
for (row_type row = 0; row < nrows(); ++row) { for (row_type row = 0; row < nrows(); ++row) {
if (row) if (row)
res += ';'; os << ';';
res += '['; os << '[';
for (col_type col = 0; col < ncols(); ++col) { for (col_type col = 0; col < ncols(); ++col)
res += cell(index(row, col)).octavize(); os << cell(index(row, col)) << ' ';
res += ' '; os <<']';
}
res += ']';
} }
res += ']'; os <<']';
return res;
} }
string MathGridInset::maplize() const void MathGridInset::maplize(MapleStream & os) const
{ {
string res = "array(["; os << "array([";
for (row_type row = 0; row < nrows(); ++row) { for (row_type row = 0; row < nrows(); ++row) {
if (row) if (row)
res += ','; os << ',';
res += '['; os << '[';
for (col_type col = 0; col < ncols(); ++col) { for (col_type col = 0; col < ncols(); ++col) {
if (col) if (col)
res += ','; os << ',';
res += cell(index(row, col)).maplize(); os << cell(index(row, col));
} }
res += ']'; os << ']';
} }
res += "])"; os << "])";
return res;
} }

View File

@ -150,9 +150,9 @@ public:
void setDefaults(); void setDefaults();
/// ///
string octavize() const; void octavize(OctaveStream &) const;
/// ///
string maplize() const; void maplize(MapleStream &) const;
protected: protected:
/// returns proper 'end of line' code for LaTeX /// 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.os_);
writeNormal(os);
return os.str();
return string();
} }
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 "xarray.h"
#include "math_defs.h" #include "math_defs.h"
#include "LString.h" #include "math_mathmlstream.h"
/** Abstract base class for all math objects. /** Abstract base class for all math objects.
A math insets is for use of the math editor only, it isn't a 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 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 &); 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; bool idxDown(int & idx, int & pos) const;
/// ///
string octavize() const; void mathmlize(MathMLStream &) const;
///
void octavize(OctaveStream &) const;
}; };
#endif #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) if (nuc)
res += nuc->maplize(); os << nuc;
if (hasDown() && down().data_.size()) if (hasDown() && down().data_.size())
res += "[" + down().data_.maplize() + "]"; os << '[' << down().data_ << ']';
if (hasUp() && up().data_.size()) if (hasUp() && up().data_.size())
res += "^(" + up().data_.maplize() + ")"; os << "^(" << up().data_ << ')';
return res;
} }
string MathScriptInset::octavize(MathInset const * nuc) const void MathScriptInset::octavize(MathInset const * nuc, OctaveStream & os) const
{
return maplize(nuc);
}
string MathScriptInset::mathmlize(MathInset const * nuc) const
{ {
if (nuc) if (nuc)
return nuc->mathmlize(); os << nuc;
return string(); 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); 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: public:
/// returns x offset for main part /// 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; void metrics(MathMetricsInfo const & st) const;
/// ///
string maplize() const; void maplize(MapleStream &) const;
///
void mathmlize(MathMLStream &) const;
}; };
#endif #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(); if (code_ != LM_TC_VAR || str_.size() <= 1) {
} os << str_.c_str();
return;
}
string MathStringInset::maplize() const
{
if (code_ != LM_TC_VAR)
return str_;
if (str_.size() <= 1)
return str_;
string res;
// insert '*' between adjacent chars if type is LM_TC_VAR // insert '*' between adjacent chars if type is LM_TC_VAR
res += str_[0]; os << str_[0];
for (string::size_type i = 1; i < str_.size(); ++i) { for (string::size_type i = 1; i < str_.size(); ++i)
res += '*'; os << '*' << str_[i];
res += str_[i]; }
}
return res;
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; int width() const;
/// ///
string & str(); void octavize(OctaveStream &) const;
/// ///
string octavize() const; void maplize(MapleStream &) const;
/// ///
string maplize() const; void mathmlize(MathMLStream &) const;
private: private:
/// the string /// 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") if (sym_->name == "cdot")
return "*"; os << '*';
return sym_->name; 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") if (sym_->name == "cdot")
return "*"; os << '*';
return sym_->name; else
os << sym_->name.c_str();
} }
string MathSymbolInset::mathmlize() const
{
return sym_->name;
}

View File

@ -31,11 +31,11 @@ public:
bool takesLimits() const; 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: private:
/// ///
MathTextCodes code() const; MathTextCodes code() const;