]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Context.cpp
more Lexer;
[lyx.git] / src / tex2lyx / Context.cpp
1 /**
2  * \file Context.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Context.h"
14 #include "Layout.h"
15
16 #include "support/lstrings.h"
17
18 #include <iostream>
19
20 using namespace std;
21 using namespace lyx::support;
22
23 namespace lyx {
24
25 namespace {
26
27 void begin_layout(ostream & os, Layout const * const & layout, TeXFont const & font,
28                   TeXFont const & normalfont)
29 {
30         os << "\n\\begin_layout " << to_utf8(layout->name()) << "\n";
31         // FIXME: This is not enough for things like
32         // \\Huge par1 \\par par2
33         output_font_change(os, normalfont, font);
34 }
35
36
37 void end_layout(ostream & os)
38 {
39         os << "\n\\end_layout\n";
40 }
41
42
43 void begin_deeper(ostream & os)
44 {
45         os << "\n\\begin_deeper";
46 }
47
48
49 void end_deeper(ostream & os)
50 {
51         os << "\n\\end_deeper";
52 }
53
54 }
55
56
57 bool operator==(TeXFont const & f1, TeXFont const & f2)
58 {
59         return
60                 f1.size == f2.size &&
61                 f1.family == f2.family &&
62                 f1.series == f2.series &&
63                 f1.shape == f2.shape;
64 }
65
66
67 void output_font_change(ostream & os, TeXFont const & oldfont,
68                         TeXFont const & newfont)
69 {
70         if (oldfont.family != newfont.family)
71                 os << "\n\\family " << newfont.family << '\n';
72         if (oldfont.series != newfont.series)
73                 os << "\n\\series " << newfont.series << '\n';
74         if (oldfont.shape != newfont.shape)
75                 os << "\n\\shape " << newfont.shape << '\n';
76         if (oldfont.size != newfont.size)
77                 os << "\n\\size " << newfont.size << '\n';
78 }
79
80
81 TeXFont Context::normalfont;
82 bool Context::empty = true;
83
84
85 Context::Context(bool need_layout_,
86                  TeX2LyXDocClass const & textclass_,
87                  Layout const * layout_, Layout const * parent_layout_,
88                  TeXFont font_)
89         : need_layout(need_layout_),
90           need_end_layout(false), need_end_deeper(false),
91           has_item(false), deeper_paragraph(false),
92           new_layout_allowed(true), textclass(textclass_),
93           layout(layout_), parent_layout(parent_layout_),
94           font(font_)
95 {
96         if (!layout)
97                 layout = &textclass.defaultLayout();
98         if (!parent_layout)
99                 parent_layout = &textclass.defaultLayout();
100 }
101
102
103 Context::~Context()
104 {
105         if (!extra_stuff.empty())
106                 cerr << "Bug: Ignoring extra stuff '" << extra_stuff
107                           << '\'' << endl;
108 }
109
110
111 void Context::check_layout(ostream & os)
112 {
113         if (need_layout) {
114                 check_end_layout(os);
115
116                 // are we in a list-like environment?
117                 if (layout->isEnvironment()
118                     && layout->latextype != LATEX_ENVIRONMENT) {
119                         // A list-like environment
120                         if (has_item) {
121                                 // a new item. If we had a standard
122                                 // paragraph before, we have to end it.
123                                 if (deeper_paragraph) {
124                                         end_deeper(os);
125                                         deeper_paragraph = false;
126                                 }
127                                 begin_layout(os, layout, font, normalfont);
128                                 has_item = false;
129                         } else {
130                                 // a standard paragraph in an
131                                 // enumeration. We have to recognize
132                                 // that this may require a begin_deeper.
133                                 if (!deeper_paragraph)
134                                         begin_deeper(os);
135                                 begin_layout(os, &textclass.defaultLayout(),
136                                              font, normalfont);
137                                 deeper_paragraph = true;
138                         }
139                 } else {
140                         // No list-like environment
141                         begin_layout(os, layout, font, normalfont);
142                 }
143                 need_layout = false;
144                 need_end_layout = true;
145                 if (!extra_stuff.empty()) {
146                         os << extra_stuff;
147                         extra_stuff.erase();
148                 }
149                 os << "\n";
150                 empty = false;
151         }
152 }
153
154
155 void Context::check_end_layout(ostream & os)
156 {
157         if (need_end_layout) {
158                 end_layout(os);
159                 need_end_layout = false;
160         }
161 }
162
163
164 void Context::check_deeper(ostream & os)
165 {
166         if (parent_layout->isEnvironment()) {
167                 // We start a nested environment.
168                 // We need to increase the depth.
169                 if (need_end_deeper) {
170                         // no need to have \end_deeper \begin_deeper
171                         need_end_deeper = false;
172                 } else {
173                         begin_deeper(os);
174                         need_end_deeper = true;
175                 }
176         } else
177                 check_end_deeper(os);
178 }
179
180
181 void Context::check_end_deeper(ostream & os)
182 {
183         if (need_end_deeper) {
184                 end_deeper(os);
185                 need_end_deeper = false;
186         }
187         if (deeper_paragraph) {
188                 end_deeper(os);
189                 deeper_paragraph = false;
190         }
191 }
192
193
194 void Context::set_item()
195 {
196         need_layout = true;
197         has_item = true;
198 }
199
200
201 void Context::new_paragraph(ostream & os)
202 {
203         check_end_layout(os);
204         need_layout = true;
205 }
206
207
208 void Context::add_extra_stuff(string const & stuff)
209 {
210         if (!contains(extra_stuff, stuff))
211                 extra_stuff += stuff;
212 }
213
214
215 void Context::dump(ostream & os, string const & desc) const
216 {
217         os << "\n" << desc <<" [";
218         if (need_layout)
219                 os << "need_layout ";
220         if (need_end_layout)
221                 os << "need_end_layout ";
222         if (need_end_deeper)
223                 os << "need_end_deeper ";
224         if (has_item)
225                 os << "has_item ";
226         if (deeper_paragraph)
227                 os << "deeper_paragraph ";
228         if (new_layout_allowed)
229                 os << "new_layout_allowed ";
230         if (!extra_stuff.empty())
231                 os << "extrastuff=[" << extra_stuff << "] ";
232         os << "textclass=" << textclass.name()
233            << " layout=" << to_utf8(layout->name())
234            << " parent_layout=" << to_utf8(parent_layout->name()) << "] font=["
235            << font.size << ' ' << font.family << ' ' << font.series << ' '
236            << font.shape << ']' << endl;
237 }
238
239
240 } // namespace lyx