]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/Context.cpp
ff3c85d114a7b1892311b3cc6a59db2caa22c6d4
[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
22 namespace lyx {
23
24 namespace {
25
26 void begin_layout(ostream & os, LayoutPtr const & layout, TeXFont const & font,
27                   TeXFont const & normalfont)
28 {
29         os << "\n\\begin_layout " << to_utf8(layout->name()) << "\n";
30         // FIXME: This is not enough for things like
31         // \\Huge par1 \\par par2
32         output_font_change(os, normalfont, font);
33 }
34
35
36 void end_layout(ostream & os)
37 {
38         os << "\n\\end_layout\n";
39 }
40
41
42 void begin_deeper(ostream & os)
43 {
44         os << "\n\\begin_deeper";
45 }
46
47
48 void end_deeper(ostream & os)
49 {
50         os << "\n\\end_deeper";
51 }
52
53 }
54
55
56 bool operator==(TeXFont const & f1, TeXFont const & f2)
57 {
58         return
59                 f1.size == f2.size &&
60                 f1.family == f2.family &&
61                 f1.series == f2.series &&
62                 f1.shape == f2.shape;
63 }
64
65
66 void output_font_change(ostream & os, TeXFont const & oldfont,
67                         TeXFont const & newfont)
68 {
69         if (oldfont.family != newfont.family)
70                 os << "\n\\family " << newfont.family << '\n';
71         if (oldfont.series != newfont.series)
72                 os << "\n\\series " << newfont.series << '\n';
73         if (oldfont.shape != newfont.shape)
74                 os << "\n\\shape " << newfont.shape << '\n';
75         if (oldfont.size != newfont.size)
76                 os << "\n\\size " << newfont.size << '\n';
77 }
78
79
80 TeXFont Context::normalfont;
81 bool Context::empty = true;
82
83
84 Context::Context(bool need_layout_,
85                  TextClass const & textclass_,
86                  LayoutPtr layout_, LayoutPtr parent_layout_,
87                  TeXFont font_)
88         : need_layout(need_layout_),
89           need_end_layout(false), need_end_deeper(false),
90           has_item(false), deeper_paragraph(false),
91           new_layout_allowed(true), textclass(textclass_),
92           layout(layout_), parent_layout(parent_layout_),
93           font(font_)
94 {
95         if (!layout.get())
96                 layout = textclass.defaultLayout();
97         if (!parent_layout.get())
98                 parent_layout = textclass.defaultLayout();
99 }
100
101
102 Context::~Context()
103 {
104         if (!extra_stuff.empty())
105                 cerr << "Bug: Ignoring extra stuff '" << extra_stuff
106                           << '\'' << endl;
107 }
108
109
110 void Context::check_layout(ostream & os)
111 {
112         if (need_layout) {
113                 check_end_layout(os);
114
115                 // are we in a list-like environment?
116                 if (layout->isEnvironment()
117                     && layout->latextype != LATEX_ENVIRONMENT) {
118                         // A list-like environment
119                         if (has_item) {
120                                 // a new item. If we had a standard
121                                 // paragraph before, we have to end it.
122                                 if (deeper_paragraph) {
123                                         end_deeper(os);
124                                         deeper_paragraph = false;
125                                 }
126                                 begin_layout(os, layout, font, normalfont);
127                                 has_item = false;
128                         } else {
129                                 // a standard paragraph in an
130                                 // enumeration. We have to recognize
131                                 // that this may require a begin_deeper.
132                                 if (!deeper_paragraph)
133                                         begin_deeper(os);
134                                 begin_layout(os, textclass.defaultLayout(),
135                                              font, normalfont);
136                                 deeper_paragraph = true;
137                         }
138                 } else {
139                         // No list-like environment
140                         begin_layout(os, layout, font, normalfont);
141                 }
142                 need_layout = false;
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(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=" << to_utf8(layout->name())
233            << " parent_layout=" << to_utf8(parent_layout->name()) << "] font=["
234            << font.size << ' ' << font.family << ' ' << font.series << ' '
235            << font.shape << ']' << endl;
236 }
237
238
239 } // namespace lyx