]> git.lyx.org Git - lyx.git/blob - src/mathed/MathStream.h
MathML: add M/ETagInline.
[lyx.git] / src / mathed / MathStream.h
1 // -*- C++ -*-
2 /**
3  * \file MathStream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MATH_MATHMLSTREAM_H
13 #define MATH_MATHMLSTREAM_H
14
15 #include "InsetMath.h"
16
17 #include "TexRow.h"
18 #include "texstream.h"
19
20 #include "support/Changer.h"
21 #include "support/strfwd.h"
22
23
24 namespace lyx {
25
26 class Encoding;
27 class MathAtom;
28 class MathData;
29
30 //
31 // LaTeX/LyX
32 //
33
34 class TeXMathStream {
35 public:
36         ///
37         enum OutputType {
38                 wsDefault,
39                 wsDryrun,
40                 wsPreview
41         };
42         ///
43         enum UlemCmdType {
44                 NONE,
45                 UNDERLINE,
46                 STRIKEOUT
47         };
48         ///
49         explicit TeXMathStream(otexrowstream & os, bool fragile = false,
50                                bool latex = false, OutputType output = wsDefault,
51                                Encoding const * encoding = nullptr);
52         ///
53         ~TeXMathStream();
54         ///
55         int line() const { return line_; }
56         ///
57         bool fragile() const { return fragile_; }
58         ///
59         bool latex() const { return latex_; }
60         ///
61         OutputType output() const { return output_; }
62         ///
63         otexrowstream & os() { return os_; }
64         ///
65         TexRow & texrow() { return os_.texrow(); }
66         ///
67         bool & firstitem() { return firstitem_; }
68         ///
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         /// or when a prime immediately follows a superscript
88         void useBraces(bool braces);
89         /// write braces if a space is pending and next char is [
90         /// or when a prime immediately follows a superscript
91         bool useBraces() const { return usebraces_; }
92         /// tell whether to write the closing brace of \ensuremath
93         void pendingBrace(bool brace);
94         /// tell whether to write the closing brace of \ensuremath
95         bool pendingBrace() const { return pendingbrace_; }
96         /// tell whether we are in text mode or not when producing latex code
97         void textMode(bool textmode);
98         /// tell whether we are in text mode or not when producing latex code
99         bool textMode() const { return textmode_; }
100         /// tell whether we are allowed to switch mode when producing latex code
101         void lockedMode(bool locked);
102         /// tell whether we are allowed to switch mode when producing latex code
103         bool lockedMode() const { return locked_; }
104         /// tell whether to use only ascii chars when producing latex code
105         void asciiOnly(bool ascii);
106         /// tell whether to use only ascii chars when producing latex code
107         bool asciiOnly() const { return ascii_; }
108         /// tell whether we are in a MathClass inset
109         void inMathClass(bool mathclass) { mathclass_ = mathclass; };
110         /// tell whether we are in a MathClass inset
111         bool inMathClass() const { return mathclass_; }
112         /// LaTeX encoding
113         Encoding const * encoding() const { return encoding_; }
114
115         /// Temporarily change the TexRow information about the outer row entry.
116         Changer changeRowEntry(TexRow::RowEntry entry);
117         /// TexRow::starts the innermost outer math inset
118         /// returns true if the outer row entry will appear at this line
119         bool startOuterRow();
120 private:
121         ///
122         otexrowstream & os_;
123         /// do we have to write \\protect sometimes
124         bool fragile_ = false;
125         /// are we at the beginning of an MathData?
126         bool firstitem_ = false;
127         /// are we writing to .tex?
128         int latex_ = false;
129         /// output type (default, source preview, instant preview)?
130         OutputType output_ = wsDefault;
131         /// do we have a space pending?
132         bool pendingspace_ = false;
133         /// do we have to write braces when a space is pending and [ follows,
134         /// or when a prime immediately follows a superscript?
135         bool usebraces_ = false;
136         /// do we have a brace pending?
137         bool pendingbrace_ = false;
138         /// are we in text mode when producing latex code?
139         bool textmode_ = false;
140         /// are we allowed to switch mode when producing latex code?
141         bool locked_ = false;
142         /// should we use only ascii chars when producing latex code?
143         bool ascii_ = false;
144         /// are we allowed to output an immediately following newline?
145         bool canbreakline_ = true;
146         /// should we take care for striking out display math?
147         bool mathsout_ = false;
148         /// what ulem command are we inside (none, underline, strikeout)?
149         UlemCmdType ulemcmd_ = NONE;
150         ///
151         int line_ = 0;
152         ///
153         Encoding const * encoding_ = nullptr;
154         /// Row entry we are in
155         TexRow::RowEntry row_entry_ = TexRow::row_none;
156         /// whether we are in a MathClass inset
157         bool mathclass_ = false;
158 };
159
160 ///
161 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
162 ///
163 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
164 ///
165 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
166 ///
167 TeXMathStream & operator<<(TeXMathStream &, char const * const);
168 ///
169 TeXMathStream & operator<<(TeXMathStream &, char);
170 ///
171 TeXMathStream & operator<<(TeXMathStream &, int);
172 ///
173 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
174
175 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
176 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
177                 bool macro = false, bool textmode_macro = false);
178
179 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
180 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
181
182
183 /**
184  * MathEnsurer - utility class for ensuring math mode
185  *
186  * A local variable of this type can be used to either ensure math mode
187  * or delay the writing of a pending brace when outputting LaTeX.
188  * A LyX InsetMathMacro is always assumed needing a math mode environment, while
189  * no assumption is made for macros defined through \newcommand or \def.
190  *
191  * Example 1:
192  *
193  *      MathEnsurer ensurer(os);
194  *
195  * If not already in math mode, inserts an \ensuremath command followed
196  * by an open brace. This brace will be automatically closed when exiting
197  * math mode. Math mode is automatically exited when writing something
198  * that doesn't explicitly require math mode.
199  *
200  * Example 2:
201  *
202  *      MathEnsurer ensurer(os, false);
203  *
204  * Simply suspend writing a closing brace until the end of ensurer's scope.
205  *
206  * Example 3:
207  *
208  *      MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
209  *
210  * This form is mainly used for math macros as they are treated specially.
211  * In essence, the macros defined in the lib/symbols file and tagged as
212  * textmode will be enclosed in \lyxmathsym if they appear in a math mode
213  * environment, while macros defined in the preamble or ERT are left as is.
214  * The third parameter must be set to true and the fourth parameter has also
215  * to be specified. Only the following 3 different cases are handled.
216  *
217  * When the needs_mathmode parameter is true the behavior is as in Example 1.
218  * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
219  *
220  * When the needs_mathmode and textmode_macro parameters are both false the
221  * macro is left in the same (text or math mode) environment it was entered.
222  * This is because it is assumed that the macro was designed for that mode
223  * and we have no way to tell the contrary.
224  * This is the case for macros defined by using \newcommand or \def in ERT.
225  *
226  * When the needs_mathmode parameter is false while textmode_macro is true the
227  * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
228  * This is the case for the macros tagged as textmode in lib/symbols.
229  */
230 class MathEnsurer
231 {
232 public:
233         ///
234         explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
235                              bool macro = false, bool textmode_macro = false)
236                 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
237         ///
238         ~MathEnsurer() { os_.pendingBrace(brace_); }
239 private:
240         ///
241         TeXMathStream & os_;
242         ///
243         bool brace_;
244 };
245
246
247 /**
248  * ModeSpecifier - utility class for specifying a given mode (math or text)
249  *
250  * A local variable of this type can be used to specify that a command or
251  * environment works in a given mode. For example, \mbox works in text
252  * mode, but \boxed works in math mode. Note that no mode changing commands
253  * are needed, but we have to track the current mode, hence this class.
254  * This is only used when exporting to latex and helps determining whether
255  * the mode needs being temporarily switched when a command would not work
256  * in the current mode. As there are cases where this switching is to be
257  * avoided, the optional third parameter can be used to lock the mode.
258  * When the mode is locked, the optional fourth parameter specifies whether
259  * strings are to be output by using a suitable ascii representation.
260  *
261  * Example 1:
262  *
263  *      ModeSpecifier specifier(os, TEXT_MODE);
264  *
265  * Sets the current mode to text mode and allows mode switching.
266  *
267  * Example 2:
268  *
269  *      ModeSpecifier specifier(os, TEXT_MODE, true);
270  *
271  * Sets the current mode to text mode and disallows mode switching.
272  *
273  * Example 3:
274  *
275  *      ModeSpecifier specifier(os, TEXT_MODE, true, true);
276  *
277  * Sets the current mode to text mode, disallows mode switching, and outputs
278  * strings as ascii only.
279  *
280  * At the end of specifier's scope the mode is reset to its previous value.
281  */
282 class ModeSpecifier
283 {
284 public:
285         ///
286         explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
287                                bool locked = false, bool ascii = false)
288                 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
289         ///
290         ~ModeSpecifier()
291         {
292                 os_.textMode(oldmodes_ & 0x01);
293                 os_.lockedMode(oldmodes_ & 0x02);
294                 os_.asciiOnly(oldmodes_ & 0x04);
295         }
296 private:
297         ///
298         TeXMathStream & os_;
299         ///
300         int oldmodes_;
301 };
302
303
304
305 //
306 //  MathML
307 //
308
309
310 /// Start tag.
311 class MTag {
312 public:
313         ///
314         MTag(char const * const tag, std::string const & attr = std::string())
315                 : tag_(tag), attr_(attr) {}
316         ///
317         char const * const tag_;
318         ///
319         std::string attr_;
320 };
321
322 /// Start inline tag.
323 class MTagInline {
324 public:
325         ///
326         MTagInline(char const * const tag, std::string const & attr = std::string())
327                 : tag_(tag), attr_(attr) {}
328         ///
329         char const * const tag_;
330         ///
331         std::string attr_;
332 };
333
334
335 /// End tag.
336 class ETag {
337 public:
338         ///
339         explicit ETag(char const * const tag) : tag_(tag) {}
340         ///
341         char const * const tag_;
342 };
343
344
345 /// End inlinetag.
346 class ETagInline {
347 public:
348         ///
349         explicit ETagInline(char const * const tag) : tag_(tag) {}
350         ///
351         char const * const tag_;
352 };
353
354
355 /// Compound tag (no content, directly closed).
356 class CTag {
357 public:
358         ///
359         CTag(char const * const tag, std::string const & attr = "")
360             : tag_(tag), attr_(attr) {}
361         ///
362         char const * const tag_;
363     ///
364     std::string attr_;
365 };
366
367
368 /// Throw MathExportException to signal that the attempt to export
369 /// some math in the current format did not succeed. E.g., we can't
370 /// export xymatrix as MathML, so that will throw, and we'll fall back
371 /// to images.
372 class MathExportException : public std::exception {};
373
374
375 class MathMLStream {
376 public:
377         /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
378         explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
379         ///
380         void cr();
381         ///
382         odocstream & os() { return os_; }
383         ///
384         int line() const { return line_; }
385         ///
386         int & tab() { return tab_; }
387         ///
388         friend MathMLStream & operator<<(MathMLStream &, char const *);
389         ///
390         void defer(docstring const &);
391         ///
392         void defer(std::string const &);
393         ///
394         docstring deferred() const;
395         ///
396         bool inText() const { return in_text_; }
397         ///
398         std::string xmlns() const { return xmlns_; }
399         ///
400         bool xmlMode() const { return xml_mode_; }
401         /// Returns the tag name prefixed by the name space if needed.
402         std::string namespacedTag(std::string const & tag) const {
403                 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
404         }
405 private:
406         ///
407         void setTextMode(bool t) { in_text_ = t; }
408         ///
409         odocstream & os_;
410         ///
411         int tab_;
412         ///
413         int line_;
414         ///
415         bool in_text_;
416         ///
417         odocstringstream deferred_;
418         ///
419         std::string xmlns_;
420         ///
421         bool xml_mode_;
422         ///
423         friend class SetMode;
424 };
425
426 ///
427 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
428 ///
429 MathMLStream & operator<<(MathMLStream &, MathData const &);
430 ///
431 MathMLStream & operator<<(MathMLStream &, docstring const &);
432 ///
433 MathMLStream & operator<<(MathMLStream &, char const *);
434 ///
435 MathMLStream & operator<<(MathMLStream &, char);
436 ///
437 MathMLStream & operator<<(MathMLStream &, char_type);
438 ///
439 MathMLStream & operator<<(MathMLStream &, MTag const &);
440 ///
441 MathMLStream & operator<<(MathMLStream &, MTagInline const &);
442 ///
443 MathMLStream & operator<<(MathMLStream &, ETag const &);
444 ///
445 MathMLStream & operator<<(MathMLStream &, ETagInline const &);
446 ///
447 MathMLStream & operator<<(MathMLStream &, CTag const &);
448
449
450 /// A simpler version of ModeSpecifier, for MathML
451 class SetMode {
452 public:
453         ///
454         explicit SetMode(MathMLStream & ms, bool text);
455         ///
456         ~SetMode();
457 private:
458         ///
459         MathMLStream & ms_;
460         ///
461         bool was_text_;
462 };
463
464
465 class HtmlStream {
466 public:
467         ///
468         explicit HtmlStream(odocstream & os);
469         ///
470         void cr();
471         ///
472         odocstream & os() { return os_; }
473         ///
474         int line() const { return line_; }
475         ///
476         int & tab() { return tab_; }
477         ///
478         friend HtmlStream & operator<<(HtmlStream &, char const *);
479         ///
480         void defer(docstring const &);
481         ///
482         void defer(std::string const &);
483         ///
484         docstring deferred() const;
485         ///
486         bool inText() const { return in_text_; }
487 private:
488         ///
489         void setTextMode(bool t) { in_text_ = t; }
490         ///
491         odocstream & os_;
492         ///
493         int tab_;
494         ///
495         int line_;
496         ///
497         bool in_text_;
498         ///
499         odocstringstream deferred_;
500         ///
501         friend class SetHTMLMode;
502 };
503
504 ///
505 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
506 ///
507 HtmlStream & operator<<(HtmlStream &, MathData const &);
508 ///
509 HtmlStream & operator<<(HtmlStream &, docstring const &);
510 ///
511 HtmlStream & operator<<(HtmlStream &, char const *);
512 ///
513 HtmlStream & operator<<(HtmlStream &, char);
514 ///
515 HtmlStream & operator<<(HtmlStream &, char_type);
516 ///
517 HtmlStream & operator<<(HtmlStream &, MTag const &);
518 ///
519 HtmlStream & operator<<(HtmlStream &, ETag const &);
520
521
522 class SetHTMLMode {
523 public:
524         ///
525         explicit SetHTMLMode(HtmlStream & os, bool text);
526         ///
527         ~SetHTMLMode();
528 private:
529         ///
530         HtmlStream & os_;
531         ///
532         bool was_text_;
533 };
534
535
536 //
537 // Debugging
538 //
539
540 class NormalStream {
541 public:
542         ///
543         explicit NormalStream(odocstream & os) : os_(os) {}
544         ///
545         odocstream & os() { return os_; }
546 private:
547         ///
548         odocstream & os_;
549 };
550
551 ///
552 NormalStream & operator<<(NormalStream &, MathAtom const &);
553 ///
554 NormalStream & operator<<(NormalStream &, MathData const &);
555 ///
556 NormalStream & operator<<(NormalStream &, docstring const &);
557 ///
558 NormalStream & operator<<(NormalStream &, char const *);
559 ///
560 NormalStream & operator<<(NormalStream &, char);
561 ///
562 NormalStream & operator<<(NormalStream &, int);
563
564
565 //
566 // Maple
567 //
568
569
570 class MapleStream {
571 public:
572         ///
573         explicit MapleStream(odocstream & os) : os_(os) {}
574         ///
575         odocstream & os() { return os_; }
576 private:
577         ///
578         odocstream & os_;
579 };
580
581
582 ///
583 MapleStream & operator<<(MapleStream &, MathAtom const &);
584 ///
585 MapleStream & operator<<(MapleStream &, MathData const &);
586 ///
587 MapleStream & operator<<(MapleStream &, docstring const &);
588 ///
589 MapleStream & operator<<(MapleStream &, char_type);
590 ///
591 MapleStream & operator<<(MapleStream &, char const *);
592 ///
593 MapleStream & operator<<(MapleStream &, char);
594 ///
595 MapleStream & operator<<(MapleStream &, int);
596
597
598 //
599 // Maxima
600 //
601
602
603 class MaximaStream {
604 public:
605         ///
606         explicit MaximaStream(odocstream & os) : os_(os) {}
607         ///
608         odocstream & os() { return os_; }
609 private:
610         ///
611         odocstream & os_;
612 };
613
614
615 ///
616 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
617 ///
618 MaximaStream & operator<<(MaximaStream &, MathData const &);
619 ///
620 MaximaStream & operator<<(MaximaStream &, docstring const &);
621 ///
622 MaximaStream & operator<<(MaximaStream &, char_type);
623 ///
624 MaximaStream & operator<<(MaximaStream &, char const *);
625 ///
626 MaximaStream & operator<<(MaximaStream &, char);
627 ///
628 MaximaStream & operator<<(MaximaStream &, int);
629
630
631 //
632 // Mathematica
633 //
634
635
636 class MathematicaStream {
637 public:
638         ///
639         explicit MathematicaStream(odocstream & os) : os_(os) {}
640         ///
641         odocstream & os() { return os_; }
642 private:
643         ///
644         odocstream & os_;
645 };
646
647
648 ///
649 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
650 ///
651 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
652 ///
653 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
654 ///
655 MathematicaStream & operator<<(MathematicaStream &, char const *);
656 ///
657 MathematicaStream & operator<<(MathematicaStream &, char);
658 ///
659 MathematicaStream & operator<<(MathematicaStream &, int);
660
661
662 //
663 // Octave
664 //
665
666
667 class OctaveStream {
668 public:
669         ///
670         explicit OctaveStream(odocstream & os) : os_(os) {}
671         ///
672         odocstream & os() { return os_; }
673 private:
674         ///
675         odocstream & os_;
676 };
677
678 ///
679 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
680 ///
681 OctaveStream & operator<<(OctaveStream &, MathData const &);
682 ///
683 OctaveStream & operator<<(OctaveStream &, docstring const &);
684 ///
685 OctaveStream & operator<<(OctaveStream &, char_type);
686 ///
687 OctaveStream & operator<<(OctaveStream &, char const *);
688 ///
689 OctaveStream & operator<<(OctaveStream &, char);
690 ///
691 OctaveStream & operator<<(OctaveStream &, int);
692
693
694 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);
695
696 } // namespace lyx
697
698 #endif