]> git.lyx.org Git - lyx.git/blob - src/metricsinfo.C
bb14576342a68163af4a899f3c9e440fb3994421
[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 lyx::char_type;
24 using lyx::docstring;
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, LyXFont 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, LyXFont 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)
54 {
55         base.bv = bv;
56 }
57
58
59 void PainterInfo::draw(int x, int y, char_type c)
60 {
61         pain.text(x, y, c, base.font);
62 }
63
64
65 void PainterInfo::draw(int x, int y, docstring const & str)
66 {
67         pain.text(x, y, str, base.font);
68 }
69
70
71 Styles smallerScriptStyle(Styles st)
72 {
73         switch (st) {
74                 case LM_ST_DISPLAY:
75                 case LM_ST_TEXT:
76                         return LM_ST_SCRIPT;
77                 case LM_ST_SCRIPT:
78                 case LM_ST_SCRIPTSCRIPT:
79                 default: // shut up compiler
80                         return LM_ST_SCRIPTSCRIPT;
81         }
82 }
83
84 ScriptChanger::ScriptChanger(MetricsBase & mb)
85         : StyleChanger(mb, smallerScriptStyle(mb.style))
86 {}
87
88
89
90 Styles smallerFracStyle(Styles st)
91 {
92         switch (st) {
93                 case LM_ST_DISPLAY:
94                         return LM_ST_TEXT;
95                 case LM_ST_TEXT:
96                         return LM_ST_SCRIPT;
97                 case LM_ST_SCRIPT:
98                 case LM_ST_SCRIPTSCRIPT:
99                 default: // shut up compiler
100                         return LM_ST_SCRIPTSCRIPT;
101         }
102 }
103
104
105 FracChanger::FracChanger(MetricsBase & mb)
106         : StyleChanger(mb, smallerFracStyle(mb.style))
107 {}
108
109
110
111 ArrayChanger::ArrayChanger(MetricsBase & mb)
112         : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
113 {}
114
115
116 ShapeChanger::ShapeChanger(LyXFont & font, LyXFont::FONT_SHAPE shape)
117         : Changer<LyXFont, LyXFont::FONT_SHAPE>(font)
118 {
119         save_ = orig_.shape();
120         orig_.setShape(shape);
121 }
122
123 ShapeChanger::~ShapeChanger()
124 {
125         orig_.setShape(save_);
126 }
127
128
129
130 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
131         :       Changer<MetricsBase>(mb)
132 {
133         static const int diff[4][4] =
134                 { { 0, 0, -3, -5 },
135                   { 0, 0, -3, -5 },
136                   { 3, 3,  0, -2 },
137                   { 5, 5,  2,  0 } };
138         save_ = mb;
139         int t = diff[mb.style][style];
140         if (t > 0)
141                 while (t--)
142                         mb.font.incSize();
143         else
144                 while (t++)
145                         mb.font.decSize();
146         mb.style = style;
147 }
148
149
150 StyleChanger::~StyleChanger()
151 {
152         orig_ = save_;
153 }
154
155
156
157 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
158         :       Changer<MetricsBase>(mb)
159 {
160         save_ = mb;
161         LyXFont::FONT_SIZE oldsize = save_.font.size();
162         mb.fontname = name;
163         mb.font = LyXFont();
164         augmentFont(mb.font, name);
165         mb.font.setSize(oldsize);
166 }
167
168
169 FontSetChanger::~FontSetChanger()
170 {
171         orig_ = save_;
172 }
173
174
175 WidthChanger::WidthChanger(MetricsBase & mb, int w)
176         :       Changer<MetricsBase>(mb)
177 {
178         save_ = mb;
179         mb.textwidth = w;
180 }
181
182
183 WidthChanger::~WidthChanger()
184 {
185         orig_ = save_;
186 }
187
188
189
190
191 ColorChanger::ColorChanger(LyXFont & font, string const & color)
192         : Changer<LyXFont, string>(font)
193 {
194         save_ = lcolor.getFromLyXName(color);
195         font.setColor(lcolor.getFromLyXName(color));
196 }
197
198
199 ColorChanger::~ColorChanger()
200 {
201         orig_.setColor(lcolor.getFromLyXName(save_));
202 }