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 /// record whether we can write an immediately following newline char
63 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
64 /// tell whether we can write an immediately following newline char
65 bool canBreakLine() const { return canbreakline_; }
66 /// writes space if next thing is isalpha()
67 void pendingSpace(bool how);
68 /// writes space if next thing is isalpha()
69 bool pendingSpace() const { return pendingspace_; }
70 /// tell whether to write the closing brace of \ensuremath
71 void pendingBrace(bool brace);
72 /// tell whether to write the closing brace of \ensuremath
73 bool pendingBrace() const { return pendingbrace_; }
74 /// tell whether we are in text mode or not when producing latex code
75 void textMode(bool textmode);
76 /// tell whether we are in text mode or not when producing latex code
77 bool textMode() const { return textmode_; }
78 /// tell whether we are allowed to switch mode when producing latex code
79 void lockedMode(bool locked);
80 /// tell whether we are allowed to switch mode when producing latex code
81 bool lockedMode() const { return locked_; }
82 /// tell whether to use only ascii chars when producing latex code
83 void asciiOnly(bool ascii);
84 /// tell whether to use only ascii chars when producing latex code
85 bool asciiOnly() const { return ascii_; }
87 Encoding const * encoding() const { return encoding_; }
91 /// do we have to write \\protect sometimes
93 /// are we at the beginning of an MathData?
95 /// are we writing to .tex?
97 /// output type (default, source preview, instant preview)?
99 /// do we have a space pending?
101 /// do we have a brace pending?
103 /// are we in text mode when producing latex code?
105 /// are we allowed to switch mode when producing latex code?
107 /// should we use only ascii chars when producing latex code?
109 /// are we allowed to output an immediately following newline?
114 Encoding const * encoding_;
118 WriteStream & operator<<(WriteStream &, MathAtom const &);
120 WriteStream & operator<<(WriteStream &, MathData const &);
122 WriteStream & operator<<(WriteStream &, docstring const &);
124 WriteStream & operator<<(WriteStream &, char const * const);
126 WriteStream & operator<<(WriteStream &, char);
128 WriteStream & operator<<(WriteStream &, int);
130 WriteStream & operator<<(WriteStream &, unsigned int);
132 /// ensure math mode, possibly by opening \ensuremath
133 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
135 /// ensure the requested mode, possibly by closing \ensuremath
136 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
140 * MathEnsurer - utility class for ensuring math mode
142 * A local variable of this type can be used to either ensure math mode
143 * or delay the writing of a pending brace when outputting LaTeX.
147 * MathEnsurer ensurer(os);
149 * If not already in math mode, inserts an \ensuremath command followed
150 * by an open brace. This brace will be automatically closed when exiting
151 * math mode. Math mode is automatically exited when writing something
152 * that doesn't explicitly require math mode.
156 * MathEnsurer ensurer(os, false);
158 * Simply suspend writing a closing brace until the end of ensurer's scope.
162 * MathEnsurer ensurer(os, needs_math_mode, true);
164 * The third parameter is set to true only for a user defined macro, which
165 * needs special handling. When it is a MathMacro, the needs_math_mode
166 * parameter is true and the behavior is as in Example 1. When the
167 * needs_math_mode parameter is false (not a MathMacro) and the macro
168 * was entered in a text box and we are in math mode, the mode is reset
169 * to text. This is because the macro was probably designed for text mode
170 * (given that it was entered in text mode and we have no way to tell the
177 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
178 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
180 ~MathEnsurer() { os_.pendingBrace(brace_); }
190 * ModeSpecifier - utility class for specifying a given mode (math or text)
192 * A local variable of this type can be used to specify that a command or
193 * environment works in a given mode. For example, \mbox works in text
194 * mode, but \boxed works in math mode. Note that no mode changing commands
195 * are needed, but we have to track the current mode, hence this class.
196 * This is only used when exporting to latex and helps determining whether
197 * the mode needs being temporarily switched when a command would not work
198 * in the current mode. As there are cases where this switching is to be
199 * avoided, the optional third parameter can be used to lock the mode.
200 * When the mode is locked, the optional fourth parameter specifies whether
201 * strings are to be output by using a suitable ascii representation.
205 * ModeSpecifier specifier(os, TEXT_MODE);
207 * Sets the current mode to text mode and allows mode switching.
211 * ModeSpecifier specifier(os, TEXT_MODE, true);
213 * Sets the current mode to text mode and disallows mode switching.
217 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
219 * Sets the current mode to text mode, disallows mode switching, and outputs
220 * strings as ascii only.
222 * At the end of specifier's scope the mode is reset to its previous value.
228 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
229 bool locked = false, bool ascii = false)
230 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
234 os_.textMode(oldmodes_ & 0x01);
235 os_.lockedMode(oldmodes_ & 0x02);
236 os_.asciiOnly(oldmodes_ & 0x04);
254 MTag(char const * const tag, std::string attr = "")
255 : tag_(tag), attr_(attr) {}
257 char const * const tag_;
265 ETag(char const * const tag) : tag_(tag) {}
267 char const * const tag_;
271 /// Throw MathExportException to signal that the attempt to export
272 /// some math in the current format did not succeed. E.g., we can't
273 /// export xymatrix as MathML, so that will throw, and we'll fall back
275 class MathExportException : public std::exception {};
281 explicit MathStream(odocstream & os);
285 odocstream & os() { return os_; }
287 int line() const { return line_; }
289 int & tab() { return tab_; }
291 friend MathStream & operator<<(MathStream &, char const *);
293 void defer(docstring const &);
295 void defer(std::string const &);
297 docstring deferred() const;
299 bool inText() const { return in_text_; }
302 void setTextMode(bool t) { in_text_ = t; }
312 odocstringstream deferred_;
314 friend class SetMode;
318 MathStream & operator<<(MathStream &, MathAtom const &);
320 MathStream & operator<<(MathStream &, MathData const &);
322 MathStream & operator<<(MathStream &, docstring const &);
324 MathStream & operator<<(MathStream &, char const *);
326 MathStream & operator<<(MathStream &, char);
328 MathStream & operator<<(MathStream &, char_type);
330 MathStream & operator<<(MathStream &, MTag const &);
332 MathStream & operator<<(MathStream &, ETag const &);
335 /// A simpler version of ModeSpecifier, for MathML
339 explicit SetMode(MathStream & os, bool text);
353 explicit HtmlStream(odocstream & os);
357 odocstream & os() { return os_; }
359 int line() const { return line_; }
361 int & tab() { return tab_; }
363 friend HtmlStream & operator<<(HtmlStream &, char const *);
365 void defer(docstring const &);
367 void defer(std::string const &);
369 docstring deferred() const;
371 bool inText() const { return in_text_; }
374 void setTextMode(bool t) { in_text_ = t; }
384 odocstringstream deferred_;
386 friend class SetHTMLMode;
390 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
392 HtmlStream & operator<<(HtmlStream &, MathData const &);
394 HtmlStream & operator<<(HtmlStream &, docstring const &);
396 HtmlStream & operator<<(HtmlStream &, char const *);
398 HtmlStream & operator<<(HtmlStream &, char);
400 HtmlStream & operator<<(HtmlStream &, char_type);
402 HtmlStream & operator<<(HtmlStream &, MTag const &);
404 HtmlStream & operator<<(HtmlStream &, ETag const &);
410 explicit SetHTMLMode(HtmlStream & os, bool text);
428 explicit NormalStream(odocstream & os) : os_(os) {}
430 odocstream & os() { return os_; }
437 NormalStream & operator<<(NormalStream &, MathAtom const &);
439 NormalStream & operator<<(NormalStream &, MathData const &);
441 NormalStream & operator<<(NormalStream &, docstring const &);
443 NormalStream & operator<<(NormalStream &, char const *);
445 NormalStream & operator<<(NormalStream &, char);
447 NormalStream & operator<<(NormalStream &, int);
458 explicit MapleStream(odocstream & os) : os_(os) {}
460 odocstream & os() { return os_; }
468 MapleStream & operator<<(MapleStream &, MathAtom const &);
470 MapleStream & operator<<(MapleStream &, MathData const &);
472 MapleStream & operator<<(MapleStream &, docstring const &);
474 MapleStream & operator<<(MapleStream &, char_type);
476 MapleStream & operator<<(MapleStream &, char const *);
478 MapleStream & operator<<(MapleStream &, char);
480 MapleStream & operator<<(MapleStream &, int);
491 explicit MaximaStream(odocstream & os) : os_(os) {}
493 odocstream & os() { return os_; }
501 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
503 MaximaStream & operator<<(MaximaStream &, MathData const &);
505 MaximaStream & operator<<(MaximaStream &, docstring const &);
507 MaximaStream & operator<<(MaximaStream &, char_type);
509 MaximaStream & operator<<(MaximaStream &, char const *);
511 MaximaStream & operator<<(MaximaStream &, char);
513 MaximaStream & operator<<(MaximaStream &, int);
521 class MathematicaStream {
524 explicit MathematicaStream(odocstream & os) : os_(os) {}
526 odocstream & os() { return os_; }
534 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
536 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
538 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
540 MathematicaStream & operator<<(MathematicaStream &, char const *);
542 MathematicaStream & operator<<(MathematicaStream &, char);
544 MathematicaStream & operator<<(MathematicaStream &, int);
555 explicit OctaveStream(odocstream & os) : os_(os) {}
557 odocstream & os() { return os_; }
564 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
566 OctaveStream & operator<<(OctaveStream &, MathData const &);
568 OctaveStream & operator<<(OctaveStream &, docstring const &);
570 OctaveStream & operator<<(OctaveStream &, char_type);
572 OctaveStream & operator<<(OctaveStream &, char const *);
574 OctaveStream & operator<<(OctaveStream &, char);
576 OctaveStream & operator<<(OctaveStream &, int);