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 void useBraces(bool braces);
88 /// write braces if a space is pending and next char is [
89 bool useBraces() const { return usebraces_; }
90 /// tell whether to write the closing brace of \ensuremath
91 void pendingBrace(bool brace);
92 /// tell whether to write the closing brace of \ensuremath
93 bool pendingBrace() const { return pendingbrace_; }
94 /// tell whether we are in text mode or not when producing latex code
95 void textMode(bool textmode);
96 /// tell whether we are in text mode or not when producing latex code
97 bool textMode() const { return textmode_; }
98 /// tell whether we are allowed to switch mode when producing latex code
99 void lockedMode(bool locked);
100 /// tell whether we are allowed to switch mode when producing latex code
101 bool lockedMode() const { return locked_; }
102 /// tell whether to use only ascii chars when producing latex code
103 void asciiOnly(bool ascii);
104 /// tell whether to use only ascii chars when producing latex code
105 bool asciiOnly() const { return ascii_; }
106 /// tell whether we are in a MathClass inset
107 void inMathClass(bool mathclass) { mathclass_ = mathclass; };
108 /// tell whether we are in a MathClass inset
109 bool inMathClass() const { return mathclass_; }
111 Encoding const * encoding() const { return encoding_; }
113 /// Temporarily change the TexRow information about the outer row entry.
114 Changer changeRowEntry(TexRow::RowEntry entry);
115 /// TexRow::starts the innermost outer math inset
116 /// returns true if the outer row entry will appear at this line
117 bool startOuterRow();
121 /// do we have to write \\protect sometimes
122 bool fragile_ = false;
123 /// are we at the beginning of an MathData?
124 bool firstitem_ = false;
125 /// are we writing to .tex?
127 /// output type (default, source preview, instant preview)?
128 OutputType output_ = wsDefault;
129 /// do we have a space pending?
130 bool pendingspace_ = false;
131 /// do we have to write braces when a space is pending and [ follows?
132 bool usebraces_ = false;
133 /// do we have a brace pending?
134 bool pendingbrace_ = false;
135 /// are we in text mode when producing latex code?
136 bool textmode_ = false;
137 /// are we allowed to switch mode when producing latex code?
138 bool locked_ = false;
139 /// should we use only ascii chars when producing latex code?
141 /// are we allowed to output an immediately following newline?
142 bool canbreakline_ = true;
143 /// should we take care for striking out display math?
144 bool mathsout_ = false;
145 /// what ulem command are we inside (none, underline, strikeout)?
146 UlemCmdType ulemcmd_ = NONE;
150 Encoding const * encoding_ = nullptr;
151 /// Row entry we are in
152 TexRow::RowEntry row_entry_ = TexRow::row_none;
153 /// whether we are in a MathClass inset
154 bool mathclass_ = false;
158 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
160 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
162 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
164 TeXMathStream & operator<<(TeXMathStream &, char const * const);
166 TeXMathStream & operator<<(TeXMathStream &, char);
168 TeXMathStream & operator<<(TeXMathStream &, int);
170 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
172 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
173 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
174 bool macro = false, bool textmode_macro = false);
176 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
177 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
181 * MathEnsurer - utility class for ensuring math mode
183 * A local variable of this type can be used to either ensure math mode
184 * or delay the writing of a pending brace when outputting LaTeX.
185 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
186 * no assumption is made for macros defined through \newcommand or \def.
190 * MathEnsurer ensurer(os);
192 * If not already in math mode, inserts an \ensuremath command followed
193 * by an open brace. This brace will be automatically closed when exiting
194 * math mode. Math mode is automatically exited when writing something
195 * that doesn't explicitly require math mode.
199 * MathEnsurer ensurer(os, false);
201 * Simply suspend writing a closing brace until the end of ensurer's scope.
205 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
207 * This form is mainly used for math macros as they are treated specially.
208 * In essence, the macros defined in the lib/symbols file and tagged as
209 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
210 * environment, while macros defined in the preamble or ERT are left as is.
211 * The third parameter must be set to true and the fourth parameter has also
212 * to be specified. Only the following 3 different cases are handled.
214 * When the needs_mathmode parameter is true the behavior is as in Example 1.
215 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
217 * When the needs_mathmode and textmode_macro parameters are both false the
218 * macro is left in the same (text or math mode) environment it was entered.
219 * This is because it is assumed that the macro was designed for that mode
220 * and we have no way to tell the contrary.
221 * This is the case for macros defined by using \newcommand or \def in ERT.
223 * When the needs_mathmode parameter is false while textmode_macro is true the
224 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
225 * This is the case for the macros tagged as textmode in lib/symbols.
231 explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
232 bool macro = false, bool textmode_macro = false)
233 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
235 ~MathEnsurer() { os_.pendingBrace(brace_); }
245 * ModeSpecifier - utility class for specifying a given mode (math or text)
247 * A local variable of this type can be used to specify that a command or
248 * environment works in a given mode. For example, \mbox works in text
249 * mode, but \boxed works in math mode. Note that no mode changing commands
250 * are needed, but we have to track the current mode, hence this class.
251 * This is only used when exporting to latex and helps determining whether
252 * the mode needs being temporarily switched when a command would not work
253 * in the current mode. As there are cases where this switching is to be
254 * avoided, the optional third parameter can be used to lock the mode.
255 * When the mode is locked, the optional fourth parameter specifies whether
256 * strings are to be output by using a suitable ascii representation.
260 * ModeSpecifier specifier(os, TEXT_MODE);
262 * Sets the current mode to text mode and allows mode switching.
266 * ModeSpecifier specifier(os, TEXT_MODE, true);
268 * Sets the current mode to text mode and disallows mode switching.
272 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
274 * Sets the current mode to text mode, disallows mode switching, and outputs
275 * strings as ascii only.
277 * At the end of specifier's scope the mode is reset to its previous value.
283 explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
284 bool locked = false, bool ascii = false)
285 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
289 os_.textMode(oldmodes_ & 0x01);
290 os_.lockedMode(oldmodes_ & 0x02);
291 os_.asciiOnly(oldmodes_ & 0x04);
311 MTag(char const * const tag, std::string const & attr = std::string())
312 : tag_(tag), attr_(attr) {}
314 char const * const tag_;
324 explicit ETag(char const * const tag) : tag_(tag) {}
326 char const * const tag_;
330 /// Compound tag (no content, directly closed).
334 CTag(char const * const tag, std::string const & attr = "")
335 : tag_(tag), attr_(attr) {}
337 char const * const tag_;
343 /// Throw MathExportException to signal that the attempt to export
344 /// some math in the current format did not succeed. E.g., we can't
345 /// export xymatrix as MathML, so that will throw, and we'll fall back
347 class MathExportException : public std::exception {};
352 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
353 explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
357 odocstream & os() { return os_; }
359 int line() const { return line_; }
361 int & tab() { return tab_; }
363 friend MathMLStream & operator<<(MathMLStream &, char const *);
365 void defer(docstring const &);
367 void defer(std::string const &);
369 docstring deferred() const;
371 bool inText() const { return in_text_; }
373 std::string xmlns() const { return xmlns_; }
375 bool xmlMode() const { return xml_mode_; }
376 /// Returns the tag name prefixed by the name space if needed.
377 std::string namespacedTag(std::string const & tag) const {
378 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
382 void setTextMode(bool t) { in_text_ = t; }
392 odocstringstream deferred_;
398 friend class SetMode;
402 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
404 MathMLStream & operator<<(MathMLStream &, MathData const &);
406 MathMLStream & operator<<(MathMLStream &, docstring const &);
408 MathMLStream & operator<<(MathMLStream &, char const *);
410 MathMLStream & operator<<(MathMLStream &, char);
412 MathMLStream & operator<<(MathMLStream &, char_type);
414 MathMLStream & operator<<(MathMLStream &, MTag const &);
416 MathMLStream & operator<<(MathMLStream &, ETag const &);
418 MathMLStream & operator<<(MathMLStream &, CTag const &);
421 /// A simpler version of ModeSpecifier, for MathML
425 explicit SetMode(MathMLStream & ms, bool text);
439 explicit HtmlStream(odocstream & os);
443 odocstream & os() { return os_; }
445 int line() const { return line_; }
447 int & tab() { return tab_; }
449 friend HtmlStream & operator<<(HtmlStream &, char const *);
451 void defer(docstring const &);
453 void defer(std::string const &);
455 docstring deferred() const;
457 bool inText() const { return in_text_; }
460 void setTextMode(bool t) { in_text_ = t; }
470 odocstringstream deferred_;
472 friend class SetHTMLMode;
476 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
478 HtmlStream & operator<<(HtmlStream &, MathData const &);
480 HtmlStream & operator<<(HtmlStream &, docstring const &);
482 HtmlStream & operator<<(HtmlStream &, char const *);
484 HtmlStream & operator<<(HtmlStream &, char);
486 HtmlStream & operator<<(HtmlStream &, char_type);
488 HtmlStream & operator<<(HtmlStream &, MTag const &);
490 HtmlStream & operator<<(HtmlStream &, ETag const &);
496 explicit SetHTMLMode(HtmlStream & os, bool text);
514 explicit NormalStream(odocstream & os) : os_(os) {}
516 odocstream & os() { return os_; }
523 NormalStream & operator<<(NormalStream &, MathAtom const &);
525 NormalStream & operator<<(NormalStream &, MathData const &);
527 NormalStream & operator<<(NormalStream &, docstring const &);
529 NormalStream & operator<<(NormalStream &, char const *);
531 NormalStream & operator<<(NormalStream &, char);
533 NormalStream & operator<<(NormalStream &, int);
544 explicit MapleStream(odocstream & os) : os_(os) {}
546 odocstream & os() { return os_; }
554 MapleStream & operator<<(MapleStream &, MathAtom const &);
556 MapleStream & operator<<(MapleStream &, MathData const &);
558 MapleStream & operator<<(MapleStream &, docstring const &);
560 MapleStream & operator<<(MapleStream &, char_type);
562 MapleStream & operator<<(MapleStream &, char const *);
564 MapleStream & operator<<(MapleStream &, char);
566 MapleStream & operator<<(MapleStream &, int);
577 explicit MaximaStream(odocstream & os) : os_(os) {}
579 odocstream & os() { return os_; }
587 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
589 MaximaStream & operator<<(MaximaStream &, MathData const &);
591 MaximaStream & operator<<(MaximaStream &, docstring const &);
593 MaximaStream & operator<<(MaximaStream &, char_type);
595 MaximaStream & operator<<(MaximaStream &, char const *);
597 MaximaStream & operator<<(MaximaStream &, char);
599 MaximaStream & operator<<(MaximaStream &, int);
607 class MathematicaStream {
610 explicit MathematicaStream(odocstream & os) : os_(os) {}
612 odocstream & os() { return os_; }
620 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
622 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
624 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
626 MathematicaStream & operator<<(MathematicaStream &, char const *);
628 MathematicaStream & operator<<(MathematicaStream &, char);
630 MathematicaStream & operator<<(MathematicaStream &, int);
641 explicit OctaveStream(odocstream & os) : os_(os) {}
643 odocstream & os() { return os_; }
650 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
652 OctaveStream & operator<<(OctaveStream &, MathData const &);
654 OctaveStream & operator<<(OctaveStream &, docstring const &);
656 OctaveStream & operator<<(OctaveStream &, char_type);
658 OctaveStream & operator<<(OctaveStream &, char const *);
660 OctaveStream & operator<<(OctaveStream &, char);
662 OctaveStream & operator<<(OctaveStream &, int);
665 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);