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"
22 #include "support/debug.h"
23 #include "support/docstring.h"
24 #include "support/textutils.h"
35 //////////////////////////////////////////////////////////////////////
38 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
45 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
52 NormalStream & operator<<(NormalStream & ns, docstring const & s)
59 NormalStream & operator<<(NormalStream & ns, const string & s)
61 ns.os() << from_utf8(s);
66 NormalStream & operator<<(NormalStream & ns, char const * s)
73 NormalStream & operator<<(NormalStream & ns, char c)
80 NormalStream & operator<<(NormalStream & ns, int i)
88 /////////////////////////////////////////////////////////////////
91 TeXMathStream & operator<<(TeXMathStream & ws, docstring const & s)
93 // Skip leading '\n' if we had already output a newline char
95 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
97 // Check whether there's something to output
98 if (s.length() <= first)
101 if (ws.pendingBrace()) {
103 ws.pendingBrace(false);
104 ws.pendingSpace(false);
106 } else if (ws.pendingSpace()) {
107 if (isAlphaASCII(s[first]))
109 else if (s[first] == '[' && ws.useBraces())
111 else if (s[first] == ' ' && ws.textMode())
113 ws.pendingSpace(false);
114 } else if (ws.useBraces()) {
115 if (s[first] == '\'')
119 ws.os() << s.substr(first);
121 char_type lastchar = 0;
122 docstring::const_iterator dit = s.begin() + first;
123 docstring::const_iterator end = s.end();
124 for (; dit != end; ++dit) {
126 if (lastchar == '\n')
130 ws.canBreakLine(lastchar != '\n');
135 TeXMathStream::TeXMathStream(otexrowstream & os, bool fragile, bool latex,
136 OutputType output, Encoding const * encoding)
137 : os_(os), fragile_(fragile), latex_(latex),
138 output_(output), encoding_(encoding)
142 TeXMathStream::~TeXMathStream()
146 else if (pendingspace_)
151 void TeXMathStream::addlines(unsigned int n)
157 void TeXMathStream::pendingSpace(bool space)
159 pendingspace_ = space;
165 void TeXMathStream::useBraces(bool braces)
171 void TeXMathStream::pendingBrace(bool brace)
173 pendingbrace_ = brace;
177 void TeXMathStream::textMode(bool textmode)
179 textmode_ = textmode;
183 void TeXMathStream::lockedMode(bool locked)
189 void TeXMathStream::asciiOnly(bool ascii)
195 Changer TeXMathStream::changeRowEntry(TexRow::RowEntry entry)
197 return changeVar(row_entry_, entry);
201 bool TeXMathStream::startOuterRow()
203 if (TexRow::isNone(row_entry_))
205 return texrow().start(row_entry_);
209 TeXMathStream & operator<<(TeXMathStream & ws, MathAtom const & at)
216 TeXMathStream & operator<<(TeXMathStream & ws, MathData const & ar)
223 TeXMathStream & operator<<(TeXMathStream & ws, char const * s)
230 TeXMathStream & operator<<(TeXMathStream & ws, char c)
232 if (c == '\n' && !ws.canBreakLine())
235 if (ws.pendingBrace()) {
237 ws.pendingBrace(false);
238 ws.pendingSpace(false);
240 } else if (ws.pendingSpace()) {
243 else if (c == '[' && ws.useBraces())
245 else if (c == ' ' && ws.textMode())
247 ws.pendingSpace(false);
248 } else if (ws.useBraces()) {
256 ws.canBreakLine(c != '\n');
261 TeXMathStream & operator<<(TeXMathStream & ws, int i)
263 if (ws.pendingBrace()) {
265 ws.pendingBrace(false);
269 ws.canBreakLine(true);
274 TeXMathStream & operator<<(TeXMathStream & ws, unsigned int i)
276 if (ws.pendingBrace()) {
278 ws.pendingBrace(false);
282 ws.canBreakLine(true);
287 //////////////////////////////////////////////////////////////////////
290 MathMLStream::MathMLStream(odocstream & os, std::string const & xmlns, bool xmlMode)
291 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
295 void MathMLStream::cr()
298 for (int i = 0; i < tab(); ++i)
303 void MathMLStream::defer(docstring const & s)
309 void MathMLStream::defer(string const & s)
311 deferred_ << from_utf8(s);
315 docstring MathMLStream::deferred() const
317 return deferred_.str();
321 MathMLStream & operator<<(MathMLStream & ms, MathAtom const & at)
328 MathMLStream & operator<<(MathMLStream & ms, MathData const & ar)
335 MathMLStream & operator<<(MathMLStream & ms, char const * s)
342 MathMLStream & operator<<(MathMLStream & ms, char c)
349 MathMLStream & operator<<(MathMLStream & ms, char_type c)
356 MathMLStream & operator<<(MathMLStream & ms, MTag const & t)
360 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
361 if (!t.attr_.empty())
362 ms.os() << " " << from_ascii(t.attr_);
368 MathMLStream & operator<<(MathMLStream & ms, MTagInline const & t)
371 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
372 if (!t.attr_.empty())
373 ms.os() << " " << from_ascii(t.attr_);
379 MathMLStream & operator<<(MathMLStream & ms, ETag const & t)
384 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
389 MathMLStream & operator<<(MathMLStream & ms, ETagInline const & t)
391 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
396 MathMLStream & operator<<(MathMLStream & ms, CTag const & t)
399 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
400 if (!t.attr_.empty())
401 ms.os() << " " << from_utf8(t.attr_);
407 MathMLStream & operator<<(MathMLStream & ms, docstring const & s)
414 //////////////////////////////////////////////////////////////////////
417 HtmlStream::HtmlStream(odocstream & os)
418 : os_(os), tab_(0), line_(0), in_text_(false)
422 void HtmlStream::defer(docstring const & s)
428 void HtmlStream::defer(string const & s)
430 deferred_ << from_utf8(s);
434 docstring HtmlStream::deferred() const
436 return deferred_.str();
440 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
447 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
454 HtmlStream & operator<<(HtmlStream & ms, char const * s)
461 HtmlStream & operator<<(HtmlStream & ms, char c)
468 HtmlStream & operator<<(HtmlStream & ms, char_type c)
475 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
477 ms.os() << '<' << from_ascii(t.tag_);
478 if (!t.attr_.empty())
479 ms.os() << " " << from_ascii(t.attr_);
485 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
487 ms.os() << "</" << from_ascii(t.tag_) << '>';
492 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
499 //////////////////////////////////////////////////////////////////////
502 SetMode::SetMode(MathMLStream & ms, bool text)
505 was_text_ = ms_.inText();
506 ms_.setTextMode(text);
512 ms_.setTextMode(was_text_);
516 //////////////////////////////////////////////////////////////////////
519 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
522 was_text_ = os_.inText();
523 os_.setTextMode(text);
527 SetHTMLMode::~SetHTMLMode()
529 os_.setTextMode(was_text_);
533 //////////////////////////////////////////////////////////////////////
536 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
543 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
550 MapleStream & operator<<(MapleStream & ms, char const * s)
557 MapleStream & operator<<(MapleStream & ms, char c)
564 MapleStream & operator<<(MapleStream & ms, int i)
571 MapleStream & operator<<(MapleStream & ms, char_type c)
578 MapleStream & operator<<(MapleStream & ms, docstring const & s)
585 //////////////////////////////////////////////////////////////////////
588 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
595 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
602 MaximaStream & operator<<(MaximaStream & ms, char const * s)
609 MaximaStream & operator<<(MaximaStream & ms, char c)
616 MaximaStream & operator<<(MaximaStream & ms, int i)
623 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
630 MaximaStream & operator<<(MaximaStream & ms, char_type c)
637 //////////////////////////////////////////////////////////////////////
640 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
647 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
654 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
661 MathematicaStream & operator<<(MathematicaStream & ms, char c)
668 MathematicaStream & operator<<(MathematicaStream & ms, int i)
675 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
682 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
689 //////////////////////////////////////////////////////////////////////
692 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
699 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
706 OctaveStream & operator<<(OctaveStream & ns, char const * s)
713 OctaveStream & operator<<(OctaveStream & ns, char c)
720 OctaveStream & operator<<(OctaveStream & ns, int i)
727 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
734 OctaveStream & operator<<(OctaveStream & ns, char_type c)
741 OctaveStream & operator<<(OctaveStream & os, string const & s)
743 os.os() << from_utf8(s);
748 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
750 // For the basic symbols, no difference between XML and HTML.
751 if (name.size() == 1) {
752 char_type const c = name[0];
754 return from_ascii("<");
756 return from_ascii(">");
759 } else if (name.size() == 2 && name[0] == '\\') {
760 char_type const c = name[1];
762 return from_ascii("{");
764 return from_ascii("}");
766 MathWordList const & words = mathedWordList();
767 MathWordList::const_iterator it = words.find(name);
768 if (it != words.end()) {
769 // Only difference between XML and HTML, based on the contents read by MathFactory.
770 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
773 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");