]> git.lyx.org Git - lyx.git/blob - src/mathed/MathStream.h
Cmake export tests: Added sublabel handling also to revertedTests
[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 "support/strfwd.h"
16
17 #include "InsetMath.h"
18 // FIXME: Move to individual insets
19 #include "MetricsInfo.h"
20 #include "texstream.h"
21
22
23 namespace lyx {
24
25 class Encoding;
26 class InsetMath;
27 class MathAtom;
28 class MathData;
29
30 //
31 // LaTeX/LyX
32 //
33
34 class WriteStream {
35 public:
36         ///
37         enum OutputType {
38                 wsDefault,
39                 wsDryrun,
40                 wsPreview
41         };
42         ///
43         WriteStream(otexrowstream & os, bool fragile, bool latex, OutputType output,
44                                 Encoding const * encoding = 0);
45         ///
46         explicit WriteStream(otexrowstream & os);
47         ///
48         ~WriteStream();
49         ///
50         int line() const { return line_; }
51         ///
52         bool fragile() const { return fragile_; }
53         ///
54         bool latex() const { return latex_; }
55         ///
56         OutputType output() const { return output_; }
57         ///
58         otexrowstream & os() { return os_; }
59         ///
60         TexRow & texrow() { return os_.texrow(); }
61         ///
62         bool & firstitem() { return firstitem_; }
63         ///
64         void addlines(unsigned int);
65         /// record whether we can write an immediately following newline char
66         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
67         /// tell whether we can write an immediately following newline char
68         bool canBreakLine() const { return canbreakline_; }
69         /// writes space if next thing is isalpha()
70         void pendingSpace(bool how);
71         /// writes space if next thing is isalpha()
72         bool pendingSpace() const { return pendingspace_; }
73         /// tell whether to write the closing brace of \ensuremath
74         void pendingBrace(bool brace);
75         /// tell whether to write the closing brace of \ensuremath
76         bool pendingBrace() const { return pendingbrace_; }
77         /// tell whether we are in text mode or not when producing latex code
78         void textMode(bool textmode);
79         /// tell whether we are in text mode or not when producing latex code
80         bool textMode() const { return textmode_; }
81         /// tell whether we are allowed to switch mode when producing latex code
82         void lockedMode(bool locked);
83         /// tell whether we are allowed to switch mode when producing latex code
84         bool lockedMode() const { return locked_; }
85         /// tell whether to use only ascii chars when producing latex code
86         void asciiOnly(bool ascii);
87         /// tell whether to use only ascii chars when producing latex code
88         bool asciiOnly() const { return ascii_; }
89         /// LaTeX encoding
90         Encoding const * encoding() const { return encoding_; }
91
92         /// maintains a stack of texrow informations about outer math insets.
93         /// push an entry
94         void pushRowEntry(TexRow::RowEntry entry);
95         /// pop an entry
96         void popRowEntry();
97         /// TexRow::starts the innermost outer math inset
98         /// returns true if the outer row entry will appear at this line
99         bool startOuterRow();
100 private:
101         ///
102         otexrowstream & os_;
103         /// do we have to write \\protect sometimes
104         bool fragile_;
105         /// are we at the beginning of an MathData?
106         bool firstitem_;
107         /// are we writing to .tex?
108         int latex_;
109         /// output type (default, source preview, instant preview)?
110         OutputType output_;
111         /// do we have a space pending?
112         bool pendingspace_;
113         /// do we have a brace pending?
114         bool pendingbrace_;
115         /// are we in text mode when producing latex code?
116         bool textmode_;
117         /// are we allowed to switch mode when producing latex code?
118         bool locked_;
119         /// should we use only ascii chars when producing latex code?
120         bool ascii_;
121         /// are we allowed to output an immediately following newline?
122         bool canbreakline_;
123         ///
124         int line_;
125         ///
126         Encoding const * encoding_;
127         ///
128         std::vector<TexRow::RowEntry> outer_row_entries_;
129 };
130
131 ///
132 WriteStream & operator<<(WriteStream &, MathAtom const &);
133 ///
134 WriteStream & operator<<(WriteStream &, MathData const &);
135 ///
136 WriteStream & operator<<(WriteStream &, docstring const &);
137 ///
138 WriteStream & operator<<(WriteStream &, char const * const);
139 ///
140 WriteStream & operator<<(WriteStream &, char);
141 ///
142 WriteStream & operator<<(WriteStream &, int);
143 ///
144 WriteStream & operator<<(WriteStream &, unsigned int);
145
146 /// ensure math mode, possibly by opening \ensuremath
147 bool ensureMath(WriteStream & os, bool needs_math_mode = true, bool macro = false);
148
149 /// ensure the requested mode, possibly by closing \ensuremath
150 int ensureMode(WriteStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
151
152
153 /**
154  * MathEnsurer - utility class for ensuring math mode
155  *
156  * A local variable of this type can be used to either ensure math mode
157  * or delay the writing of a pending brace when outputting LaTeX.
158  *
159  * Example 1:
160  *
161  *      MathEnsurer ensurer(os);
162  *
163  * If not already in math mode, inserts an \ensuremath command followed
164  * by an open brace. This brace will be automatically closed when exiting
165  * math mode. Math mode is automatically exited when writing something
166  * that doesn't explicitly require math mode.
167  *
168  * Example 2:
169  *
170  *      MathEnsurer ensurer(os, false);
171  *
172  * Simply suspend writing a closing brace until the end of ensurer's scope.
173  *
174  * Example 3:
175  *
176  *      MathEnsurer ensurer(os, needs_math_mode, true);
177  *
178  * The third parameter is set to true only for a user defined macro, which
179  * needs special handling. When it is a MathMacro, the needs_math_mode
180  * parameter is true and the behavior is as in Example 1. When the
181  * needs_math_mode parameter is false (not a MathMacro) and the macro
182  * was entered in a text box and we are in math mode, the mode is reset
183  * to text. This is because the macro was probably designed for text mode
184  * (given that it was entered in text mode and we have no way to tell the
185  * contrary).
186  */
187 class MathEnsurer
188 {
189 public:
190         ///
191         explicit MathEnsurer(WriteStream & os, bool needs_math_mode = true, bool macro = false)
192                 : os_(os), brace_(ensureMath(os, needs_math_mode, macro)) {}
193         ///
194         ~MathEnsurer() { os_.pendingBrace(brace_); }
195 private:
196         ///
197         WriteStream & os_;
198         ///
199         bool brace_;
200 };
201
202
203 /**
204  * ModeSpecifier - utility class for specifying a given mode (math or text)
205  *
206  * A local variable of this type can be used to specify that a command or
207  * environment works in a given mode. For example, \mbox works in text
208  * mode, but \boxed works in math mode. Note that no mode changing commands
209  * are needed, but we have to track the current mode, hence this class.
210  * This is only used when exporting to latex and helps determining whether
211  * the mode needs being temporarily switched when a command would not work
212  * in the current mode. As there are cases where this switching is to be
213  * avoided, the optional third parameter can be used to lock the mode.
214  * When the mode is locked, the optional fourth parameter specifies whether
215  * strings are to be output by using a suitable ascii representation.
216  *
217  * Example 1:
218  *
219  *      ModeSpecifier specifier(os, TEXT_MODE);
220  *
221  * Sets the current mode to text mode and allows mode switching.
222  *
223  * Example 2:
224  *
225  *      ModeSpecifier specifier(os, TEXT_MODE, true);
226  *
227  * Sets the current mode to text mode and disallows mode switching.
228  *
229  * Example 3:
230  *
231  *      ModeSpecifier specifier(os, TEXT_MODE, true, true);
232  *
233  * Sets the current mode to text mode, disallows mode switching, and outputs
234  * strings as ascii only.
235  *
236  * At the end of specifier's scope the mode is reset to its previous value.
237  */
238 class ModeSpecifier
239 {
240 public:
241         ///
242         explicit ModeSpecifier(WriteStream & os, InsetMath::mode_type mode,
243                                 bool locked = false, bool ascii = false)
244                 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
245         ///
246         ~ModeSpecifier()
247         {
248                 os_.textMode(oldmodes_ & 0x01);
249                 os_.lockedMode(oldmodes_ & 0x02);
250                 os_.asciiOnly(oldmodes_ & 0x04);
251         }
252 private:
253         ///
254         WriteStream & os_;
255         ///
256         int oldmodes_;
257 };
258
259
260
261 //
262 //  MathML
263 //
264
265 class MTag {
266 public:
267         ///
268         MTag(char const * const tag, std::string attr = "") 
269                 : tag_(tag), attr_(attr) {}
270         ///
271         char const * const tag_;
272         ///
273         std::string attr_;
274 };
275
276 class ETag {
277 public:
278         ///
279         ETag(char const * const tag) : tag_(tag) {}
280         ///
281         char const * const tag_;
282 };
283
284
285 /// Throw MathExportException to signal that the attempt to export
286 /// some math in the current format did not succeed. E.g., we can't
287 /// export xymatrix as MathML, so that will throw, and we'll fall back
288 /// to images.
289 class MathExportException : public std::exception {};
290
291
292 class MathStream {
293 public:
294         ///
295         explicit MathStream(odocstream & os);
296         ///
297         void cr();
298         ///
299         odocstream & os() { return os_; }
300         ///
301         int line() const { return line_; }
302         ///
303         int & tab() { return tab_; }
304         ///
305         friend MathStream & operator<<(MathStream &, char const *);
306         ///
307         void defer(docstring const &);
308         ///
309         void defer(std::string const &);
310         ///
311         docstring deferred() const;
312         ///
313         bool inText() const { return in_text_; }
314 private:
315         ///
316         void setTextMode(bool t) { in_text_ = t; }
317         ///
318         odocstream & os_;
319         ///
320         int tab_;
321         ///
322         int line_;
323         ///
324         bool in_text_;
325         ///
326         odocstringstream deferred_;
327         ///
328         friend class SetMode;
329 };
330
331 ///
332 MathStream & operator<<(MathStream &, MathAtom const &);
333 ///
334 MathStream & operator<<(MathStream &, MathData const &);
335 ///
336 MathStream & operator<<(MathStream &, docstring const &);
337 ///
338 MathStream & operator<<(MathStream &, char const *);
339 ///
340 MathStream & operator<<(MathStream &, char);
341 ///
342 MathStream & operator<<(MathStream &, char_type);
343 ///
344 MathStream & operator<<(MathStream &, MTag const &);
345 ///
346 MathStream & operator<<(MathStream &, ETag const &);
347
348
349 /// A simpler version of ModeSpecifier, for MathML
350 class SetMode {
351 public:
352         ///
353         explicit SetMode(MathStream & os, bool text);
354         ///
355         ~SetMode();
356 private:
357         ///
358         MathStream & os_;
359         ///
360         bool was_text_;
361 };
362
363
364 class HtmlStream {
365 public:
366         ///
367         explicit HtmlStream(odocstream & os);
368         ///
369         void cr();
370         ///
371         odocstream & os() { return os_; }
372         ///
373         int line() const { return line_; }
374         ///
375         int & tab() { return tab_; }
376         ///
377         friend HtmlStream & operator<<(HtmlStream &, char const *);
378         ///
379         void defer(docstring const &);
380         ///
381         void defer(std::string const &);
382         ///
383         docstring deferred() const;
384         ///
385         bool inText() const { return in_text_; }
386 private:
387         ///
388         void setTextMode(bool t) { in_text_ = t; }
389         ///
390         odocstream & os_;
391         ///
392         int tab_;
393         ///
394         int line_;
395         ///
396         bool in_text_;
397         ///
398         odocstringstream deferred_;
399         ///
400         friend class SetHTMLMode;
401 };
402
403 ///
404 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
405 ///
406 HtmlStream & operator<<(HtmlStream &, MathData const &);
407 ///
408 HtmlStream & operator<<(HtmlStream &, docstring const &);
409 ///
410 HtmlStream & operator<<(HtmlStream &, char const *);
411 ///
412 HtmlStream & operator<<(HtmlStream &, char);
413 ///
414 HtmlStream & operator<<(HtmlStream &, char_type);
415 ///
416 HtmlStream & operator<<(HtmlStream &, MTag const &);
417 ///
418 HtmlStream & operator<<(HtmlStream &, ETag const &);
419
420
421 class SetHTMLMode {
422 public:
423         ///
424         explicit SetHTMLMode(HtmlStream & os, bool text);
425         ///
426         ~SetHTMLMode();
427 private:
428         ///
429         HtmlStream & os_;
430         ///
431         bool was_text_;
432 };
433
434
435 //
436 // Debugging
437 //
438
439 class NormalStream {
440 public:
441         ///
442         explicit NormalStream(odocstream & os) : os_(os) {}
443         ///
444         odocstream & os() { return os_; }
445 private:
446         ///
447         odocstream & os_;
448 };
449
450 ///
451 NormalStream & operator<<(NormalStream &, MathAtom const &);
452 ///
453 NormalStream & operator<<(NormalStream &, MathData const &);
454 ///
455 NormalStream & operator<<(NormalStream &, docstring const &);
456 ///
457 NormalStream & operator<<(NormalStream &, char const *);
458 ///
459 NormalStream & operator<<(NormalStream &, char);
460 ///
461 NormalStream & operator<<(NormalStream &, int);
462
463
464 //
465 // Maple
466 //
467
468
469 class MapleStream {
470 public:
471         ///
472         explicit MapleStream(odocstream & os) : os_(os) {}
473         ///
474         odocstream & os() { return os_; }
475 private:
476         ///
477         odocstream & os_;
478 };
479
480
481 ///
482 MapleStream & operator<<(MapleStream &, MathAtom const &);
483 ///
484 MapleStream & operator<<(MapleStream &, MathData const &);
485 ///
486 MapleStream & operator<<(MapleStream &, docstring const &);
487 ///
488 MapleStream & operator<<(MapleStream &, char_type);
489 ///
490 MapleStream & operator<<(MapleStream &, char const *);
491 ///
492 MapleStream & operator<<(MapleStream &, char);
493 ///
494 MapleStream & operator<<(MapleStream &, int);
495
496
497 //
498 // Maxima
499 //
500
501
502 class MaximaStream {
503 public:
504         ///
505         explicit MaximaStream(odocstream & os) : os_(os) {}
506         ///
507         odocstream & os() { return os_; }
508 private:
509         ///
510         odocstream & os_;
511 };
512
513
514 ///
515 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
516 ///
517 MaximaStream & operator<<(MaximaStream &, MathData const &);
518 ///
519 MaximaStream & operator<<(MaximaStream &, docstring const &);
520 ///
521 MaximaStream & operator<<(MaximaStream &, char_type);
522 ///
523 MaximaStream & operator<<(MaximaStream &, char const *);
524 ///
525 MaximaStream & operator<<(MaximaStream &, char);
526 ///
527 MaximaStream & operator<<(MaximaStream &, int);
528
529
530 //
531 // Mathematica
532 //
533
534
535 class MathematicaStream {
536 public:
537         ///
538         explicit MathematicaStream(odocstream & os) : os_(os) {}
539         ///
540         odocstream & os() { return os_; }
541 private:
542         ///
543         odocstream & os_;
544 };
545
546
547 ///
548 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
549 ///
550 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
551 ///
552 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
553 ///
554 MathematicaStream & operator<<(MathematicaStream &, char const *);
555 ///
556 MathematicaStream & operator<<(MathematicaStream &, char);
557 ///
558 MathematicaStream & operator<<(MathematicaStream &, int);
559
560
561 //
562 // Octave
563 //
564
565
566 class OctaveStream {
567 public:
568         ///
569         explicit OctaveStream(odocstream & os) : os_(os) {}
570         ///
571         odocstream & os() { return os_; }
572 private:
573         ///
574         odocstream & os_;
575 };
576
577 ///
578 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
579 ///
580 OctaveStream & operator<<(OctaveStream &, MathData const &);
581 ///
582 OctaveStream & operator<<(OctaveStream &, docstring const &);
583 ///
584 OctaveStream & operator<<(OctaveStream &, char_type);
585 ///
586 OctaveStream & operator<<(OctaveStream &, char const *);
587 ///
588 OctaveStream & operator<<(OctaveStream &, char);
589 ///
590 OctaveStream & operator<<(OctaveStream &, int);
591
592 } // namespace lyx
593
594 #endif