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"
18 #include "texstream.h"
20 #include "support/Changer.h"
21 #include "support/strfwd.h"
22 #include "support/unique_ptr.h"
51 explicit WriteStream(otexrowstream & os, bool fragile = false,
52 bool latex = false, OutputType output = wsDefault,
53 Encoding const * encoding = 0);
57 int line() const { return line_; }
59 bool fragile() const { return fragile_; }
61 bool latex() const { return latex_; }
63 OutputType output() const { return output_; }
65 otexrowstream & os() { return os_; }
67 TexRow & texrow() { return os_.texrow(); }
69 bool & firstitem() { return firstitem_; }
71 void addlines(unsigned int);
72 /// record whether we can write an immediately following newline char
73 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
74 /// tell whether we can write an immediately following newline char
75 bool canBreakLine() const { return canbreakline_; }
76 /// record whether we have to take care for striking out display math
77 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
78 /// tell whether we have to take care for striking out display math
79 bool strikeoutMath() const { return mathsout_; }
80 /// record which ulem command type we are inside
81 void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
82 /// tell which ulem command type we are inside
83 UlemCmdType ulemCmd() const { return ulemcmd_; }
84 /// writes space if next thing is isalpha()
85 void pendingSpace(bool how);
86 /// writes space if next thing is isalpha()
87 bool pendingSpace() const { return pendingspace_; }
88 /// tell whether to write the closing brace of \ensuremath
89 void pendingBrace(bool brace);
90 /// tell whether to write the closing brace of \ensuremath
91 bool pendingBrace() const { return pendingbrace_; }
92 /// tell whether we are in text mode or not when producing latex code
93 void textMode(bool textmode);
94 /// tell whether we are in text mode or not when producing latex code
95 bool textMode() const { return textmode_; }
96 /// tell whether we are allowed to switch mode when producing latex code
97 void lockedMode(bool locked);
98 /// tell whether we are allowed to switch mode when producing latex code
99 bool lockedMode() const { return locked_; }
100 /// tell whether to use only ascii chars when producing latex code
101 void asciiOnly(bool ascii);
102 /// tell whether to use only ascii chars when producing latex code
103 bool asciiOnly() const { return ascii_; }
105 Encoding const * encoding() const { return encoding_; }
107 /// Temporarily change the TexRow information about the outer row entry.
108 Changer changeRowEntry(TexRow::RowEntry entry);
109 /// TexRow::starts the innermost outer math inset
110 /// returns true if the outer row entry will appear at this line
111 bool startOuterRow();
115 /// do we have to write \\protect sometimes
117 /// are we at the beginning of an MathData?
119 /// are we writing to .tex?
121 /// output type (default, source preview, instant preview)?
123 /// do we have a space pending?
125 /// do we have a brace pending?
127 /// are we in text mode when producing latex code?
129 /// are we allowed to switch mode when producing latex code?
131 /// should we use only ascii chars when producing latex code?
133 /// are we allowed to output an immediately following newline?
135 /// should we take care for striking out display math?
137 /// what ulem command are we inside (none, underline, strikeout)?
138 UlemCmdType ulemcmd_;
142 Encoding const * encoding_;
143 /// Row entry we are in
144 TexRow::RowEntry row_entry_;
148 WriteStream & operator<<(WriteStream &, MathAtom const &);
150 WriteStream & operator<<(WriteStream &, MathData const &);
152 WriteStream & operator<<(WriteStream &, docstring const &);
154 WriteStream & operator<<(WriteStream &, char const * const);
156 WriteStream & operator<<(WriteStream &, char);
158 WriteStream & operator<<(WriteStream &, int);
160 WriteStream & operator<<(WriteStream &, unsigned int);
162 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
163 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
164 bool macro = false, bool textmode_macro = false);
166 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
167 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
171 * MathEnsurer - utility class for ensuring math mode
173 * A local variable of this type can be used to either ensure math mode
174 * or delay the writing of a pending brace when outputting LaTeX.
175 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
176 * no assumption is made for macros defined through \newcommand or \def.
180 * MathEnsurer ensurer(os);
182 * If not already in math mode, inserts an \ensuremath command followed
183 * by an open brace. This brace will be automatically closed when exiting
184 * math mode. Math mode is automatically exited when writing something
185 * that doesn't explicitly require math mode.
189 * MathEnsurer ensurer(os, false);
191 * Simply suspend writing a closing brace until the end of ensurer's scope.
195 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
197 * This form is mainly used for math macros as they are treated specially.
198 * In essence, the macros defined in the lib/symbols file and tagged as
199 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
200 * environment, while macros defined in the preamble or ERT are left as is.
201 * The third parameter must be set to true and the fourth parameter has also
202 * to be specified. Only the following 3 different cases are handled.
204 * When the needs_mathmode parameter is true the behavior is as in Example 1.
205 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
207 * When the needs_mathmode and textmode_macro parameters are both false the
208 * macro is left in the same (text or math mode) environment it was entered.
209 * This is because it is assumed that the macro was designed for that mode
210 * and we have no way to tell the contrary.
211 * This is the case for macros defined by using \newcommand or \def in ERT.
213 * When the needs_mathmode parameter is false while textmode_macro is true the
214 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
215 * This is the case for the macros tagged as textmode in lib/symbols.
221 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
222 bool macro = false, bool textmode_macro = false)
223 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
225 ~MathEnsurer() { os_.pendingBrace(brace_); }
235 * ModeSpecifier - utility class for specifying a given mode (math or text)
237 * A local variable of this type can be used to specify that a command or
238 * environment works in a given mode. For example, \mbox works in text
239 * mode, but \boxed works in math mode. Note that no mode changing commands
240 * are needed, but we have to track the current mode, hence this class.
241 * This is only used when exporting to latex and helps determining whether
242 * the mode needs being temporarily switched when a command would not work
243 * in the current mode. As there are cases where this switching is to be
244 * avoided, the optional third parameter can be used to lock the mode.
245 * When the mode is locked, the optional fourth parameter specifies whether
246 * strings are to be output by using a suitable ascii representation.
250 * ModeSpecifier specifier(os, TEXT_MODE);
252 * Sets the current mode to text mode and allows mode switching.
256 * ModeSpecifier specifier(os, TEXT_MODE, true);
258 * Sets the current mode to text mode and disallows mode switching.
262 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
264 * Sets the current mode to text mode, disallows mode switching, and outputs
265 * strings as ascii only.
267 * At the end of specifier's scope the mode is reset to its previous value.
273 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
274 bool locked = false, bool ascii = false)
275 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
279 os_.textMode(oldmodes_ & 0x01);
280 os_.lockedMode(oldmodes_ & 0x02);
281 os_.asciiOnly(oldmodes_ & 0x04);
299 MTag(char const * const tag, std::string const & attr = std::string())
300 : tag_(tag), attr_(attr) {}
302 char const * const tag_;
310 explicit ETag(char const * const tag) : tag_(tag) {}
312 char const * const tag_;
316 /// Throw MathExportException to signal that the attempt to export
317 /// some math in the current format did not succeed. E.g., we can't
318 /// export xymatrix as MathML, so that will throw, and we'll fall back
320 class MathExportException : public std::exception {};
326 explicit MathStream(odocstream & os);
330 odocstream & os() { return os_; }
332 int line() const { return line_; }
334 int & tab() { return tab_; }
336 friend MathStream & operator<<(MathStream &, char const *);
338 void defer(docstring const &);
340 void defer(std::string const &);
342 docstring deferred() const;
344 bool inText() const { return in_text_; }
347 void setTextMode(bool t) { in_text_ = t; }
357 odocstringstream deferred_;
359 friend class SetMode;
363 MathStream & operator<<(MathStream &, MathAtom const &);
365 MathStream & operator<<(MathStream &, MathData const &);
367 MathStream & operator<<(MathStream &, docstring const &);
369 MathStream & operator<<(MathStream &, char const *);
371 MathStream & operator<<(MathStream &, char);
373 MathStream & operator<<(MathStream &, char_type);
375 MathStream & operator<<(MathStream &, MTag const &);
377 MathStream & operator<<(MathStream &, ETag const &);
380 /// A simpler version of ModeSpecifier, for MathML
384 explicit SetMode(MathStream & os, bool text);
398 explicit HtmlStream(odocstream & os);
402 odocstream & os() { return os_; }
404 int line() const { return line_; }
406 int & tab() { return tab_; }
408 friend HtmlStream & operator<<(HtmlStream &, char const *);
410 void defer(docstring const &);
412 void defer(std::string const &);
414 docstring deferred() const;
416 bool inText() const { return in_text_; }
419 void setTextMode(bool t) { in_text_ = t; }
429 odocstringstream deferred_;
431 friend class SetHTMLMode;
435 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
437 HtmlStream & operator<<(HtmlStream &, MathData const &);
439 HtmlStream & operator<<(HtmlStream &, docstring const &);
441 HtmlStream & operator<<(HtmlStream &, char const *);
443 HtmlStream & operator<<(HtmlStream &, char);
445 HtmlStream & operator<<(HtmlStream &, char_type);
447 HtmlStream & operator<<(HtmlStream &, MTag const &);
449 HtmlStream & operator<<(HtmlStream &, ETag const &);
455 explicit SetHTMLMode(HtmlStream & os, bool text);
473 explicit NormalStream(odocstream & os) : os_(os) {}
475 odocstream & os() { return os_; }
482 NormalStream & operator<<(NormalStream &, MathAtom const &);
484 NormalStream & operator<<(NormalStream &, MathData const &);
486 NormalStream & operator<<(NormalStream &, docstring const &);
488 NormalStream & operator<<(NormalStream &, char const *);
490 NormalStream & operator<<(NormalStream &, char);
492 NormalStream & operator<<(NormalStream &, int);
503 explicit MapleStream(odocstream & os) : os_(os) {}
505 odocstream & os() { return os_; }
513 MapleStream & operator<<(MapleStream &, MathAtom const &);
515 MapleStream & operator<<(MapleStream &, MathData const &);
517 MapleStream & operator<<(MapleStream &, docstring const &);
519 MapleStream & operator<<(MapleStream &, char_type);
521 MapleStream & operator<<(MapleStream &, char const *);
523 MapleStream & operator<<(MapleStream &, char);
525 MapleStream & operator<<(MapleStream &, int);
536 explicit MaximaStream(odocstream & os) : os_(os) {}
538 odocstream & os() { return os_; }
546 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
548 MaximaStream & operator<<(MaximaStream &, MathData const &);
550 MaximaStream & operator<<(MaximaStream &, docstring const &);
552 MaximaStream & operator<<(MaximaStream &, char_type);
554 MaximaStream & operator<<(MaximaStream &, char const *);
556 MaximaStream & operator<<(MaximaStream &, char);
558 MaximaStream & operator<<(MaximaStream &, int);
566 class MathematicaStream {
569 explicit MathematicaStream(odocstream & os) : os_(os) {}
571 odocstream & os() { return os_; }
579 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
581 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
583 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
585 MathematicaStream & operator<<(MathematicaStream &, char const *);
587 MathematicaStream & operator<<(MathematicaStream &, char);
589 MathematicaStream & operator<<(MathematicaStream &, int);
600 explicit OctaveStream(odocstream & os) : os_(os) {}
602 odocstream & os() { return os_; }
609 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
611 OctaveStream & operator<<(OctaveStream &, MathData const &);
613 OctaveStream & operator<<(OctaveStream &, docstring const &);
615 OctaveStream & operator<<(OctaveStream &, char_type);
617 OctaveStream & operator<<(OctaveStream &, char const *);
619 OctaveStream & operator<<(OctaveStream &, char);
621 OctaveStream & operator<<(OctaveStream &, int);
624 docstring convertDelimToXMLEscape(docstring const & name);