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