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