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 "InsetMath.h"
16 #include "texstream.h"
18 #include "support/Changer.h"
19 #include "support/strfwd.h"
42 WriteStream(otexrowstream & os, bool fragile, bool latex, OutputType output,
43 Encoding const * encoding = 0);
45 explicit WriteStream(otexrowstream & 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 otexrowstream & os() { return os_; }
59 TexRow & texrow() { return os_.texrow(); }
61 bool & firstitem() { return firstitem_; }
63 void addlines(unsigned int);
64 /// record whether we can write an immediately following newline char
65 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
66 /// tell whether we can write an immediately following newline char
67 bool canBreakLine() const { return canbreakline_; }
68 /// writes space if next thing is isalpha()
69 void pendingSpace(bool how);
70 /// writes space if next thing is isalpha()
71 bool pendingSpace() const { return pendingspace_; }
72 /// tell whether to write the closing brace of \ensuremath
73 void pendingBrace(bool brace);
74 /// tell whether to write the closing brace of \ensuremath
75 bool pendingBrace() const { return pendingbrace_; }
76 /// tell whether we are in text mode or not when producing latex code
77 void textMode(bool textmode);
78 /// tell whether we are in text mode or not when producing latex code
79 bool textMode() const { return textmode_; }
80 /// tell whether we are allowed to switch mode when producing latex code
81 void lockedMode(bool locked);
82 /// tell whether we are allowed to switch mode when producing latex code
83 bool lockedMode() const { return locked_; }
84 /// tell whether to use only ascii chars when producing latex code
85 void asciiOnly(bool ascii);
86 /// tell whether to use only ascii chars when producing latex code
87 bool asciiOnly() const { return ascii_; }
89 Encoding const * encoding() const { return encoding_; }
91 /// Temporarily change the TexRow information about the outer row entry.
92 Changer changeRowEntry(TexRow::RowEntry entry);
93 /// TexRow::starts the innermost outer math inset
94 /// returns true if the outer row entry will appear at this line
99 /// do we have to write \\protect sometimes
101 /// are we at the beginning of an MathData?
103 /// are we writing to .tex?
105 /// output type (default, source preview, instant preview)?
107 /// do we have a space pending?
109 /// do we have a brace pending?
111 /// are we in text mode when producing latex code?
113 /// are we allowed to switch mode when producing latex code?
115 /// should we use only ascii chars when producing latex code?
117 /// are we allowed to output an immediately following newline?
122 Encoding const * encoding_;
123 /// Row entry we are in
124 TexRow::RowEntry row_entry_;
128 WriteStream & operator<<(WriteStream &, MathAtom const &);
130 WriteStream & operator<<(WriteStream &, MathData const &);
132 WriteStream & operator<<(WriteStream &, docstring const &);
134 WriteStream & operator<<(WriteStream &, char const * const);
136 WriteStream & operator<<(WriteStream &, char);
138 WriteStream & operator<<(WriteStream &, int);
140 WriteStream & operator<<(WriteStream &, unsigned int);
142 /// ensure math mode, possibly by opening \ensuremath
143 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
145 /// ensure the requested mode, possibly by closing \ensuremath
146 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
150 * MathEnsurer - utility class for ensuring math mode
152 * A local variable of this type can be used to either ensure math mode
153 * or delay the writing of a pending brace when outputting LaTeX.
157 * MathEnsurer ensurer(os);
159 * If not already in math mode, inserts an \ensuremath command followed
160 * by an open brace. This brace will be automatically closed when exiting
161 * math mode. Math mode is automatically exited when writing something
162 * that doesn't explicitly require math mode.
166 * MathEnsurer ensurer(os, false);
168 * Simply suspend writing a closing brace until the end of ensurer's scope.
172 * MathEnsurer ensurer(os, needs_math_mode, true);
174 * The third parameter is set to true only for a user defined macro, which
175 * needs special handling. When it is a MathMacro, the needs_math_mode
176 * parameter is true and the behavior is as in Example 1. When the
177 * needs_math_mode parameter is false (not a MathMacro) and the macro
178 * was entered in a text box and we are in math mode, the mode is reset
179 * to text. This is because the macro was probably designed for text mode
180 * (given that it was entered in text mode and we have no way to tell the
187 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
188 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
190 ~MathEnsurer() { os_.pendingBrace(brace_); }
200 * ModeSpecifier - utility class for specifying a given mode (math or text)
202 * A local variable of this type can be used to specify that a command or
203 * environment works in a given mode. For example, \mbox works in text
204 * mode, but \boxed works in math mode. Note that no mode changing commands
205 * are needed, but we have to track the current mode, hence this class.
206 * This is only used when exporting to latex and helps determining whether
207 * the mode needs being temporarily switched when a command would not work
208 * in the current mode. As there are cases where this switching is to be
209 * avoided, the optional third parameter can be used to lock the mode.
210 * When the mode is locked, the optional fourth parameter specifies whether
211 * strings are to be output by using a suitable ascii representation.
215 * ModeSpecifier specifier(os, TEXT_MODE);
217 * Sets the current mode to text mode and allows mode switching.
221 * ModeSpecifier specifier(os, TEXT_MODE, true);
223 * Sets the current mode to text mode and disallows mode switching.
227 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
229 * Sets the current mode to text mode, disallows mode switching, and outputs
230 * strings as ascii only.
232 * At the end of specifier's scope the mode is reset to its previous value.
238 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
239 bool locked = false, bool ascii = false)
240 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
244 os_.textMode(oldmodes_ & 0x01);
245 os_.lockedMode(oldmodes_ & 0x02);
246 os_.asciiOnly(oldmodes_ & 0x04);
264 MTag(char const * const tag, std::string attr = "")
265 : tag_(tag), attr_(attr) {}
267 char const * const tag_;
275 ETag(char const * const tag) : tag_(tag) {}
277 char const * const tag_;
281 /// Throw MathExportException to signal that the attempt to export
282 /// some math in the current format did not succeed. E.g., we can't
283 /// export xymatrix as MathML, so that will throw, and we'll fall back
285 class MathExportException : public std::exception {};
291 explicit MathStream(odocstream & os);
295 odocstream & os() { return os_; }
297 int line() const { return line_; }
299 int & tab() { return tab_; }
301 friend MathStream & operator<<(MathStream &, char const *);
303 void defer(docstring const &);
305 void defer(std::string const &);
307 docstring deferred() const;
309 bool inText() const { return in_text_; }
312 void setTextMode(bool t) { in_text_ = t; }
322 odocstringstream deferred_;
324 friend class SetMode;
328 MathStream & operator<<(MathStream &, MathAtom const &);
330 MathStream & operator<<(MathStream &, MathData const &);
332 MathStream & operator<<(MathStream &, docstring const &);
334 MathStream & operator<<(MathStream &, char const *);
336 MathStream & operator<<(MathStream &, char);
338 MathStream & operator<<(MathStream &, char_type);
340 MathStream & operator<<(MathStream &, MTag const &);
342 MathStream & operator<<(MathStream &, ETag const &);
345 /// A simpler version of ModeSpecifier, for MathML
349 explicit SetMode(MathStream & os, bool text);
363 explicit HtmlStream(odocstream & os);
367 odocstream & os() { return os_; }
369 int line() const { return line_; }
371 int & tab() { return tab_; }
373 friend HtmlStream & operator<<(HtmlStream &, char const *);
375 void defer(docstring const &);
377 void defer(std::string const &);
379 docstring deferred() const;
381 bool inText() const { return in_text_; }
384 void setTextMode(bool t) { in_text_ = t; }
394 odocstringstream deferred_;
396 friend class SetHTMLMode;
400 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
402 HtmlStream & operator<<(HtmlStream &, MathData const &);
404 HtmlStream & operator<<(HtmlStream &, docstring const &);
406 HtmlStream & operator<<(HtmlStream &, char const *);
408 HtmlStream & operator<<(HtmlStream &, char);
410 HtmlStream & operator<<(HtmlStream &, char_type);
412 HtmlStream & operator<<(HtmlStream &, MTag const &);
414 HtmlStream & operator<<(HtmlStream &, ETag const &);
420 explicit SetHTMLMode(HtmlStream & os, bool text);
438 explicit NormalStream(odocstream & os) : os_(os) {}
440 odocstream & os() { return os_; }
447 NormalStream & operator<<(NormalStream &, MathAtom const &);
449 NormalStream & operator<<(NormalStream &, MathData const &);
451 NormalStream & operator<<(NormalStream &, docstring const &);
453 NormalStream & operator<<(NormalStream &, char const *);
455 NormalStream & operator<<(NormalStream &, char);
457 NormalStream & operator<<(NormalStream &, int);
468 explicit MapleStream(odocstream & os) : os_(os) {}
470 odocstream & os() { return os_; }
478 MapleStream & operator<<(MapleStream &, MathAtom const &);
480 MapleStream & operator<<(MapleStream &, MathData const &);
482 MapleStream & operator<<(MapleStream &, docstring const &);
484 MapleStream & operator<<(MapleStream &, char_type);
486 MapleStream & operator<<(MapleStream &, char const *);
488 MapleStream & operator<<(MapleStream &, char);
490 MapleStream & operator<<(MapleStream &, int);
501 explicit MaximaStream(odocstream & os) : os_(os) {}
503 odocstream & os() { return os_; }
511 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
513 MaximaStream & operator<<(MaximaStream &, MathData const &);
515 MaximaStream & operator<<(MaximaStream &, docstring const &);
517 MaximaStream & operator<<(MaximaStream &, char_type);
519 MaximaStream & operator<<(MaximaStream &, char const *);
521 MaximaStream & operator<<(MaximaStream &, char);
523 MaximaStream & operator<<(MaximaStream &, int);
531 class MathematicaStream {
534 explicit MathematicaStream(odocstream & os) : os_(os) {}
536 odocstream & os() { return os_; }
544 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
546 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
548 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
550 MathematicaStream & operator<<(MathematicaStream &, char const *);
552 MathematicaStream & operator<<(MathematicaStream &, char);
554 MathematicaStream & operator<<(MathematicaStream &, int);
565 explicit OctaveStream(odocstream & os) : os_(os) {}
567 odocstream & os() { return os_; }
574 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
576 OctaveStream & operator<<(OctaveStream &, MathData const &);
578 OctaveStream & operator<<(OctaveStream &, docstring const &);
580 OctaveStream & operator<<(OctaveStream &, char_type);
582 OctaveStream & operator<<(OctaveStream &, char const *);
584 OctaveStream & operator<<(OctaveStream &, char);
586 OctaveStream & operator<<(OctaveStream &, int);