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