2 * \file MetricsInfo.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "BufferView.h"
15 #include "MetricsInfo.h"
17 #include "insets/Inset.h"
19 #include "mathed/MathSupport.h"
21 #include "frontends/Painter.h"
23 #include "support/docstring.h"
25 #include "support/lassert.h"
32 MetricsBase::MetricsBase()
33 : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
38 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
39 : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
44 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth,
45 MacroContext const & mc)
46 : base(bv, font, textwidth), macrocontext(mc)
50 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
51 : pain(painter), ltr_pos(false), erased_(false), selected(false),
52 full_repaint(true), background_color(Color_background)
58 void PainterInfo::draw(int x, int y, char_type c)
60 pain.text(x, y, c, base.font);
64 void PainterInfo::draw(int x, int y, docstring const & str)
66 pain.text(x, y, str, base.font);
70 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
72 ColorCode const color_bg = inset->backgroundColor();
75 // This inset is in a selection
76 return Color_selection;
78 if (color_bg != Color_none)
79 // This inset has its own color
82 if (background_color == Color_none)
83 // This inset has no own color and does not inherit a color
84 return Color_background;
86 // This inset has no own color, but inherits a color
87 return background_color;
93 Styles smallerScriptStyle(Styles st)
100 case LM_ST_SCRIPTSCRIPT:
101 default: // shut up compiler
102 return LM_ST_SCRIPTSCRIPT;
106 ScriptChanger::ScriptChanger(MetricsBase & mb)
107 : StyleChanger(mb, smallerScriptStyle(mb.style))
112 Styles smallerFracStyle(Styles st)
120 case LM_ST_SCRIPTSCRIPT:
121 default: // shut up compiler
122 return LM_ST_SCRIPTSCRIPT;
127 FracChanger::FracChanger(MetricsBase & mb)
128 : StyleChanger(mb, smallerFracStyle(mb.style))
133 ArrayChanger::ArrayChanger(MetricsBase & mb)
134 : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
138 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
139 : Changer<FontInfo, FontShape>(font)
141 save_ = orig_.shape();
142 orig_.setShape(shape);
146 ShapeChanger::~ShapeChanger()
148 orig_.setShape(save_);
153 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
154 : Changer<MetricsBase>(mb)
156 static const int diff[4][4] =
162 int t = diff[mb.style][style];
173 StyleChanger::~StyleChanger()
180 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
181 : Changer<MetricsBase>(mb)
184 FontSize oldsize = save_.font.size();
187 augmentFont(mb.font, from_ascii(name));
188 mb.font.setSize(oldsize);
192 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
193 : Changer<MetricsBase>(mb)
196 FontSize oldsize = save_.font.size();
197 mb.fontname = to_utf8(name);
199 augmentFont(mb.font, name);
200 mb.font.setSize(oldsize);
204 FontSetChanger::~FontSetChanger()
210 WidthChanger::WidthChanger(MetricsBase & mb, int w)
211 : Changer<MetricsBase>(mb)
218 WidthChanger::~WidthChanger()
226 ColorChanger::ColorChanger(FontInfo & font, string const & color)
227 : Changer<FontInfo, string>(font)
229 save_ = lcolor.getFromLyXName(color);
230 font.setColor(lcolor.getFromLyXName(color));
234 ColorChanger::~ColorChanger()
236 orig_.setColor(lcolor.getFromLyXName(save_));