]> git.lyx.org Git - features.git/blob - src/mathed/MathStream.h
Fix broken Apple speller interface
[features.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 = "", bool xmlMode = false);
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         ///
402         bool xmlMode() const { return xml_mode_; }
403         /// Returns the tag name prefixed by the name space if needed.
404         std::string namespacedTag(std::string const & tag) const {
405                 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
406         }
407         /// Returns the current math style in the stream.
408         const MathStyle & getFontMathStyle() const { return font_math_style_; }
409         /// Returns the current math style in the stream.
410         void setFontMathStyle(const MathStyle style) { font_math_style_ = style; }
411 private:
412         ///
413         void setTextMode(bool t) { in_text_ = t; }
414         ///
415         odocstream & os_;
416         ///
417         int tab_;
418         ///
419         int line_;
420         ///
421         bool in_text_;
422         ///
423         odocstringstream deferred_;
424         ///
425         std::string xmlns_;
426         ///
427         bool xml_mode_;
428         /// The only important part of a FontInfo object.
429         MathStyle font_math_style_;
430         ///
431         friend class SetMode;
432 };
433
434 ///
435 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
436 ///
437 MathMLStream & operator<<(MathMLStream &, MathData const &);
438 ///
439 MathMLStream & operator<<(MathMLStream &, docstring const &);
440 ///
441 MathMLStream & operator<<(MathMLStream &, char const *);
442 ///
443 MathMLStream & operator<<(MathMLStream &, char);
444 ///
445 MathMLStream & operator<<(MathMLStream &, char_type);
446 ///
447 MathMLStream & operator<<(MathMLStream &, MTag const &);
448 ///
449 MathMLStream & operator<<(MathMLStream &, MTagInline const &);
450 ///
451 MathMLStream & operator<<(MathMLStream &, ETag const &);
452 ///
453 MathMLStream & operator<<(MathMLStream &, ETagInline const &);
454 ///
455 MathMLStream & operator<<(MathMLStream &, CTag const &);
456
457
458 /// A simpler version of ModeSpecifier, for MathML
459 class SetMode {
460 public:
461         ///
462         explicit SetMode(MathMLStream & ms, bool text);
463         ///
464         ~SetMode();
465 private:
466         ///
467         MathMLStream & ms_;
468         ///
469         bool was_text_;
470 };
471
472
473 class HtmlStream {
474 public:
475         ///
476         explicit HtmlStream(odocstream & os);
477         ///
478         void cr();
479         ///
480         odocstream & os() { return os_; }
481         ///
482         int line() const { return line_; }
483         ///
484         int & tab() { return tab_; }
485         ///
486         friend HtmlStream & operator<<(HtmlStream &, char const *);
487         ///
488         void defer(docstring const &);
489         ///
490         void defer(std::string const &);
491         ///
492         docstring deferred() const;
493         ///
494         bool inText() const { return in_text_; }
495 private:
496         ///
497         void setTextMode(bool t) { in_text_ = t; }
498         ///
499         odocstream & os_;
500         ///
501         int tab_;
502         ///
503         int line_;
504         ///
505         bool in_text_;
506         ///
507         odocstringstream deferred_;
508         ///
509         friend class SetHTMLMode;
510 };
511
512 ///
513 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
514 ///
515 HtmlStream & operator<<(HtmlStream &, MathData const &);
516 ///
517 HtmlStream & operator<<(HtmlStream &, docstring const &);
518 ///
519 HtmlStream & operator<<(HtmlStream &, char const *);
520 ///
521 HtmlStream & operator<<(HtmlStream &, char);
522 ///
523 HtmlStream & operator<<(HtmlStream &, char_type);
524 ///
525 HtmlStream & operator<<(HtmlStream &, MTag const &);
526 ///
527 HtmlStream & operator<<(HtmlStream &, ETag const &);
528
529
530 class SetHTMLMode {
531 public:
532         ///
533         explicit SetHTMLMode(HtmlStream & os, bool text);
534         ///
535         ~SetHTMLMode();
536 private:
537         ///
538         HtmlStream & os_;
539         ///
540         bool was_text_;
541 };
542
543
544 //
545 // Debugging
546 //
547
548 class NormalStream {
549 public:
550         ///
551         explicit NormalStream(odocstream & os) : os_(os) {}
552         ///
553         odocstream & os() { return os_; }
554 private:
555         ///
556         odocstream & os_;
557 };
558
559 ///
560 NormalStream & operator<<(NormalStream &, MathAtom const &);
561 ///
562 NormalStream & operator<<(NormalStream &, MathData const &);
563 ///
564 NormalStream & operator<<(NormalStream &, docstring const &);
565 ///
566 NormalStream & operator<<(NormalStream &, char const *);
567 ///
568 NormalStream & operator<<(NormalStream &, char);
569 ///
570 NormalStream & operator<<(NormalStream &, int);
571
572
573 //
574 // Maple
575 //
576
577
578 class MapleStream {
579 public:
580         ///
581         explicit MapleStream(odocstream & os) : os_(os) {}
582         ///
583         odocstream & os() { return os_; }
584 private:
585         ///
586         odocstream & os_;
587 };
588
589
590 ///
591 MapleStream & operator<<(MapleStream &, MathAtom const &);
592 ///
593 MapleStream & operator<<(MapleStream &, MathData const &);
594 ///
595 MapleStream & operator<<(MapleStream &, docstring const &);
596 ///
597 MapleStream & operator<<(MapleStream &, char_type);
598 ///
599 MapleStream & operator<<(MapleStream &, char const *);
600 ///
601 MapleStream & operator<<(MapleStream &, char);
602 ///
603 MapleStream & operator<<(MapleStream &, int);
604
605
606 //
607 // Maxima
608 //
609
610
611 class MaximaStream {
612 public:
613         ///
614         explicit MaximaStream(odocstream & os) : os_(os) {}
615         ///
616         odocstream & os() { return os_; }
617 private:
618         ///
619         odocstream & os_;
620 };
621
622
623 ///
624 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
625 ///
626 MaximaStream & operator<<(MaximaStream &, MathData const &);
627 ///
628 MaximaStream & operator<<(MaximaStream &, docstring const &);
629 ///
630 MaximaStream & operator<<(MaximaStream &, char_type);
631 ///
632 MaximaStream & operator<<(MaximaStream &, char const *);
633 ///
634 MaximaStream & operator<<(MaximaStream &, char);
635 ///
636 MaximaStream & operator<<(MaximaStream &, int);
637
638
639 //
640 // Mathematica
641 //
642
643
644 class MathematicaStream {
645 public:
646         ///
647         explicit MathematicaStream(odocstream & os) : os_(os) {}
648         ///
649         odocstream & os() { return os_; }
650 private:
651         ///
652         odocstream & os_;
653 };
654
655
656 ///
657 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
658 ///
659 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
660 ///
661 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
662 ///
663 MathematicaStream & operator<<(MathematicaStream &, char const *);
664 ///
665 MathematicaStream & operator<<(MathematicaStream &, char);
666 ///
667 MathematicaStream & operator<<(MathematicaStream &, int);
668
669
670 //
671 // Octave
672 //
673
674
675 class OctaveStream {
676 public:
677         ///
678         explicit OctaveStream(odocstream & os) : os_(os) {}
679         ///
680         odocstream & os() { return os_; }
681 private:
682         ///
683         odocstream & os_;
684 };
685
686 ///
687 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
688 ///
689 OctaveStream & operator<<(OctaveStream &, MathData const &);
690 ///
691 OctaveStream & operator<<(OctaveStream &, docstring const &);
692 ///
693 OctaveStream & operator<<(OctaveStream &, char_type);
694 ///
695 OctaveStream & operator<<(OctaveStream &, char const *);
696 ///
697 OctaveStream & operator<<(OctaveStream &, char);
698 ///
699 OctaveStream & operator<<(OctaveStream &, int);
700
701
702 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);
703
704 } // namespace lyx
705
706 #endif