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"
20 #include "texstream.h"
43 WriteStream(otexrowstream & os, bool fragile, bool latex, OutputType output,
44 Encoding const * encoding = 0);
46 explicit WriteStream(otexrowstream & os);
50 int line() const { return line_; }
52 bool fragile() const { return fragile_; }
54 bool latex() const { return latex_; }
56 OutputType output() const { return output_; }
58 otexrowstream & os() { return os_; }
60 TexRow & texrow() { return os_.texrow(); }
62 bool & firstitem() { return firstitem_; }
64 void addlines(unsigned int);
65 /// record whether we can write an immediately following newline char
66 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
67 /// tell whether we can write an immediately following newline char
68 bool canBreakLine() const { return canbreakline_; }
69 /// writes space if next thing is isalpha()
70 void pendingSpace(bool how);
71 /// writes space if next thing is isalpha()
72 bool pendingSpace() const { return pendingspace_; }
73 /// tell whether to write the closing brace of \ensuremath
74 void pendingBrace(bool brace);
75 /// tell whether to write the closing brace of \ensuremath
76 bool pendingBrace() const { return pendingbrace_; }
77 /// tell whether we are in text mode or not when producing latex code
78 void textMode(bool textmode);
79 /// tell whether we are in text mode or not when producing latex code
80 bool textMode() const { return textmode_; }
81 /// tell whether we are allowed to switch mode when producing latex code
82 void lockedMode(bool locked);
83 /// tell whether we are allowed to switch mode when producing latex code
84 bool lockedMode() const { return locked_; }
85 /// tell whether to use only ascii chars when producing latex code
86 void asciiOnly(bool ascii);
87 /// tell whether to use only ascii chars when producing latex code
88 bool asciiOnly() const { return ascii_; }
90 Encoding const * encoding() const { return encoding_; }
92 /// maintains a stack of texrow informations about outer math insets.
94 void pushRowEntry(TexRow::RowEntry entry);
97 /// TexRow::starts the innermost outer math inset
98 /// returns true if the outer row entry will appear at this line
103 /// do we have to write \\protect sometimes
105 /// are we at the beginning of an MathData?
107 /// are we writing to .tex?
109 /// output type (default, source preview, instant preview)?
111 /// do we have a space pending?
113 /// do we have a brace pending?
115 /// are we in text mode when producing latex code?
117 /// are we allowed to switch mode when producing latex code?
119 /// should we use only ascii chars when producing latex code?
121 /// are we allowed to output an immediately following newline?
126 Encoding const * encoding_;
128 std::vector<TexRow::RowEntry> outer_row_entries_;
132 WriteStream & operator<<(WriteStream &, MathAtom const &);
134 WriteStream & operator<<(WriteStream &, MathData const &);
136 WriteStream & operator<<(WriteStream &, docstring const &);
138 WriteStream & operator<<(WriteStream &, char const * const);
140 WriteStream & operator<<(WriteStream &, char);
142 WriteStream & operator<<(WriteStream &, int);
144 WriteStream & operator<<(WriteStream &, unsigned int);
146 /// ensure math mode, possibly by opening \ensuremath
147 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
149 /// ensure the requested mode, possibly by closing \ensuremath
150 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
154 * MathEnsurer - utility class for ensuring math mode
156 * A local variable of this type can be used to either ensure math mode
157 * or delay the writing of a pending brace when outputting LaTeX.
161 * MathEnsurer ensurer(os);
163 * If not already in math mode, inserts an \ensuremath command followed
164 * by an open brace. This brace will be automatically closed when exiting
165 * math mode. Math mode is automatically exited when writing something
166 * that doesn't explicitly require math mode.
170 * MathEnsurer ensurer(os, false);
172 * Simply suspend writing a closing brace until the end of ensurer's scope.
176 * MathEnsurer ensurer(os, needs_math_mode, true);
178 * The third parameter is set to true only for a user defined macro, which
179 * needs special handling. When it is a MathMacro, the needs_math_mode
180 * parameter is true and the behavior is as in Example 1. When the
181 * needs_math_mode parameter is false (not a MathMacro) and the macro
182 * was entered in a text box and we are in math mode, the mode is reset
183 * to text. This is because the macro was probably designed for text mode
184 * (given that it was entered in text mode and we have no way to tell the
191 explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
192 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
194 ~MathEnsurer() { os_.pendingBrace(brace_); }
204 * ModeSpecifier - utility class for specifying a given mode (math or text)
206 * A local variable of this type can be used to specify that a command or
207 * environment works in a given mode. For example, \mbox works in text
208 * mode, but \boxed works in math mode. Note that no mode changing commands
209 * are needed, but we have to track the current mode, hence this class.
210 * This is only used when exporting to latex and helps determining whether
211 * the mode needs being temporarily switched when a command would not work
212 * in the current mode. As there are cases where this switching is to be
213 * avoided, the optional third parameter can be used to lock the mode.
214 * When the mode is locked, the optional fourth parameter specifies whether
215 * strings are to be output by using a suitable ascii representation.
219 * ModeSpecifier specifier(os, TEXT_MODE);
221 * Sets the current mode to text mode and allows mode switching.
225 * ModeSpecifier specifier(os, TEXT_MODE, true);
227 * Sets the current mode to text mode and disallows mode switching.
231 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
233 * Sets the current mode to text mode, disallows mode switching, and outputs
234 * strings as ascii only.
236 * At the end of specifier's scope the mode is reset to its previous value.
242 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
243 bool locked = false, bool ascii = false)
244 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
248 os_.textMode(oldmodes_ & 0x01);
249 os_.lockedMode(oldmodes_ & 0x02);
250 os_.asciiOnly(oldmodes_ & 0x04);
268 MTag(char const * const tag, std::string attr = "")
269 : tag_(tag), attr_(attr) {}
271 char const * const tag_;
279 ETag(char const * const tag) : tag_(tag) {}
281 char const * const tag_;
285 /// Throw MathExportException to signal that the attempt to export
286 /// some math in the current format did not succeed. E.g., we can't
287 /// export xymatrix as MathML, so that will throw, and we'll fall back
289 class MathExportException : public std::exception {};
295 explicit MathStream(odocstream & os);
299 odocstream & os() { return os_; }
301 int line() const { return line_; }
303 int & tab() { return tab_; }
305 friend MathStream & operator<<(MathStream &, char const *);
307 void defer(docstring const &);
309 void defer(std::string const &);
311 docstring deferred() const;
313 bool inText() const { return in_text_; }
316 void setTextMode(bool t) { in_text_ = t; }
326 odocstringstream deferred_;
328 friend class SetMode;
332 MathStream & operator<<(MathStream &, MathAtom const &);
334 MathStream & operator<<(MathStream &, MathData const &);
336 MathStream & operator<<(MathStream &, docstring const &);
338 MathStream & operator<<(MathStream &, char const *);
340 MathStream & operator<<(MathStream &, char);
342 MathStream & operator<<(MathStream &, char_type);
344 MathStream & operator<<(MathStream &, MTag const &);
346 MathStream & operator<<(MathStream &, ETag const &);
349 /// A simpler version of ModeSpecifier, for MathML
353 explicit SetMode(MathStream & os, bool text);
367 explicit HtmlStream(odocstream & os);
371 odocstream & os() { return os_; }
373 int line() const { return line_; }
375 int & tab() { return tab_; }
377 friend HtmlStream & operator<<(HtmlStream &, char const *);
379 void defer(docstring const &);
381 void defer(std::string const &);
383 docstring deferred() const;
385 bool inText() const { return in_text_; }
388 void setTextMode(bool t) { in_text_ = t; }
398 odocstringstream deferred_;
400 friend class SetHTMLMode;
404 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
406 HtmlStream & operator<<(HtmlStream &, MathData const &);
408 HtmlStream & operator<<(HtmlStream &, docstring const &);
410 HtmlStream & operator<<(HtmlStream &, char const *);
412 HtmlStream & operator<<(HtmlStream &, char);
414 HtmlStream & operator<<(HtmlStream &, char_type);
416 HtmlStream & operator<<(HtmlStream &, MTag const &);
418 HtmlStream & operator<<(HtmlStream &, ETag const &);
424 explicit SetHTMLMode(HtmlStream & os, bool text);
442 explicit NormalStream(odocstream & os) : os_(os) {}
444 odocstream & os() { return os_; }
451 NormalStream & operator<<(NormalStream &, MathAtom const &);
453 NormalStream & operator<<(NormalStream &, MathData const &);
455 NormalStream & operator<<(NormalStream &, docstring const &);
457 NormalStream & operator<<(NormalStream &, char const *);
459 NormalStream & operator<<(NormalStream &, char);
461 NormalStream & operator<<(NormalStream &, int);
472 explicit MapleStream(odocstream & os) : os_(os) {}
474 odocstream & os() { return os_; }
482 MapleStream & operator<<(MapleStream &, MathAtom const &);
484 MapleStream & operator<<(MapleStream &, MathData const &);
486 MapleStream & operator<<(MapleStream &, docstring const &);
488 MapleStream & operator<<(MapleStream &, char_type);
490 MapleStream & operator<<(MapleStream &, char const *);
492 MapleStream & operator<<(MapleStream &, char);
494 MapleStream & operator<<(MapleStream &, int);
505 explicit MaximaStream(odocstream & os) : os_(os) {}
507 odocstream & os() { return os_; }
515 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
517 MaximaStream & operator<<(MaximaStream &, MathData const &);
519 MaximaStream & operator<<(MaximaStream &, docstring const &);
521 MaximaStream & operator<<(MaximaStream &, char_type);
523 MaximaStream & operator<<(MaximaStream &, char const *);
525 MaximaStream & operator<<(MaximaStream &, char);
527 MaximaStream & operator<<(MaximaStream &, int);
535 class MathematicaStream {
538 explicit MathematicaStream(odocstream & os) : os_(os) {}
540 odocstream & os() { return os_; }
548 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
550 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
552 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
554 MathematicaStream & operator<<(MathematicaStream &, char const *);
556 MathematicaStream & operator<<(MathematicaStream &, char);
558 MathematicaStream & operator<<(MathematicaStream &, int);
569 explicit OctaveStream(odocstream & os) : os_(os) {}
571 odocstream & os() { return os_; }
578 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
580 OctaveStream & operator<<(OctaveStream &, MathData const &);
582 OctaveStream & operator<<(OctaveStream &, docstring const &);
584 OctaveStream & operator<<(OctaveStream &, char_type);
586 OctaveStream & operator<<(OctaveStream &, char const *);
588 OctaveStream & operator<<(OctaveStream &, char);
590 OctaveStream & operator<<(OctaveStream &, int);