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"
16 #include "texstream.h"
18 #include "support/Changer.h"
19 #include "support/strfwd.h"
20 #include "support/unique_ptr.h"
44 explicit WriteStream(otexrowstream & os, bool fragile = false,
45 bool latex = false, OutputType output = wsDefault,
46 Encoding const * encoding = 0);
50 int line() const { return line_; }
52 bool fragile() const { return fragile_; }
54 bool latex() const { return latex_; }
56 OutputType output() const { return output_; }
58 otexrowstream & os() { return os_; }
60 TexRow & texrow() { return os_.texrow(); }
62 bool & firstitem() { return firstitem_; }
64 void addlines(unsigned int);
65 /// record whether we can write an immediately following newline char
66 void canBreakLine(bool breakline) { canbreakline_ = breakline; }
67 /// tell whether we can write an immediately following newline char
68 bool canBreakLine() const { return canbreakline_; }
69 /// record whether we have to take care for striking out display math
70 void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
71 /// tell whether we have to take care for striking out display math
72 bool strikeoutMath() const { return mathsout_; }
73 /// writes space if next thing is isalpha()
74 void pendingSpace(bool how);
75 /// writes space if next thing is isalpha()
76 bool pendingSpace() const { return pendingspace_; }
77 /// tell whether to write the closing brace of \ensuremath
78 void pendingBrace(bool brace);
79 /// tell whether to write the closing brace of \ensuremath
80 bool pendingBrace() const { return pendingbrace_; }
81 /// tell whether we are in text mode or not when producing latex code
82 void textMode(bool textmode);
83 /// tell whether we are in text mode or not when producing latex code
84 bool textMode() const { return textmode_; }
85 /// tell whether we are allowed to switch mode when producing latex code
86 void lockedMode(bool locked);
87 /// tell whether we are allowed to switch mode when producing latex code
88 bool lockedMode() const { return locked_; }
89 /// tell whether to use only ascii chars when producing latex code
90 void asciiOnly(bool ascii);
91 /// tell whether to use only ascii chars when producing latex code
92 bool asciiOnly() const { return ascii_; }
94 Encoding const * encoding() const { return encoding_; }
96 /// Temporarily change the TexRow information about the outer row entry.
97 Changer changeRowEntry(RowEntry entry);
98 /// TexRow::starts the innermost outer math inset
99 /// returns true if the outer row entry will appear at this line
100 bool startOuterRow();
104 /// do we have to write \\protect sometimes
106 /// are we at the beginning of an MathData?
108 /// are we writing to .tex?
110 /// output type (default, source preview, instant preview)?
112 /// do we have a space pending?
114 /// do we have a brace pending?
116 /// are we in text mode when producing latex code?
118 /// are we allowed to switch mode when producing latex code?
120 /// should we use only ascii chars when producing latex code?
122 /// are we allowed to output an immediately following newline?
124 /// should we take care for striking out display math?
129 Encoding const * encoding_;
130 /// Row entry we are in
131 /// (it is a pointer to allow forward-declaration)
132 unique_ptr<RowEntry> row_entry_;
136 WriteStream & operator<<(WriteStream &, MathAtom const &);
138 WriteStream & operator<<(WriteStream &, MathData const &);
140 WriteStream & operator<<(WriteStream &, docstring const &);
142 WriteStream & operator<<(WriteStream &, char const * const);
144 WriteStream & operator<<(WriteStream &, char);
146 WriteStream & operator<<(WriteStream &, int);
148 WriteStream & operator<<(WriteStream &, unsigned int);
150 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
151 bool ensureMath(WriteStream & os, bool needs_mathmode = true,
152 bool macro = false, bool textmode_macro = false);
154 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
155 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
159 * MathEnsurer - utility class for ensuring math mode
161 * A local variable of this type can be used to either ensure math mode
162 * or delay the writing of a pending brace when outputting LaTeX.
163 * A LyX MathMacro is always assumed needing a math mode environment, while
164 * no assumption is made for macros defined through \newcommand or \def.
168 * MathEnsurer ensurer(os);
170 * If not already in math mode, inserts an \ensuremath command followed
171 * by an open brace. This brace will be automatically closed when exiting
172 * math mode. Math mode is automatically exited when writing something
173 * that doesn't explicitly require math mode.
177 * MathEnsurer ensurer(os, false);
179 * Simply suspend writing a closing brace until the end of ensurer's scope.
183 * MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
185 * This form is mainly used for math macros as they are treated specially.
186 * In essence, the macros defined in the lib/symbols file and tagged as
187 * textmode will be enclosed in \lyxmathsym if they appear in a math mode
188 * environment, while macros defined in the preamble or ERT are left as is.
189 * The third parameter must be set to true and the fourth parameter has also
190 * to be specified. Only the following 3 different cases are handled.
192 * When the needs_mathmode parameter is true the behavior is as in Example 1.
193 * This is the case for a LyX MathMacro or a macro not tagged as textmode.
195 * When the needs_mathmode and textmode_macro parameters are both false the
196 * macro is left in the same (text or math mode) environment it was entered.
197 * This is because it is assumed that the macro was designed for that mode
198 * and we have no way to tell the contrary.
199 * This is the case for macros defined by using \newcommand or \def in ERT.
201 * When the needs_mathmode parameter is false while textmode_macro is true the
202 * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
203 * This is the case for the macros tagged as textmode in lib/symbols.
209 explicit MathEnsurer(WriteStream & os, bool needs_mathmode = true,
210 bool macro = false, bool textmode_macro = false)
211 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
213 ~MathEnsurer() { os_.pendingBrace(brace_); }
223 * ModeSpecifier - utility class for specifying a given mode (math or text)
225 * A local variable of this type can be used to specify that a command or
226 * environment works in a given mode. For example, \mbox works in text
227 * mode, but \boxed works in math mode. Note that no mode changing commands
228 * are needed, but we have to track the current mode, hence this class.
229 * This is only used when exporting to latex and helps determining whether
230 * the mode needs being temporarily switched when a command would not work
231 * in the current mode. As there are cases where this switching is to be
232 * avoided, the optional third parameter can be used to lock the mode.
233 * When the mode is locked, the optional fourth parameter specifies whether
234 * strings are to be output by using a suitable ascii representation.
238 * ModeSpecifier specifier(os, TEXT_MODE);
240 * Sets the current mode to text mode and allows mode switching.
244 * ModeSpecifier specifier(os, TEXT_MODE, true);
246 * Sets the current mode to text mode and disallows mode switching.
250 * ModeSpecifier specifier(os, TEXT_MODE, true, true);
252 * Sets the current mode to text mode, disallows mode switching, and outputs
253 * strings as ascii only.
255 * At the end of specifier's scope the mode is reset to its previous value.
261 explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
262 bool locked = false, bool ascii = false)
263 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
267 os_.textMode(oldmodes_ & 0x01);
268 os_.lockedMode(oldmodes_ & 0x02);
269 os_.asciiOnly(oldmodes_ & 0x04);
287 MTag(char const * const tag, std::string attr = "")
288 : tag_(tag), attr_(attr) {}
290 char const * const tag_;
298 ETag(char const * const tag) : tag_(tag) {}
300 char const * const tag_;
304 /// Throw MathExportException to signal that the attempt to export
305 /// some math in the current format did not succeed. E.g., we can't
306 /// export xymatrix as MathML, so that will throw, and we'll fall back
308 class MathExportException : public std::exception {};
314 explicit MathStream(odocstream & os);
318 odocstream & os() { return os_; }
320 int line() const { return line_; }
322 int & tab() { return tab_; }
324 friend MathStream & operator<<(MathStream &, char const *);
326 void defer(docstring const &);
328 void defer(std::string const &);
330 docstring deferred() const;
332 bool inText() const { return in_text_; }
335 void setTextMode(bool t) { in_text_ = t; }
345 odocstringstream deferred_;
347 friend class SetMode;
351 MathStream & operator<<(MathStream &, MathAtom const &);
353 MathStream & operator<<(MathStream &, MathData const &);
355 MathStream & operator<<(MathStream &, docstring const &);
357 MathStream & operator<<(MathStream &, char const *);
359 MathStream & operator<<(MathStream &, char);
361 MathStream & operator<<(MathStream &, char_type);
363 MathStream & operator<<(MathStream &, MTag const &);
365 MathStream & operator<<(MathStream &, ETag const &);
368 /// A simpler version of ModeSpecifier, for MathML
372 explicit SetMode(MathStream & os, bool text);
386 explicit HtmlStream(odocstream & os);
390 odocstream & os() { return os_; }
392 int line() const { return line_; }
394 int & tab() { return tab_; }
396 friend HtmlStream & operator<<(HtmlStream &, char const *);
398 void defer(docstring const &);
400 void defer(std::string const &);
402 docstring deferred() const;
404 bool inText() const { return in_text_; }
407 void setTextMode(bool t) { in_text_ = t; }
417 odocstringstream deferred_;
419 friend class SetHTMLMode;
423 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
425 HtmlStream & operator<<(HtmlStream &, MathData const &);
427 HtmlStream & operator<<(HtmlStream &, docstring const &);
429 HtmlStream & operator<<(HtmlStream &, char const *);
431 HtmlStream & operator<<(HtmlStream &, char);
433 HtmlStream & operator<<(HtmlStream &, char_type);
435 HtmlStream & operator<<(HtmlStream &, MTag const &);
437 HtmlStream & operator<<(HtmlStream &, ETag const &);
443 explicit SetHTMLMode(HtmlStream & os, bool text);
461 explicit NormalStream(odocstream & os) : os_(os) {}
463 odocstream & os() { return os_; }
470 NormalStream & operator<<(NormalStream &, MathAtom const &);
472 NormalStream & operator<<(NormalStream &, MathData const &);
474 NormalStream & operator<<(NormalStream &, docstring const &);
476 NormalStream & operator<<(NormalStream &, char const *);
478 NormalStream & operator<<(NormalStream &, char);
480 NormalStream & operator<<(NormalStream &, int);
491 explicit MapleStream(odocstream & os) : os_(os) {}
493 odocstream & os() { return os_; }
501 MapleStream & operator<<(MapleStream &, MathAtom const &);
503 MapleStream & operator<<(MapleStream &, MathData const &);
505 MapleStream & operator<<(MapleStream &, docstring const &);
507 MapleStream & operator<<(MapleStream &, char_type);
509 MapleStream & operator<<(MapleStream &, char const *);
511 MapleStream & operator<<(MapleStream &, char);
513 MapleStream & operator<<(MapleStream &, int);
524 explicit MaximaStream(odocstream & os) : os_(os) {}
526 odocstream & os() { return os_; }
534 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
536 MaximaStream & operator<<(MaximaStream &, MathData const &);
538 MaximaStream & operator<<(MaximaStream &, docstring const &);
540 MaximaStream & operator<<(MaximaStream &, char_type);
542 MaximaStream & operator<<(MaximaStream &, char const *);
544 MaximaStream & operator<<(MaximaStream &, char);
546 MaximaStream & operator<<(MaximaStream &, int);
554 class MathematicaStream {
557 explicit MathematicaStream(odocstream & os) : os_(os) {}
559 odocstream & os() { return os_; }
567 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
569 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
571 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
573 MathematicaStream & operator<<(MathematicaStream &, char const *);
575 MathematicaStream & operator<<(MathematicaStream &, char);
577 MathematicaStream & operator<<(MathematicaStream &, int);
588 explicit OctaveStream(odocstream & os) : os_(os) {}
590 odocstream & os() { return os_; }
597 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
599 OctaveStream & operator<<(OctaveStream &, MathData const &);
601 OctaveStream & operator<<(OctaveStream &, docstring const &);
603 OctaveStream & operator<<(OctaveStream &, char_type);
605 OctaveStream & operator<<(OctaveStream &, char const *);
607 OctaveStream & operator<<(OctaveStream &, char);
609 OctaveStream & operator<<(OctaveStream &, int);
612 docstring convertDelimToXMLEscape(docstring const & name);