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"
42 WriteStream(odocstream
& os
, bool fragile
, bool latex
, OutputType output
,
43 Encoding
const * encoding
= 0);
45 explicit WriteStream(odocstream
& os
);
49 int line() const { return line_
; }
51 bool fragile() const { return fragile_
; }
53 bool latex() const { return latex_
; }
55 OutputType
output() const { return output_
; }
57 odocstream
& os() { return os_
; }
59 bool & firstitem() { return firstitem_
; }
61 void addlines(unsigned int);
62 /// writes space if next thing is isalpha()
63 void pendingSpace(bool how
);
64 /// writes space if next thing is isalpha()
65 bool pendingSpace() const { return pendingspace_
; }
66 /// tell whether to write the closing brace of \ensuremath
67 void pendingBrace(bool brace
);
68 /// tell whether to write the closing brace of \ensuremath
69 bool pendingBrace() const { return pendingbrace_
; }
70 /// tell whether we are in text mode or not when producing latex code
71 void textMode(bool textmode
);
72 /// tell whether we are in text mode or not when producing latex code
73 bool textMode() const { return textmode_
; }
74 /// tell whether we are allowed to switch mode when producing latex code
75 void lockedMode(bool locked
);
76 /// tell whether we are allowed to switch mode when producing latex code
77 bool lockedMode() const { return locked_
; }
79 Encoding
const * encoding() const { return encoding_
; }
83 /// do we have to write \\protect sometimes
85 /// are we at the beginning of an MathData?
87 /// are we writing to .tex?
89 /// output type (default, source preview, instant preview)?
91 /// do we have a space pending?
93 /// do we have a brace pending?
95 /// are we in text mode when producing latex code?
97 /// are we allowed to switch mode when producing latex code?
102 Encoding
const * encoding_
;
106 WriteStream
& operator<<(WriteStream
&, MathAtom
const &);
108 WriteStream
& operator<<(WriteStream
&, MathData
const &);
110 WriteStream
& operator<<(WriteStream
&, docstring
const &);
112 WriteStream
& operator<<(WriteStream
&, char const * const);
114 WriteStream
& operator<<(WriteStream
&, char);
116 WriteStream
& operator<<(WriteStream
&, int);
118 WriteStream
& operator<<(WriteStream
&, unsigned int);
120 /// ensure math mode, possibly by opening \ensuremath
121 bool ensureMath(WriteStream
& os
, bool needs_math_mode
= true, bool macro
= false);
123 /// ensure the requested mode, possibly by closing \ensuremath
124 int ensureMode(WriteStream
& os
, InsetMath::mode_type mode
, bool locked
);
128 * MathEnsurer - utility class for ensuring math mode
130 * A local variable of this type can be used to either ensure math mode
131 * or delay the writing of a pending brace when outputting LaTeX.
135 * MathEnsurer ensurer(os);
137 * If not already in math mode, inserts an \ensuremath command followed
138 * by an open brace. This brace will be automatically closed when exiting
139 * math mode. Math mode is automatically exited when writing something
140 * that doesn't explicitly require math mode.
144 * MathEnsurer ensurer(os, false);
146 * Simply suspend writing a closing brace until the end of ensurer's scope.
150 * MathEnsurer ensurer(os, needs_math_mode, true);
152 * The third parameter is set to true only for a user defined macro, which
153 * needs special handling. When it is a MathMacro, the needs_math_mode
154 * parameter is true and the behavior is as in Example 1. When the
155 * needs_math_mode parameter is false (not a MathMacro) and the macro
156 * was entered in a text box and we are in math mode, the mode is reset
157 * to text. This is because the macro was probably designed for text mode
158 * (given that it was entered in text mode and we have no way to tell the
165 explicit MathEnsurer(WriteStream
& os
, bool needs_math_mode
= true, bool macro
= false)
166 : os_(os
), brace_(ensureMath(os
, needs_math_mode
, macro
)) {}
168 ~MathEnsurer() { os_
.pendingBrace(brace_
); }
178 * ModeSpecifier - utility class for specifying a given mode (math or text)
180 * A local variable of this type can be used to specify that a command or
181 * environment works in a given mode. For example, \mbox works in text
182 * mode, but \boxed works in math mode. Note that no mode changing commands
183 * are needed, but we have to track the current mode, hence this class.
184 * This is only used when exporting to latex and helps determining whether
185 * the mode needs being temporarily switched when a command would not work
186 * in the current mode. As there are cases where this switching is to be
187 * avoided, the optional third parameter can be used to lock the mode.
191 * ModeSpecifier specifier(os, TEXT_MODE);
193 * Sets the current mode to text mode and allows mode switching.
197 * ModeSpecifier specifier(os, TEXT_MODE, true);
199 * Sets the current mode to text mode and disallows mode switching.
201 * At the end of specifier's scope the mode is reset to its previous value.
207 explicit ModeSpecifier(WriteStream
& os
, InsetMath::mode_type mode
,
209 : os_(os
), oldmodes_(ensureMode(os
, mode
, locked
)) {}
213 os_
.textMode(oldmodes_
& 1);
214 os_
.lockedMode(oldmodes_
& 2);
232 MTag(char const * const tag
) : tag_(tag
) {}
234 char const * const tag_
;
240 ETag(char const * const tag
) : tag_(tag
) {}
242 char const * const tag_
;
248 explicit MathStream(odocstream
& os
);
252 odocstream
& os() { return os_
; }
254 int line() const { return line_
; }
256 int & tab() { return tab_
; }
258 friend MathStream
& operator<<(MathStream
&, char const *);
271 MathStream
& operator<<(MathStream
&, MathAtom
const &);
273 MathStream
& operator<<(MathStream
&, MathData
const &);
275 MathStream
& operator<<(MathStream
&, docstring
const &);
277 MathStream
& operator<<(MathStream
&, char const *);
279 MathStream
& operator<<(MathStream
&, char);
281 MathStream
& operator<<(MathStream
&, MTag
const &);
283 MathStream
& operator<<(MathStream
&, ETag
const &);
294 explicit NormalStream(odocstream
& os
) : os_(os
) {}
296 odocstream
& os() { return os_
; }
303 NormalStream
& operator<<(NormalStream
&, MathAtom
const &);
305 NormalStream
& operator<<(NormalStream
&, MathData
const &);
307 NormalStream
& operator<<(NormalStream
&, docstring
const &);
309 NormalStream
& operator<<(NormalStream
&, char const *);
311 NormalStream
& operator<<(NormalStream
&, char);
313 NormalStream
& operator<<(NormalStream
&, int);
324 explicit MapleStream(odocstream
& os
) : os_(os
) {}
326 odocstream
& os() { return os_
; }
334 MapleStream
& operator<<(MapleStream
&, MathAtom
const &);
336 MapleStream
& operator<<(MapleStream
&, MathData
const &);
338 MapleStream
& operator<<(MapleStream
&, docstring
const &);
340 MapleStream
& operator<<(MapleStream
&, char_type
);
342 MapleStream
& operator<<(MapleStream
&, char const *);
344 MapleStream
& operator<<(MapleStream
&, char);
346 MapleStream
& operator<<(MapleStream
&, int);
357 explicit MaximaStream(odocstream
& os
) : os_(os
) {}
359 odocstream
& os() { return os_
; }
367 MaximaStream
& operator<<(MaximaStream
&, MathAtom
const &);
369 MaximaStream
& operator<<(MaximaStream
&, MathData
const &);
371 MaximaStream
& operator<<(MaximaStream
&, docstring
const &);
373 MaximaStream
& operator<<(MaximaStream
&, char_type
);
375 MaximaStream
& operator<<(MaximaStream
&, char const *);
377 MaximaStream
& operator<<(MaximaStream
&, char);
379 MaximaStream
& operator<<(MaximaStream
&, int);
387 class MathematicaStream
{
390 explicit MathematicaStream(odocstream
& os
) : os_(os
) {}
392 odocstream
& os() { return os_
; }
400 MathematicaStream
& operator<<(MathematicaStream
&, MathAtom
const &);
402 MathematicaStream
& operator<<(MathematicaStream
&, MathData
const &);
404 MathematicaStream
& operator<<(MathematicaStream
&, docstring
const &);
406 MathematicaStream
& operator<<(MathematicaStream
&, char const *);
408 MathematicaStream
& operator<<(MathematicaStream
&, char);
410 MathematicaStream
& operator<<(MathematicaStream
&, int);
421 explicit OctaveStream(odocstream
& os
) : os_(os
) {}
423 odocstream
& os() { return os_
; }
430 OctaveStream
& operator<<(OctaveStream
&, MathAtom
const &);
432 OctaveStream
& operator<<(OctaveStream
&, MathData
const &);
434 OctaveStream
& operator<<(OctaveStream
&, docstring
const &);
436 OctaveStream
& operator<<(OctaveStream
&, char_type
);
438 OctaveStream
& operator<<(OctaveStream
&, char const *);
440 OctaveStream
& operator<<(OctaveStream
&, char);
442 OctaveStream
& operator<<(OctaveStream
&, int);