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/docstring.h"
20 #include "support/RefChanger.h"
21 #include "support/textutils.h"
32 //////////////////////////////////////////////////////////////////////
35 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
42 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
49 NormalStream & operator<<(NormalStream & ns, docstring const & s)
56 NormalStream & operator<<(NormalStream & ns, const string & s)
58 ns.os() << from_utf8(s);
63 NormalStream & operator<<(NormalStream & ns, char const * s)
70 NormalStream & operator<<(NormalStream & ns, char c)
77 NormalStream & operator<<(NormalStream & ns, int i)
85 /////////////////////////////////////////////////////////////////
88 WriteStream & operator<<(WriteStream & ws, docstring const & s)
90 // Skip leading '\n' if we had already output a newline char
92 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
94 // Check whether there's something to output
95 if (s.length() <= first)
98 if (ws.pendingBrace()) {
100 ws.pendingBrace(false);
101 ws.pendingSpace(false);
103 } else if (ws.pendingSpace()) {
104 if (isAlphaASCII(s[first]))
106 else if (s[first] == ' ' && ws.textMode())
108 ws.pendingSpace(false);
110 ws.os() << s.substr(first);
112 char_type lastchar = 0;
113 docstring::const_iterator dit = s.begin() + first;
114 docstring::const_iterator end = s.end();
115 for (; dit != end; ++dit) {
117 if (lastchar == '\n')
121 ws.canBreakLine(lastchar != '\n');
126 WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
127 OutputType output, Encoding const * encoding)
128 : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
129 output_(output), pendingspace_(false), pendingbrace_(false),
130 textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
131 line_(0), encoding_(encoding), row_entry_(TexRow::row_none)
135 WriteStream::~WriteStream()
139 else if (pendingspace_)
144 void WriteStream::addlines(unsigned int n)
150 void WriteStream::pendingSpace(bool how)
156 void WriteStream::pendingBrace(bool brace)
158 pendingbrace_ = brace;
162 void WriteStream::textMode(bool textmode)
164 textmode_ = textmode;
168 void WriteStream::lockedMode(bool locked)
174 void WriteStream::asciiOnly(bool ascii)
180 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
182 return make_change(row_entry_, entry);
186 bool WriteStream::startOuterRow()
188 if (TexRow::isNone(row_entry_))
190 return texrow().start(row_entry_);
194 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
201 WriteStream & operator<<(WriteStream & ws, MathData const & ar)
208 WriteStream & operator<<(WriteStream & ws, char const * s)
215 WriteStream & operator<<(WriteStream & ws, char c)
217 if (c == '\n' && !ws.canBreakLine())
220 if (ws.pendingBrace()) {
222 ws.pendingBrace(false);
223 ws.pendingSpace(false);
225 } else if (ws.pendingSpace()) {
228 else if (c == ' ' && ws.textMode())
230 ws.pendingSpace(false);
235 ws.canBreakLine(c != '\n');
240 WriteStream & operator<<(WriteStream & ws, int i)
242 if (ws.pendingBrace()) {
244 ws.pendingBrace(false);
248 ws.canBreakLine(true);
253 WriteStream & operator<<(WriteStream & ws, unsigned int i)
255 if (ws.pendingBrace()) {
257 ws.pendingBrace(false);
261 ws.canBreakLine(true);
266 //////////////////////////////////////////////////////////////////////
269 MathStream::MathStream(odocstream & os)
270 : os_(os), tab_(0), line_(0), in_text_(false)
274 void MathStream::cr()
277 for (int i = 0; i < tab(); ++i)
282 void MathStream::defer(docstring const & s)
288 void MathStream::defer(string const & s)
290 deferred_ << from_utf8(s);
294 docstring MathStream::deferred() const
296 return deferred_.str();
300 MathStream & operator<<(MathStream & ms, MathAtom const & at)
307 MathStream & operator<<(MathStream & ms, MathData const & ar)
314 MathStream & operator<<(MathStream & ms, char const * s)
321 MathStream & operator<<(MathStream & ms, char c)
328 MathStream & operator<<(MathStream & ms, char_type c)
335 MathStream & operator<<(MathStream & ms, MTag const & t)
339 ms.os() << '<' << from_ascii(t.tag_);
340 if (!t.attr_.empty())
341 ms.os() << " " << from_ascii(t.attr_);
347 MathStream & operator<<(MathStream & ms, ETag const & t)
352 ms.os() << "</" << from_ascii(t.tag_) << '>';
357 MathStream & operator<<(MathStream & ms, docstring const & s)
364 //////////////////////////////////////////////////////////////////////
367 HtmlStream::HtmlStream(odocstream & os)
368 : os_(os), tab_(0), line_(0), in_text_(false)
372 void HtmlStream::defer(docstring const & s)
378 void HtmlStream::defer(string const & s)
380 deferred_ << from_utf8(s);
384 docstring HtmlStream::deferred() const
386 return deferred_.str();
390 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
397 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
404 HtmlStream & operator<<(HtmlStream & ms, char const * s)
411 HtmlStream & operator<<(HtmlStream & ms, char c)
418 HtmlStream & operator<<(HtmlStream & ms, char_type c)
425 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
427 ms.os() << '<' << from_ascii(t.tag_);
428 if (!t.attr_.empty())
429 ms.os() << " " << from_ascii(t.attr_);
435 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
437 ms.os() << "</" << from_ascii(t.tag_) << '>';
442 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
449 //////////////////////////////////////////////////////////////////////
452 SetMode::SetMode(MathStream & os, bool text)
455 was_text_ = os_.inText();
456 os_.setTextMode(text);
462 os_.setTextMode(was_text_);
466 //////////////////////////////////////////////////////////////////////
469 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
472 was_text_ = os_.inText();
473 os_.setTextMode(text);
477 SetHTMLMode::~SetHTMLMode()
479 os_.setTextMode(was_text_);
483 //////////////////////////////////////////////////////////////////////
486 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
493 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
500 MapleStream & operator<<(MapleStream & ms, char const * s)
507 MapleStream & operator<<(MapleStream & ms, char c)
514 MapleStream & operator<<(MapleStream & ms, int i)
521 MapleStream & operator<<(MapleStream & ms, char_type c)
528 MapleStream & operator<<(MapleStream & ms, docstring const & s)
535 //////////////////////////////////////////////////////////////////////
538 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
545 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
552 MaximaStream & operator<<(MaximaStream & ms, char const * s)
559 MaximaStream & operator<<(MaximaStream & ms, char c)
566 MaximaStream & operator<<(MaximaStream & ms, int i)
573 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
580 MaximaStream & operator<<(MaximaStream & ms, char_type c)
587 //////////////////////////////////////////////////////////////////////
590 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
597 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
604 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
611 MathematicaStream & operator<<(MathematicaStream & ms, char c)
618 MathematicaStream & operator<<(MathematicaStream & ms, int i)
625 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
632 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
639 //////////////////////////////////////////////////////////////////////
642 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
649 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
656 OctaveStream & operator<<(OctaveStream & ns, char const * s)
663 OctaveStream & operator<<(OctaveStream & ns, char c)
670 OctaveStream & operator<<(OctaveStream & ns, int i)
677 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
684 OctaveStream & operator<<(OctaveStream & ns, char_type c)
691 OctaveStream & operator<<(OctaveStream & os, string const & s)
693 os.os() << from_utf8(s);
698 docstring convertDelimToXMLEscape(docstring const & name)
700 if (name.size() == 1) {
701 char_type const c = name[0];
703 return from_ascii("<");
705 return from_ascii(">");
709 MathWordList const & words = mathedWordList();
710 MathWordList::const_iterator it = words.find(name);
711 if (it != words.end()) {
712 docstring const escape = it->second.xmlname;
715 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");