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"
49 explicit TeXMathStream(otexrowstream & os, bool fragile = false,
50 bool latex = false, OutputType output = wsDefault,
51 Encoding const * encoding = nullptr);
55 int line() const { return line_; }
57 bool fragile() const { return fragile_; }
59 bool latex() const { return latex_; }
61 OutputType output() const { return output_; }
63 otexrowstream & os() { return os_; }
65 TexRow & texrow() { return os_.texrow(); }
67 bool & firstitem() { return firstitem_; }
69 void addlines(unsigned int);
70 /// record whether we can write an immediately following newline char
71 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
72 /// tell whether we can write an immediately following newline char
73 bool canBreakLine() const { return canbreakline_; }
74 /// record whether we have to take care for striking out display math
75 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
76 /// tell whether we have to take care for striking out display math
77 bool strikeoutMath() const { return mathsout_; }
78 /// record which ulem command type we are inside
79 void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
80 /// tell which ulem command type we are inside
81 UlemCmdType ulemCmd() const { return ulemcmd_; }
82 /// writes space if next thing is isalpha()
83 void pendingSpace(bool space);
84 /// writes space if next thing is isalpha()
85 bool pendingSpace() const { return pendingspace_; }
86 /// write braces if a space is pending and next char is [
87 /// or when a prime immediately follows a superscript
88 void useBraces(bool braces);
89 /// write braces if a space is pending and next char is [
90 /// or when a prime immediately follows a superscript
91 bool useBraces() const { return usebraces_; }
92 /// tell whether to write the closing brace of \ensuremath
93 void pendingBrace(bool brace);
94 /// tell whether to write the closing brace of \ensuremath
95 bool pendingBrace() const { return pendingbrace_; }
96 /// tell whether we are in text mode or not when producing latex code
97 void textMode(bool textmode);
98 /// tell whether we are in text mode or not when producing latex code
99 bool textMode() const { return textmode_; }
100 /// tell whether we are allowed to switch mode when producing latex code
101 void lockedMode(bool locked);
102 /// tell whether we are allowed to switch mode when producing latex code
103 bool lockedMode() const { return locked_; }
104 /// tell whether to use only ascii chars when producing latex code
105 void asciiOnly(bool ascii);
106 /// tell whether to use only ascii chars when producing latex code
107 bool asciiOnly() const { return ascii_; }
108 /// tell whether we are in a MathClass inset
109 void inMathClass(bool mathclass) { mathclass_ = mathclass; };
110 /// tell whether we are in a MathClass inset
111 bool inMathClass() const { return mathclass_; }
113 Encoding const * encoding() const { return encoding_; }
115 /// Temporarily change the TexRow information about the outer row entry.
116 Changer changeRowEntry(TexRow::RowEntry entry);
117 /// TexRow::starts the innermost outer math inset
118 /// returns true if the outer row entry will appear at this line
119 bool startOuterRow();
123 /// do we have to write \\protect sometimes
124 bool fragile_ = false;
125 /// are we at the beginning of an MathData?
126 bool firstitem_ = false;
127 /// are we writing to .tex?
129 /// output type (default, source preview, instant preview)?
130 OutputType output_ = wsDefault;
131 /// do we have a space pending?
132 bool pendingspace_ = false;
133 /// do we have to write braces when a space is pending and [ follows,
134 /// or when a prime immediately follows a superscript?
135 bool usebraces_ = false;
136 /// do we have a brace pending?
137 bool pendingbrace_ = false;
138 /// are we in text mode when producing latex code?
139 bool textmode_ = false;
140 /// are we allowed to switch mode when producing latex code?
141 bool locked_ = false;
142 /// should we use only ascii chars when producing latex code?
144 /// are we allowed to output an immediately following newline?
145 bool canbreakline_ = true;
146 /// should we take care for striking out display math?
147 bool mathsout_ = false;
148 /// what ulem command are we inside (none, underline, strikeout)?
149 UlemCmdType ulemcmd_ = NONE;
153 Encoding const * encoding_ = nullptr;
154 /// Row entry we are in
155 TexRow::RowEntry row_entry_ = TexRow::row_none;
156 /// whether we are in a MathClass inset
157 bool mathclass_ = false;
161 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
163 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
165 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
167 TeXMathStream & operator<<(TeXMathStream &, char const * const);
169 TeXMathStream & operator<<(TeXMathStream &, char);
171 TeXMathStream & operator<<(TeXMathStream &, int);
173 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
175 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
176 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
177 bool macro = false, bool textmode_macro = false);
179 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
180 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
184 * MathEnsurer - utility class for ensuring math mode
186 * A local variable of this type can be used to either ensure math mode
187 * or delay the writing of a pending brace when outputting LaTeX.
188 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
189 * no assumption is made for macros defined through \newcommand or \def.
193 * MathEnsurer ensurer(os);
195 * If not already in math mode, inserts an \ensuremath command followed
196 * by an open brace. This brace will be automatically closed when exiting
197 * math mode. Math mode is automatically exited when writing something
198 * that doesn't explicitly require math mode.
202 * MathEnsurer ensurer(os, false);
204 * Simply suspend writing a closing brace until the end of ensurer's scope.
208 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
210 * This form is mainly used for math macros as they are treated specially.
211 * In essence, the macros defined in the lib/symbols file and tagged as
212 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
213 * environment, while macros defined in the preamble or ERT are left as is.
214 * The third parameter must be set to true and the fourth parameter has also
215 * to be specified. Only the following 3 different cases are handled.
217 * When the needs_mathmode parameter is true the behavior is as in Example 1.
218 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
220 * When the needs_mathmode and textmode_macro parameters are both false the
221 * macro is left in the same (text or math mode) environment it was entered.
222 * This is because it is assumed that the macro was designed for that mode
223 * and we have no way to tell the contrary.
224 * This is the case for macros defined by using \newcommand or \def in ERT.
226 * When the needs_mathmode parameter is false while textmode_macro is true the
227 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
228 * This is the case for the macros tagged as textmode in lib/symbols.
234 explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
235 bool macro = false, bool textmode_macro = false)
236 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
238 ~MathEnsurer() { os_.pendingBrace(brace_); }
248 * ModeSpecifier - utility class for specifying a given mode (math or text)
250 * A local variable of this type can be used to specify that a command or
251 * environment works in a given mode. For example, \mbox works in text
252 * mode, but \boxed works in math mode. Note that no mode changing commands
253 * are needed, but we have to track the current mode, hence this class.
254 * This is only used when exporting to latex and helps determining whether
255 * the mode needs being temporarily switched when a command would not work
256 * in the current mode. As there are cases where this switching is to be
257 * avoided, the optional third parameter can be used to lock the mode.
258 * When the mode is locked, the optional fourth parameter specifies whether
259 * strings are to be output by using a suitable ascii representation.
263 * ModeSpecifier specifier(os, TEXT_MODE);
265 * Sets the current mode to text mode and allows mode switching.
269 * ModeSpecifier specifier(os, TEXT_MODE, true);
271 * Sets the current mode to text mode and disallows mode switching.
275 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
277 * Sets the current mode to text mode, disallows mode switching, and outputs
278 * strings as ascii only.
280 * At the end of specifier's scope the mode is reset to its previous value.
286 explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
287 bool locked = false, bool ascii = false)
288 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
292 os_.textMode(oldmodes_ & 0x01);
293 os_.lockedMode(oldmodes_ & 0x02);
294 os_.asciiOnly(oldmodes_ & 0x04);
314 MTag(char const * const tag, std::string const & attr = std::string())
315 : tag_(tag), attr_(attr) {}
317 char const * const tag_;
327 explicit ETag(char const * const tag) : tag_(tag) {}
329 char const * const tag_;
333 /// Compound tag (no content, directly closed).
337 CTag(char const * const tag, std::string const & attr = "")
338 : tag_(tag), attr_(attr) {}
340 char const * const tag_;
346 /// Throw MathExportException to signal that the attempt to export
347 /// some math in the current format did not succeed. E.g., we can't
348 /// export xymatrix as MathML, so that will throw, and we'll fall back
350 class MathExportException : public std::exception {};
355 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
356 explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
360 odocstream & os() { return os_; }
362 int line() const { return line_; }
364 int & tab() { return tab_; }
366 friend MathMLStream & operator<<(MathMLStream &, char const *);
368 void defer(docstring const &);
370 void defer(std::string const &);
372 docstring deferred() const;
374 bool inText() const { return in_text_; }
376 std::string xmlns() const { return xmlns_; }
378 bool xmlMode() const { return xml_mode_; }
379 /// Returns the tag name prefixed by the name space if needed.
380 std::string namespacedTag(std::string const & tag) const {
381 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
385 void setTextMode(bool t) { in_text_ = t; }
395 odocstringstream deferred_;
401 friend class SetMode;
405 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
407 MathMLStream & operator<<(MathMLStream &, MathData const &);
409 MathMLStream & operator<<(MathMLStream &, docstring const &);
411 MathMLStream & operator<<(MathMLStream &, char const *);
413 MathMLStream & operator<<(MathMLStream &, char);
415 MathMLStream & operator<<(MathMLStream &, char_type);
417 MathMLStream & operator<<(MathMLStream &, MTag const &);
419 MathMLStream & operator<<(MathMLStream &, ETag const &);
421 MathMLStream & operator<<(MathMLStream &, CTag const &);
424 /// A simpler version of ModeSpecifier, for MathML
428 explicit SetMode(MathMLStream & ms, bool text);
442 explicit HtmlStream(odocstream & os);
446 odocstream & os() { return os_; }
448 int line() const { return line_; }
450 int & tab() { return tab_; }
452 friend HtmlStream & operator<<(HtmlStream &, char const *);
454 void defer(docstring const &);
456 void defer(std::string const &);
458 docstring deferred() const;
460 bool inText() const { return in_text_; }
463 void setTextMode(bool t) { in_text_ = t; }
473 odocstringstream deferred_;
475 friend class SetHTMLMode;
479 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
481 HtmlStream & operator<<(HtmlStream &, MathData const &);
483 HtmlStream & operator<<(HtmlStream &, docstring const &);
485 HtmlStream & operator<<(HtmlStream &, char const *);
487 HtmlStream & operator<<(HtmlStream &, char);
489 HtmlStream & operator<<(HtmlStream &, char_type);
491 HtmlStream & operator<<(HtmlStream &, MTag const &);
493 HtmlStream & operator<<(HtmlStream &, ETag const &);
499 explicit SetHTMLMode(HtmlStream & os, bool text);
517 explicit NormalStream(odocstream & os) : os_(os) {}
519 odocstream & os() { return os_; }
526 NormalStream & operator<<(NormalStream &, MathAtom const &);
528 NormalStream & operator<<(NormalStream &, MathData const &);
530 NormalStream & operator<<(NormalStream &, docstring const &);
532 NormalStream & operator<<(NormalStream &, char const *);
534 NormalStream & operator<<(NormalStream &, char);
536 NormalStream & operator<<(NormalStream &, int);
547 explicit MapleStream(odocstream & os) : os_(os) {}
549 odocstream & os() { return os_; }
557 MapleStream & operator<<(MapleStream &, MathAtom const &);
559 MapleStream & operator<<(MapleStream &, MathData const &);
561 MapleStream & operator<<(MapleStream &, docstring const &);
563 MapleStream & operator<<(MapleStream &, char_type);
565 MapleStream & operator<<(MapleStream &, char const *);
567 MapleStream & operator<<(MapleStream &, char);
569 MapleStream & operator<<(MapleStream &, int);
580 explicit MaximaStream(odocstream & os) : os_(os) {}
582 odocstream & os() { return os_; }
590 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
592 MaximaStream & operator<<(MaximaStream &, MathData const &);
594 MaximaStream & operator<<(MaximaStream &, docstring const &);
596 MaximaStream & operator<<(MaximaStream &, char_type);
598 MaximaStream & operator<<(MaximaStream &, char const *);
600 MaximaStream & operator<<(MaximaStream &, char);
602 MaximaStream & operator<<(MaximaStream &, int);
610 class MathematicaStream {
613 explicit MathematicaStream(odocstream & os) : os_(os) {}
615 odocstream & os() { return os_; }
623 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
625 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
627 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
629 MathematicaStream & operator<<(MathematicaStream &, char const *);
631 MathematicaStream & operator<<(MathematicaStream &, char);
633 MathematicaStream & operator<<(MathematicaStream &, int);
644 explicit OctaveStream(odocstream & os) : os_(os) {}
646 odocstream & os() { return os_; }
653 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
655 OctaveStream & operator<<(OctaveStream &, MathData const &);
657 OctaveStream & operator<<(OctaveStream &, docstring const &);
659 OctaveStream & operator<<(OctaveStream &, char_type);
661 OctaveStream & operator<<(OctaveStream &, char const *);
663 OctaveStream & operator<<(OctaveStream &, char);
665 OctaveStream & operator<<(OctaveStream &, int);
668 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);