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