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"
21 #include "support/docstring.h"
22 #include "support/textutils.h"
33 //////////////////////////////////////////////////////////////////////
36 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
43 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
50 NormalStream & operator<<(NormalStream & ns, docstring const & s)
57 NormalStream & operator<<(NormalStream & ns, const string & s)
59 ns.os() << from_utf8(s);
64 NormalStream & operator<<(NormalStream & ns, char const * s)
71 NormalStream & operator<<(NormalStream & ns, char c)
78 NormalStream & operator<<(NormalStream & ns, int i)
86 /////////////////////////////////////////////////////////////////
89 WriteStream & operator<<(WriteStream & ws, docstring const & s)
91 // Skip leading '\n' if we had already output a newline char
93 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
95 // Check whether there's something to output
96 if (s.length() <= first)
99 if (ws.pendingBrace()) {
101 ws.pendingBrace(false);
102 ws.pendingSpace(false);
104 } else if (ws.pendingSpace()) {
105 if (isAlphaASCII(s[first]))
107 else if (s[first] == ' ' && ws.textMode())
109 ws.pendingSpace(false);
111 ws.os() << s.substr(first);
113 char_type lastchar = 0;
114 docstring::const_iterator dit = s.begin() + first;
115 docstring::const_iterator end = s.end();
116 for (; dit != end; ++dit) {
118 if (lastchar == '\n')
122 ws.canBreakLine(lastchar != '\n');
127 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
128 OutputType output, Encoding const * encoding)
129 : os_(os), fragile_(fragile), latex_(latex),
130 output_(output), encoding_(encoding)
134 WriteStream::~WriteStream()
138 else if (pendingspace_)
143 void WriteStream::addlines(unsigned int n)
149 void WriteStream::pendingSpace(bool how)
155 void WriteStream::pendingBrace(bool brace)
157 pendingbrace_ = brace;
161 void WriteStream::textMode(bool textmode)
163 textmode_ = textmode;
167 void WriteStream::lockedMode(bool locked)
173 void WriteStream::asciiOnly(bool ascii)
179 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
181 return changeVar(row_entry_, entry);
185 bool WriteStream::startOuterRow()
187 if (TexRow::isNone(row_entry_))
189 return texrow().start(row_entry_);
193 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
200 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
207 WriteStream & operator<<(WriteStream & ws, char const * s)
214 WriteStream & operator<<(WriteStream & ws, char c)
216 if (c == '\n' && !ws.canBreakLine())
219 if (ws.pendingBrace()) {
221 ws.pendingBrace(false);
222 ws.pendingSpace(false);
224 } else if (ws.pendingSpace()) {
227 else if (c == ' ' && ws.textMode())
229 ws.pendingSpace(false);
234 ws.canBreakLine(c != '\n');
239 WriteStream & operator<<(WriteStream & ws, int i)
241 if (ws.pendingBrace()) {
243 ws.pendingBrace(false);
247 ws.canBreakLine(true);
252 WriteStream & operator<<(WriteStream & ws, unsigned int i)
254 if (ws.pendingBrace()) {
256 ws.pendingBrace(false);
260 ws.canBreakLine(true);
265 //////////////////////////////////////////////////////////////////////
268 MathStream::MathStream(odocstream & os, std::string const & xmlns, bool xmlMode)
269 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
273 void MathStream::cr()
276 for (int i = 0; i < tab(); ++i)
281 void MathStream::defer(docstring const & s)
287 void MathStream::defer(string const & s)
289 deferred_ << from_utf8(s);
293 docstring MathStream::deferred() const
295 return deferred_.str();
299 MathStream & operator<<(MathStream & ms, MathAtom const & at)
306 MathStream & operator<<(MathStream & ms, MathData const & ar)
313 MathStream & operator<<(MathStream & ms, char const * s)
320 MathStream & operator<<(MathStream & ms, char c)
327 MathStream & operator<<(MathStream & ms, char_type c)
334 MathStream & operator<<(MathStream & ms, MTag const & t)
338 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
339 if (!t.attr_.empty())
340 ms.os() << " " << from_ascii(t.attr_);
346 MathStream & operator<<(MathStream & ms, ETag const & t)
351 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
356 MathStream & operator<<(MathStream & ms, CTag const & t)
359 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
360 if (!t.attr_.empty())
361 ms.os() << " " << from_utf8(t.attr_);
367 MathStream & operator<<(MathStream & ms, docstring const & s)
374 //////////////////////////////////////////////////////////////////////
377 HtmlStream::HtmlStream(odocstream & os)
378 : os_(os), tab_(0), line_(0), in_text_(false)
382 void HtmlStream::defer(docstring const & s)
388 void HtmlStream::defer(string const & s)
390 deferred_ << from_utf8(s);
394 docstring HtmlStream::deferred() const
396 return deferred_.str();
400 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
407 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
414 HtmlStream & operator<<(HtmlStream & ms, char const * s)
421 HtmlStream & operator<<(HtmlStream & ms, char c)
428 HtmlStream & operator<<(HtmlStream & ms, char_type c)
435 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
437 ms.os() << '<' << from_ascii(t.tag_);
438 if (!t.attr_.empty())
439 ms.os() << " " << from_ascii(t.attr_);
445 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
447 ms.os() << "</" << from_ascii(t.tag_) << '>';
452 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
459 //////////////////////////////////////////////////////////////////////
462 SetMode::SetMode(MathStream & ms, bool text)
465 was_text_ = ms_.inText();
466 ms_.setTextMode(text);
472 ms_.setTextMode(was_text_);
476 //////////////////////////////////////////////////////////////////////
479 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
482 was_text_ = os_.inText();
483 os_.setTextMode(text);
487 SetHTMLMode::~SetHTMLMode()
489 os_.setTextMode(was_text_);
493 //////////////////////////////////////////////////////////////////////
496 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
503 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
510 MapleStream & operator<<(MapleStream & ms, char const * s)
517 MapleStream & operator<<(MapleStream & ms, char c)
524 MapleStream & operator<<(MapleStream & ms, int i)
531 MapleStream & operator<<(MapleStream & ms, char_type c)
538 MapleStream & operator<<(MapleStream & ms, docstring const & s)
545 //////////////////////////////////////////////////////////////////////
548 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
555 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
562 MaximaStream & operator<<(MaximaStream & ms, char const * s)
569 MaximaStream & operator<<(MaximaStream & ms, char c)
576 MaximaStream & operator<<(MaximaStream & ms, int i)
583 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
590 MaximaStream & operator<<(MaximaStream & ms, char_type c)
597 //////////////////////////////////////////////////////////////////////
600 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
607 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
614 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
621 MathematicaStream & operator<<(MathematicaStream & ms, char c)
628 MathematicaStream & operator<<(MathematicaStream & ms, int i)
635 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
642 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
649 //////////////////////////////////////////////////////////////////////
652 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
659 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
666 OctaveStream & operator<<(OctaveStream & ns, char const * s)
673 OctaveStream & operator<<(OctaveStream & ns, char c)
680 OctaveStream & operator<<(OctaveStream & ns, int i)
687 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
694 OctaveStream & operator<<(OctaveStream & ns, char_type c)
701 OctaveStream & operator<<(OctaveStream & os, string const & s)
703 os.os() << from_utf8(s);
708 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
710 // For the basic symbols, no difference between XML and HTML.
711 if (name.size() == 1) {
712 char_type const c = name[0];
714 return from_ascii("<");
716 return from_ascii(">");
719 } else if (name.size() == 2 && name[0] == '\\') {
720 char_type const c = name[1];
722 return from_ascii("{");
724 return from_ascii("}");
726 MathWordList const & words = mathedWordList();
727 MathWordList::const_iterator it = words.find(name);
728 if (it != words.end()) {
729 // Only difference between XML and HTML, based on the contents read by MathFactory.
730 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
733 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");