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 "support/strfwd.h"
17 #include "InsetMath.h"
18 // FIXME: Move to individual insets
19 #include "MetricsInfo.h"
35 WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun);
37 explicit WriteStream(odocstream & os);
41 int line() const { return line_; }
43 bool fragile() const { return fragile_; }
45 bool latex() const { return latex_; }
47 bool dryrun() const { return dryrun_; }
49 odocstream & os() { return os_; }
51 bool & firstitem() { return firstitem_; }
53 void addlines(unsigned int);
54 /// writes space if next thing is isalpha()
55 void pendingSpace(bool how);
56 /// writes space if next thing is isalpha()
57 bool pendingSpace() const { return pendingspace_; }
58 /// tell whether to write the closing brace of \ensuremath
59 void pendingBrace(bool brace);
60 /// tell whether to write the closing brace of \ensuremath
61 bool pendingBrace() const { return pendingbrace_; }
62 /// tell whether we are in text mode or not when producing latex code
63 void textMode(bool textmode);
64 /// tell whether we are in text mode or not when producing latex code
65 bool textMode() const { return textmode_; }
69 /// do we have to write \\protect sometimes
71 /// are we at the beginning of an MathData?
73 /// are we writing to .tex?
75 /// is it for preview?
77 /// do we have a space pending?
79 /// do we have a brace pending?
81 /// are we in text mode when producing latex code?
88 WriteStream & operator<<(WriteStream &, MathAtom const &);
90 WriteStream & operator<<(WriteStream &, MathData const &);
92 WriteStream & operator<<(WriteStream &, docstring const &);
94 WriteStream & operator<<(WriteStream &, char const * const);
96 WriteStream & operator<<(WriteStream &, char);
98 WriteStream & operator<<(WriteStream &, int);
100 WriteStream & operator<<(WriteStream &, unsigned int);
102 /// ensure math mode, possibly by opening \ensuremath
103 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
105 /// ensure the requested mode, possibly by closing \ensuremath
106 bool ensureMode(WriteStream & os, InsetMath::mode_type mode);
110 * MathEnsurer - utility class for ensuring math mode
112 * A local variable of this type can be used to either ensure math mode
113 * or delay the writing of a pending brace when outputting LaTeX.
117 * MathEnsurer ensurer(os);
119 * If not already in math mode, inserts an \ensuremath command followed
120 * by an open brace. This brace will be automatically closed when exiting
121 * math mode. Math mode is automatically exited when writing something
122 * that doesn't explicitly require math mode.
126 * MathEnsurer ensurer(os, false);
128 * Simply suspend writing a closing brace until the end of ensurer's scope.
132 * MathEnsurer ensurer(os, needs_math_mode, true);
134 * The third parameter is set to true only for a user defined macro, which
135 * needs special handling. When it is a MathMacro, the needs_math_mode
136 * parameter is true and the behavior is as in Example 1. When the
137 * needs_math_mode parameter is false (not a MathMacro) and the macro
138 * was entered in a text box and we are in math mode, the mode is reset
139 * to text. This is because the macro was probably designed for text mode
140 * (given that it was entered in text mode and we have no way to tell the
147 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
148 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
150 ~MathEnsurer() { os_.pendingBrace(brace_); }
160 * ModeSpecifier - utility class for specifying a given mode (math or text)
162 * A local variable of this type can be used to specify that a command or
163 * environment works in a given mode. For example, \mbox works in text
164 * mode, but \boxed works in math mode. Note that no mode changing commands
165 * are needed, but we have to track the current mode, hence this class.
169 * ModeSpecifier specifier(os, TEXT_MODE);
171 * Sets the current mode to text mode.
173 * At the end of specifier's scope the mode is reset to its previous value.
179 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode)
180 : os_(os), textmode_(ensureMode(os, mode)) {}
182 ~ModeSpecifier() { os_.textMode(textmode_); }
199 MTag(char const * const tag) : tag_(tag) {}
201 char const * const tag_;
207 ETag(char const * const tag) : tag_(tag) {}
209 char const * const tag_;
215 explicit MathStream(odocstream & os);
219 odocstream & os() { return os_; }
221 int line() const { return line_; }
223 int & tab() { return tab_; }
225 friend MathStream & operator<<(MathStream &, char const *);
238 MathStream & operator<<(MathStream &, MathAtom const &);
240 MathStream & operator<<(MathStream &, MathData const &);
242 MathStream & operator<<(MathStream &, docstring const &);
244 MathStream & operator<<(MathStream &, char const *);
246 MathStream & operator<<(MathStream &, char);
248 MathStream & operator<<(MathStream &, MTag const &);
250 MathStream & operator<<(MathStream &, ETag const &);
261 explicit NormalStream(odocstream & os) : os_(os) {}
263 odocstream & os() { return os_; }
270 NormalStream & operator<<(NormalStream &, MathAtom const &);
272 NormalStream & operator<<(NormalStream &, MathData const &);
274 NormalStream & operator<<(NormalStream &, docstring const &);
276 NormalStream & operator<<(NormalStream &, char const *);
278 NormalStream & operator<<(NormalStream &, char);
280 NormalStream & operator<<(NormalStream &, int);
291 explicit MapleStream(odocstream & os) : os_(os) {}
293 odocstream & os() { return os_; }
301 MapleStream & operator<<(MapleStream &, MathAtom const &);
303 MapleStream & operator<<(MapleStream &, MathData const &);
305 MapleStream & operator<<(MapleStream &, docstring const &);
307 MapleStream & operator<<(MapleStream &, char_type);
309 MapleStream & operator<<(MapleStream &, char const *);
311 MapleStream & operator<<(MapleStream &, char);
313 MapleStream & operator<<(MapleStream &, int);
324 explicit MaximaStream(odocstream & os) : os_(os) {}
326 odocstream & os() { return os_; }
334 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
336 MaximaStream & operator<<(MaximaStream &, MathData const &);
338 MaximaStream & operator<<(MaximaStream &, docstring const &);
340 MaximaStream & operator<<(MaximaStream &, char_type);
342 MaximaStream & operator<<(MaximaStream &, char const *);
344 MaximaStream & operator<<(MaximaStream &, char);
346 MaximaStream & operator<<(MaximaStream &, int);
354 class MathematicaStream {
357 explicit MathematicaStream(odocstream & os) : os_(os) {}
359 odocstream & os() { return os_; }
367 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
369 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
371 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
373 MathematicaStream & operator<<(MathematicaStream &, char const *);
375 MathematicaStream & operator<<(MathematicaStream &, char);
377 MathematicaStream & operator<<(MathematicaStream &, int);
388 explicit OctaveStream(odocstream & os) : os_(os) {}
390 odocstream & os() { return os_; }
397 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
399 OctaveStream & operator<<(OctaveStream &, MathData const &);
401 OctaveStream & operator<<(OctaveStream &, docstring const &);
403 OctaveStream & operator<<(OctaveStream &, char_type);
405 OctaveStream & operator<<(OctaveStream &, char const *);
407 OctaveStream & operator<<(OctaveStream &, char);
409 OctaveStream & operator<<(OctaveStream &, int);