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