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, ETag const & t)
373 ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
378 MathMLStream & operator<<(MathMLStream & ms, CTag const & t)
381 ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
382 if (!t.attr_.empty())
383 ms.os() << " " << from_utf8(t.attr_);
389 MathMLStream & operator<<(MathMLStream & ms, docstring const & s)
396 //////////////////////////////////////////////////////////////////////
399 HtmlStream::HtmlStream(odocstream & os)
400 : os_(os), tab_(0), line_(0), in_text_(false)
404 void HtmlStream::defer(docstring const & s)
410 void HtmlStream::defer(string const & s)
412 deferred_ << from_utf8(s);
416 docstring HtmlStream::deferred() const
418 return deferred_.str();
422 HtmlStream & operator<<(HtmlStream & ms, MathAtom const & at)
429 HtmlStream & operator<<(HtmlStream & ms, MathData const & ar)
436 HtmlStream & operator<<(HtmlStream & ms, char const * s)
443 HtmlStream & operator<<(HtmlStream & ms, char c)
450 HtmlStream & operator<<(HtmlStream & ms, char_type c)
457 HtmlStream & operator<<(HtmlStream & ms, MTag const & t)
459 ms.os() << '<' << from_ascii(t.tag_);
460 if (!t.attr_.empty())
461 ms.os() << " " << from_ascii(t.attr_);
467 HtmlStream & operator<<(HtmlStream & ms, ETag const & t)
469 ms.os() << "</" << from_ascii(t.tag_) << '>';
474 HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
481 //////////////////////////////////////////////////////////////////////
484 SetMode::SetMode(MathMLStream & ms, bool text)
487 was_text_ = ms_.inText();
488 ms_.setTextMode(text);
494 ms_.setTextMode(was_text_);
498 //////////////////////////////////////////////////////////////////////
501 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
504 was_text_ = os_.inText();
505 os_.setTextMode(text);
509 SetHTMLMode::~SetHTMLMode()
511 os_.setTextMode(was_text_);
515 //////////////////////////////////////////////////////////////////////
518 MapleStream & operator<<(MapleStream & ms, MathAtom const & at)
525 MapleStream & operator<<(MapleStream & ms, MathData const & ar)
532 MapleStream & operator<<(MapleStream & ms, char const * s)
539 MapleStream & operator<<(MapleStream & ms, char c)
546 MapleStream & operator<<(MapleStream & ms, int i)
553 MapleStream & operator<<(MapleStream & ms, char_type c)
560 MapleStream & operator<<(MapleStream & ms, docstring const & s)
567 //////////////////////////////////////////////////////////////////////
570 MaximaStream & operator<<(MaximaStream & ms, MathAtom const & at)
577 MaximaStream & operator<<(MaximaStream & ms, MathData const & ar)
584 MaximaStream & operator<<(MaximaStream & ms, char const * s)
591 MaximaStream & operator<<(MaximaStream & ms, char c)
598 MaximaStream & operator<<(MaximaStream & ms, int i)
605 MaximaStream & operator<<(MaximaStream & ms, docstring const & s)
612 MaximaStream & operator<<(MaximaStream & ms, char_type c)
619 //////////////////////////////////////////////////////////////////////
622 MathematicaStream & operator<<(MathematicaStream & ms, MathAtom const & at)
629 MathematicaStream & operator<<(MathematicaStream & ms, MathData const & ar)
636 MathematicaStream & operator<<(MathematicaStream & ms, char const * s)
643 MathematicaStream & operator<<(MathematicaStream & ms, char c)
650 MathematicaStream & operator<<(MathematicaStream & ms, int i)
657 MathematicaStream & operator<<(MathematicaStream & ms, docstring const & s)
664 MathematicaStream & operator<<(MathematicaStream & ms, char_type c)
671 //////////////////////////////////////////////////////////////////////
674 OctaveStream & operator<<(OctaveStream & ns, MathAtom const & at)
681 OctaveStream & operator<<(OctaveStream & ns, MathData const & ar)
688 OctaveStream & operator<<(OctaveStream & ns, char const * s)
695 OctaveStream & operator<<(OctaveStream & ns, char c)
702 OctaveStream & operator<<(OctaveStream & ns, int i)
709 OctaveStream & operator<<(OctaveStream & ns, docstring const & s)
716 OctaveStream & operator<<(OctaveStream & ns, char_type c)
723 OctaveStream & operator<<(OctaveStream & os, string const & s)
725 os.os() << from_utf8(s);
730 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
732 // For the basic symbols, no difference between XML and HTML.
733 if (name.size() == 1) {
734 char_type const c = name[0];
736 return from_ascii("<");
738 return from_ascii(">");
741 } else if (name.size() == 2 && name[0] == '\\') {
742 char_type const c = name[1];
744 return from_ascii("{");
746 return from_ascii("}");
748 MathWordList const & words = mathedWordList();
749 MathWordList::const_iterator it = words.find(name);
750 if (it != words.end()) {
751 // Only difference between XML and HTML, based on the contents read by MathFactory.
752 docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
755 LYXERR0("Unable to find `" << name <<"' in the mathWordList.");