]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
Partially revert r34995, which broke math output. Not sure why yet....
[lyx.git] / src / MetricsInfo.cpp
1 /**
2  * \file MetricsInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "ColorSet.h"
15 #include "MetricsInfo.h"
16
17 #include "insets/Inset.h"
18
19 #include "mathed/MathSupport.h"
20
21 #include "frontends/Painter.h"
22
23 #include "support/docstring.h"
24 #include "support/lassert.h"
25
26 using namespace std;
27
28
29 namespace lyx {
30
31 /////////////////////////////////////////////////////////////////////////
32 //
33 // MetricsBase
34 //
35 /////////////////////////////////////////////////////////////////////////
36
37 MetricsBase::MetricsBase()
38         : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
39           textwidth(0)
40 {}
41
42
43 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
44         : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
45           textwidth(w)
46 {}
47
48
49 /////////////////////////////////////////////////////////////////////////
50 //
51 // MetricsInfo
52 //
53 /////////////////////////////////////////////////////////////////////////
54
55 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth, 
56         MacroContext const & mc)
57         : base(bv, font, textwidth), macrocontext(mc)
58 {}
59
60
61 /////////////////////////////////////////////////////////////////////////
62 //
63 // PainterInfo
64 //
65 /////////////////////////////////////////////////////////////////////////
66
67 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
68         : pain(painter), ltr_pos(false), change_(), selected(false),
69         full_repaint(true), background_color(Color_background)
70 {
71         base.bv = bv;
72 }
73
74
75 void PainterInfo::draw(int x, int y, char_type c)
76 {
77         pain.text(x, y, c, base.font);
78 }
79
80
81 void PainterInfo::draw(int x, int y, docstring const & str)
82 {
83         pain.text(x, y, str, base.font);
84 }
85
86
87 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
88 {
89         ColorCode const color_bg = inset->backgroundColor(*this);
90
91         if (selected && sel)
92                 // This inset is in a selection
93                 return Color_selection;
94         else {
95                 if (color_bg != Color_none)
96                         // This inset has its own color
97                         return color_bg;
98                 else {
99                         if (background_color == Color_none)
100                                 // This inset has no own color and does not inherit a color
101                                 return Color_background;
102                         else
103                                 // This inset has no own color, but inherits a color
104                                 return background_color;
105                 }
106         }
107 }
108
109
110 /////////////////////////////////////////////////////////////////////////
111 //
112 // ScriptChanger
113 //
114 /////////////////////////////////////////////////////////////////////////
115
116 Styles smallerScriptStyle(Styles st)
117 {
118         switch (st) {
119                 case LM_ST_DISPLAY:
120                 case LM_ST_TEXT:
121                         return LM_ST_SCRIPT;
122                 case LM_ST_SCRIPT:
123                 case LM_ST_SCRIPTSCRIPT:
124                 default: // shut up compiler
125                         return LM_ST_SCRIPTSCRIPT;
126         }
127 }
128
129
130 ScriptChanger::ScriptChanger(MetricsBase & mb)
131         : StyleChanger(mb, smallerScriptStyle(mb.style))
132 {}
133
134
135 /////////////////////////////////////////////////////////////////////////
136 //
137 // FracChanger
138 //
139 /////////////////////////////////////////////////////////////////////////
140
141 Styles smallerFracStyle(Styles st)
142 {
143         switch (st) {
144                 case LM_ST_DISPLAY:
145                         return LM_ST_TEXT;
146                 case LM_ST_TEXT:
147                         return LM_ST_SCRIPT;
148                 case LM_ST_SCRIPT:
149                 case LM_ST_SCRIPTSCRIPT:
150                 default: // shut up compiler
151                         return LM_ST_SCRIPTSCRIPT;
152         }
153 }
154
155
156 FracChanger::FracChanger(MetricsBase & mb)
157         : StyleChanger(mb, smallerFracStyle(mb.style))
158 {}
159
160
161 /////////////////////////////////////////////////////////////////////////
162 //
163 // ArrayChanger
164 //
165 /////////////////////////////////////////////////////////////////////////
166
167 ArrayChanger::ArrayChanger(MetricsBase & mb)
168         : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
169 {}
170
171
172 /////////////////////////////////////////////////////////////////////////
173 //
174 // ShapeChanger
175 //
176 /////////////////////////////////////////////////////////////////////////
177
178 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
179         : Changer<FontInfo, FontShape>(font)
180 {
181         save_ = orig_.shape();
182         orig_.setShape(shape);
183 }
184
185
186 ShapeChanger::~ShapeChanger()
187 {
188         orig_.setShape(save_);
189 }
190
191
192 /////////////////////////////////////////////////////////////////////////
193 //
194 // StyleChanger
195 //
196 /////////////////////////////////////////////////////////////////////////
197
198 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
199         : Changer<MetricsBase>(mb)
200 {
201         static const int diff[4][4] =
202                 { { 0, 0, -3, -5 },
203                   { 0, 0, -3, -5 },
204                   { 3, 3,  0, -2 },
205                   { 5, 5,  2,  0 } };
206         save_ = mb;
207         int t = diff[mb.style][style];
208         if (t > 0)
209                 while (t--)
210                         mb.font.incSize();
211         else
212                 while (t++)
213                         mb.font.decSize();
214         mb.style = style;
215 }
216
217
218 StyleChanger::~StyleChanger()
219 {
220         orig_ = save_;
221 }
222
223
224 /////////////////////////////////////////////////////////////////////////
225 //
226 // FontSetChanger
227 //
228 /////////////////////////////////////////////////////////////////////////
229
230 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name,
231                                 bool really_change_font)
232         : Changer<MetricsBase>(mb), change_(really_change_font)
233 {
234         if (change_) {
235                 save_ = mb;
236                 FontSize oldsize = save_.font.size();
237                 ColorCode oldcolor = save_.font.color();
238                 docstring const oldname = from_ascii(save_.fontname);
239                 mb.fontname = name;
240                 mb.font = sane_font;
241                 augmentFont(mb.font, from_ascii(name));
242                 mb.font.setSize(oldsize);
243                 if (string(name) != "lyxtex"
244                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
245                         || (isMathFont(oldname) && oldcolor != Color_math)))
246                         mb.font.setColor(oldcolor);
247         }
248 }
249
250
251 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name,
252                                 bool really_change_font)
253         : Changer<MetricsBase>(mb), change_(really_change_font)
254 {
255         if (change_) {
256                 save_ = mb;
257                 FontSize oldsize = save_.font.size();
258                 ColorCode oldcolor = save_.font.color();
259                 docstring const oldname = from_ascii(save_.fontname);
260                 mb.fontname = to_utf8(name);
261                 mb.font = sane_font;
262                 augmentFont(mb.font, name);
263                 mb.font.setSize(oldsize);
264                 if (name != "lyxtex"
265                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
266                         || (isMathFont(oldname) && oldcolor != Color_math)))
267                         mb.font.setColor(oldcolor);
268         }
269 }
270
271
272 FontSetChanger::~FontSetChanger()
273 {
274         if (change_)
275                 orig_ = save_;
276 }
277
278
279 /////////////////////////////////////////////////////////////////////////
280 //
281 // WidthChanger
282 //
283 /////////////////////////////////////////////////////////////////////////
284
285 WidthChanger::WidthChanger(MetricsBase & mb, int w)
286         : Changer<MetricsBase>(mb)
287 {
288         save_ = mb;
289         mb.textwidth = w;
290 }
291
292
293 WidthChanger::~WidthChanger()
294 {
295         orig_ = save_;
296 }
297
298
299 /////////////////////////////////////////////////////////////////////////
300 //
301 // ColorChanger
302 //
303 /////////////////////////////////////////////////////////////////////////
304
305 ColorChanger::ColorChanger(FontInfo & font, docstring const & color,
306                            bool really_change_color)
307         : Changer<FontInfo, ColorCode>(font), change_(really_change_color)
308 {
309         if (change_) {
310                 save_ = font.color();
311                 font.setColor(lcolor.getFromLyXName(to_utf8(color)));
312         }
313 }
314
315
316 ColorChanger::~ColorChanger()
317 {
318         if (change_)
319                 orig_.setColor(save_);
320 }
321
322
323 } // namespace lyx