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"
51 explicit WriteStream(otexrowstream & os, bool fragile = false,
52 bool latex = false, OutputType output = wsDefault,
53 Encoding const * encoding = 0);
57 int line() const { return line_; }
59 bool fragile() const { return fragile_; }
61 bool latex() const { return latex_; }
63 OutputType output() const { return output_; }
65 otexrowstream & os() { return os_; }
67 TexRow & texrow() { return os_.texrow(); }
69 bool & firstitem() { return firstitem_; }
71 void addlines(unsigned int);
72 /// record whether we can write an immediately following newline char
73 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
74 /// tell whether we can write an immediately following newline char
75 bool canBreakLine() const { return canbreakline_; }
76 /// record whether we have to take care for striking out display math
77 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
78 /// tell whether we have to take care for striking out display math
79 bool strikeoutMath() const { return mathsout_; }
80 /// record which ulem command type we are inside
81 void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
82 /// tell which ulem command type we are inside
83 UlemCmdType ulemCmd() const { return ulemcmd_; }
84 /// record whether we are in the argument of a math macro
85 void insideMacro(bool insidemacro) { insidemacro_ = insidemacro; }
86 /// tell whether we are in the argument of a math macro
87 bool insideMacro() const { return insidemacro_; }
88 /// writes space if next thing is isalpha()
89 void pendingSpace(bool how);
90 /// writes space if next thing is isalpha()
91 bool pendingSpace() const { return pendingspace_; }
92 /// tell whether to write the closing brace of \ensuremath
93 void pendingBrace(bool brace);
94 /// tell whether to write the closing brace of \ensuremath
95 bool pendingBrace() const { return pendingbrace_; }
96 /// tell whether we are in text mode or not when producing latex code
97 void textMode(bool textmode);
98 /// tell whether we are in text mode or not when producing latex code
99 bool textMode() const { return textmode_; }
100 /// tell whether we are allowed to switch mode when producing latex code
101 void lockedMode(bool locked);
102 /// tell whether we are allowed to switch mode when producing latex code
103 bool lockedMode() const { return locked_; }
104 /// tell whether to use only ascii chars when producing latex code
105 void asciiOnly(bool ascii);
106 /// tell whether to use only ascii chars when producing latex code
107 bool asciiOnly() const { return ascii_; }
109 Encoding const * encoding() const { return encoding_; }
111 /// Temporarily change the TexRow information about the outer row entry.
112 Changer changeRowEntry(TexRow::RowEntry entry);
113 /// TexRow::starts the innermost outer math inset
114 /// returns true if the outer row entry will appear at this line
115 bool startOuterRow();
119 /// do we have to write \\protect sometimes
121 /// are we at the beginning of an MathData?
123 /// are we writing to .tex?
125 /// output type (default, source preview, instant preview)?
127 /// are we in the argument of a math macro?
129 /// do we have a space pending?
131 /// do we have a brace pending?
133 /// are we in text mode when producing latex code?
135 /// are we allowed to switch mode when producing latex code?
137 /// should we use only ascii chars when producing latex code?
139 /// are we allowed to output an immediately following newline?
141 /// should we take care for striking out display math?
143 /// what ulem command are we inside (none, underline, strikeout)?
144 UlemCmdType ulemcmd_;
148 Encoding const * encoding_;
149 /// Row entry we are in
150 TexRow::RowEntry row_entry_;
154 WriteStream & operator<<(WriteStream &, MathAtom const &);
156 WriteStream & operator<<(WriteStream &, MathData const &);
158 WriteStream & operator<<(WriteStream &, docstring const &);
160 WriteStream & operator<<(WriteStream &, char const * const);
162 WriteStream & operator<<(WriteStream &, char);
164 WriteStream & operator<<(WriteStream &, int);
166 WriteStream & operator<<(WriteStream &, unsigned int);
168 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
169 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
170 bool macro = false, bool textmode_macro = false);
172 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
173 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
177 * MathEnsurer - utility class for ensuring math mode
179 * A local variable of this type can be used to either ensure math mode
180 * or delay the writing of a pending brace when outputting LaTeX.
181 * A LyX MathMacro is always assumed needing a math mode environment, while
182 * no assumption is made for macros defined through \newcommand or \def.
186 * MathEnsurer ensurer(os);
188 * If not already in math mode, inserts an \ensuremath command followed
189 * by an open brace. This brace will be automatically closed when exiting
190 * math mode. Math mode is automatically exited when writing something
191 * that doesn't explicitly require math mode.
195 * MathEnsurer ensurer(os, false);
197 * Simply suspend writing a closing brace until the end of ensurer's scope.
201 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
203 * This form is mainly used for math macros as they are treated specially.
204 * In essence, the macros defined in the lib/symbols file and tagged as
205 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
206 * environment, while macros defined in the preamble or ERT are left as is.
207 * The third parameter must be set to true and the fourth parameter has also
208 * to be specified. Only the following 3 different cases are handled.
210 * When the needs_mathmode parameter is true the behavior is as in Example 1.
211 * This is the case for a LyX MathMacro or a macro not tagged as textmode.
213 * When the needs_mathmode and textmode_macro parameters are both false the
214 * macro is left in the same (text or math mode) environment it was entered.
215 * This is because it is assumed that the macro was designed for that mode
216 * and we have no way to tell the contrary.
217 * This is the case for macros defined by using \newcommand or \def in ERT.
219 * When the needs_mathmode parameter is false while textmode_macro is true the
220 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
221 * This is the case for the macros tagged as textmode in lib/symbols.
227 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
228 bool macro = false, bool textmode_macro = false)
229 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
231 ~MathEnsurer() { os_.pendingBrace(brace_); }
241 * ModeSpecifier - utility class for specifying a given mode (math or text)
243 * A local variable of this type can be used to specify that a command or
244 * environment works in a given mode. For example, \mbox works in text
245 * mode, but \boxed works in math mode. Note that no mode changing commands
246 * are needed, but we have to track the current mode, hence this class.
247 * This is only used when exporting to latex and helps determining whether
248 * the mode needs being temporarily switched when a command would not work
249 * in the current mode. As there are cases where this switching is to be
250 * avoided, the optional third parameter can be used to lock the mode.
251 * When the mode is locked, the optional fourth parameter specifies whether
252 * strings are to be output by using a suitable ascii representation.
256 * ModeSpecifier specifier(os, TEXT_MODE);
258 * Sets the current mode to text mode and allows mode switching.
262 * ModeSpecifier specifier(os, TEXT_MODE, true);
264 * Sets the current mode to text mode and disallows mode switching.
268 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
270 * Sets the current mode to text mode, disallows mode switching, and outputs
271 * strings as ascii only.
273 * At the end of specifier's scope the mode is reset to its previous value.
279 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
280 bool locked = false, bool ascii = false)
281 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
285 os_.textMode(oldmodes_ & 0x01);
286 os_.lockedMode(oldmodes_ & 0x02);
287 os_.asciiOnly(oldmodes_ & 0x04);
305 MTag(char const * const tag, std::string attr = "")
306 : tag_(tag), attr_(attr) {}
308 char const * const tag_;
316 ETag(char const * const tag) : tag_(tag) {}
318 char const * const tag_;
322 /// Throw MathExportException to signal that the attempt to export
323 /// some math in the current format did not succeed. E.g., we can't
324 /// export xymatrix as MathML, so that will throw, and we'll fall back
326 class MathExportException : public std::exception {};
332 explicit MathStream(odocstream & os);
336 odocstream & os() { return os_; }
338 int line() const { return line_; }
340 int & tab() { return tab_; }
342 friend MathStream & operator<<(MathStream &, char const *);
344 void defer(docstring const &);
346 void defer(std::string const &);
348 docstring deferred() const;
350 bool inText() const { return in_text_; }
353 void setTextMode(bool t) { in_text_ = t; }
363 odocstringstream deferred_;
365 friend class SetMode;
369 MathStream & operator<<(MathStream &, MathAtom const &);
371 MathStream & operator<<(MathStream &, MathData const &);
373 MathStream & operator<<(MathStream &, docstring const &);
375 MathStream & operator<<(MathStream &, char const *);
377 MathStream & operator<<(MathStream &, char);
379 MathStream & operator<<(MathStream &, char_type);
381 MathStream & operator<<(MathStream &, MTag const &);
383 MathStream & operator<<(MathStream &, ETag const &);
386 /// A simpler version of ModeSpecifier, for MathML
390 explicit SetMode(MathStream & os, bool text);
404 explicit HtmlStream(odocstream & os);
408 odocstream & os() { return os_; }
410 int line() const { return line_; }
412 int & tab() { return tab_; }
414 friend HtmlStream & operator<<(HtmlStream &, char const *);
416 void defer(docstring const &);
418 void defer(std::string const &);
420 docstring deferred() const;
422 bool inText() const { return in_text_; }
425 void setTextMode(bool t) { in_text_ = t; }
435 odocstringstream deferred_;
437 friend class SetHTMLMode;
441 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
443 HtmlStream & operator<<(HtmlStream &, MathData const &);
445 HtmlStream & operator<<(HtmlStream &, docstring const &);
447 HtmlStream & operator<<(HtmlStream &, char const *);
449 HtmlStream & operator<<(HtmlStream &, char);
451 HtmlStream & operator<<(HtmlStream &, char_type);
453 HtmlStream & operator<<(HtmlStream &, MTag const &);
455 HtmlStream & operator<<(HtmlStream &, ETag const &);
461 explicit SetHTMLMode(HtmlStream & os, bool text);
479 explicit NormalStream(odocstream & os) : os_(os) {}
481 odocstream & os() { return os_; }
488 NormalStream & operator<<(NormalStream &, MathAtom const &);
490 NormalStream & operator<<(NormalStream &, MathData const &);
492 NormalStream & operator<<(NormalStream &, docstring const &);
494 NormalStream & operator<<(NormalStream &, char const *);
496 NormalStream & operator<<(NormalStream &, char);
498 NormalStream & operator<<(NormalStream &, int);
509 explicit MapleStream(odocstream & os) : os_(os) {}
511 odocstream & os() { return os_; }
519 MapleStream & operator<<(MapleStream &, MathAtom const &);
521 MapleStream & operator<<(MapleStream &, MathData const &);
523 MapleStream & operator<<(MapleStream &, docstring const &);
525 MapleStream & operator<<(MapleStream &, char_type);
527 MapleStream & operator<<(MapleStream &, char const *);
529 MapleStream & operator<<(MapleStream &, char);
531 MapleStream & operator<<(MapleStream &, int);
542 explicit MaximaStream(odocstream & os) : os_(os) {}
544 odocstream & os() { return os_; }
552 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
554 MaximaStream & operator<<(MaximaStream &, MathData const &);
556 MaximaStream & operator<<(MaximaStream &, docstring const &);
558 MaximaStream & operator<<(MaximaStream &, char_type);
560 MaximaStream & operator<<(MaximaStream &, char const *);
562 MaximaStream & operator<<(MaximaStream &, char);
564 MaximaStream & operator<<(MaximaStream &, int);
572 class MathematicaStream {
575 explicit MathematicaStream(odocstream & os) : os_(os) {}
577 odocstream & os() { return os_; }
585 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
587 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
589 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
591 MathematicaStream & operator<<(MathematicaStream &, char const *);
593 MathematicaStream & operator<<(MathematicaStream &, char);
595 MathematicaStream & operator<<(MathematicaStream &, int);
606 explicit OctaveStream(odocstream & os) : os_(os) {}
608 odocstream & os() { return os_; }
615 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
617 OctaveStream & operator<<(OctaveStream &, MathData const &);
619 OctaveStream & operator<<(OctaveStream &, docstring const &);
621 OctaveStream & operator<<(OctaveStream &, char_type);
623 OctaveStream & operator<<(OctaveStream &, char const *);
625 OctaveStream & operator<<(OctaveStream &, char);
627 OctaveStream & operator<<(OctaveStream &, int);
630 docstring convertDelimToXMLEscape(docstring const & name);