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