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"
16 #include "MathExtern.h"
18 #include "support/docstring.h"
19 #include "support/RefChanger.h"
20 #include "support/textutils.h"
31 //////////////////////////////////////////////////////////////////////
34 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
41 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
48 NormalStream & operator<<(NormalStream & ns, docstring const & s)
55 NormalStream & operator<<(NormalStream & ns, const string & s)
57 ns.os() << from_utf8(s);
62 NormalStream & operator<<(NormalStream & ns, char const * s)
69 NormalStream & operator<<(NormalStream & ns, char c)
76 NormalStream & operator<<(NormalStream & ns, int i)
84 /////////////////////////////////////////////////////////////////
87 WriteStream & operator<<(WriteStream & ws, docstring const & s)
89 // Skip leading '\n' if we had already output a newline char
91 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
93 // Check whether there's something to output
94 if (s.length() <= first)
97 if (ws.pendingBrace()) {
99 ws.pendingBrace(false);
100 ws.pendingSpace(false);
102 } else if (ws.pendingSpace()) {
103 if (isAlphaASCII(s[first]))
105 else if (s[first] == ' ' && ws.textMode())
107 ws.pendingSpace(false);
109 ws.os() << s.substr(first);
111 char_type lastchar = 0;
112 docstring::const_iterator dit = s.begin() + first;
113 docstring::const_iterator end = s.end();
114 for (; dit != end; ++dit) {
116 if (lastchar == '\n')
120 ws.canBreakLine(lastchar != '\n');
125 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
126 OutputType output, Encoding const * encoding)
132 encoding_ = encoding;
136 WriteStream::WriteStream(otexrowstream & os)
137 : os_(os), fragile_(false), firstitem_(false), latex_(false),
138 output_(wsDefault), pendingspace_(false), pendingbrace_(false),
139 textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
140 line_(0), encoding_(0), row_entry_(TexRow::row_none)
144 WriteStream::~WriteStream()
148 else if (pendingspace_)
153 void WriteStream::addlines(unsigned int n)
159 void WriteStream::pendingSpace(bool how)
165 void WriteStream::pendingBrace(bool brace)
167 pendingbrace_ = brace;
171 void WriteStream::textMode(bool textmode)
173 textmode_ = textmode;
177 void WriteStream::lockedMode(bool locked)
183 void WriteStream::asciiOnly(bool ascii)
189 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
191 return make_change(row_entry_, entry);
195 bool WriteStream::startOuterRow()
197 if (TexRow::isNone(row_entry_))
199 return texrow().start(row_entry_);
203 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
210 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
217 WriteStream & operator<<(WriteStream & ws, char const * s)
224 WriteStream & operator<<(WriteStream & ws, char c)
226 if (c == '\n' && !ws.canBreakLine())
229 if (ws.pendingBrace()) {
231 ws.pendingBrace(false);
232 ws.pendingSpace(false);
234 } else if (ws.pendingSpace()) {
237 else if (c == ' ' && ws.textMode())
239 ws.pendingSpace(false);
244 ws.canBreakLine(c != '\n');
249 WriteStream & operator<<(WriteStream & ws, int i)
251 if (ws.pendingBrace()) {
253 ws.pendingBrace(false);
257 ws.canBreakLine(true);
262 WriteStream & operator<<(WriteStream & ws, unsigned int i)
264 if (ws.pendingBrace()) {
266 ws.pendingBrace(false);
270 ws.canBreakLine(true);
275 //////////////////////////////////////////////////////////////////////
278 MathStream::MathStream(odocstream & os)
279 : os_(os), tab_(0), line_(0), in_text_(false)
283 void MathStream::cr()
286 for (int i = 0; i < tab(); ++i)
291 void MathStream::defer(docstring const & s)
297 void MathStream::defer(string const & s)
299 deferred_ << from_utf8(s);
303 docstring MathStream::deferred() const
305 return deferred_.str();
309 MathStream & operator<<(MathStream & ms, MathAtom const & at)
316 MathStream & operator<<(MathStream & ms, MathData const & ar)
323 MathStream & operator<<(MathStream & ms, char const * s)
330 MathStream & operator<<(MathStream & ms, char c)
337 MathStream & operator<<(MathStream & ms, char_type c)
344 MathStream & operator<<(MathStream & ms, MTag const & t)
348 ms.os() << '<' << from_ascii(t.tag_);
349 if (!t.attr_.empty())
350 ms.os() << " " << from_ascii(t.attr_);
356 MathStream & operator<<(MathStream & ms, ETag const & t)
361 ms.os() << "</" << from_ascii(t.tag_) << '>';
366 MathStream & operator<<(MathStream & ms, docstring const & s)
373 //////////////////////////////////////////////////////////////////////
376 HtmlStream::HtmlStream(odocstream & os)
377 : os_(os), tab_(0), line_(0), in_text_(false)
381 void HtmlStream::defer(docstring const & s)
387 void HtmlStream::defer(string const & s)
389 deferred_ << from_utf8(s);
393 docstring HtmlStream::deferred() const
395 return deferred_.str();
399 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
406 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
413 HtmlStream & operator<<(HtmlStream & ms, char const * s)
420 HtmlStream & operator<<(HtmlStream & ms, char c)
427 HtmlStream & operator<<(HtmlStream & ms, char_type c)
434 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
436 ms.os() << '<' << from_ascii(t.tag_);
437 if (!t.attr_.empty())
438 ms.os() << " " << from_ascii(t.attr_);
444 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
446 ms.os() << "</" << from_ascii(t.tag_) << '>';
451 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
458 //////////////////////////////////////////////////////////////////////
461 SetMode::SetMode(MathStream & os, bool text)
464 was_text_ = os_.inText();
465 os_.setTextMode(text);
471 os_.setTextMode(was_text_);
475 //////////////////////////////////////////////////////////////////////
478 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
481 was_text_ = os_.inText();
482 os_.setTextMode(text);
486 SetHTMLMode::~SetHTMLMode()
488 os_.setTextMode(was_text_);
492 //////////////////////////////////////////////////////////////////////
495 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
502 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
509 MapleStream & operator<<(MapleStream & ms, char const * s)
516 MapleStream & operator<<(MapleStream & ms, char c)
523 MapleStream & operator<<(MapleStream & ms, int i)
530 MapleStream & operator<<(MapleStream & ms, char_type c)
537 MapleStream & operator<<(MapleStream & ms, docstring const & s)
544 //////////////////////////////////////////////////////////////////////
547 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
554 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
561 MaximaStream & operator<<(MaximaStream & ms, char const * s)
568 MaximaStream & operator<<(MaximaStream & ms, char c)
575 MaximaStream & operator<<(MaximaStream & ms, int i)
582 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
589 MaximaStream & operator<<(MaximaStream & ms, char_type c)
596 //////////////////////////////////////////////////////////////////////
599 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
606 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
613 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
620 MathematicaStream & operator<<(MathematicaStream & ms, char c)
627 MathematicaStream & operator<<(MathematicaStream & ms, int i)
634 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
641 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
648 //////////////////////////////////////////////////////////////////////
651 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
658 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
665 OctaveStream & operator<<(OctaveStream & ns, char const * s)
672 OctaveStream & operator<<(OctaveStream & ns, char c)
679 OctaveStream & operator<<(OctaveStream & ns, int i)
686 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
693 OctaveStream & operator<<(OctaveStream & ns, char_type c)
700 OctaveStream & operator<<(OctaveStream & os, string const & s)
702 os.os() << from_utf8(s);