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), firstitem_(false), latex_(latex),
130 output_(output), pendingspace_(false), pendingbrace_(false),
131 textmode_(false), locked_(false), ascii_(false), canbreakline_(true),
132 mathsout_(false), ulemcmd_(NONE), line_(0), encoding_(encoding),
133 row_entry_(TexRow::row_none), mathclass_(false)
137 WriteStream::~WriteStream()
141 else if (pendingspace_)
146 void WriteStream::addlines(unsigned int n)
152 void WriteStream::pendingSpace(bool how)
158 void WriteStream::pendingBrace(bool brace)
160 pendingbrace_ = brace;
164 void WriteStream::textMode(bool textmode)
166 textmode_ = textmode;
170 void WriteStream::lockedMode(bool locked)
176 void WriteStream::asciiOnly(bool ascii)
182 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
184 return changeVar(row_entry_, entry);
188 bool WriteStream::startOuterRow()
190 if (TexRow::isNone(row_entry_))
192 return texrow().start(row_entry_);
196 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
203 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
210 WriteStream & operator<<(WriteStream & ws, char const * s)
217 WriteStream & operator<<(WriteStream & ws, char c)
219 if (c == '\n' && !ws.canBreakLine())
222 if (ws.pendingBrace()) {
224 ws.pendingBrace(false);
225 ws.pendingSpace(false);
227 } else if (ws.pendingSpace()) {
230 else if (c == ' ' && ws.textMode())
232 ws.pendingSpace(false);
237 ws.canBreakLine(c != '\n');
242 WriteStream & operator<<(WriteStream & ws, int i)
244 if (ws.pendingBrace()) {
246 ws.pendingBrace(false);
250 ws.canBreakLine(true);
255 WriteStream & operator<<(WriteStream & ws, unsigned int i)
257 if (ws.pendingBrace()) {
259 ws.pendingBrace(false);
263 ws.canBreakLine(true);
268 //////////////////////////////////////////////////////////////////////
271 MathStream::MathStream(odocstream & os, std::string const & xmlns, bool xmlMode)
272 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
276 void MathStream::cr()
279 for (int i = 0; i < tab(); ++i)
284 void MathStream::defer(docstring const & s)
290 void MathStream::defer(string const & s)
292 deferred_ << from_utf8(s);
296 docstring MathStream::deferred() const
298 return deferred_.str();
302 MathStream & operator<<(MathStream & ms, MathAtom const & at)
309 MathStream & operator<<(MathStream & ms, MathData const & ar)
316 MathStream & operator<<(MathStream & ms, char const * s)
323 MathStream & operator<<(MathStream & ms, char c)
330 MathStream & operator<<(MathStream & ms, char_type c)
337 MathStream & operator<<(MathStream & ms, MTag const & t)
341 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
342 if (!t.attr_.empty())
343 ms.os() << " " << from_ascii(t.attr_);
349 MathStream & operator<<(MathStream & ms, ETag const & t)
354 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
359 MathStream & operator<<(MathStream & ms, CTag const & t)
362 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
363 if (!t.attr_.empty())
364 ms.os() << " " << from_utf8(t.attr_);
370 MathStream & operator<<(MathStream & ms, docstring const & s)
377 //////////////////////////////////////////////////////////////////////
380 HtmlStream::HtmlStream(odocstream & os)
381 : os_(os), tab_(0), line_(0), in_text_(false)
385 void HtmlStream::defer(docstring const & s)
391 void HtmlStream::defer(string const & s)
393 deferred_ << from_utf8(s);
397 docstring HtmlStream::deferred() const
399 return deferred_.str();
403 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
410 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
417 HtmlStream & operator<<(HtmlStream & ms, char const * s)
424 HtmlStream & operator<<(HtmlStream & ms, char c)
431 HtmlStream & operator<<(HtmlStream & ms, char_type c)
438 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
440 ms.os() << '<' << from_ascii(t.tag_);
441 if (!t.attr_.empty())
442 ms.os() << " " << from_ascii(t.attr_);
448 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
450 ms.os() << "</" << from_ascii(t.tag_) << '>';
455 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
462 //////////////////////////////////////////////////////////////////////
465 SetMode::SetMode(MathStream & ms, bool text)
468 was_text_ = ms_.inText();
469 ms_.setTextMode(text);
475 ms_.setTextMode(was_text_);
479 //////////////////////////////////////////////////////////////////////
482 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
485 was_text_ = os_.inText();
486 os_.setTextMode(text);
490 SetHTMLMode::~SetHTMLMode()
492 os_.setTextMode(was_text_);
496 //////////////////////////////////////////////////////////////////////
499 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
506 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
513 MapleStream & operator<<(MapleStream & ms, char const * s)
520 MapleStream & operator<<(MapleStream & ms, char c)
527 MapleStream & operator<<(MapleStream & ms, int i)
534 MapleStream & operator<<(MapleStream & ms, char_type c)
541 MapleStream & operator<<(MapleStream & ms, docstring const & s)
548 //////////////////////////////////////////////////////////////////////
551 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
558 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
565 MaximaStream & operator<<(MaximaStream & ms, char const * s)
572 MaximaStream & operator<<(MaximaStream & ms, char c)
579 MaximaStream & operator<<(MaximaStream & ms, int i)
586 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
593 MaximaStream & operator<<(MaximaStream & ms, char_type c)
600 //////////////////////////////////////////////////////////////////////
603 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
610 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
617 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
624 MathematicaStream & operator<<(MathematicaStream & ms, char c)
631 MathematicaStream & operator<<(MathematicaStream & ms, int i)
638 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
645 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
652 //////////////////////////////////////////////////////////////////////
655 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
662 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
669 OctaveStream & operator<<(OctaveStream & ns, char const * s)
676 OctaveStream & operator<<(OctaveStream & ns, char c)
683 OctaveStream & operator<<(OctaveStream & ns, int i)
690 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
697 OctaveStream & operator<<(OctaveStream & ns, char_type c)
704 OctaveStream & operator<<(OctaveStream & os, string const & s)
706 os.os() << from_utf8(s);
711 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
713 // For the basic symbols, no difference between XML and HTML.
714 if (name.size() == 1) {
715 char_type const c = name[0];
717 return from_ascii("<");
719 return from_ascii(">");
722 } else if (name.size() == 2 && name[0] == '\\') {
723 char_type const c = name[1];
725 return from_ascii("{");
727 return from_ascii("}");
729 MathWordList const & words = mathedWordList();
730 MathWordList::const_iterator it = words.find(name);
731 if (it != words.end()) {
732 // Only difference between XML and HTML, based on the contents read by MathFactory.
733 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
736 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");