]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
pimpl not needed here
[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 "Color.h"
15 #include "MetricsInfo.h"
16
17 #include "mathed/MathSupport.h"
18
19 #include "frontends/Painter.h"
20
21 #include <boost/assert.hpp>
22
23
24 namespace lyx {
25
26 using std::string;
27
28
29 MetricsBase::MetricsBase()
30         : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
31           textwidth(0)
32 {}
33
34
35 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
36         : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
37           textwidth(w)
38 {}
39
40
41 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth, 
42         MacroContext const & mc)
43         : base(bv, font, textwidth), macrocontext(mc)
44 {}
45
46
47 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
48         : pain(painter), ltr_pos(false), erased_(false), full_repaint(true),
49         background_color(Color_background)
50 {
51         base.bv = bv;
52 }
53
54
55 void PainterInfo::draw(int x, int y, char_type c)
56 {
57         pain.text(x, y, c, base.font);
58 }
59
60
61 void PainterInfo::draw(int x, int y, docstring const & str)
62 {
63         pain.text(x, y, str, base.font);
64 }
65
66
67 Styles smallerScriptStyle(Styles st)
68 {
69         switch (st) {
70                 case LM_ST_DISPLAY:
71                 case LM_ST_TEXT:
72                         return LM_ST_SCRIPT;
73                 case LM_ST_SCRIPT:
74                 case LM_ST_SCRIPTSCRIPT:
75                 default: // shut up compiler
76                         return LM_ST_SCRIPTSCRIPT;
77         }
78 }
79
80 ScriptChanger::ScriptChanger(MetricsBase & mb)
81         : StyleChanger(mb, smallerScriptStyle(mb.style))
82 {}
83
84
85
86 Styles smallerFracStyle(Styles st)
87 {
88         switch (st) {
89                 case LM_ST_DISPLAY:
90                         return LM_ST_TEXT;
91                 case LM_ST_TEXT:
92                         return LM_ST_SCRIPT;
93                 case LM_ST_SCRIPT:
94                 case LM_ST_SCRIPTSCRIPT:
95                 default: // shut up compiler
96                         return LM_ST_SCRIPTSCRIPT;
97         }
98 }
99
100
101 FracChanger::FracChanger(MetricsBase & mb)
102         : StyleChanger(mb, smallerFracStyle(mb.style))
103 {}
104
105
106
107 ArrayChanger::ArrayChanger(MetricsBase & mb)
108         : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
109 {}
110
111
112 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
113         : Changer<FontInfo, FontShape>(font)
114 {
115         save_ = orig_.shape();
116         orig_.setShape(shape);
117 }
118
119
120 ShapeChanger::~ShapeChanger()
121 {
122         orig_.setShape(save_);
123 }
124
125
126
127 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
128         :       Changer<MetricsBase>(mb)
129 {
130         static const int diff[4][4] =
131                 { { 0, 0, -3, -5 },
132                   { 0, 0, -3, -5 },
133                   { 3, 3,  0, -2 },
134                   { 5, 5,  2,  0 } };
135         save_ = mb;
136         int t = diff[mb.style][style];
137         if (t > 0)
138                 while (t--)
139                         mb.font.incSize();
140         else
141                 while (t++)
142                         mb.font.decSize();
143         mb.style = style;
144 }
145
146
147 StyleChanger::~StyleChanger()
148 {
149         orig_ = save_;
150 }
151
152
153
154 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
155         :       Changer<MetricsBase>(mb)
156 {
157         save_ = mb;
158         FontSize oldsize = save_.font.size();
159         mb.fontname = name;
160         mb.font = sane_font;
161         augmentFont(mb.font, from_ascii(name));
162         mb.font.setSize(oldsize);
163 }
164
165
166 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
167         :       Changer<MetricsBase>(mb)
168 {
169         save_ = mb;
170         FontSize oldsize = save_.font.size();
171         mb.fontname = to_utf8(name);
172         mb.font = sane_font;
173         augmentFont(mb.font, name);
174         mb.font.setSize(oldsize);
175 }
176
177
178 FontSetChanger::~FontSetChanger()
179 {
180         orig_ = save_;
181 }
182
183
184 WidthChanger::WidthChanger(MetricsBase & mb, int w)
185         :       Changer<MetricsBase>(mb)
186 {
187         save_ = mb;
188         mb.textwidth = w;
189 }
190
191
192 WidthChanger::~WidthChanger()
193 {
194         orig_ = save_;
195 }
196
197
198
199
200 ColorChanger::ColorChanger(FontInfo & font, string const & color)
201         : Changer<FontInfo, string>(font)
202 {
203         save_ = lcolor.getFromLyXName(color);
204         font.setColor(lcolor.getFromLyXName(color));
205 }
206
207
208 ColorChanger::~ColorChanger()
209 {
210         orig_.setColor(lcolor.getFromLyXName(save_));
211 }
212
213
214 } // namespace lyx