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() { in_text_ = true; }
304 void setMathMode() { in_text_ = false; }
314 odocstringstream deferred_;
316 friend class SetMode;
320 MathStream & operator<<(MathStream &, MathAtom const &);
322 MathStream & operator<<(MathStream &, MathData const &);
324 MathStream & operator<<(MathStream &, docstring const &);
326 MathStream & operator<<(MathStream &, char const *);
328 MathStream & operator<<(MathStream &, char);
330 MathStream & operator<<(MathStream &, char_type);
332 MathStream & operator<<(MathStream &, MTag const &);
334 MathStream & operator<<(MathStream &, ETag const &);
337 /// A simpler version of ModeSpecifier, for MathML
338 // FIXME There are still problems here with nesting, at least
339 // potentially. The problem is that true nesting of text mode isn't
340 // actually possible. I.e., we can't have:
341 // <mtext><mtext></mtext></mtext>
342 // So we have to have:
343 // <mtext></mtext><mtext></mtext><mtext></mtext>
344 // instead, where the last is really a continuation of the first.
345 // We'll need some kind of stack to remember all that.
349 explicit SetMode(MathStream & os, bool text, std::string const & attrs);
351 explicit SetMode(MathStream & os, bool text);
356 void init(bool, std::string const &);
369 explicit HtmlStream(odocstream & os);
373 odocstream & os() { return os_; }
375 int line() const { return line_; }
377 int & tab() { return tab_; }
379 friend HtmlStream & operator<<(HtmlStream &, char const *);
381 void defer(docstring const &);
383 void defer(std::string const &);
385 docstring deferred() const;
387 bool inText() const { return in_text_; }
390 void setTextMode() { in_text_ = true; }
392 void setMathMode() { in_text_ = false; }
402 odocstringstream deferred_;
404 friend class SetHTMLMode;
408 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
410 HtmlStream & operator<<(HtmlStream &, MathData const &);
412 HtmlStream & operator<<(HtmlStream &, docstring const &);
414 HtmlStream & operator<<(HtmlStream &, char const *);
416 HtmlStream & operator<<(HtmlStream &, char);
418 HtmlStream & operator<<(HtmlStream &, char_type);
420 HtmlStream & operator<<(HtmlStream &, MTag const &);
422 HtmlStream & operator<<(HtmlStream &, ETag const &);
428 explicit SetHTMLMode(HtmlStream & os, bool text, std::string attrs);
430 explicit SetHTMLMode(HtmlStream & os, bool text);
435 void init(bool, std::string const &);
452 explicit NormalStream(odocstream & os) : os_(os) {}
454 odocstream & os() { return os_; }
461 NormalStream & operator<<(NormalStream &, MathAtom const &);
463 NormalStream & operator<<(NormalStream &, MathData const &);
465 NormalStream & operator<<(NormalStream &, docstring const &);
467 NormalStream & operator<<(NormalStream &, char const *);
469 NormalStream & operator<<(NormalStream &, char);
471 NormalStream & operator<<(NormalStream &, int);
482 explicit MapleStream(odocstream & os) : os_(os) {}
484 odocstream & os() { return os_; }
492 MapleStream & operator<<(MapleStream &, MathAtom const &);
494 MapleStream & operator<<(MapleStream &, MathData const &);
496 MapleStream & operator<<(MapleStream &, docstring const &);
498 MapleStream & operator<<(MapleStream &, char_type);
500 MapleStream & operator<<(MapleStream &, char const *);
502 MapleStream & operator<<(MapleStream &, char);
504 MapleStream & operator<<(MapleStream &, int);
515 explicit MaximaStream(odocstream & os) : os_(os) {}
517 odocstream & os() { return os_; }
525 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
527 MaximaStream & operator<<(MaximaStream &, MathData const &);
529 MaximaStream & operator<<(MaximaStream &, docstring const &);
531 MaximaStream & operator<<(MaximaStream &, char_type);
533 MaximaStream & operator<<(MaximaStream &, char const *);
535 MaximaStream & operator<<(MaximaStream &, char);
537 MaximaStream & operator<<(MaximaStream &, int);
545 class MathematicaStream {
548 explicit MathematicaStream(odocstream & os) : os_(os) {}
550 odocstream & os() { return os_; }
558 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
560 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
562 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
564 MathematicaStream & operator<<(MathematicaStream &, char const *);
566 MathematicaStream & operator<<(MathematicaStream &, char);
568 MathematicaStream & operator<<(MathematicaStream &, int);
579 explicit OctaveStream(odocstream & os) : os_(os) {}
581 odocstream & os() { return os_; }
588 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
590 OctaveStream & operator<<(OctaveStream &, MathData const &);
592 OctaveStream & operator<<(OctaveStream &, docstring const &);
594 OctaveStream & operator<<(OctaveStream &, char_type);
596 OctaveStream & operator<<(OctaveStream &, char const *);
598 OctaveStream & operator<<(OctaveStream &, char);
600 OctaveStream & operator<<(OctaveStream &, int);