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 WriteStream(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 how);
84 /// writes space if next thing is isalpha()
85 bool pendingSpace() const { return pendingspace_; }
86 /// tell whether to write the closing brace of \ensuremath
87 void pendingBrace(bool brace);
88 /// tell whether to write the closing brace of \ensuremath
89 bool pendingBrace() const { return pendingbrace_; }
90 /// tell whether we are in text mode or not when producing latex code
91 void textMode(bool textmode);
92 /// tell whether we are in text mode or not when producing latex code
93 bool textMode() const { return textmode_; }
94 /// tell whether we are allowed to switch mode when producing latex code
95 void lockedMode(bool locked);
96 /// tell whether we are allowed to switch mode when producing latex code
97 bool lockedMode() const { return locked_; }
98 /// tell whether to use only ascii chars when producing latex code
99 void asciiOnly(bool ascii);
100 /// tell whether to use only ascii chars when producing latex code
101 bool asciiOnly() const { return ascii_; }
102 /// tell whether we are in a MathClass inset
103 void inMathClass(bool mathclass) { mathclass_ = mathclass; };
104 /// tell whether we are in a MathClass inset
105 bool inMathClass() const { return mathclass_; }
107 Encoding const * encoding() const { return encoding_; }
109 /// Temporarily change the TexRow information about the outer row entry.
110 Changer changeRowEntry(TexRow::RowEntry entry);
111 /// TexRow::starts the innermost outer math inset
112 /// returns true if the outer row entry will appear at this line
113 bool startOuterRow();
117 /// do we have to write \\protect sometimes
118 bool fragile_ = false;
119 /// are we at the beginning of an MathData?
120 bool firstitem_ = false;
121 /// are we writing to .tex?
123 /// output type (default, source preview, instant preview)?
124 OutputType output_ = wsDefault;
125 /// do we have a space pending?
126 bool pendingspace_ = false;
127 /// do we have a brace pending?
128 bool pendingbrace_ = false;
129 /// are we in text mode when producing latex code?
130 bool textmode_ = false;
131 /// are we allowed to switch mode when producing latex code?
132 bool locked_ = false;
133 /// should we use only ascii chars when producing latex code?
135 /// are we allowed to output an immediately following newline?
136 bool canbreakline_ = true;
137 /// should we take care for striking out display math?
138 bool mathsout_ = false;
139 /// what ulem command are we inside (none, underline, strikeout)?
140 UlemCmdType ulemcmd_ = NONE;
144 Encoding const * encoding_ = nullptr;
145 /// Row entry we are in
146 TexRow::RowEntry row_entry_ = TexRow::row_none;
147 /// whether we are in a MathClass inset
148 bool mathclass_ = false;
152 WriteStream & operator<<(WriteStream &, MathAtom const &);
154 WriteStream & operator<<(WriteStream &, MathData const &);
156 WriteStream & operator<<(WriteStream &, docstring const &);
158 WriteStream & operator<<(WriteStream &, char const * const);
160 WriteStream & operator<<(WriteStream &, char);
162 WriteStream & operator<<(WriteStream &, int);
164 WriteStream & operator<<(WriteStream &, unsigned int);
166 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
167 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
168 bool macro = false, bool textmode_macro = false);
170 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
171 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
175 * MathEnsurer - utility class for ensuring math mode
177 * A local variable of this type can be used to either ensure math mode
178 * or delay the writing of a pending brace when outputting LaTeX.
179 * A LyX InsetMathMacro is always assumed needing a math mode environment, while
180 * no assumption is made for macros defined through \newcommand or \def.
184 * MathEnsurer ensurer(os);
186 * If not already in math mode, inserts an \ensuremath command followed
187 * by an open brace. This brace will be automatically closed when exiting
188 * math mode. Math mode is automatically exited when writing something
189 * that doesn't explicitly require math mode.
193 * MathEnsurer ensurer(os, false);
195 * Simply suspend writing a closing brace until the end of ensurer's scope.
199 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
201 * This form is mainly used for math macros as they are treated specially.
202 * In essence, the macros defined in the lib/symbols file and tagged as
203 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
204 * environment, while macros defined in the preamble or ERT are left as is.
205 * The third parameter must be set to true and the fourth parameter has also
206 * to be specified. Only the following 3 different cases are handled.
208 * When the needs_mathmode parameter is true the behavior is as in Example 1.
209 * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
211 * When the needs_mathmode and textmode_macro parameters are both false the
212 * macro is left in the same (text or math mode) environment it was entered.
213 * This is because it is assumed that the macro was designed for that mode
214 * and we have no way to tell the contrary.
215 * This is the case for macros defined by using \newcommand or \def in ERT.
217 * When the needs_mathmode parameter is false while textmode_macro is true the
218 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
219 * This is the case for the macros tagged as textmode in lib/symbols.
225 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
226 bool macro = false, bool textmode_macro = false)
227 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
229 ~MathEnsurer() { os_.pendingBrace(brace_); }
239 * ModeSpecifier - utility class for specifying a given mode (math or text)
241 * A local variable of this type can be used to specify that a command or
242 * environment works in a given mode. For example, \mbox works in text
243 * mode, but \boxed works in math mode. Note that no mode changing commands
244 * are needed, but we have to track the current mode, hence this class.
245 * This is only used when exporting to latex and helps determining whether
246 * the mode needs being temporarily switched when a command would not work
247 * in the current mode. As there are cases where this switching is to be
248 * avoided, the optional third parameter can be used to lock the mode.
249 * When the mode is locked, the optional fourth parameter specifies whether
250 * strings are to be output by using a suitable ascii representation.
254 * ModeSpecifier specifier(os, TEXT_MODE);
256 * Sets the current mode to text mode and allows mode switching.
260 * ModeSpecifier specifier(os, TEXT_MODE, true);
262 * Sets the current mode to text mode and disallows mode switching.
266 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
268 * Sets the current mode to text mode, disallows mode switching, and outputs
269 * strings as ascii only.
271 * At the end of specifier's scope the mode is reset to its previous value.
277 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
278 bool locked = false, bool ascii = false)
279 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
283 os_.textMode(oldmodes_ & 0x01);
284 os_.lockedMode(oldmodes_ & 0x02);
285 os_.asciiOnly(oldmodes_ & 0x04);
305 MTag(char const * const tag, std::string const & attr = std::string())
306 : tag_(tag), attr_(attr) {}
308 char const * const tag_;
318 explicit ETag(char const * const tag) : tag_(tag) {}
320 char const * const tag_;
324 /// Compound tag (no content, directly closed).
328 CTag(char const * const tag, std::string const & attr = "")
329 : tag_(tag), attr_(attr) {}
331 char const * const tag_;
337 /// Throw MathExportException to signal that the attempt to export
338 /// some math in the current format did not succeed. E.g., we can't
339 /// export xymatrix as MathML, so that will throw, and we'll fall back
341 class MathExportException : public std::exception {};
346 /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
347 explicit MathStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
351 odocstream & os() { return os_; }
353 int line() const { return line_; }
355 int & tab() { return tab_; }
357 friend MathStream & operator<<(MathStream &, char const *);
359 void defer(docstring const &);
361 void defer(std::string const &);
363 docstring deferred() const;
365 bool inText() const { return in_text_; }
367 std::string xmlns() const { return xmlns_; }
369 bool xmlMode() const { return xml_mode_; }
370 /// Returns the tag name prefixed by the name space if needed.
371 std::string namespacedTag(std::string const & tag) const {
372 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
376 void setTextMode(bool t) { in_text_ = t; }
386 odocstringstream deferred_;
392 friend class SetMode;
396 MathStream & operator<<(MathStream &, MathAtom const &);
398 MathStream & operator<<(MathStream &, MathData const &);
400 MathStream & operator<<(MathStream &, docstring const &);
402 MathStream & operator<<(MathStream &, char const *);
404 MathStream & operator<<(MathStream &, char);
406 MathStream & operator<<(MathStream &, char_type);
408 MathStream & operator<<(MathStream &, MTag const &);
410 MathStream & operator<<(MathStream &, ETag const &);
412 MathStream & operator<<(MathStream &, CTag const &);
415 /// A simpler version of ModeSpecifier, for MathML
419 explicit SetMode(MathStream & ms, bool text);
433 explicit HtmlStream(odocstream & os);
437 odocstream & os() { return os_; }
439 int line() const { return line_; }
441 int & tab() { return tab_; }
443 friend HtmlStream & operator<<(HtmlStream &, char const *);
445 void defer(docstring const &);
447 void defer(std::string const &);
449 docstring deferred() const;
451 bool inText() const { return in_text_; }
454 void setTextMode(bool t) { in_text_ = t; }
464 odocstringstream deferred_;
466 friend class SetHTMLMode;
470 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
472 HtmlStream & operator<<(HtmlStream &, MathData const &);
474 HtmlStream & operator<<(HtmlStream &, docstring const &);
476 HtmlStream & operator<<(HtmlStream &, char const *);
478 HtmlStream & operator<<(HtmlStream &, char);
480 HtmlStream & operator<<(HtmlStream &, char_type);
482 HtmlStream & operator<<(HtmlStream &, MTag const &);
484 HtmlStream & operator<<(HtmlStream &, ETag const &);
490 explicit SetHTMLMode(HtmlStream & os, bool text);
508 explicit NormalStream(odocstream & os) : os_(os) {}
510 odocstream & os() { return os_; }
517 NormalStream & operator<<(NormalStream &, MathAtom const &);
519 NormalStream & operator<<(NormalStream &, MathData const &);
521 NormalStream & operator<<(NormalStream &, docstring const &);
523 NormalStream & operator<<(NormalStream &, char const *);
525 NormalStream & operator<<(NormalStream &, char);
527 NormalStream & operator<<(NormalStream &, int);
538 explicit MapleStream(odocstream & os) : os_(os) {}
540 odocstream & os() { return os_; }
548 MapleStream & operator<<(MapleStream &, MathAtom const &);
550 MapleStream & operator<<(MapleStream &, MathData const &);
552 MapleStream & operator<<(MapleStream &, docstring const &);
554 MapleStream & operator<<(MapleStream &, char_type);
556 MapleStream & operator<<(MapleStream &, char const *);
558 MapleStream & operator<<(MapleStream &, char);
560 MapleStream & operator<<(MapleStream &, int);
571 explicit MaximaStream(odocstream & os) : os_(os) {}
573 odocstream & os() { return os_; }
581 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
583 MaximaStream & operator<<(MaximaStream &, MathData const &);
585 MaximaStream & operator<<(MaximaStream &, docstring const &);
587 MaximaStream & operator<<(MaximaStream &, char_type);
589 MaximaStream & operator<<(MaximaStream &, char const *);
591 MaximaStream & operator<<(MaximaStream &, char);
593 MaximaStream & operator<<(MaximaStream &, int);
601 class MathematicaStream {
604 explicit MathematicaStream(odocstream & os) : os_(os) {}
606 odocstream & os() { return os_; }
614 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
616 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
618 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
620 MathematicaStream & operator<<(MathematicaStream &, char const *);
622 MathematicaStream & operator<<(MathematicaStream &, char);
624 MathematicaStream & operator<<(MathematicaStream &, int);
635 explicit OctaveStream(odocstream & os) : os_(os) {}
637 odocstream & os() { return os_; }
644 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
646 OctaveStream & operator<<(OctaveStream &, MathData const &);
648 OctaveStream & operator<<(OctaveStream &, docstring const &);
650 OctaveStream & operator<<(OctaveStream &, char_type);
652 OctaveStream & operator<<(OctaveStream &, char const *);
654 OctaveStream & operator<<(OctaveStream &, char);
656 OctaveStream & operator<<(OctaveStream &, int);
659 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);