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"
16 #include "texstream.h"
18 #include "support/Changer.h"
19 #include "support/strfwd.h"
42 explicit WriteStream(otexrowstream & os, bool fragile = false,
43 bool latex = false, OutputType output = wsDefault,
44 Encoding const * encoding = 0);
48 int line() const { return line_; }
50 bool fragile() const { return fragile_; }
52 bool latex() const { return latex_; }
54 OutputType output() const { return output_; }
56 otexrowstream & os() { return os_; }
58 TexRow & texrow() { return os_.texrow(); }
60 bool & firstitem() { return firstitem_; }
62 void addlines(unsigned int);
63 /// record whether we can write an immediately following newline char
64 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
65 /// tell whether we can write an immediately following newline char
66 bool canBreakLine() const { return canbreakline_; }
67 /// writes space if next thing is isalpha()
68 void pendingSpace(bool how);
69 /// writes space if next thing is isalpha()
70 bool pendingSpace() const { return pendingspace_; }
71 /// tell whether to write the closing brace of \ensuremath
72 void pendingBrace(bool brace);
73 /// tell whether to write the closing brace of \ensuremath
74 bool pendingBrace() const { return pendingbrace_; }
75 /// tell whether we are in text mode or not when producing latex code
76 void textMode(bool textmode);
77 /// tell whether we are in text mode or not when producing latex code
78 bool textMode() const { return textmode_; }
79 /// tell whether we are allowed to switch mode when producing latex code
80 void lockedMode(bool locked);
81 /// tell whether we are allowed to switch mode when producing latex code
82 bool lockedMode() const { return locked_; }
83 /// tell whether to use only ascii chars when producing latex code
84 void asciiOnly(bool ascii);
85 /// tell whether to use only ascii chars when producing latex code
86 bool asciiOnly() const { return ascii_; }
88 Encoding const * encoding() const { return encoding_; }
90 /// Temporarily change the TexRow information about the outer row entry.
91 Changer changeRowEntry(TexRow::RowEntry entry);
92 /// TexRow::starts the innermost outer math inset
93 /// returns true if the outer row entry will appear at this line
98 /// do we have to write \\protect sometimes
100 /// are we at the beginning of an MathData?
102 /// are we writing to .tex?
104 /// output type (default, source preview, instant preview)?
106 /// do we have a space pending?
108 /// do we have a brace pending?
110 /// are we in text mode when producing latex code?
112 /// are we allowed to switch mode when producing latex code?
114 /// should we use only ascii chars when producing latex code?
116 /// are we allowed to output an immediately following newline?
121 Encoding const * encoding_;
122 /// Row entry we are in
123 TexRow::RowEntry row_entry_;
127 WriteStream & operator<<(WriteStream &, MathAtom const &);
129 WriteStream & operator<<(WriteStream &, MathData const &);
131 WriteStream & operator<<(WriteStream &, docstring const &);
133 WriteStream & operator<<(WriteStream &, char const * const);
135 WriteStream & operator<<(WriteStream &, char);
137 WriteStream & operator<<(WriteStream &, int);
139 WriteStream & operator<<(WriteStream &, unsigned int);
141 /// ensure math mode, possibly by opening \ensuremath
142 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
144 /// ensure the requested mode, possibly by closing \ensuremath
145 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
149 * MathEnsurer - utility class for ensuring math mode
151 * A local variable of this type can be used to either ensure math mode
152 * or delay the writing of a pending brace when outputting LaTeX.
156 * MathEnsurer ensurer(os);
158 * If not already in math mode, inserts an \ensuremath command followed
159 * by an open brace. This brace will be automatically closed when exiting
160 * math mode. Math mode is automatically exited when writing something
161 * that doesn't explicitly require math mode.
165 * MathEnsurer ensurer(os, false);
167 * Simply suspend writing a closing brace until the end of ensurer's scope.
171 * MathEnsurer ensurer(os, needs_math_mode, true);
173 * The third parameter is set to true only for a user defined macro, which
174 * needs special handling. When it is a MathMacro, the needs_math_mode
175 * parameter is true and the behavior is as in Example 1. When the
176 * needs_math_mode parameter is false (not a MathMacro) and the macro
177 * was entered in a text box and we are in math mode, the mode is reset
178 * to text. This is because the macro was probably designed for text mode
179 * (given that it was entered in text mode and we have no way to tell the
186 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
187 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
189 ~MathEnsurer() { os_.pendingBrace(brace_); }
199 * ModeSpecifier - utility class for specifying a given mode (math or text)
201 * A local variable of this type can be used to specify that a command or
202 * environment works in a given mode. For example, \mbox works in text
203 * mode, but \boxed works in math mode. Note that no mode changing commands
204 * are needed, but we have to track the current mode, hence this class.
205 * This is only used when exporting to latex and helps determining whether
206 * the mode needs being temporarily switched when a command would not work
207 * in the current mode. As there are cases where this switching is to be
208 * avoided, the optional third parameter can be used to lock the mode.
209 * When the mode is locked, the optional fourth parameter specifies whether
210 * strings are to be output by using a suitable ascii representation.
214 * ModeSpecifier specifier(os, TEXT_MODE);
216 * Sets the current mode to text mode and allows mode switching.
220 * ModeSpecifier specifier(os, TEXT_MODE, true);
222 * Sets the current mode to text mode and disallows mode switching.
226 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
228 * Sets the current mode to text mode, disallows mode switching, and outputs
229 * strings as ascii only.
231 * At the end of specifier's scope the mode is reset to its previous value.
237 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
238 bool locked = false, bool ascii = false)
239 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
243 os_.textMode(oldmodes_ & 0x01);
244 os_.lockedMode(oldmodes_ & 0x02);
245 os_.asciiOnly(oldmodes_ & 0x04);
263 MTag(char const * const tag, std::string attr = "")
264 : tag_(tag), attr_(attr) {}
266 char const * const tag_;
274 ETag(char const * const tag) : tag_(tag) {}
276 char const * const tag_;
280 /// Throw MathExportException to signal that the attempt to export
281 /// some math in the current format did not succeed. E.g., we can't
282 /// export xymatrix as MathML, so that will throw, and we'll fall back
284 class MathExportException : public std::exception {};
290 explicit MathStream(odocstream & os);
294 odocstream & os() { return os_; }
296 int line() const { return line_; }
298 int & tab() { return tab_; }
300 friend MathStream & operator<<(MathStream &, char const *);
302 void defer(docstring const &);
304 void defer(std::string const &);
306 docstring deferred() const;
308 bool inText() const { return in_text_; }
311 void setTextMode(bool t) { in_text_ = t; }
321 odocstringstream deferred_;
323 friend class SetMode;
327 MathStream & operator<<(MathStream &, MathAtom const &);
329 MathStream & operator<<(MathStream &, MathData const &);
331 MathStream & operator<<(MathStream &, docstring const &);
333 MathStream & operator<<(MathStream &, char const *);
335 MathStream & operator<<(MathStream &, char);
337 MathStream & operator<<(MathStream &, char_type);
339 MathStream & operator<<(MathStream &, MTag const &);
341 MathStream & operator<<(MathStream &, ETag const &);
344 /// A simpler version of ModeSpecifier, for MathML
348 explicit SetMode(MathStream & os, bool text);
362 explicit HtmlStream(odocstream & os);
366 odocstream & os() { return os_; }
368 int line() const { return line_; }
370 int & tab() { return tab_; }
372 friend HtmlStream & operator<<(HtmlStream &, char const *);
374 void defer(docstring const &);
376 void defer(std::string const &);
378 docstring deferred() const;
380 bool inText() const { return in_text_; }
383 void setTextMode(bool t) { in_text_ = t; }
393 odocstringstream deferred_;
395 friend class SetHTMLMode;
399 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
401 HtmlStream & operator<<(HtmlStream &, MathData const &);
403 HtmlStream & operator<<(HtmlStream &, docstring const &);
405 HtmlStream & operator<<(HtmlStream &, char const *);
407 HtmlStream & operator<<(HtmlStream &, char);
409 HtmlStream & operator<<(HtmlStream &, char_type);
411 HtmlStream & operator<<(HtmlStream &, MTag const &);
413 HtmlStream & operator<<(HtmlStream &, ETag const &);
419 explicit SetHTMLMode(HtmlStream & os, bool text);
437 explicit NormalStream(odocstream & os) : os_(os) {}
439 odocstream & os() { return os_; }
446 NormalStream & operator<<(NormalStream &, MathAtom const &);
448 NormalStream & operator<<(NormalStream &, MathData const &);
450 NormalStream & operator<<(NormalStream &, docstring const &);
452 NormalStream & operator<<(NormalStream &, char const *);
454 NormalStream & operator<<(NormalStream &, char);
456 NormalStream & operator<<(NormalStream &, int);
467 explicit MapleStream(odocstream & os) : os_(os) {}
469 odocstream & os() { return os_; }
477 MapleStream & operator<<(MapleStream &, MathAtom const &);
479 MapleStream & operator<<(MapleStream &, MathData const &);
481 MapleStream & operator<<(MapleStream &, docstring const &);
483 MapleStream & operator<<(MapleStream &, char_type);
485 MapleStream & operator<<(MapleStream &, char const *);
487 MapleStream & operator<<(MapleStream &, char);
489 MapleStream & operator<<(MapleStream &, int);
500 explicit MaximaStream(odocstream & os) : os_(os) {}
502 odocstream & os() { return os_; }
510 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
512 MaximaStream & operator<<(MaximaStream &, MathData const &);
514 MaximaStream & operator<<(MaximaStream &, docstring const &);
516 MaximaStream & operator<<(MaximaStream &, char_type);
518 MaximaStream & operator<<(MaximaStream &, char const *);
520 MaximaStream & operator<<(MaximaStream &, char);
522 MaximaStream & operator<<(MaximaStream &, int);
530 class MathematicaStream {
533 explicit MathematicaStream(odocstream & os) : os_(os) {}
535 odocstream & os() { return os_; }
543 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
545 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
547 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
549 MathematicaStream & operator<<(MathematicaStream &, char const *);
551 MathematicaStream & operator<<(MathematicaStream &, char);
553 MathematicaStream & operator<<(MathematicaStream &, int);
564 explicit OctaveStream(odocstream & os) : os_(os) {}
566 odocstream & os() { return os_; }
573 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
575 OctaveStream & operator<<(OctaveStream &, MathData const &);
577 OctaveStream & operator<<(OctaveStream &, docstring const &);
579 OctaveStream & operator<<(OctaveStream &, char_type);
581 OctaveStream & operator<<(OctaveStream &, char const *);
583 OctaveStream & operator<<(OctaveStream &, char);
585 OctaveStream & operator<<(OctaveStream &, int);