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"
36 //////////////////////////////////////////////////////////////////////
39 NormalStream & operator<<(NormalStream & ns, MathAtom const & at)
46 NormalStream & operator<<(NormalStream & ns, MathData const & ar)
53 NormalStream & operator<<(NormalStream & ns, docstring const & s)
60 NormalStream & operator<<(NormalStream & ns, const string & s)
62 ns.os() << from_utf8(s);
67 NormalStream & operator<<(NormalStream & ns, char const * s)
74 NormalStream & operator<<(NormalStream & ns, char c)
81 NormalStream & operator<<(NormalStream & ns, int i)
89 /////////////////////////////////////////////////////////////////
92 TeXMathStream & operator<<(TeXMathStream & ws, docstring const & s)
94 // Skip leading '\n' if we had already output a newline char
96 (s.length() > 0 && (s[0] != '\n' || ws.canBreakLine())) ? 0 : 1;
98 // Check whether there's something to output
99 if (s.length() <= first)
102 if (ws.pendingBrace()) {
104 ws.pendingBrace(false);
105 ws.pendingSpace(false);
107 } else if (ws.pendingSpace()) {
108 if (isAlphaASCII(s[first]))
110 else if (s[first] == '[' && ws.useBraces())
112 else if (s[first] == ' ' && ws.textMode())
114 ws.pendingSpace(false);
115 } else if (ws.useBraces()) {
116 if (s[first] == '\'')
120 ws.os() << s.substr(first);
122 char_type lastchar = 0;
123 docstring::const_iterator dit = s.begin() + first;
124 docstring::const_iterator end = s.end();
125 for (; dit != end; ++dit) {
127 if (lastchar == '\n')
131 ws.canBreakLine(lastchar != '\n');
136 TeXMathStream::TeXMathStream(otexrowstream & os, bool fragile, bool latex,
137 OutputType output, Encoding const * encoding)
138 : os_(os), fragile_(fragile), latex_(latex),
139 output_(output), encoding_(encoding)
143 TeXMathStream::~TeXMathStream()
147 else if (pendingspace_)
152 void TeXMathStream::addlines(unsigned int n)
158 void TeXMathStream::pendingSpace(bool space)
160 pendingspace_ = space;
166 void TeXMathStream::useBraces(bool braces)
172 void TeXMathStream::pendingBrace(bool brace)
174 pendingbrace_ = brace;
178 void TeXMathStream::textMode(bool textmode)
180 textmode_ = textmode;
184 void TeXMathStream::lockedMode(bool locked)
190 void TeXMathStream::asciiOnly(bool ascii)
196 Changer TeXMathStream::changeRowEntry(TexRow::RowEntry entry)
198 return changeVar(row_entry_, entry);
202 bool TeXMathStream::startOuterRow()
204 if (TexRow::isNone(row_entry_))
206 return texrow().start(row_entry_);
210 TeXMathStream & operator<<(TeXMathStream & ws, MathAtom const & at)
217 TeXMathStream & operator<<(TeXMathStream & ws, MathData const & ar)
224 TeXMathStream & operator<<(TeXMathStream & ws, char const * s)
231 TeXMathStream & operator<<(TeXMathStream & 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.useBraces())
246 else if (c == ' ' && ws.textMode())
248 ws.pendingSpace(false);
249 } else if (ws.useBraces()) {
257 ws.canBreakLine(c != '\n');
262 TeXMathStream & operator<<(TeXMathStream & ws, int i)
264 if (ws.pendingBrace()) {
266 ws.pendingBrace(false);
270 ws.canBreakLine(true);
275 TeXMathStream & operator<<(TeXMathStream & ws, unsigned int i)
277 if (ws.pendingBrace()) {
279 ws.pendingBrace(false);
283 ws.canBreakLine(true);
288 //////////////////////////////////////////////////////////////////////
291 MathMLStream::MathMLStream(odocstream & os, std::string const & xmlns, bool xmlMode)
292 : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
295 font_math_style_ = TEXT_STYLE;
297 font_math_style_ = DISPLAY_STYLE;
301 void MathMLStream::cr()
304 for (int i = 0; i < tab(); ++i)
309 void MathMLStream::defer(docstring const & s)
315 void MathMLStream::defer(string const & s)
317 deferred_ << from_utf8(s);
321 docstring MathMLStream::deferred() const
323 return deferred_.str();
327 MathMLStream & operator<<(MathMLStream & ms, MathAtom const & at)
334 MathMLStream & operator<<(MathMLStream & ms, MathData const & ar)
341 MathMLStream & operator<<(MathMLStream & ms, char const * s)
348 MathMLStream & operator<<(MathMLStream & ms, char c)
355 MathMLStream & operator<<(MathMLStream & ms, char_type c)
362 MathMLStream & operator<<(MathMLStream & ms, MTag const & t)
366 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
367 if (!t.attr_.empty())
368 ms.os() << " " << from_ascii(t.attr_);
374 MathMLStream & operator<<(MathMLStream & ms, MTagInline const & t)
377 ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
378 if (!t.attr_.empty())
379 ms.os() << " " << from_ascii(t.attr_);
385 MathMLStream & operator<<(MathMLStream & ms, ETag const & t)
390 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
395 MathMLStream & operator<<(MathMLStream & ms, ETagInline const & t)
397 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
402 MathMLStream & operator<<(MathMLStream & ms, CTag const & t)
405 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
406 if (!t.attr_.empty())
407 ms.os() << " " << from_utf8(t.attr_);
413 MathMLStream & operator<<(MathMLStream & ms, docstring const & s)
420 //////////////////////////////////////////////////////////////////////
423 HtmlStream::HtmlStream(odocstream & os)
424 : os_(os), tab_(0), line_(0), in_text_(false)
428 void HtmlStream::defer(docstring const & s)
434 void HtmlStream::defer(string const & s)
436 deferred_ << from_utf8(s);
440 docstring HtmlStream::deferred() const
442 return deferred_.str();
446 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
453 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
460 HtmlStream & operator<<(HtmlStream & ms, char const * s)
467 HtmlStream & operator<<(HtmlStream & ms, char c)
474 HtmlStream & operator<<(HtmlStream & ms, char_type c)
481 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
483 ms.os() << '<' << from_ascii(t.tag_);
484 if (!t.attr_.empty())
485 ms.os() << " " << from_ascii(t.attr_);
491 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
493 ms.os() << "</" << from_ascii(t.tag_) << '>';
498 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
505 //////////////////////////////////////////////////////////////////////
508 SetMode::SetMode(MathMLStream & ms, bool text)
511 was_text_ = ms_.inText();
512 ms_.setTextMode(text);
518 ms_.setTextMode(was_text_);
522 //////////////////////////////////////////////////////////////////////
525 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
528 was_text_ = os_.inText();
529 os_.setTextMode(text);
533 SetHTMLMode::~SetHTMLMode()
535 os_.setTextMode(was_text_);
539 //////////////////////////////////////////////////////////////////////
542 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
549 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
556 MapleStream & operator<<(MapleStream & ms, char const * s)
563 MapleStream & operator<<(MapleStream & ms, char c)
570 MapleStream & operator<<(MapleStream & ms, int i)
577 MapleStream & operator<<(MapleStream & ms, char_type c)
584 MapleStream & operator<<(MapleStream & ms, docstring const & s)
591 //////////////////////////////////////////////////////////////////////
594 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
601 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
608 MaximaStream & operator<<(MaximaStream & ms, char const * s)
615 MaximaStream & operator<<(MaximaStream & ms, char c)
622 MaximaStream & operator<<(MaximaStream & ms, int i)
629 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
636 MaximaStream & operator<<(MaximaStream & ms, char_type c)
643 //////////////////////////////////////////////////////////////////////
646 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
653 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
660 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
667 MathematicaStream & operator<<(MathematicaStream & ms, char c)
674 MathematicaStream & operator<<(MathematicaStream & ms, int i)
681 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
688 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
695 //////////////////////////////////////////////////////////////////////
698 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
705 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
712 OctaveStream & operator<<(OctaveStream & ns, char const * s)
719 OctaveStream & operator<<(OctaveStream & ns, char c)
726 OctaveStream & operator<<(OctaveStream & ns, int i)
733 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
740 OctaveStream & operator<<(OctaveStream & ns, char_type c)
747 OctaveStream & operator<<(OctaveStream & os, string const & s)
749 os.os() << from_utf8(s);
754 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
756 // For the basic symbols, no difference between XML and HTML.
757 if (name.size() == 1) {
758 char_type const c = name[0];
760 return from_ascii("<");
762 return from_ascii(">");
765 } else if (name.size() == 2 && name[0] == '\\') {
766 char_type const c = name[1];
768 return from_ascii("{");
770 return from_ascii("}");
772 MathWordList const & words = mathedWordList();
773 MathWordList::const_iterator it = words.find(name);
774 if (it != words.end()) {
775 // Only difference between XML and HTML, based on the contents read by MathFactory.
776 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
779 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");