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"
19 #include "support/textutils.h"
20 #include "support/docstring.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)
127 : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
128 output_(output), pendingspace_(false), pendingbrace_(false),
129 textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
130 line_(0), encoding_(encoding)
134 WriteStream::WriteStream(otexrowstream & os)
135 : os_(os), fragile_(false), firstitem_(false), latex_(false),
136 output_(wsDefault), pendingspace_(false), pendingbrace_(false),
137 textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
138 line_(0), encoding_(0)
142 WriteStream::~WriteStream()
146 else if (pendingspace_)
151 void WriteStream::addlines(unsigned int n)
157 void WriteStream::pendingSpace(bool how)
163 void WriteStream::pendingBrace(bool brace)
165 pendingbrace_ = brace;
169 void WriteStream::textMode(bool textmode)
171 textmode_ = textmode;
175 void WriteStream::lockedMode(bool locked)
181 void WriteStream::asciiOnly(bool ascii)
187 void WriteStream::pushRowEntry(TexRow::RowEntry entry)
189 outer_row_entries_.push_back(entry);
193 void WriteStream::popRowEntry()
195 if (!outer_row_entries_.empty())
196 outer_row_entries_.pop_back();
200 bool WriteStream::startOuterRow()
202 size_t n = outer_row_entries_.size();
204 return texrow().start(outer_row_entries_[n - 1]);
210 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
217 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
224 WriteStream & operator<<(WriteStream & ws, char const * s)
231 WriteStream & operator<<(WriteStream & ws, char c)
233 if (c == '\n' && !ws.canBreakLine())
236 if (ws.pendingBrace()) {
238 ws.pendingBrace(false);
239 ws.pendingSpace(false);
241 } else if (ws.pendingSpace()) {
244 else if (c == ' ' && ws.textMode())
246 ws.pendingSpace(false);
251 ws.canBreakLine(c != '\n');
256 WriteStream & operator<<(WriteStream & ws, int i)
258 if (ws.pendingBrace()) {
260 ws.pendingBrace(false);
264 ws.canBreakLine(true);
269 WriteStream & operator<<(WriteStream & ws, unsigned int i)
271 if (ws.pendingBrace()) {
273 ws.pendingBrace(false);
277 ws.canBreakLine(true);
282 //////////////////////////////////////////////////////////////////////
285 MathStream::MathStream(odocstream & os)
286 : os_(os), tab_(0), line_(0), in_text_(false)
290 void MathStream::cr()
293 for (int i = 0; i < tab(); ++i)
298 void MathStream::defer(docstring const & s)
304 void MathStream::defer(string const & s)
306 deferred_ << from_utf8(s);
310 docstring MathStream::deferred() const
312 return deferred_.str();
316 MathStream & operator<<(MathStream & ms, MathAtom const & at)
323 MathStream & operator<<(MathStream & ms, MathData const & ar)
330 MathStream & operator<<(MathStream & ms, char const * s)
337 MathStream & operator<<(MathStream & ms, char c)
344 MathStream & operator<<(MathStream & ms, char_type c)
351 MathStream & operator<<(MathStream & ms, MTag const & t)
355 ms.os() << '<' << from_ascii(t.tag_);
356 if (!t.attr_.empty())
357 ms.os() << " " << from_ascii(t.attr_);
363 MathStream & operator<<(MathStream & ms, ETag const & t)
368 ms.os() << "</" << from_ascii(t.tag_) << '>';
373 MathStream & operator<<(MathStream & ms, docstring const & s)
380 //////////////////////////////////////////////////////////////////////
383 HtmlStream::HtmlStream(odocstream & os)
384 : os_(os), tab_(0), line_(0), in_text_(false)
388 void HtmlStream::defer(docstring const & s)
394 void HtmlStream::defer(string const & s)
396 deferred_ << from_utf8(s);
400 docstring HtmlStream::deferred() const
402 return deferred_.str();
406 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
413 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
420 HtmlStream & operator<<(HtmlStream & ms, char const * s)
427 HtmlStream & operator<<(HtmlStream & ms, char c)
434 HtmlStream & operator<<(HtmlStream & ms, char_type c)
441 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
443 ms.os() << '<' << from_ascii(t.tag_);
444 if (!t.attr_.empty())
445 ms.os() << " " << from_ascii(t.attr_);
451 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
453 ms.os() << "</" << from_ascii(t.tag_) << '>';
458 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
465 //////////////////////////////////////////////////////////////////////
468 SetMode::SetMode(MathStream & os, bool text)
471 was_text_ = os_.inText();
472 os_.setTextMode(text);
478 os_.setTextMode(was_text_);
482 //////////////////////////////////////////////////////////////////////
485 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
488 was_text_ = os_.inText();
489 os_.setTextMode(text);
493 SetHTMLMode::~SetHTMLMode()
495 os_.setTextMode(was_text_);
499 //////////////////////////////////////////////////////////////////////
502 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
509 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
516 MapleStream & operator<<(MapleStream & ms, char const * s)
523 MapleStream & operator<<(MapleStream & ms, char c)
530 MapleStream & operator<<(MapleStream & ms, int i)
537 MapleStream & operator<<(MapleStream & ms, char_type c)
544 MapleStream & operator<<(MapleStream & ms, docstring const & s)
551 //////////////////////////////////////////////////////////////////////
554 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
561 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
568 MaximaStream & operator<<(MaximaStream & ms, char const * s)
575 MaximaStream & operator<<(MaximaStream & ms, char c)
582 MaximaStream & operator<<(MaximaStream & ms, int i)
589 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
596 MaximaStream & operator<<(MaximaStream & ms, char_type c)
603 //////////////////////////////////////////////////////////////////////
606 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
613 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
620 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
627 MathematicaStream & operator<<(MathematicaStream & ms, char c)
634 MathematicaStream & operator<<(MathematicaStream & ms, int i)
641 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
648 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
655 //////////////////////////////////////////////////////////////////////
658 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
665 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
672 OctaveStream & operator<<(OctaveStream & ns, char const * s)
679 OctaveStream & operator<<(OctaveStream & ns, char c)
686 OctaveStream & operator<<(OctaveStream & ns, int i)
693 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
700 OctaveStream & operator<<(OctaveStream & ns, char_type c)
707 OctaveStream & operator<<(OctaveStream & os, string const & s)
709 os.os() << from_utf8(s);
714 docstring convertDelimToXMLEscape(docstring const & name)
716 if (name.size() == 1) {
717 char_type const c = name[0];
719 return from_ascii("<");
721 return from_ascii(">");
725 MathWordList const & words = mathedWordList();
726 MathWordList::const_iterator it = words.find(name);
727 if (it != words.end()) {
728 docstring const escape = it->second.xmlname;
731 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");