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_; }
68 /// tell whether we are allowed to switch mode when producing latex code
69 void lockedMode(bool locked);
70 /// tell whether we are allowed to switch mode when producing latex code
71 bool lockedMode() const { return locked_; }
73 Encoding const * encoding() const { return encoding_; }
77 /// do we have to write \\protect sometimes
79 /// are we at the beginning of an MathData?
81 /// are we writing to .tex?
83 /// is it for preview?
85 /// do we have a space pending?
87 /// do we have a brace pending?
89 /// are we in text mode when producing latex code?
91 /// are we allowed to switch mode when producing latex code?
96 Encoding const * encoding_;
100 WriteStream & operator<<(WriteStream &, MathAtom const &);
102 WriteStream & operator<<(WriteStream &, MathData const &);
104 WriteStream & operator<<(WriteStream &, docstring const &);
106 WriteStream & operator<<(WriteStream &, char const * const);
108 WriteStream & operator<<(WriteStream &, char);
110 WriteStream & operator<<(WriteStream &, int);
112 WriteStream & operator<<(WriteStream &, unsigned int);
114 /// ensure math mode, possibly by opening \ensuremath
115 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
117 /// ensure the requested mode, possibly by closing \ensuremath
118 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked);
122 * MathEnsurer - utility class for ensuring math mode
124 * A local variable of this type can be used to either ensure math mode
125 * or delay the writing of a pending brace when outputting LaTeX.
129 * MathEnsurer ensurer(os);
131 * If not already in math mode, inserts an \ensuremath command followed
132 * by an open brace. This brace will be automatically closed when exiting
133 * math mode. Math mode is automatically exited when writing something
134 * that doesn't explicitly require math mode.
138 * MathEnsurer ensurer(os, false);
140 * Simply suspend writing a closing brace until the end of ensurer's scope.
144 * MathEnsurer ensurer(os, needs_math_mode, true);
146 * The third parameter is set to true only for a user defined macro, which
147 * needs special handling. When it is a MathMacro, the needs_math_mode
148 * parameter is true and the behavior is as in Example 1. When the
149 * needs_math_mode parameter is false (not a MathMacro) and the macro
150 * was entered in a text box and we are in math mode, the mode is reset
151 * to text. This is because the macro was probably designed for text mode
152 * (given that it was entered in text mode and we have no way to tell the
159 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
160 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
162 ~MathEnsurer() { os_.pendingBrace(brace_); }
172 * ModeSpecifier - utility class for specifying a given mode (math or text)
174 * A local variable of this type can be used to specify that a command or
175 * environment works in a given mode. For example, \mbox works in text
176 * mode, but \boxed works in math mode. Note that no mode changing commands
177 * are needed, but we have to track the current mode, hence this class.
178 * This is only used when exporting to latex and helps determining whether
179 * the mode needs being temporarily switched when a command would not work
180 * in the current mode. As there are cases where this switching is to be
181 * avoided, the optional third parameter can be used to lock the mode.
185 * ModeSpecifier specifier(os, TEXT_MODE);
187 * Sets the current mode to text mode and allows mode switching.
191 * ModeSpecifier specifier(os, TEXT_MODE, true);
193 * Sets the current mode to text mode and disallows mode switching.
195 * At the end of specifier's scope the mode is reset to its previous value.
201 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
203 : os_(os), oldmodes_(ensureMode(os, mode, locked)) {}
207 os_.textMode(oldmodes_ & 1);
208 os_.lockedMode(oldmodes_ & 2);
226 MTag(char const * const tag) : tag_(tag) {}
228 char const * const tag_;
234 ETag(char const * const tag) : tag_(tag) {}
236 char const * const tag_;
242 explicit MathStream(odocstream & os);
246 odocstream & os() { return os_; }
248 int line() const { return line_; }
250 int & tab() { return tab_; }
252 friend MathStream & operator<<(MathStream &, char const *);
265 MathStream & operator<<(MathStream &, MathAtom const &);
267 MathStream & operator<<(MathStream &, MathData const &);
269 MathStream & operator<<(MathStream &, docstring const &);
271 MathStream & operator<<(MathStream &, char const *);
273 MathStream & operator<<(MathStream &, char);
275 MathStream & operator<<(MathStream &, MTag const &);
277 MathStream & operator<<(MathStream &, ETag const &);
288 explicit NormalStream(odocstream & os) : os_(os) {}
290 odocstream & os() { return os_; }
297 NormalStream & operator<<(NormalStream &, MathAtom const &);
299 NormalStream & operator<<(NormalStream &, MathData const &);
301 NormalStream & operator<<(NormalStream &, docstring const &);
303 NormalStream & operator<<(NormalStream &, char const *);
305 NormalStream & operator<<(NormalStream &, char);
307 NormalStream & operator<<(NormalStream &, int);
318 explicit MapleStream(odocstream & os) : os_(os) {}
320 odocstream & os() { return os_; }
328 MapleStream & operator<<(MapleStream &, MathAtom const &);
330 MapleStream & operator<<(MapleStream &, MathData const &);
332 MapleStream & operator<<(MapleStream &, docstring const &);
334 MapleStream & operator<<(MapleStream &, char_type);
336 MapleStream & operator<<(MapleStream &, char const *);
338 MapleStream & operator<<(MapleStream &, char);
340 MapleStream & operator<<(MapleStream &, int);
351 explicit MaximaStream(odocstream & os) : os_(os) {}
353 odocstream & os() { return os_; }
361 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
363 MaximaStream & operator<<(MaximaStream &, MathData const &);
365 MaximaStream & operator<<(MaximaStream &, docstring const &);
367 MaximaStream & operator<<(MaximaStream &, char_type);
369 MaximaStream & operator<<(MaximaStream &, char const *);
371 MaximaStream & operator<<(MaximaStream &, char);
373 MaximaStream & operator<<(MaximaStream &, int);
381 class MathematicaStream {
384 explicit MathematicaStream(odocstream & os) : os_(os) {}
386 odocstream & os() { return os_; }
394 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
396 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
398 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
400 MathematicaStream & operator<<(MathematicaStream &, char const *);
402 MathematicaStream & operator<<(MathematicaStream &, char);
404 MathematicaStream & operator<<(MathematicaStream &, int);
415 explicit OctaveStream(odocstream & os) : os_(os) {}
417 odocstream & os() { return os_; }
424 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
426 OctaveStream & operator<<(OctaveStream &, MathData const &);
428 OctaveStream & operator<<(OctaveStream &, docstring const &);
430 OctaveStream & operator<<(OctaveStream &, char_type);
432 OctaveStream & operator<<(OctaveStream &, char const *);
434 OctaveStream & operator<<(OctaveStream &, char);
436 OctaveStream & operator<<(OctaveStream &, int);