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