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