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"
36 WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun,
37 Encoding const * encoding = 0);
39 explicit WriteStream(odocstream & os);
43 int line() const { return line_; }
45 bool fragile() const { return fragile_; }
47 bool latex() const { return latex_; }
49 bool dryrun() const { return dryrun_; }
51 odocstream & os() { return os_; }
53 bool & firstitem() { return firstitem_; }
55 void addlines(unsigned int);
56 /// writes space if next thing is isalpha()
57 void pendingSpace(bool how);
58 /// writes space if next thing is isalpha()
59 bool pendingSpace() const { return pendingspace_; }
60 /// tell whether to write the closing brace of \ensuremath
61 void pendingBrace(bool brace);
62 /// tell whether to write the closing brace of \ensuremath
63 bool pendingBrace() const { return pendingbrace_; }
64 /// tell whether we are in text mode or not when producing latex code
65 void textMode(bool textmode);
66 /// tell whether we are in text mode or not when producing latex code
67 bool textMode() const { return textmode_; }
69 Encoding const * encoding() const { return encoding_; }
73 /// do we have to write \\protect sometimes
75 /// are we at the beginning of an MathData?
77 /// are we writing to .tex?
79 /// is it for preview?
81 /// do we have a space pending?
83 /// do we have a brace pending?
85 /// are we in text mode when producing latex code?
90 Encoding const * encoding_;
94 WriteStream & operator<<(WriteStream &, MathAtom const &);
96 WriteStream & operator<<(WriteStream &, MathData const &);
98 WriteStream & operator<<(WriteStream &, docstring const &);
100 WriteStream & operator<<(WriteStream &, char const * const);
102 WriteStream & operator<<(WriteStream &, char);
104 WriteStream & operator<<(WriteStream &, int);
106 WriteStream & operator<<(WriteStream &, unsigned int);
108 /// ensure math mode, possibly by opening \ensuremath
109 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
111 /// ensure the requested mode, possibly by closing \ensuremath
112 bool ensureMode(WriteStream & os, InsetMath::mode_type mode);
116 * MathEnsurer - utility class for ensuring math mode
118 * A local variable of this type can be used to either ensure math mode
119 * or delay the writing of a pending brace when outputting LaTeX.
123 * MathEnsurer ensurer(os);
125 * If not already in math mode, inserts an \ensuremath command followed
126 * by an open brace. This brace will be automatically closed when exiting
127 * math mode. Math mode is automatically exited when writing something
128 * that doesn't explicitly require math mode.
132 * MathEnsurer ensurer(os, false);
134 * Simply suspend writing a closing brace until the end of ensurer's scope.
138 * MathEnsurer ensurer(os, needs_math_mode, true);
140 * The third parameter is set to true only for a user defined macro, which
141 * needs special handling. When it is a MathMacro, the needs_math_mode
142 * parameter is true and the behavior is as in Example 1. When the
143 * needs_math_mode parameter is false (not a MathMacro) and the macro
144 * was entered in a text box and we are in math mode, the mode is reset
145 * to text. This is because the macro was probably designed for text mode
146 * (given that it was entered in text mode and we have no way to tell the
153 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
154 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
156 ~MathEnsurer() { os_.pendingBrace(brace_); }
166 * ModeSpecifier - utility class for specifying a given mode (math or text)
168 * A local variable of this type can be used to specify that a command or
169 * environment works in a given mode. For example, \mbox works in text
170 * mode, but \boxed works in math mode. Note that no mode changing commands
171 * are needed, but we have to track the current mode, hence this class.
175 * ModeSpecifier specifier(os, TEXT_MODE);
177 * Sets the current mode to text mode.
179 * At the end of specifier's scope the mode is reset to its previous value.
185 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode)
186 : os_(os), textmode_(ensureMode(os, mode)) {}
188 ~ModeSpecifier() { os_.textMode(textmode_); }
205 MTag(char const * const tag) : tag_(tag) {}
207 char const * const tag_;
213 ETag(char const * const tag) : tag_(tag) {}
215 char const * const tag_;
221 explicit MathStream(odocstream & os);
225 odocstream & os() { return os_; }
227 int line() const { return line_; }
229 int & tab() { return tab_; }
231 friend MathStream & operator<<(MathStream &, char const *);
244 MathStream & operator<<(MathStream &, MathAtom const &);
246 MathStream & operator<<(MathStream &, MathData const &);
248 MathStream & operator<<(MathStream &, docstring const &);
250 MathStream & operator<<(MathStream &, char const *);
252 MathStream & operator<<(MathStream &, char);
254 MathStream & operator<<(MathStream &, MTag const &);
256 MathStream & operator<<(MathStream &, ETag const &);
267 explicit NormalStream(odocstream & os) : os_(os) {}
269 odocstream & os() { return os_; }
276 NormalStream & operator<<(NormalStream &, MathAtom const &);
278 NormalStream & operator<<(NormalStream &, MathData const &);
280 NormalStream & operator<<(NormalStream &, docstring const &);
282 NormalStream & operator<<(NormalStream &, char const *);
284 NormalStream & operator<<(NormalStream &, char);
286 NormalStream & operator<<(NormalStream &, int);
297 explicit MapleStream(odocstream & os) : os_(os) {}
299 odocstream & os() { return os_; }
307 MapleStream & operator<<(MapleStream &, MathAtom const &);
309 MapleStream & operator<<(MapleStream &, MathData const &);
311 MapleStream & operator<<(MapleStream &, docstring const &);
313 MapleStream & operator<<(MapleStream &, char_type);
315 MapleStream & operator<<(MapleStream &, char const *);
317 MapleStream & operator<<(MapleStream &, char);
319 MapleStream & operator<<(MapleStream &, int);
330 explicit MaximaStream(odocstream & os) : os_(os) {}
332 odocstream & os() { return os_; }
340 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
342 MaximaStream & operator<<(MaximaStream &, MathData const &);
344 MaximaStream & operator<<(MaximaStream &, docstring const &);
346 MaximaStream & operator<<(MaximaStream &, char_type);
348 MaximaStream & operator<<(MaximaStream &, char const *);
350 MaximaStream & operator<<(MaximaStream &, char);
352 MaximaStream & operator<<(MaximaStream &, int);
360 class MathematicaStream {
363 explicit MathematicaStream(odocstream & os) : os_(os) {}
365 odocstream & os() { return os_; }
373 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
375 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
377 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
379 MathematicaStream & operator<<(MathematicaStream &, char const *);
381 MathematicaStream & operator<<(MathematicaStream &, char);
383 MathematicaStream & operator<<(MathematicaStream &, int);
394 explicit OctaveStream(odocstream & os) : os_(os) {}
396 odocstream & os() { return os_; }
403 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
405 OctaveStream & operator<<(OctaveStream &, MathData const &);
407 OctaveStream & operator<<(OctaveStream &, docstring const &);
409 OctaveStream & operator<<(OctaveStream &, char_type);
411 OctaveStream & operator<<(OctaveStream &, char const *);
413 OctaveStream & operator<<(OctaveStream &, char);
415 OctaveStream & operator<<(OctaveStream &, int);