]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/context.C
small font tweaks
[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 void output_font_change(ostream & os, Font const & oldfont,
55                         Font const & newfont)
56 {
57         if (oldfont.family != newfont.family)
58                 os << "\n\\family " << newfont.family << '\n';
59         if (oldfont.series != newfont.series)
60                 os << "\n\\series " << newfont.series << '\n';
61         if (oldfont.shape != newfont.shape)
62                 os << "\n\\shape " << newfont.shape << '\n';
63         if (oldfont.size != newfont.size)
64                 os << "\n\\size " << newfont.size << '\n';
65 }
66
67
68 Font Context::normalfont;
69
70
71 Context::Context(bool need_layout_,
72                  LyXTextClass const & textclass_,
73                  LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_,
74                  Font font_)
75         : need_layout(need_layout_),
76           need_end_layout(false), need_end_deeper(false),
77           has_item(false), deeper_paragraph(false),
78           textclass(textclass_),
79           layout(layout_), parent_layout(parent_layout_),
80           font(font_)
81 {
82         if (!layout.get())
83                 layout = textclass.defaultLayout();
84         if (!parent_layout.get())
85                 parent_layout = textclass.defaultLayout();
86 }
87
88
89 void Context::check_layout(ostream & os)
90 {
91         if (need_layout) {
92                 check_end_layout(os);
93
94                 // are we in a list-like environment?
95                 if (layout->isEnvironment()
96                     && layout->latextype != LATEX_ENVIRONMENT) {
97                         // A list-like environment
98                         if (has_item) {
99                                 // a new item. If we had a standard
100                                 // paragraph before, we have to end it.
101                                 if (deeper_paragraph) {
102                                         end_deeper(os);
103                                         deeper_paragraph = false;
104                                 }
105                                 begin_layout(os, layout, font, normalfont);
106                                 has_item = false;
107                                 need_layout=false;
108                                 need_end_layout = true;
109                         } else {
110                                 // a standard paragraph in an
111                                 // enumeration. We have to recognize
112                                 // that this may require a begin_deeper.
113                                 if (!deeper_paragraph)
114                                         begin_deeper(os);
115                                 begin_layout(os, textclass.defaultLayout(),
116                                              font, normalfont);
117                                 need_layout=false;
118                                 need_end_layout = true;
119                                 deeper_paragraph = true;
120                         }
121                 } else {
122                         // No list-like environment
123                         begin_layout(os, layout, font, normalfont);
124                         need_layout=false;
125                         need_end_layout = true;
126                 }
127                 if (!extra_stuff.empty()) {
128                         os << extra_stuff;
129                         extra_stuff.erase();
130                 }
131                 os << "\n";
132         }
133 }
134
135
136 void Context::check_end_layout(ostream & os)
137 {
138         if (need_end_layout) {
139                 end_layout(os);
140                 need_end_layout = false;
141         }
142 }
143
144
145 void Context::check_deeper(ostream & os)
146 {
147         if (parent_layout->isEnvironment()) {
148                 // We start a nested environment.
149                 // We need to increase the depth.
150                 if (need_end_deeper) {
151                         // no need to have \end_deeper \begin_deeper
152                         need_end_deeper = false;
153                 } else {
154                         begin_deeper(os);
155                         need_end_deeper = true;
156                 }
157         } else
158                 check_end_deeper(os);
159 }
160
161
162 void Context::check_end_deeper(ostream & os)
163 {
164         if (need_end_deeper) {
165                 end_deeper(os);
166                 need_end_deeper = false;
167         }
168         if (deeper_paragraph) {
169                 end_deeper(os);
170                 deeper_paragraph = false;
171         }
172 }
173
174
175 void Context::set_item()
176 {
177         need_layout = true;
178         has_item = true;
179 }
180
181
182 void Context::new_paragraph(ostream & os)
183 {
184         check_end_layout(os);
185         need_layout = true;
186 }
187
188
189 void Context::add_extra_stuff(std::string const & stuff)
190 {
191         if (!lyx::support::contains(extra_stuff, stuff))
192                 extra_stuff += stuff;
193 }
194
195
196 void Context::dump(ostream & os, string const & desc) const
197 {
198         os << "\n" << desc <<" [";
199         if (need_layout)
200                 os << "need_layout ";
201         if (need_end_layout)
202                 os << "need_end_layout ";
203         if (need_end_deeper)
204                 os << "need_end_deeper ";
205         if (has_item)
206                 os << "has_item ";
207         if (deeper_paragraph)
208                 os << "deeper_paragraph ";
209         if (!extra_stuff.empty())
210                 os << "extrastuff=[" << extra_stuff << "] ";
211         os << "layout=" << layout->name();
212         os << " parent_layout=" << parent_layout->name() << "]" << endl;
213 }