Another minor change, but this should almost get us to the point that we
[lyx.git] / src / tex2lyx / Context.cpp
blob33050489a09f003deacaecb2a09b2bcf6da62eb7
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.
6 * \author Jean-Marc Lasgouttes
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Context.h"
14 #include "Layout.h"
16 #include "support/lstrings.h"
18 #include <iostream>
20 using namespace std;
21 using namespace lyx::support;
23 namespace lyx {
25 namespace {
27 void end_layout(ostream & os)
29 os << "\n\\end_layout\n";
33 void begin_deeper(ostream & os)
35 os << "\n\\begin_deeper";
39 void end_deeper(ostream & os)
41 os << "\n\\end_deeper";
47 bool operator==(TeXFont const & f1, TeXFont const & f2)
49 return
50 f1.size == f2.size &&
51 f1.family == f2.family &&
52 f1.series == f2.series &&
53 f1.shape == f2.shape;
57 void output_font_change(ostream & os, TeXFont const & oldfont,
58 TeXFont const & newfont)
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';
71 TeXFont Context::normalfont;
72 bool Context::empty = true;
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_)
86 if (!layout)
87 layout = &textclass.defaultLayout();
88 if (!parent_layout)
89 parent_layout = &textclass.defaultLayout();
93 Context::~Context()
95 if (!par_extra_stuff.empty())
96 cerr << "Bug: Ignoring par-level extra stuff '"
97 << par_extra_stuff << '\'' << endl;
101 void Context::begin_layout(ostream & os, Layout const * const & l)
103 os << "\n\\begin_layout " << to_utf8(l->name()) << "\n";
104 if (!extra_stuff.empty()) {
105 os << extra_stuff;
107 if (!par_extra_stuff.empty()) {
108 os << par_extra_stuff;
109 par_extra_stuff.erase();
111 // FIXME: This is not enough for things like
112 // \\Huge par1 \\par par2
113 output_font_change(os, normalfont, font);
117 void Context::check_layout(ostream & os)
119 if (need_layout) {
120 check_end_layout(os);
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;
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;
144 } else {
145 // No list-like environment
146 begin_layout(os, layout);
148 need_layout = false;
149 need_end_layout = true;
150 os << "\n";
151 empty = false;
156 void Context::check_end_layout(ostream & os)
158 if (need_end_layout) {
159 end_layout(os);
160 need_end_layout = false;
165 void Context::check_deeper(ostream & os)
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;
177 } else
178 check_end_deeper(os);
182 void Context::check_end_deeper(ostream & os)
184 if (need_end_deeper) {
185 end_deeper(os);
186 need_end_deeper = false;
188 if (deeper_paragraph) {
189 end_deeper(os);
190 deeper_paragraph = false;
195 void Context::set_item()
197 need_layout = true;
198 has_item = true;
202 void Context::new_paragraph(ostream & os)
204 check_end_layout(os);
205 need_layout = true;
209 void Context::add_extra_stuff(string const & stuff)
211 if (!contains(extra_stuff, stuff))
212 extra_stuff += stuff;
216 void Context::add_par_extra_stuff(string const & stuff)
218 if (!contains(par_extra_stuff, stuff))
219 par_extra_stuff += stuff;
223 void Context::dump(ostream & os, string const & desc) const
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;
250 } // namespace lyx