Update lyx examples to latest file format (for 1.5.0 release)
[lyx.git] / src / MetricsInfo.cpp
blob9ad46cd50e43f25234030beed43e5db63792ba11
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.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "BufferView.h"
14 #include "Color.h"
15 #include "MetricsInfo.h"
17 #include "mathed/MathSupport.h"
19 #include "frontends/Painter.h"
21 #include <boost/assert.hpp>
24 namespace lyx {
26 using std::string;
29 MetricsBase::MetricsBase()
30 : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
31 textwidth(0)
35 MetricsBase::MetricsBase(BufferView * b, Font const & f, int w)
36 : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
37 textwidth(w)
42 MetricsInfo::MetricsInfo()
46 MetricsInfo::MetricsInfo(BufferView * bv, Font const & font, int textwidth)
47 : base(bv, font, textwidth)
52 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
53 : pain(painter), ltr_pos(false), erased_(false)
55 base.bv = bv;
59 void PainterInfo::draw(int x, int y, char_type c)
61 pain.text(x, y, c, base.font);
65 void PainterInfo::draw(int x, int y, docstring const & str)
67 pain.text(x, y, str, base.font);
71 Styles smallerScriptStyle(Styles st)
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;
84 ScriptChanger::ScriptChanger(MetricsBase & mb)
85 : StyleChanger(mb, smallerScriptStyle(mb.style))
90 Styles smallerFracStyle(Styles st)
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;
105 FracChanger::FracChanger(MetricsBase & mb)
106 : StyleChanger(mb, smallerFracStyle(mb.style))
111 ArrayChanger::ArrayChanger(MetricsBase & mb)
112 : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
117 ShapeChanger::ShapeChanger(Font & font, Font::FONT_SHAPE shape)
118 : Changer<Font, Font::FONT_SHAPE>(font)
120 save_ = orig_.shape();
121 orig_.setShape(shape);
125 ShapeChanger::~ShapeChanger()
127 orig_.setShape(save_);
132 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
133 : Changer<MetricsBase>(mb)
135 static const int diff[4][4] =
136 { { 0, 0, -3, -5 },
137 { 0, 0, -3, -5 },
138 { 3, 3, 0, -2 },
139 { 5, 5, 2, 0 } };
140 save_ = mb;
141 int t = diff[mb.style][style];
142 if (t > 0)
143 while (t--)
144 mb.font.incSize();
145 else
146 while (t++)
147 mb.font.decSize();
148 mb.style = style;
152 StyleChanger::~StyleChanger()
154 orig_ = save_;
159 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
160 : Changer<MetricsBase>(mb)
162 save_ = mb;
163 Font::FONT_SIZE oldsize = save_.font.size();
164 mb.fontname = name;
165 mb.font = Font();
166 augmentFont(mb.font, from_ascii(name));
167 mb.font.setSize(oldsize);
171 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
172 : Changer<MetricsBase>(mb)
174 save_ = mb;
175 Font::FONT_SIZE oldsize = save_.font.size();
176 mb.fontname = to_utf8(name);
177 mb.font = Font();
178 augmentFont(mb.font, name);
179 mb.font.setSize(oldsize);
183 FontSetChanger::~FontSetChanger()
185 orig_ = save_;
189 WidthChanger::WidthChanger(MetricsBase & mb, int w)
190 : Changer<MetricsBase>(mb)
192 save_ = mb;
193 mb.textwidth = w;
197 WidthChanger::~WidthChanger()
199 orig_ = save_;
205 ColorChanger::ColorChanger(Font & font, string const & color)
206 : Changer<Font, string>(font)
208 save_ = lcolor.getFromLyXName(color);
209 font.setColor(lcolor.getFromLyXName(color));
213 ColorChanger::~ColorChanger()
215 orig_.setColor(lcolor.getFromLyXName(save_));
219 } // namespace lyx