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