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"
50 explicit TeXMathStream(otexrowstream & os, bool fragile = false,
51 bool latex = false, OutputType output = wsDefault,
52 Encoding const * encoding = nullptr);
56 int line() const { return line_; }
58 bool fragile() const { return fragile_; }
60 bool latex() const { return latex_; }
62 OutputType output() const { return output_; }
64 otexrowstream & os() { return os_; }
66 TexRow & texrow() { return os_.texrow(); }
68 bool & firstitem() { return firstitem_; }
70 void addlines(unsigned int);
71 /// record whether we can write an immediately following newline char
72 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
73 /// tell whether we can write an immediately following newline char
74 bool canBreakLine() const { return canbreakline_; }
75 /// record whether we have to take care for striking out display math
76 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
77 /// tell whether we have to take care for striking out display math
78 bool strikeoutMath() const { return mathsout_; }
79 /// record which ulem command type we are inside
80 void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
81 /// tell which ulem command type we are inside
82 UlemCmdType ulemCmd() const { return ulemcmd_; }
83 /// writes space if next thing is isalpha()
84 void pendingSpace(bool space);
85 /// writes space if next thing is isalpha()
86 bool pendingSpace() const { return pendingspace_; }
87 /// write braces if a space is pending and next char is [
88 /// or when a prime immediately follows a superscript
89 void useBraces(bool braces);
90 /// write braces if a space is pending and next char is [
91 /// or when a prime immediately follows a superscript
92 bool useBraces() const { return usebraces_; }
93 /// tell whether to write the closing brace of \ensuremath
94 void pendingBrace(bool brace);
95 /// tell whether to write the closing brace of \ensuremath
96 bool pendingBrace() const { return pendingbrace_; }
97 /// tell whether we are in text mode or not when producing latex code
98 void textMode(bool textmode);
99 /// tell whether we are in text mode or not when producing latex code
100 bool textMode() const { return textmode_; }
101 /// tell whether we are allowed to switch mode when producing latex code
102 void lockedMode(bool locked);
103 /// tell whether we are allowed to switch mode when producing latex code
104 bool lockedMode() const { return locked_; }
105 /// tell whether to use only ascii chars when producing latex code
106 void asciiOnly(bool ascii);
107 /// tell whether to use only ascii chars when producing latex code
108 bool asciiOnly() const { return ascii_; }
109 /// tell whether we are in a MathClass inset
110 void inMathClass(bool mathclass) { mathclass_ = mathclass; }
111 /// tell whether we are in a MathClass inset
112 bool inMathClass() const { return mathclass_; }
114 Encoding const * encoding() const { return encoding_; }
116 /// Temporarily change the TexRow information about the outer row entry.
117 Changer changeRowEntry(TexRow::RowEntry entry);
118 /// TexRow::starts the innermost outer math inset
119 /// returns true if the outer row entry will appear at this line
120 bool startOuterRow();
124 /// do we have to write \\protect sometimes
125 bool fragile_ = false;
126 /// are we at the beginning of an MathData?
127 bool firstitem_ = false;
128 /// are we writing to .tex?
130 /// output type (default, source preview, instant preview)?
131 OutputType output_ = wsDefault;
132 /// do we have a space pending?
133 bool pendingspace_ = false;
134 /// do we have to write braces when a space is pending and [ follows,
135 /// or when a prime immediately follows a superscript?
136 bool usebraces_ = false;
137 /// do we have a brace pending?
138 bool pendingbrace_ = false;
139 /// are we in text mode when producing latex code?
140 bool textmode_ = false;
141 /// are we allowed to switch mode when producing latex code?
142 bool locked_ = false;
143 /// should we use only ascii chars when producing latex code?
145 /// are we allowed to output an immediately following newline?
146 bool canbreakline_ = true;
147 /// should we take care for striking out display math?
148 bool mathsout_ = false;
149 /// what ulem command are we inside (none, underline, strikeout)?
150 UlemCmdType ulemcmd_ = NONE;
154 Encoding const * encoding_ = nullptr;
155 /// Row entry we are in
156 TexRow::RowEntry row_entry_ = TexRow::row_none;
157 /// whether we are in a MathClass inset
158 bool mathclass_ = false;
162 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
164 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
166 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
168 TeXMathStream & operator<<(TeXMathStream &, char const * const);
170 TeXMathStream & operator<<(TeXMathStream &, char);
172 TeXMathStream & operator<<(TeXMathStream &, int);
174 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
176 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
177 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
178 bool macro = false, bool textmode_macro = false);
180 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
181 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
185 * MathEnsurer - utility class for ensuring math mode
187 * A local variable of this type can be used to either ensure math mode
188 * or delay the writing of a pending brace when outputting LaTeX.
189 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
190 * no assumption is made for macros defined through \newcommand or \def.
194 * MathEnsurer ensurer(os);
196 * If not already in math mode, inserts an \ensuremath command followed
197 * by an open brace. This brace will be automatically closed when exiting
198 * math mode. Math mode is automatically exited when writing something
199 * that doesn't explicitly require math mode.
203 * MathEnsurer ensurer(os, false);
205 * Simply suspend writing a closing brace until the end of ensurer's scope.
209 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
211 * This form is mainly used for math macros as they are treated specially.
212 * In essence, the macros defined in the lib/symbols file and tagged as
213 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
214 * environment, while macros defined in the preamble or ERT are left as is.
215 * The third parameter must be set to true and the fourth parameter has also
216 * to be specified. Only the following 3 different cases are handled.
218 * When the needs_mathmode parameter is true the behavior is as in Example 1.
219 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
221 * When the needs_mathmode and textmode_macro parameters are both false the
222 * macro is left in the same (text or math mode) environment it was entered.
223 * This is because it is assumed that the macro was designed for that mode
224 * and we have no way to tell the contrary.
225 * This is the case for macros defined by using \newcommand or \def in ERT.
227 * When the needs_mathmode parameter is false while textmode_macro is true the
228 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
229 * This is the case for the macros tagged as textmode in lib/symbols.
235 explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
236 bool macro = false, bool textmode_macro = false)
237 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
239 ~MathEnsurer() { os_.pendingBrace(brace_); }
249 * ModeSpecifier - utility class for specifying a given mode (math or text)
251 * A local variable of this type can be used to specify that a command or
252 * environment works in a given mode. For example, \mbox works in text
253 * mode, but \boxed works in math mode. Note that no mode changing commands
254 * are needed, but we have to track the current mode, hence this class.
255 * This is only used when exporting to latex and helps determining whether
256 * the mode needs being temporarily switched when a command would not work
257 * in the current mode. As there are cases where this switching is to be
258 * avoided, the optional third parameter can be used to lock the mode.
259 * When the mode is locked, the optional fourth parameter specifies whether
260 * strings are to be output by using a suitable ascii representation.
264 * ModeSpecifier specifier(os, TEXT_MODE);
266 * Sets the current mode to text mode and allows mode switching.
270 * ModeSpecifier specifier(os, TEXT_MODE, true);
272 * Sets the current mode to text mode and disallows mode switching.
276 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
278 * Sets the current mode to text mode, disallows mode switching, and outputs
279 * strings as ascii only.
281 * At the end of specifier's scope the mode is reset to its previous value.
287 explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
288 bool locked = false, bool ascii = false)
289 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
293 os_.textMode(oldmodes_ & 0x01);
294 os_.lockedMode(oldmodes_ & 0x02);
295 os_.asciiOnly(oldmodes_ & 0x04);
315 MTag(char const * const tag, std::string const & attr = std::string())
316 : tag_(tag), attr_(attr) {}
318 char const * const tag_;
323 /// Start inline tag.
327 MTagInline(char const * const tag, std::string const & attr = std::string())
328 : tag_(tag), attr_(attr) {}
330 char const * const tag_;
340 explicit ETag(char const * const tag) : tag_(tag) {}
342 char const * const tag_;
350 explicit ETagInline(char const * const tag) : tag_(tag) {}
352 char const * const tag_;
356 /// Compound tag (no content, directly closed).
360 CTag(char const * const tag, std::string const & attr = "")
361 : tag_(tag), attr_(attr) {}
363 char const * const tag_;
369 /// Throw MathExportException to signal that the attempt to export
370 /// some math in the current format did not succeed. E.g., we can't
371 /// export xymatrix as MathML, so that will throw, and we'll fall back
373 class MathExportException : public std::exception {};
378 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
379 explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
383 odocstream & os() { return os_; }
385 int line() const { return line_; }
387 int & tab() { return tab_; }
389 friend MathMLStream & operator<<(MathMLStream &, char const *);
391 void defer(docstring const &);
393 void defer(std::string const &);
395 docstring deferred() const;
397 bool inText() const { return in_text_; }
399 std::string xmlns() const { return xmlns_; }
401 bool xmlMode() const { return xml_mode_; }
402 /// Returns the tag name prefixed by the name space if needed.
403 std::string namespacedTag(std::string const & tag) const {
404 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
406 /// Returns the current math style in the stream.
407 const MathStyle & getFontMathStyle() const { return font_math_style_; }
408 /// Returns the current math style in the stream.
409 void setFontMathStyle(const MathStyle style) { font_math_style_ = style; }
412 void setTextMode(bool t) { in_text_ = t; }
422 odocstringstream deferred_;
427 /// The only important part of a FontInfo object.
428 MathStyle font_math_style_;
430 friend class SetMode;
434 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
436 MathMLStream & operator<<(MathMLStream &, MathData const &);
438 MathMLStream & operator<<(MathMLStream &, docstring const &);
440 MathMLStream & operator<<(MathMLStream &, char const *);
442 MathMLStream & operator<<(MathMLStream &, char);
444 MathMLStream & operator<<(MathMLStream &, char_type);
446 MathMLStream & operator<<(MathMLStream &, MTag const &);
448 MathMLStream & operator<<(MathMLStream &, MTagInline const &);
450 MathMLStream & operator<<(MathMLStream &, ETag const &);
452 MathMLStream & operator<<(MathMLStream &, ETagInline const &);
454 MathMLStream & operator<<(MathMLStream &, CTag const &);
457 /// A simpler version of ModeSpecifier, for MathML
461 explicit SetMode(MathMLStream & ms, bool text);
475 explicit HtmlStream(odocstream & os);
479 odocstream & os() { return os_; }
481 int line() const { return line_; }
483 int & tab() { return tab_; }
485 friend HtmlStream & operator<<(HtmlStream &, char const *);
487 void defer(docstring const &);
489 void defer(std::string const &);
491 docstring deferred() const;
493 bool inText() const { return in_text_; }
496 void setTextMode(bool t) { in_text_ = t; }
506 odocstringstream deferred_;
508 friend class SetHTMLMode;
512 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
514 HtmlStream & operator<<(HtmlStream &, MathData const &);
516 HtmlStream & operator<<(HtmlStream &, docstring const &);
518 HtmlStream & operator<<(HtmlStream &, char const *);
520 HtmlStream & operator<<(HtmlStream &, char);
522 HtmlStream & operator<<(HtmlStream &, char_type);
524 HtmlStream & operator<<(HtmlStream &, MTag const &);
526 HtmlStream & operator<<(HtmlStream &, ETag const &);
532 explicit SetHTMLMode(HtmlStream & os, bool text);
550 explicit NormalStream(odocstream & os) : os_(os) {}
552 odocstream & os() { return os_; }
559 NormalStream & operator<<(NormalStream &, MathAtom const &);
561 NormalStream & operator<<(NormalStream &, MathData const &);
563 NormalStream & operator<<(NormalStream &, docstring const &);
565 NormalStream & operator<<(NormalStream &, char const *);
567 NormalStream & operator<<(NormalStream &, char);
569 NormalStream & operator<<(NormalStream &, int);
580 explicit MapleStream(odocstream & os) : os_(os) {}
582 odocstream & os() { return os_; }
590 MapleStream & operator<<(MapleStream &, MathAtom const &);
592 MapleStream & operator<<(MapleStream &, MathData const &);
594 MapleStream & operator<<(MapleStream &, docstring const &);
596 MapleStream & operator<<(MapleStream &, char_type);
598 MapleStream & operator<<(MapleStream &, char const *);
600 MapleStream & operator<<(MapleStream &, char);
602 MapleStream & operator<<(MapleStream &, int);
613 explicit MaximaStream(odocstream & os) : os_(os) {}
615 odocstream & os() { return os_; }
623 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
625 MaximaStream & operator<<(MaximaStream &, MathData const &);
627 MaximaStream & operator<<(MaximaStream &, docstring const &);
629 MaximaStream & operator<<(MaximaStream &, char_type);
631 MaximaStream & operator<<(MaximaStream &, char const *);
633 MaximaStream & operator<<(MaximaStream &, char);
635 MaximaStream & operator<<(MaximaStream &, int);
643 class MathematicaStream {
646 explicit MathematicaStream(odocstream & os) : os_(os) {}
648 odocstream & os() { return os_; }
656 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
658 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
660 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
662 MathematicaStream & operator<<(MathematicaStream &, char const *);
664 MathematicaStream & operator<<(MathematicaStream &, char);
666 MathematicaStream & operator<<(MathematicaStream &, int);
677 explicit OctaveStream(odocstream & os) : os_(os) {}
679 odocstream & os() { return os_; }
686 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
688 OctaveStream & operator<<(OctaveStream &, MathData const &);
690 OctaveStream & operator<<(OctaveStream &, docstring const &);
692 OctaveStream & operator<<(OctaveStream &, char_type);
694 OctaveStream & operator<<(OctaveStream &, char const *);
696 OctaveStream & operator<<(OctaveStream &, char);
698 OctaveStream & operator<<(OctaveStream &, int);
701 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);