]> git.lyx.org Git - lyx.git/blob - src/texstream.h
Improve fractions bar
[lyx.git] / src / texstream.h
1 // -*- C++ -*-
2 /**
3  * \file texstream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_TEXSTREAM_H
13 #define LYX_TEXSTREAM_H
14
15 #include "support/docstream.h"
16 #include "support/unique_ptr.h"
17
18 namespace lyx {
19
20 class TexRow;
21 struct TexString;
22
23
24 /** Wrapper class for odocstream.
25     This class is used to automatically count the lines of the exported latex
26     code.
27   */
28
29 class otexrowstream {
30 public:
31         ///
32         explicit otexrowstream(odocstream & os);
33         /// defaulted
34         ~otexrowstream();
35         ///
36         odocstream & os() { return os_; }
37         ///
38         TexRow & texrow() { return *texrow_; }
39         ///
40         unique_ptr<TexRow> releaseTexRow();
41         ///
42         void put(char_type const & c);
43         ///
44         void append(TexString ts);
45 private:
46         ///
47         odocstream & os_;
48         ///
49         unique_ptr<TexRow> texrow_;
50 };
51
52 ///
53 otexrowstream & operator<<(otexrowstream &, odocstream_manip);
54 ///
55 otexrowstream & operator<<(otexrowstream &, TexString);
56 ///
57 otexrowstream & operator<<(otexrowstream &, docstring const &);
58 ///
59 otexrowstream & operator<<(otexrowstream &, std::string const &);
60 ///
61 otexrowstream & operator<<(otexrowstream &, char const *);
62 ///
63 otexrowstream & operator<<(otexrowstream &, char);
64 ///
65 template <typename Type>
66 otexrowstream & operator<<(otexrowstream & ots, Type value);
67
68
69 /** Subclass for otexrowstream.
70     This class is used to ensure that no blank lines may be inadvertently output.
71     To this end, use the special variables "breakln" and "safebreakln" as if
72     they were iomanip's to ensure that the next output will start at the
73     beginning of a line. Using "breakln", a '\n' char will be output if needed,
74     while using "safebreakln", "%\n" will be output if needed.
75     The class also records the last output character and can tell whether
76     a paragraph break was just output.
77   */
78
79 class otexstream : public otexrowstream {
80 public:
81         ///
82         explicit otexstream(odocstream & os)
83                 : otexrowstream(os), canbreakline_(false),
84                   protectspace_(false), parbreak_(true), lastchar_(0) {}
85         ///
86         void put(char_type const & c);
87         ///
88         void append(TexString ts);
89         ///
90         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
91         ///
92         bool canBreakLine() const { return canbreakline_; }
93         ///
94         void protectSpace(bool protectspace) { protectspace_ = protectspace; }
95         ///
96         bool protectSpace() const { return protectspace_; }
97         ///
98         void lastChar(char_type const & c)
99         {
100                 parbreak_ = (!canbreakline_ && c == '\n');
101                 canbreakline_ = (c != '\n');
102                 lastchar_ = c;
103         }
104         ///
105         char_type lastChar() const { return lastchar_; }
106         ///
107         bool afterParbreak() const { return parbreak_; }
108 private:
109         ///
110         bool canbreakline_;
111         ///
112         bool protectspace_;
113         ///
114         bool parbreak_;
115         ///
116         char_type lastchar_;
117 };
118
119
120 /// because we need to pass ods_ to the base class
121 struct otexstringstream_helper { odocstringstream ods_; };
122
123 /// otexstringstream : a odocstringstream with tex/row correspondence
124 class otexstringstream : otexstringstream_helper, public otexstream {
125 public:
126         otexstringstream() : otexstringstream_helper(), otexstream(ods_) {}
127         ///
128         docstring str() const { return ods_.str(); }
129         ///
130         size_t length();
131         ///
132         bool empty() { return 0 == length(); }
133         /// move-returns the contents and reset the texstream
134         TexString release();
135 };
136
137
138 /// Helper structs for breaking a line
139 struct BreakLine {
140         char n;
141 };
142
143 struct SafeBreakLine {
144         char n;
145 };
146
147 extern BreakLine breakln;
148 extern SafeBreakLine safebreakln;
149
150 ///
151 otexstream & operator<<(otexstream &, BreakLine);
152 ///
153 otexstream & operator<<(otexstream &, SafeBreakLine);
154 ///
155 otexstream & operator<<(otexstream &, odocstream_manip);
156 ///
157 otexstream & operator<<(otexstream &, TexString);
158 ///
159 otexstream & operator<<(otexstream &, docstring const &);
160 ///
161 otexstream & operator<<(otexstream &, std::string const &);
162 ///
163 otexstream & operator<<(otexstream &, char const *);
164 ///
165 otexstream & operator<<(otexstream &, char);
166 ///
167 template <typename Type>
168 otexstream & operator<<(otexstream & ots, Type value);
169
170
171 }
172
173 #endif