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