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"
20 #include "support/unique_ptr.h"
44 explicit WriteStream(otexrowstream & os, bool fragile = false,
45 bool latex = false, OutputType output = wsDefault,
46 Encoding const * encoding = 0);
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 /// Temporarily change the TexRow information about the outer row entry.
93 Changer changeRowEntry(RowEntry entry);
94 /// TexRow::starts the innermost outer math inset
95 /// returns true if the outer row entry will appear at this line
100 /// do we have to write \\protect sometimes
102 /// are we at the beginning of an MathData?
104 /// are we writing to .tex?
106 /// output type (default, source preview, instant preview)?
108 /// do we have a space pending?
110 /// do we have a brace pending?
112 /// are we in text mode when producing latex code?
114 /// are we allowed to switch mode when producing latex code?
116 /// should we use only ascii chars when producing latex code?
118 /// are we allowed to output an immediately following newline?
123 Encoding const * encoding_;
124 /// Row entry we are in
125 /// (it is a pointer to allow forward-declaration)
126 unique_ptr<RowEntry> row_entry_;
130 WriteStream & operator<<(WriteStream &, MathAtom const &);
132 WriteStream & operator<<(WriteStream &, MathData const &);
134 WriteStream & operator<<(WriteStream &, docstring const &);
136 WriteStream & operator<<(WriteStream &, char const * const);
138 WriteStream & operator<<(WriteStream &, char);
140 WriteStream & operator<<(WriteStream &, int);
142 WriteStream & operator<<(WriteStream &, unsigned int);
144 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
145 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
146 bool macro = false, bool textmode_macro = false);
148 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
149 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
153 * MathEnsurer - utility class for ensuring math mode
155 * A local variable of this type can be used to either ensure math mode
156 * or delay the writing of a pending brace when outputting LaTeX.
157 * A LyX MathMacro is always assumed needing a math mode environment, while
158 * no assumption is made for macros defined through \newcommand or \def.
162 * MathEnsurer ensurer(os);
164 * If not already in math mode, inserts an \ensuremath command followed
165 * by an open brace. This brace will be automatically closed when exiting
166 * math mode. Math mode is automatically exited when writing something
167 * that doesn't explicitly require math mode.
171 * MathEnsurer ensurer(os, false);
173 * Simply suspend writing a closing brace until the end of ensurer's scope.
177 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
179 * This form is mainly used for math macros as they are treated specially.
180 * In essence, the macros defined in the lib/symbols file and tagged as
181 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
182 * environment, while macros defined in the preamble or ERT are left as is.
183 * The third parameter must be set to true and the fourth parameter has also
184 * to be specified. Only the following 3 different cases are handled.
186 * When the needs_mathmode parameter is true the behavior is as in Example 1.
187 * This is the case for a LyX MathMacro or a macro not tagged as textmode.
189 * When the needs_mathmode and textmode_macro parameters are both false the
190 * macro is left in the same (text or math mode) environment it was entered.
191 * This is because it is assumed that the macro was designed for that mode
192 * and we have no way to tell the contrary.
193 * This is the case for macros defined by using \newcommand or \def in ERT.
195 * When the needs_mathmode parameter is false while textmode_macro is true the
196 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
197 * This is the case for the macros tagged as textmode in lib/symbols.
203 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
204 bool macro = false, bool textmode_macro = false)
205 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
207 ~MathEnsurer() { os_.pendingBrace(brace_); }
217 * ModeSpecifier - utility class for specifying a given mode (math or text)
219 * A local variable of this type can be used to specify that a command or
220 * environment works in a given mode. For example, \mbox works in text
221 * mode, but \boxed works in math mode. Note that no mode changing commands
222 * are needed, but we have to track the current mode, hence this class.
223 * This is only used when exporting to latex and helps determining whether
224 * the mode needs being temporarily switched when a command would not work
225 * in the current mode. As there are cases where this switching is to be
226 * avoided, the optional third parameter can be used to lock the mode.
227 * When the mode is locked, the optional fourth parameter specifies whether
228 * strings are to be output by using a suitable ascii representation.
232 * ModeSpecifier specifier(os, TEXT_MODE);
234 * Sets the current mode to text mode and allows mode switching.
238 * ModeSpecifier specifier(os, TEXT_MODE, true);
240 * Sets the current mode to text mode and disallows mode switching.
244 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
246 * Sets the current mode to text mode, disallows mode switching, and outputs
247 * strings as ascii only.
249 * At the end of specifier's scope the mode is reset to its previous value.
255 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
256 bool locked = false, bool ascii = false)
257 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
261 os_.textMode(oldmodes_ & 0x01);
262 os_.lockedMode(oldmodes_ & 0x02);
263 os_.asciiOnly(oldmodes_ & 0x04);
281 MTag(char const * const tag, std::string attr = "")
282 : tag_(tag), attr_(attr) {}
284 char const * const tag_;
292 ETag(char const * const tag) : tag_(tag) {}
294 char const * const tag_;
298 /// Throw MathExportException to signal that the attempt to export
299 /// some math in the current format did not succeed. E.g., we can't
300 /// export xymatrix as MathML, so that will throw, and we'll fall back
302 class MathExportException : public std::exception {};
308 explicit MathStream(odocstream & os);
312 odocstream & os() { return os_; }
314 int line() const { return line_; }
316 int & tab() { return tab_; }
318 friend MathStream & operator<<(MathStream &, char const *);
320 void defer(docstring const &);
322 void defer(std::string const &);
324 docstring deferred() const;
326 bool inText() const { return in_text_; }
329 void setTextMode(bool t) { in_text_ = t; }
339 odocstringstream deferred_;
341 friend class SetMode;
345 MathStream & operator<<(MathStream &, MathAtom const &);
347 MathStream & operator<<(MathStream &, MathData const &);
349 MathStream & operator<<(MathStream &, docstring const &);
351 MathStream & operator<<(MathStream &, char const *);
353 MathStream & operator<<(MathStream &, char);
355 MathStream & operator<<(MathStream &, char_type);
357 MathStream & operator<<(MathStream &, MTag const &);
359 MathStream & operator<<(MathStream &, ETag const &);
362 /// A simpler version of ModeSpecifier, for MathML
366 explicit SetMode(MathStream & os, bool text);
380 explicit HtmlStream(odocstream & os);
384 odocstream & os() { return os_; }
386 int line() const { return line_; }
388 int & tab() { return tab_; }
390 friend HtmlStream & operator<<(HtmlStream &, char const *);
392 void defer(docstring const &);
394 void defer(std::string const &);
396 docstring deferred() const;
398 bool inText() const { return in_text_; }
401 void setTextMode(bool t) { in_text_ = t; }
411 odocstringstream deferred_;
413 friend class SetHTMLMode;
417 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
419 HtmlStream & operator<<(HtmlStream &, MathData const &);
421 HtmlStream & operator<<(HtmlStream &, docstring const &);
423 HtmlStream & operator<<(HtmlStream &, char const *);
425 HtmlStream & operator<<(HtmlStream &, char);
427 HtmlStream & operator<<(HtmlStream &, char_type);
429 HtmlStream & operator<<(HtmlStream &, MTag const &);
431 HtmlStream & operator<<(HtmlStream &, ETag const &);
437 explicit SetHTMLMode(HtmlStream & os, bool text);
455 explicit NormalStream(odocstream & os) : os_(os) {}
457 odocstream & os() { return os_; }
464 NormalStream & operator<<(NormalStream &, MathAtom const &);
466 NormalStream & operator<<(NormalStream &, MathData const &);
468 NormalStream & operator<<(NormalStream &, docstring const &);
470 NormalStream & operator<<(NormalStream &, char const *);
472 NormalStream & operator<<(NormalStream &, char);
474 NormalStream & operator<<(NormalStream &, int);
485 explicit MapleStream(odocstream & os) : os_(os) {}
487 odocstream & os() { return os_; }
495 MapleStream & operator<<(MapleStream &, MathAtom const &);
497 MapleStream & operator<<(MapleStream &, MathData const &);
499 MapleStream & operator<<(MapleStream &, docstring const &);
501 MapleStream & operator<<(MapleStream &, char_type);
503 MapleStream & operator<<(MapleStream &, char const *);
505 MapleStream & operator<<(MapleStream &, char);
507 MapleStream & operator<<(MapleStream &, int);
518 explicit MaximaStream(odocstream & os) : os_(os) {}
520 odocstream & os() { return os_; }
528 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
530 MaximaStream & operator<<(MaximaStream &, MathData const &);
532 MaximaStream & operator<<(MaximaStream &, docstring const &);
534 MaximaStream & operator<<(MaximaStream &, char_type);
536 MaximaStream & operator<<(MaximaStream &, char const *);
538 MaximaStream & operator<<(MaximaStream &, char);
540 MaximaStream & operator<<(MaximaStream &, int);
548 class MathematicaStream {
551 explicit MathematicaStream(odocstream & os) : os_(os) {}
553 odocstream & os() { return os_; }
561 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
563 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
565 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
567 MathematicaStream & operator<<(MathematicaStream &, char const *);
569 MathematicaStream & operator<<(MathematicaStream &, char);
571 MathematicaStream & operator<<(MathematicaStream &, int);
582 explicit OctaveStream(odocstream & os) : os_(os) {}
584 odocstream & os() { return os_; }
591 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
593 OctaveStream & operator<<(OctaveStream &, MathData const &);
595 OctaveStream & operator<<(OctaveStream &, docstring const &);
597 OctaveStream & operator<<(OctaveStream &, char_type);
599 OctaveStream & operator<<(OctaveStream &, char const *);
601 OctaveStream & operator<<(OctaveStream &, char);
603 OctaveStream & operator<<(OctaveStream &, int);
606 docstring convertDelimToXMLEscape(docstring const & name);