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"
19 #include "texstream.h"
21 #include "support/Changer.h"
22 #include "support/strfwd.h"
51 explicit TeXMathStream(otexrowstream & os, bool fragile = false,
52 bool latex = false, OutputType output = wsDefault,
53 Encoding const * encoding = nullptr);
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 space);
86 /// writes space if next thing is isalpha()
87 bool pendingSpace() const { return pendingspace_; }
88 /// write braces if a space is pending and next char is [
89 /// or when a prime immediately follows a superscript
90 void useBraces(bool braces);
91 /// write braces if a space is pending and next char is [
92 /// or when a prime immediately follows a superscript
93 bool useBraces() const { return usebraces_; }
94 /// tell whether to write the closing brace of \ensuremath
95 void pendingBrace(bool brace);
96 /// tell whether to write the closing brace of \ensuremath
97 bool pendingBrace() const { return pendingbrace_; }
98 /// tell whether we are in text mode or not when producing latex code
99 void textMode(bool textmode);
100 /// tell whether we are in text mode or not when producing latex code
101 bool textMode() const { return textmode_; }
102 /// tell whether we are allowed to switch mode when producing latex code
103 void lockedMode(bool locked);
104 /// tell whether we are allowed to switch mode when producing latex code
105 bool lockedMode() const { return locked_; }
106 /// tell whether to use only ascii chars when producing latex code
107 void asciiOnly(bool ascii);
108 /// tell whether to use only ascii chars when producing latex code
109 bool asciiOnly() const { return ascii_; }
110 /// tell whether we are in a MathClass inset
111 void inMathClass(bool mathclass) { mathclass_ = mathclass; }
112 /// tell whether we are in a MathClass inset
113 bool inMathClass() const { return mathclass_; }
115 Encoding const * encoding() const { return encoding_; }
117 /// Temporarily change the TexRow information about the outer row entry.
118 Changer changeRowEntry(TexRow::RowEntry entry);
119 /// TexRow::starts the innermost outer math inset
120 /// returns true if the outer row entry will appear at this line
121 bool startOuterRow();
125 /// do we have to write \\protect sometimes
126 bool fragile_ = false;
127 /// are we at the beginning of an MathData?
128 bool firstitem_ = false;
129 /// are we writing to .tex?
131 /// output type (default, source preview, instant preview)?
132 OutputType output_ = wsDefault;
133 /// do we have a space pending?
134 bool pendingspace_ = false;
135 /// do we have to write braces when a space is pending and [ follows,
136 /// or when a prime immediately follows a superscript?
137 bool usebraces_ = false;
138 /// do we have a brace pending?
139 bool pendingbrace_ = false;
140 /// are we in text mode when producing latex code?
141 bool textmode_ = false;
142 /// are we allowed to switch mode when producing latex code?
143 bool locked_ = false;
144 /// should we use only ascii chars when producing latex code?
146 /// are we allowed to output an immediately following newline?
147 bool canbreakline_ = true;
148 /// should we take care for striking out display math?
149 bool mathsout_ = false;
150 /// what ulem command are we inside (none, underline, strikeout)?
151 UlemCmdType ulemcmd_ = NONE;
155 Encoding const * encoding_ = nullptr;
156 /// Row entry we are in
157 TexRow::RowEntry row_entry_ = TexRow::row_none;
158 /// whether we are in a MathClass inset
159 bool mathclass_ = false;
163 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
165 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
167 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
169 TeXMathStream & operator<<(TeXMathStream &, char const * const);
171 TeXMathStream & operator<<(TeXMathStream &, char);
173 TeXMathStream & operator<<(TeXMathStream &, int);
175 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
177 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
178 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
179 bool macro = false, bool textmode_macro = false);
181 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
182 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
186 * MathEnsurer - utility class for ensuring math mode
188 * A local variable of this type can be used to either ensure math mode
189 * or delay the writing of a pending brace when outputting LaTeX.
190 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
191 * no assumption is made for macros defined through \newcommand or \def.
195 * MathEnsurer ensurer(os);
197 * If not already in math mode, inserts an \ensuremath command followed
198 * by an open brace. This brace will be automatically closed when exiting
199 * math mode. Math mode is automatically exited when writing something
200 * that doesn't explicitly require math mode.
204 * MathEnsurer ensurer(os, false);
206 * Simply suspend writing a closing brace until the end of ensurer's scope.
210 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
212 * This form is mainly used for math macros as they are treated specially.
213 * In essence, the macros defined in the lib/symbols file and tagged as
214 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
215 * environment, while macros defined in the preamble or ERT are left as is.
216 * The third parameter must be set to true and the fourth parameter has also
217 * to be specified. Only the following 3 different cases are handled.
219 * When the needs_mathmode parameter is true the behavior is as in Example 1.
220 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
222 * When the needs_mathmode and textmode_macro parameters are both false the
223 * macro is left in the same (text or math mode) environment it was entered.
224 * This is because it is assumed that the macro was designed for that mode
225 * and we have no way to tell the contrary.
226 * This is the case for macros defined by using \newcommand or \def in ERT.
228 * When the needs_mathmode parameter is false while textmode_macro is true the
229 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
230 * This is the case for the macros tagged as textmode in lib/symbols.
236 explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
237 bool macro = false, bool textmode_macro = false)
238 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
240 ~MathEnsurer() { os_.pendingBrace(brace_); }
250 * ModeSpecifier - utility class for specifying a given mode (math or text)
252 * A local variable of this type can be used to specify that a command or
253 * environment works in a given mode. For example, \mbox works in text
254 * mode, but \boxed works in math mode. Note that no mode changing commands
255 * are needed, but we have to track the current mode, hence this class.
256 * This is only used when exporting to latex and helps determining whether
257 * the mode needs being temporarily switched when a command would not work
258 * in the current mode. As there are cases where this switching is to be
259 * avoided, the optional third parameter can be used to lock the mode.
260 * When the mode is locked, the optional fourth parameter specifies whether
261 * strings are to be output by using a suitable ascii representation.
265 * ModeSpecifier specifier(os, TEXT_MODE);
267 * Sets the current mode to text mode and allows mode switching.
271 * ModeSpecifier specifier(os, TEXT_MODE, true);
273 * Sets the current mode to text mode and disallows mode switching.
277 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
279 * Sets the current mode to text mode, disallows mode switching, and outputs
280 * strings as ascii only.
282 * At the end of specifier's scope the mode is reset to its previous value.
288 explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
289 bool locked = false, bool ascii = false)
290 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
294 os_.textMode(oldmodes_ & 0x01);
295 os_.lockedMode(oldmodes_ & 0x02);
296 os_.asciiOnly(oldmodes_ & 0x04);
316 MTag(char const * const tag, std::string const & attr = std::string())
317 : tag_(tag), attr_(attr) {}
319 char const * const tag_;
324 /// Start inline tag.
328 MTagInline(char const * const tag, std::string const & attr = std::string())
329 : tag_(tag), attr_(attr) {}
331 char const * const tag_;
341 explicit ETag(char const * const tag) : tag_(tag) {}
343 char const * const tag_;
351 explicit ETagInline(char const * const tag) : tag_(tag) {}
353 char const * const tag_;
357 /// Compound tag (no content, directly closed).
361 CTag(char const * const tag, std::string const & attr = "")
362 : tag_(tag), attr_(attr) {}
364 char const * const tag_;
370 /// Throw MathExportException to signal that the attempt to export
371 /// some math in the current format did not succeed. E.g., we can't
372 /// export xymatrix as MathML, so that will throw, and we'll fall back
374 class MathExportException : public std::exception {};
379 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
380 explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
384 odocstream & os() { return os_; }
386 int line() const { return line_; }
388 int & tab() { return tab_; }
390 friend MathMLStream & operator<<(MathMLStream &, char const *);
392 void defer(docstring const &);
394 void defer(std::string const &);
396 docstring deferred() const;
398 bool inText() const { return in_text_; }
400 std::string xmlns() const { return xmlns_; }
402 bool xmlMode() const { return xml_mode_; }
403 /// Returns the tag name prefixed by the name space if needed.
404 std::string namespacedTag(std::string const & tag) const {
405 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
407 /// Returns the current math style in the stream.
408 const MathStyle & getFontMathStyle() const { return font_math_style_; }
409 /// Returns the current math style in the stream.
410 void setFontMathStyle(const MathStyle style) { font_math_style_ = style; }
413 void setTextMode(bool t) { in_text_ = t; }
423 odocstringstream deferred_;
428 /// The only important part of a FontInfo object.
429 MathStyle font_math_style_;
431 friend class SetMode;
435 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
437 MathMLStream & operator<<(MathMLStream &, MathData const &);
439 MathMLStream & operator<<(MathMLStream &, docstring const &);
441 MathMLStream & operator<<(MathMLStream &, char const *);
443 MathMLStream & operator<<(MathMLStream &, char);
445 MathMLStream & operator<<(MathMLStream &, char_type);
447 MathMLStream & operator<<(MathMLStream &, MTag const &);
449 MathMLStream & operator<<(MathMLStream &, MTagInline const &);
451 MathMLStream & operator<<(MathMLStream &, ETag const &);
453 MathMLStream & operator<<(MathMLStream &, ETagInline const &);
455 MathMLStream & operator<<(MathMLStream &, CTag const &);
458 /// A simpler version of ModeSpecifier, for MathML
462 explicit SetMode(MathMLStream & ms, bool text);
476 explicit HtmlStream(odocstream & os);
480 odocstream & os() { return os_; }
482 int line() const { return line_; }
484 int & tab() { return tab_; }
486 friend HtmlStream & operator<<(HtmlStream &, char const *);
488 void defer(docstring const &);
490 void defer(std::string const &);
492 docstring deferred() const;
494 bool inText() const { return in_text_; }
497 void setTextMode(bool t) { in_text_ = t; }
507 odocstringstream deferred_;
509 friend class SetHTMLMode;
513 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
515 HtmlStream & operator<<(HtmlStream &, MathData const &);
517 HtmlStream & operator<<(HtmlStream &, docstring const &);
519 HtmlStream & operator<<(HtmlStream &, char const *);
521 HtmlStream & operator<<(HtmlStream &, char);
523 HtmlStream & operator<<(HtmlStream &, char_type);
525 HtmlStream & operator<<(HtmlStream &, MTag const &);
527 HtmlStream & operator<<(HtmlStream &, ETag const &);
533 explicit SetHTMLMode(HtmlStream & os, bool text);
551 explicit NormalStream(odocstream & os) : os_(os) {}
553 odocstream & os() { return os_; }
560 NormalStream & operator<<(NormalStream &, MathAtom const &);
562 NormalStream & operator<<(NormalStream &, MathData const &);
564 NormalStream & operator<<(NormalStream &, docstring const &);
566 NormalStream & operator<<(NormalStream &, char const *);
568 NormalStream & operator<<(NormalStream &, char);
570 NormalStream & operator<<(NormalStream &, int);
581 explicit MapleStream(odocstream & os) : os_(os) {}
583 odocstream & os() { return os_; }
591 MapleStream & operator<<(MapleStream &, MathAtom const &);
593 MapleStream & operator<<(MapleStream &, MathData const &);
595 MapleStream & operator<<(MapleStream &, docstring const &);
597 MapleStream & operator<<(MapleStream &, char_type);
599 MapleStream & operator<<(MapleStream &, char const *);
601 MapleStream & operator<<(MapleStream &, char);
603 MapleStream & operator<<(MapleStream &, int);
614 explicit MaximaStream(odocstream & os) : os_(os) {}
616 odocstream & os() { return os_; }
624 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
626 MaximaStream & operator<<(MaximaStream &, MathData const &);
628 MaximaStream & operator<<(MaximaStream &, docstring const &);
630 MaximaStream & operator<<(MaximaStream &, char_type);
632 MaximaStream & operator<<(MaximaStream &, char const *);
634 MaximaStream & operator<<(MaximaStream &, char);
636 MaximaStream & operator<<(MaximaStream &, int);
644 class MathematicaStream {
647 explicit MathematicaStream(odocstream & os) : os_(os) {}
649 odocstream & os() { return os_; }
657 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
659 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
661 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
663 MathematicaStream & operator<<(MathematicaStream &, char const *);
665 MathematicaStream & operator<<(MathematicaStream &, char);
667 MathematicaStream & operator<<(MathematicaStream &, int);
678 explicit OctaveStream(odocstream & os) : os_(os) {}
680 odocstream & os() { return os_; }
687 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
689 OctaveStream & operator<<(OctaveStream &, MathData const &);
691 OctaveStream & operator<<(OctaveStream &, docstring const &);
693 OctaveStream & operator<<(OctaveStream &, char_type);
695 OctaveStream & operator<<(OctaveStream &, char const *);
697 OctaveStream & operator<<(OctaveStream &, char);
699 OctaveStream & operator<<(OctaveStream &, int);
702 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);