3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathStream.h"
15 #include "MathFactory.h"
17 #include "MathExtern.h"
22 #include "support/debug.h"
23 #include "support/docstring.h"
24 #include "support/textutils.h"
35 //////////////////////////////////////////////////////////////////////
38 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
45 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
52 NormalStream & operator<<(NormalStream & ns, docstring const & s)
59 NormalStream & operator<<(NormalStream & ns, const string & s)
61 ns.os() << from_utf8(s);
66 NormalStream & operator<<(NormalStream & ns, char const * s)
73 NormalStream & operator<<(NormalStream & ns, char c)
80 NormalStream & operator<<(NormalStream & ns, int i)
88 /////////////////////////////////////////////////////////////////
91 TeXMathStream & operator<<(TeXMathStream & ws, docstring const & s)
93 // Skip leading '\n' if we had already output a newline char
95 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
97 // Check whether there's something to output
98 if (s.length() <= first)
101 if (ws.pendingBrace()) {
103 ws.pendingBrace(false);
104 ws.pendingSpace(false);
106 } else if (ws.pendingSpace()) {
107 if (isAlphaASCII(s[first]))
109 else if (s[first] == '[' && ws.useBraces())
111 else if (s[first] == ' ' && ws.textMode())
113 ws.pendingSpace(false);
115 ws.os() << s.substr(first);
117 char_type lastchar = 0;
118 docstring::const_iterator dit = s.begin() + first;
119 docstring::const_iterator end = s.end();
120 for (; dit != end; ++dit) {
122 if (lastchar == '\n')
126 ws.canBreakLine(lastchar != '\n');
131 TeXMathStream::TeXMathStream(otexrowstream & os, bool fragile, bool latex,
132 OutputType output, Encoding const * encoding)
133 : os_(os), fragile_(fragile), latex_(latex),
134 output_(output), encoding_(encoding)
138 TeXMathStream::~TeXMathStream()
142 else if (pendingspace_)
147 void TeXMathStream::addlines(unsigned int n)
153 void TeXMathStream::pendingSpace(bool space)
155 pendingspace_ = space;
161 void TeXMathStream::useBraces(bool braces)
167 void TeXMathStream::pendingBrace(bool brace)
169 pendingbrace_ = brace;
173 void TeXMathStream::textMode(bool textmode)
175 textmode_ = textmode;
179 void TeXMathStream::lockedMode(bool locked)
185 void TeXMathStream::asciiOnly(bool ascii)
191 Changer TeXMathStream::changeRowEntry(TexRow::RowEntry entry)
193 return changeVar(row_entry_, entry);
197 bool TeXMathStream::startOuterRow()
199 if (TexRow::isNone(row_entry_))
201 return texrow().start(row_entry_);
205 TeXMathStream & operator<<(TeXMathStream & ws, MathAtom const & at)
212 TeXMathStream & operator<<(TeXMathStream & ws, MathData const & ar)
219 TeXMathStream & operator<<(TeXMathStream & ws, char const * s)
226 TeXMathStream & operator<<(TeXMathStream & ws, char c)
228 if (c == '\n' && !ws.canBreakLine())
231 if (ws.pendingBrace()) {
233 ws.pendingBrace(false);
234 ws.pendingSpace(false);
236 } else if (ws.pendingSpace()) {
239 else if (c == '[' && ws.useBraces())
241 else if (c == ' ' && ws.textMode())
243 ws.pendingSpace(false);
248 ws.canBreakLine(c != '\n');
253 TeXMathStream & operator<<(TeXMathStream & ws, int i)
255 if (ws.pendingBrace()) {
257 ws.pendingBrace(false);
261 ws.canBreakLine(true);
266 TeXMathStream & operator<<(TeXMathStream & ws, unsigned int i)
268 if (ws.pendingBrace()) {
270 ws.pendingBrace(false);
274 ws.canBreakLine(true);
279 //////////////////////////////////////////////////////////////////////
282 MathMLStream::MathMLStream(odocstream & os, std::string const & xmlns, bool xmlMode)
283 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
287 void MathMLStream::cr()
290 for (int i = 0; i < tab(); ++i)
295 void MathMLStream::defer(docstring const & s)
301 void MathMLStream::defer(string const & s)
303 deferred_ << from_utf8(s);
307 docstring MathMLStream::deferred() const
309 return deferred_.str();
313 MathMLStream & operator<<(MathMLStream & ms, MathAtom const & at)
320 MathMLStream & operator<<(MathMLStream & ms, MathData const & ar)
327 MathMLStream & operator<<(MathMLStream & ms, char const * s)
334 MathMLStream & operator<<(MathMLStream & ms, char c)
341 MathMLStream & operator<<(MathMLStream & ms, char_type c)
348 MathMLStream & operator<<(MathMLStream & ms, MTag const & t)
352 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
353 if (!t.attr_.empty())
354 ms.os() << " " << from_ascii(t.attr_);
360 MathMLStream & operator<<(MathMLStream & ms, ETag const & t)
365 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
370 MathMLStream & operator<<(MathMLStream & ms, CTag const & t)
373 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
374 if (!t.attr_.empty())
375 ms.os() << " " << from_utf8(t.attr_);
381 MathMLStream & operator<<(MathMLStream & ms, docstring const & s)
388 //////////////////////////////////////////////////////////////////////
391 HtmlStream::HtmlStream(odocstream & os)
392 : os_(os), tab_(0), line_(0), in_text_(false)
396 void HtmlStream::defer(docstring const & s)
402 void HtmlStream::defer(string const & s)
404 deferred_ << from_utf8(s);
408 docstring HtmlStream::deferred() const
410 return deferred_.str();
414 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
421 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
428 HtmlStream & operator<<(HtmlStream & ms, char const * s)
435 HtmlStream & operator<<(HtmlStream & ms, char c)
442 HtmlStream & operator<<(HtmlStream & ms, char_type c)
449 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
451 ms.os() << '<' << from_ascii(t.tag_);
452 if (!t.attr_.empty())
453 ms.os() << " " << from_ascii(t.attr_);
459 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
461 ms.os() << "</" << from_ascii(t.tag_) << '>';
466 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
473 //////////////////////////////////////////////////////////////////////
476 SetMode::SetMode(MathMLStream & ms, bool text)
479 was_text_ = ms_.inText();
480 ms_.setTextMode(text);
486 ms_.setTextMode(was_text_);
490 //////////////////////////////////////////////////////////////////////
493 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
496 was_text_ = os_.inText();
497 os_.setTextMode(text);
501 SetHTMLMode::~SetHTMLMode()
503 os_.setTextMode(was_text_);
507 //////////////////////////////////////////////////////////////////////
510 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
517 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
524 MapleStream & operator<<(MapleStream & ms, char const * s)
531 MapleStream & operator<<(MapleStream & ms, char c)
538 MapleStream & operator<<(MapleStream & ms, int i)
545 MapleStream & operator<<(MapleStream & ms, char_type c)
552 MapleStream & operator<<(MapleStream & ms, docstring const & s)
559 //////////////////////////////////////////////////////////////////////
562 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
569 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
576 MaximaStream & operator<<(MaximaStream & ms, char const * s)
583 MaximaStream & operator<<(MaximaStream & ms, char c)
590 MaximaStream & operator<<(MaximaStream & ms, int i)
597 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
604 MaximaStream & operator<<(MaximaStream & ms, char_type c)
611 //////////////////////////////////////////////////////////////////////
614 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
621 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
628 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
635 MathematicaStream & operator<<(MathematicaStream & ms, char c)
642 MathematicaStream & operator<<(MathematicaStream & ms, int i)
649 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
656 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
663 //////////////////////////////////////////////////////////////////////
666 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
673 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
680 OctaveStream & operator<<(OctaveStream & ns, char const * s)
687 OctaveStream & operator<<(OctaveStream & ns, char c)
694 OctaveStream & operator<<(OctaveStream & ns, int i)
701 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
708 OctaveStream & operator<<(OctaveStream & ns, char_type c)
715 OctaveStream & operator<<(OctaveStream & os, string const & s)
717 os.os() << from_utf8(s);
722 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
724 // For the basic symbols, no difference between XML and HTML.
725 if (name.size() == 1) {
726 char_type const c = name[0];
728 return from_ascii("<");
730 return from_ascii(">");
733 } else if (name.size() == 2 && name[0] == '\\') {
734 char_type const c = name[1];
736 return from_ascii("{");
738 return from_ascii("}");
740 MathWordList const & words = mathedWordList();
741 MathWordList::const_iterator it = words.find(name);
742 if (it != words.end()) {
743 // Only difference between XML and HTML, based on the contents read by MathFactory.
744 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
747 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");