3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Enrico Forestieri
8 * Full author contact details are available in file CREDITS.
13 #include "texstream.h"
17 #include "support/lstrings.h"
18 #include "support/unicode.h"
29 using lyx::support::contains;
30 using lyx::support::split;
36 otexrowstream::otexrowstream(odocstream & os)
37 : os_(os), texrow_(make_unique<TexRow>())
41 otexrowstream::~otexrowstream() = default;
44 unique_ptr<TexRow> otexrowstream::releaseTexRow()
46 auto p = make_unique<TexRow>();
52 void otexrowstream::put(char_type const & c)
60 void otexstream::put(char_type const & c)
62 bool isprotected = false;
64 if (!canbreakline_ && c == ' ') {
68 protectspace_ = false;
70 if (terminate_command_) {
71 if ((c == ' ' || c == '\0') && !isprotected)
72 // A space follows. Terminate with brackets.
74 else if (c != '\\' && c != '{' && c != '}')
75 // Non-terminating character follows. Terminate with space.
77 terminate_command_ = false;
79 otexrowstream::put(c);
84 size_t otexstringstream::length()
86 auto pos = ods_.tellp();
87 return (pos >= 0) ? size_t(pos) : 0;
91 TexString otexstringstream::release()
93 TexString ts(ods_.str(), move(texrow()));
97 ods_.str(docstring());
103 SafeBreakLine safebreakln;
104 TerminateCommand termcmd;
107 otexstream & operator<<(otexstream & ots, BreakLine)
109 if (ots.canBreakLine()) {
110 ots.otexrowstream::put('\n');
113 ots.protectSpace(false);
114 ots.terminateCommand(false);
119 otexstream & operator<<(otexstream & ots, SafeBreakLine)
121 otexrowstream & otrs = ots;
122 if (ots.canBreakLine()) {
126 ots.protectSpace(false);
127 ots.terminateCommand(false);
132 otexstream & operator<<(otexstream & ots, TerminateCommand)
134 ots.terminateCommand(true);
139 otexrowstream & operator<<(otexrowstream & ots, odocstream_manip pf)
142 if (pf == static_cast<odocstream_manip>(endl)) {
143 ots.texrow().newline();
149 otexstream & operator<<(otexstream & ots, odocstream_manip pf)
151 otexrowstream & otrs = ots;
153 if (pf == static_cast<odocstream_manip>(endl)) {
160 otexrowstream & operator<<(otexrowstream & ots, TexString ts)
163 ots.os() << move(ts.str);
164 ots.texrow().append(move(ts.texrow));
169 otexstream & operator<<(otexstream & ots, TexString ts)
171 size_t const len = ts.str.length();
172 // Check whether there is something to output
176 otexrowstream & otrs = ots;
177 bool isprotected = false;
178 char const c = ts.str[0];
179 if (ots.protectSpace()) {
180 if (!ots.canBreakLine() && c == ' ') {
184 ots.protectSpace(false);
186 if (ots.terminateCommand()) {
187 if ((c == ' ' || c == '\0') && !isprotected)
188 // A space follows. Terminate with brackets.
190 else if (c != '\\' && c != '{' && c != '}')
191 // Non-terminating character follows. Terminate with space.
193 ots.terminateCommand(false);
197 ots.canBreakLine(ts.str[len - 2] != '\n');
198 ots.lastChar(ts.str[len - 1]);
205 otexrowstream & operator<<(otexrowstream & ots, docstring const & s)
208 ots.texrow().newlines(count(s.begin(), s.end(), '\n'));
213 otexstream & operator<<(otexstream & ots, docstring const & s)
215 size_t const len = s.length();
217 // Check whether there's something to output
220 otexrowstream & otrs = ots;
221 bool isprotected = false;
223 if (ots.protectSpace()) {
224 if (!ots.canBreakLine() && c == ' ') {
228 ots.protectSpace(false);
230 if (ots.terminateCommand()) {
231 if ((c == ' ' || c == '\0') && !isprotected)
232 // A space follows. Terminate with brackets.
234 else if (c != '\\' && c != '{' && c != '}')
235 // Non-terminating character follows. Terminate with space.
237 ots.terminateCommand(false);
240 if (contains(s, 0xF0000)) {
241 // Some encoding changes for the underlying stream are embedded
242 // in the docstring. The encoding names to be used are enclosed
243 // between the code points 0xF0000 and 0xF0001, the first two
244 // characters of plane 15, which is a Private Use Area whose
245 // codepoints don't have any associated glyph.
247 docstring s2 = split(s, s1, 0xF0000);
254 docstring const s3 = split(s2, enc, 0xF0001);
255 if (!contains(s2, 0xF0001))
256 s2 = split(enc, s1, 0xF0000);
258 otrs << setEncoding(to_ascii(enc));
259 s2 = split(s3, s1, 0xF0000);
266 ots.canBreakLine(s[len - 2] != '\n');
267 ots.lastChar(s[len - 1]);
272 otexrowstream & operator<<(otexrowstream & ots, string const & s)
279 otexstream & operator<<(otexstream & ots, string const & s)
286 otexrowstream & operator<<(otexrowstream & ots, char const * s)
293 otexstream & operator<<(otexstream & ots, char const * s)
300 otexrowstream & operator<<(otexrowstream & ots, char c)
307 otexstream & operator<<(otexstream & ots, char c)
314 template <typename Type>
315 otexrowstream & operator<<(otexrowstream & ots, Type value)
321 template otexrowstream & operator<< <SetEnc>(otexrowstream & os, SetEnc);
322 template otexrowstream & operator<< <double>(otexrowstream &, double);
323 template otexrowstream & operator<< <int>(otexrowstream &, int);
324 template otexrowstream & operator<< <unsigned int>(otexrowstream &,
326 template otexrowstream & operator<< <unsigned long>(otexrowstream &,
329 #ifdef LYX_USE_LONG_LONG
330 template otexrowstream & operator<< <unsigned long long>(otexrowstream &,
335 template <typename Type>
336 otexstream & operator<<(otexstream & ots, Type value)
340 ots.protectSpace(false);
341 ots.terminateCommand(false);
345 template otexstream & operator<< <SetEnc>(otexstream & os, SetEnc);
346 template otexstream & operator<< <double>(otexstream &, double);
347 template otexstream & operator<< <int>(otexstream &, int);
348 template otexstream & operator<< <unsigned int>(otexstream &, unsigned int);
349 template otexstream & operator<< <unsigned long>(otexstream &, unsigned long);
350 #ifdef LYX_USE_LONG_LONG
351 template otexstream & operator<< <unsigned long long>(otexstream &, unsigned long long);