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