4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
12 #ifndef MATH_MATHMLSTREAM_H
13 #define MATH_MATHMLSTREAM_H
15 #include "InsetMath.h"
18 #include "texstream.h"
20 #include "support/Changer.h"
21 #include "support/strfwd.h"
22 #include "support/unique_ptr.h"
50 explicit WriteStream(otexrowstream & os, bool fragile = false,
51 bool latex = false, OutputType output = wsDefault,
52 Encoding const * encoding = 0);
56 int line() const { return line_; }
58 bool fragile() const { return fragile_; }
60 bool latex() const { return latex_; }
62 OutputType output() const { return output_; }
64 otexrowstream & os() { return os_; }
66 TexRow & texrow() { return os_.texrow(); }
68 bool & firstitem() { return firstitem_; }
70 void addlines(unsigned int);
71 /// record whether we can write an immediately following newline char
72 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
73 /// tell whether we can write an immediately following newline char
74 bool canBreakLine() const { return canbreakline_; }
75 /// record whether we have to take care for striking out display math
76 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
77 /// tell whether we have to take care for striking out display math
78 bool strikeoutMath() const { return mathsout_; }
79 /// record which ulem command type we are inside
80 void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
81 /// tell which ulem command type we are inside
82 UlemCmdType ulemCmd() const { return ulemcmd_; }
83 /// writes space if next thing is isalpha()
84 void pendingSpace(bool how);
85 /// writes space if next thing is isalpha()
86 bool pendingSpace() const { return pendingspace_; }
87 /// tell whether to write the closing brace of \ensuremath
88 void pendingBrace(bool brace);
89 /// tell whether to write the closing brace of \ensuremath
90 bool pendingBrace() const { return pendingbrace_; }
91 /// tell whether we are in text mode or not when producing latex code
92 void textMode(bool textmode);
93 /// tell whether we are in text mode or not when producing latex code
94 bool textMode() const { return textmode_; }
95 /// tell whether we are allowed to switch mode when producing latex code
96 void lockedMode(bool locked);
97 /// tell whether we are allowed to switch mode when producing latex code
98 bool lockedMode() const { return locked_; }
99 /// tell whether to use only ascii chars when producing latex code
100 void asciiOnly(bool ascii);
101 /// tell whether to use only ascii chars when producing latex code
102 bool asciiOnly() const { return ascii_; }
103 /// tell whether we are in a MathClass inset
104 void inMathClass(bool mathclass) { mathclass_ = mathclass; };
105 /// tell whether we are in a MathClass inset
106 bool inMathClass() const { return mathclass_; }
108 Encoding const * encoding() const { return encoding_; }
110 /// Temporarily change the TexRow information about the outer row entry.
111 Changer changeRowEntry(TexRow::RowEntry entry);
112 /// TexRow::starts the innermost outer math inset
113 /// returns true if the outer row entry will appear at this line
114 bool startOuterRow();
118 /// do we have to write \\protect sometimes
120 /// are we at the beginning of an MathData?
122 /// are we writing to .tex?
124 /// output type (default, source preview, instant preview)?
126 /// do we have a space pending?
128 /// do we have a brace pending?
130 /// are we in text mode when producing latex code?
132 /// are we allowed to switch mode when producing latex code?
134 /// should we use only ascii chars when producing latex code?
136 /// are we allowed to output an immediately following newline?
138 /// should we take care for striking out display math?
140 /// what ulem command are we inside (none, underline, strikeout)?
141 UlemCmdType ulemcmd_;
145 Encoding const * encoding_;
146 /// Row entry we are in
147 TexRow::RowEntry row_entry_;
148 /// whether we are in a MathClass inset
153 WriteStream & operator<<(WriteStream &, MathAtom const &);
155 WriteStream & operator<<(WriteStream &, MathData const &);
157 WriteStream & operator<<(WriteStream &, docstring const &);
159 WriteStream & operator<<(WriteStream &, char const * const);
161 WriteStream & operator<<(WriteStream &, char);
163 WriteStream & operator<<(WriteStream &, int);
165 WriteStream & operator<<(WriteStream &, unsigned int);
167 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
168 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
169 bool macro = false, bool textmode_macro = false);
171 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
172 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
176 * MathEnsurer - utility class for ensuring math mode
178 * A local variable of this type can be used to either ensure math mode
179 * or delay the writing of a pending brace when outputting LaTeX.
180 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
181 * no assumption is made for macros defined through \newcommand or \def.
185 * MathEnsurer ensurer(os);
187 * If not already in math mode, inserts an \ensuremath command followed
188 * by an open brace. This brace will be automatically closed when exiting
189 * math mode. Math mode is automatically exited when writing something
190 * that doesn't explicitly require math mode.
194 * MathEnsurer ensurer(os, false);
196 * Simply suspend writing a closing brace until the end of ensurer's scope.
200 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
202 * This form is mainly used for math macros as they are treated specially.
203 * In essence, the macros defined in the lib/symbols file and tagged as
204 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
205 * environment, while macros defined in the preamble or ERT are left as is.
206 * The third parameter must be set to true and the fourth parameter has also
207 * to be specified. Only the following 3 different cases are handled.
209 * When the needs_mathmode parameter is true the behavior is as in Example 1.
210 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
212 * When the needs_mathmode and textmode_macro parameters are both false the
213 * macro is left in the same (text or math mode) environment it was entered.
214 * This is because it is assumed that the macro was designed for that mode
215 * and we have no way to tell the contrary.
216 * This is the case for macros defined by using \newcommand or \def in ERT.
218 * When the needs_mathmode parameter is false while textmode_macro is true the
219 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
220 * This is the case for the macros tagged as textmode in lib/symbols.
226 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
227 bool macro = false, bool textmode_macro = false)
228 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
230 ~MathEnsurer() { os_.pendingBrace(brace_); }
240 * ModeSpecifier - utility class for specifying a given mode (math or text)
242 * A local variable of this type can be used to specify that a command or
243 * environment works in a given mode. For example, \mbox works in text
244 * mode, but \boxed works in math mode. Note that no mode changing commands
245 * are needed, but we have to track the current mode, hence this class.
246 * This is only used when exporting to latex and helps determining whether
247 * the mode needs being temporarily switched when a command would not work
248 * in the current mode. As there are cases where this switching is to be
249 * avoided, the optional third parameter can be used to lock the mode.
250 * When the mode is locked, the optional fourth parameter specifies whether
251 * strings are to be output by using a suitable ascii representation.
255 * ModeSpecifier specifier(os, TEXT_MODE);
257 * Sets the current mode to text mode and allows mode switching.
261 * ModeSpecifier specifier(os, TEXT_MODE, true);
263 * Sets the current mode to text mode and disallows mode switching.
267 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
269 * Sets the current mode to text mode, disallows mode switching, and outputs
270 * strings as ascii only.
272 * At the end of specifier's scope the mode is reset to its previous value.
278 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
279 bool locked = false, bool ascii = false)
280 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
284 os_.textMode(oldmodes_ & 0x01);
285 os_.lockedMode(oldmodes_ & 0x02);
286 os_.asciiOnly(oldmodes_ & 0x04);
306 MTag(char const * const tag, std::string const & attr = std::string())
307 : tag_(tag), attr_(attr) {}
309 char const * const tag_;
319 explicit ETag(char const * const tag) : tag_(tag) {}
321 char const * const tag_;
325 /// Compound tag (no content, directly closed).
329 CTag(char const * const tag, std::string const & attr = "")
330 : tag_(tag), attr_(attr) {}
332 char const * const tag_;
338 /// Throw MathExportException to signal that the attempt to export
339 /// some math in the current format did not succeed. E.g., we can't
340 /// export xymatrix as MathML, so that will throw, and we'll fall back
342 class MathExportException : public std::exception {};
347 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
348 explicit MathStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
352 odocstream & os() { return os_; }
354 int line() const { return line_; }
356 int & tab() { return tab_; }
358 friend MathStream & operator<<(MathStream &, char const *);
360 void defer(docstring const &);
362 void defer(std::string const &);
364 docstring deferred() const;
366 bool inText() const { return in_text_; }
368 std::string xmlns() const { return xmlns_; }
370 bool xmlMode() const { return xml_mode_; }
371 /// Returns the tag name prefixed by the name space if needed.
372 std::string namespacedTag(std::string const & tag) const {
373 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
377 void setTextMode(bool t) { in_text_ = t; }
387 odocstringstream deferred_;
393 friend class SetMode;
397 MathStream & operator<<(MathStream &, MathAtom const &);
399 MathStream & operator<<(MathStream &, MathData const &);
401 MathStream & operator<<(MathStream &, docstring const &);
403 MathStream & operator<<(MathStream &, char const *);
405 MathStream & operator<<(MathStream &, char);
407 MathStream & operator<<(MathStream &, char_type);
409 MathStream & operator<<(MathStream &, MTag const &);
411 MathStream & operator<<(MathStream &, ETag const &);
413 MathStream & operator<<(MathStream &, CTag const &);
416 /// A simpler version of ModeSpecifier, for MathML
420 explicit SetMode(MathStream & ms, bool text);
434 explicit HtmlStream(odocstream & os);
438 odocstream & os() { return os_; }
440 int line() const { return line_; }
442 int & tab() { return tab_; }
444 friend HtmlStream & operator<<(HtmlStream &, char const *);
446 void defer(docstring const &);
448 void defer(std::string const &);
450 docstring deferred() const;
452 bool inText() const { return in_text_; }
455 void setTextMode(bool t) { in_text_ = t; }
465 odocstringstream deferred_;
467 friend class SetHTMLMode;
471 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
473 HtmlStream & operator<<(HtmlStream &, MathData const &);
475 HtmlStream & operator<<(HtmlStream &, docstring const &);
477 HtmlStream & operator<<(HtmlStream &, char const *);
479 HtmlStream & operator<<(HtmlStream &, char);
481 HtmlStream & operator<<(HtmlStream &, char_type);
483 HtmlStream & operator<<(HtmlStream &, MTag const &);
485 HtmlStream & operator<<(HtmlStream &, ETag const &);
491 explicit SetHTMLMode(HtmlStream & os, bool text);
509 explicit NormalStream(odocstream & os) : os_(os) {}
511 odocstream & os() { return os_; }
518 NormalStream & operator<<(NormalStream &, MathAtom const &);
520 NormalStream & operator<<(NormalStream &, MathData const &);
522 NormalStream & operator<<(NormalStream &, docstring const &);
524 NormalStream & operator<<(NormalStream &, char const *);
526 NormalStream & operator<<(NormalStream &, char);
528 NormalStream & operator<<(NormalStream &, int);
539 explicit MapleStream(odocstream & os) : os_(os) {}
541 odocstream & os() { return os_; }
549 MapleStream & operator<<(MapleStream &, MathAtom const &);
551 MapleStream & operator<<(MapleStream &, MathData const &);
553 MapleStream & operator<<(MapleStream &, docstring const &);
555 MapleStream & operator<<(MapleStream &, char_type);
557 MapleStream & operator<<(MapleStream &, char const *);
559 MapleStream & operator<<(MapleStream &, char);
561 MapleStream & operator<<(MapleStream &, int);
572 explicit MaximaStream(odocstream & os) : os_(os) {}
574 odocstream & os() { return os_; }
582 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
584 MaximaStream & operator<<(MaximaStream &, MathData const &);
586 MaximaStream & operator<<(MaximaStream &, docstring const &);
588 MaximaStream & operator<<(MaximaStream &, char_type);
590 MaximaStream & operator<<(MaximaStream &, char const *);
592 MaximaStream & operator<<(MaximaStream &, char);
594 MaximaStream & operator<<(MaximaStream &, int);
602 class MathematicaStream {
605 explicit MathematicaStream(odocstream & os) : os_(os) {}
607 odocstream & os() { return os_; }
615 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
617 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
619 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
621 MathematicaStream & operator<<(MathematicaStream &, char const *);
623 MathematicaStream & operator<<(MathematicaStream &, char);
625 MathematicaStream & operator<<(MathematicaStream &, int);
636 explicit OctaveStream(odocstream & os) : os_(os) {}
638 odocstream & os() { return os_; }
645 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
647 OctaveStream & operator<<(OctaveStream &, MathData const &);
649 OctaveStream & operator<<(OctaveStream &, docstring const &);
651 OctaveStream & operator<<(OctaveStream &, char_type);
653 OctaveStream & operator<<(OctaveStream &, char const *);
655 OctaveStream & operator<<(OctaveStream &, char);
657 OctaveStream & operator<<(OctaveStream &, int);
660 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);