]> git.lyx.org Git - lyx.git/blob - src/mathed/MathStream.h
We only support gcc >= 4.9.
[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
323 /// End tag.
324 class ETag {
325 public:
326         ///
327         explicit ETag(char const * const tag) : tag_(tag) {}
328         ///
329         char const * const tag_;
330 };
331
332
333 /// Compound tag (no content, directly closed).
334 class CTag {
335 public:
336         ///
337         CTag(char const * const tag, std::string const & attr = "")
338             : tag_(tag), attr_(attr) {}
339         ///
340         char const * const tag_;
341     ///
342     std::string attr_;
343 };
344
345
346 /// Throw MathExportException to signal that the attempt to export
347 /// some math in the current format did not succeed. E.g., we can't
348 /// export xymatrix as MathML, so that will throw, and we'll fall back
349 /// to images.
350 class MathExportException : public std::exception {};
351
352
353 class MathMLStream {
354 public:
355         /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
356         explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
357         ///
358         void cr();
359         ///
360         odocstream & os() { return os_; }
361         ///
362         int line() const { return line_; }
363         ///
364         int & tab() { return tab_; }
365         ///
366         friend MathMLStream & operator<<(MathMLStream &, char const *);
367         ///
368         void defer(docstring const &);
369         ///
370         void defer(std::string const &);
371         ///
372         docstring deferred() const;
373         ///
374         bool inText() const { return in_text_; }
375         ///
376         std::string xmlns() const { return xmlns_; }
377         ///
378         bool xmlMode() const { return xml_mode_; }
379         /// Returns the tag name prefixed by the name space if needed.
380         std::string namespacedTag(std::string const & tag) const {
381                 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
382         }
383 private:
384         ///
385         void setTextMode(bool t) { in_text_ = t; }
386         ///
387         odocstream & os_;
388         ///
389         int tab_;
390         ///
391         int line_;
392         ///
393         bool in_text_;
394         ///
395         odocstringstream deferred_;
396         ///
397         std::string xmlns_;
398         ///
399         bool xml_mode_;
400         ///
401         friend class SetMode;
402 };
403
404 ///
405 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
406 ///
407 MathMLStream & operator<<(MathMLStream &, MathData const &);
408 ///
409 MathMLStream & operator<<(MathMLStream &, docstring const &);
410 ///
411 MathMLStream & operator<<(MathMLStream &, char const *);
412 ///
413 MathMLStream & operator<<(MathMLStream &, char);
414 ///
415 MathMLStream & operator<<(MathMLStream &, char_type);
416 ///
417 MathMLStream & operator<<(MathMLStream &, MTag const &);
418 ///
419 MathMLStream & operator<<(MathMLStream &, ETag const &);
420 ///
421 MathMLStream & operator<<(MathMLStream &, CTag const &);
422
423
424 /// A simpler version of ModeSpecifier, for MathML
425 class SetMode {
426 public:
427         ///
428         explicit SetMode(MathMLStream & ms, bool text);
429         ///
430         ~SetMode();
431 private:
432         ///
433         MathMLStream & ms_;
434         ///
435         bool was_text_;
436 };
437
438
439 class HtmlStream {
440 public:
441         ///
442         explicit HtmlStream(odocstream & os);
443         ///
444         void cr();
445         ///
446         odocstream & os() { return os_; }
447         ///
448         int line() const { return line_; }
449         ///
450         int & tab() { return tab_; }
451         ///
452         friend HtmlStream & operator<<(HtmlStream &, char const *);
453         ///
454         void defer(docstring const &);
455         ///
456         void defer(std::string const &);
457         ///
458         docstring deferred() const;
459         ///
460         bool inText() const { return in_text_; }
461 private:
462         ///
463         void setTextMode(bool t) { in_text_ = t; }
464         ///
465         odocstream & os_;
466         ///
467         int tab_;
468         ///
469         int line_;
470         ///
471         bool in_text_;
472         ///
473         odocstringstream deferred_;
474         ///
475         friend class SetHTMLMode;
476 };
477
478 ///
479 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
480 ///
481 HtmlStream & operator<<(HtmlStream &, MathData const &);
482 ///
483 HtmlStream & operator<<(HtmlStream &, docstring const &);
484 ///
485 HtmlStream & operator<<(HtmlStream &, char const *);
486 ///
487 HtmlStream & operator<<(HtmlStream &, char);
488 ///
489 HtmlStream & operator<<(HtmlStream &, char_type);
490 ///
491 HtmlStream & operator<<(HtmlStream &, MTag const &);
492 ///
493 HtmlStream & operator<<(HtmlStream &, ETag const &);
494
495
496 class SetHTMLMode {
497 public:
498         ///
499         explicit SetHTMLMode(HtmlStream & os, bool text);
500         ///
501         ~SetHTMLMode();
502 private:
503         ///
504         HtmlStream & os_;
505         ///
506         bool was_text_;
507 };
508
509
510 //
511 // Debugging
512 //
513
514 class NormalStream {
515 public:
516         ///
517         explicit NormalStream(odocstream & os) : os_(os) {}
518         ///
519         odocstream & os() { return os_; }
520 private:
521         ///
522         odocstream & os_;
523 };
524
525 ///
526 NormalStream & operator<<(NormalStream &, MathAtom const &);
527 ///
528 NormalStream & operator<<(NormalStream &, MathData const &);
529 ///
530 NormalStream & operator<<(NormalStream &, docstring const &);
531 ///
532 NormalStream & operator<<(NormalStream &, char const *);
533 ///
534 NormalStream & operator<<(NormalStream &, char);
535 ///
536 NormalStream & operator<<(NormalStream &, int);
537
538
539 //
540 // Maple
541 //
542
543
544 class MapleStream {
545 public:
546         ///
547         explicit MapleStream(odocstream & os) : os_(os) {}
548         ///
549         odocstream & os() { return os_; }
550 private:
551         ///
552         odocstream & os_;
553 };
554
555
556 ///
557 MapleStream & operator<<(MapleStream &, MathAtom const &);
558 ///
559 MapleStream & operator<<(MapleStream &, MathData const &);
560 ///
561 MapleStream & operator<<(MapleStream &, docstring const &);
562 ///
563 MapleStream & operator<<(MapleStream &, char_type);
564 ///
565 MapleStream & operator<<(MapleStream &, char const *);
566 ///
567 MapleStream & operator<<(MapleStream &, char);
568 ///
569 MapleStream & operator<<(MapleStream &, int);
570
571
572 //
573 // Maxima
574 //
575
576
577 class MaximaStream {
578 public:
579         ///
580         explicit MaximaStream(odocstream & os) : os_(os) {}
581         ///
582         odocstream & os() { return os_; }
583 private:
584         ///
585         odocstream & os_;
586 };
587
588
589 ///
590 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
591 ///
592 MaximaStream & operator<<(MaximaStream &, MathData const &);
593 ///
594 MaximaStream & operator<<(MaximaStream &, docstring const &);
595 ///
596 MaximaStream & operator<<(MaximaStream &, char_type);
597 ///
598 MaximaStream & operator<<(MaximaStream &, char const *);
599 ///
600 MaximaStream & operator<<(MaximaStream &, char);
601 ///
602 MaximaStream & operator<<(MaximaStream &, int);
603
604
605 //
606 // Mathematica
607 //
608
609
610 class MathematicaStream {
611 public:
612         ///
613         explicit MathematicaStream(odocstream & os) : os_(os) {}
614         ///
615         odocstream & os() { return os_; }
616 private:
617         ///
618         odocstream & os_;
619 };
620
621
622 ///
623 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
624 ///
625 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
626 ///
627 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
628 ///
629 MathematicaStream & operator<<(MathematicaStream &, char const *);
630 ///
631 MathematicaStream & operator<<(MathematicaStream &, char);
632 ///
633 MathematicaStream & operator<<(MathematicaStream &, int);
634
635
636 //
637 // Octave
638 //
639
640
641 class OctaveStream {
642 public:
643         ///
644         explicit OctaveStream(odocstream & os) : os_(os) {}
645         ///
646         odocstream & os() { return os_; }
647 private:
648         ///
649         odocstream & os_;
650 };
651
652 ///
653 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
654 ///
655 OctaveStream & operator<<(OctaveStream &, MathData const &);
656 ///
657 OctaveStream & operator<<(OctaveStream &, docstring const &);
658 ///
659 OctaveStream & operator<<(OctaveStream &, char_type);
660 ///
661 OctaveStream & operator<<(OctaveStream &, char const *);
662 ///
663 OctaveStream & operator<<(OctaveStream &, char);
664 ///
665 OctaveStream & operator<<(OctaveStream &, int);
666
667
668 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);
669
670 } // namespace lyx
671
672 #endif