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