LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / MetricsInfo.cpp
bloba90a62bc176d7fa3c5662f8412bb646ed35286b9
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 "support/docstring.h"
23 #include "support/lassert.h"
25 using namespace std;
27 namespace lyx {
30 MetricsBase::MetricsBase()
31 : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
32 textwidth(0)
36 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
37 : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
38 textwidth(w)
42 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth,
43 MacroContext const & mc)
44 : base(bv, font, textwidth), macrocontext(mc)
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)
52 base.bv = bv;
56 void PainterInfo::draw(int x, int y, char_type c)
58 pain.text(x, y, c, base.font);
62 void PainterInfo::draw(int x, int y, docstring const & str)
64 pain.text(x, y, str, base.font);
68 Styles smallerScriptStyle(Styles st)
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;
81 ScriptChanger::ScriptChanger(MetricsBase & mb)
82 : StyleChanger(mb, smallerScriptStyle(mb.style))
87 Styles smallerFracStyle(Styles st)
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;
102 FracChanger::FracChanger(MetricsBase & mb)
103 : StyleChanger(mb, smallerFracStyle(mb.style))
108 ArrayChanger::ArrayChanger(MetricsBase & mb)
109 : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
113 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
114 : Changer<FontInfo, FontShape>(font)
116 save_ = orig_.shape();
117 orig_.setShape(shape);
121 ShapeChanger::~ShapeChanger()
123 orig_.setShape(save_);
128 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
129 : Changer<MetricsBase>(mb)
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;
148 StyleChanger::~StyleChanger()
150 orig_ = save_;
155 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
156 : Changer<MetricsBase>(mb)
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);
167 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
168 : Changer<MetricsBase>(mb)
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);
179 FontSetChanger::~FontSetChanger()
181 orig_ = save_;
185 WidthChanger::WidthChanger(MetricsBase & mb, int w)
186 : Changer<MetricsBase>(mb)
188 save_ = mb;
189 mb.textwidth = w;
193 WidthChanger::~WidthChanger()
195 orig_ = save_;
201 ColorChanger::ColorChanger(FontInfo & font, string const & color)
202 : Changer<FontInfo, string>(font)
204 save_ = lcolor.getFromLyXName(color);
205 font.setColor(lcolor.getFromLyXName(color));
209 ColorChanger::~ColorChanger()
211 orig_.setColor(lcolor.getFromLyXName(save_));
215 } // namespace lyx