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"
16 #include "MetricsInfo.h"
18 #include "insets/Inset.h"
20 #include "mathed/MathSupport.h"
22 #include "frontends/FontMetrics.h"
23 #include "frontends/Painter.h"
25 #include "support/docstring.h"
26 #include "support/lassert.h"
27 #include "support/RefChanger.h"
34 /////////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////////
40 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
41 : bv(b), font(move(f)), fontname("mathnormal"),
42 textwidth(w), macro_nesting(0),
43 solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
45 if (lyxrc.currentZoom >= 200) {
46 // derive the line thickness from zoom factor
47 // the zoom is given in percent
48 // (increase thickness at 250%, 450% etc.)
49 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
50 // adjust line_offset_ too
51 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
53 if (lyxrc.currentZoom >= 100) {
54 // derive the line thickness from zoom factor
55 // the zoom is given in percent
56 // (increase thickness at 150%, 250% etc.)
57 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
62 Changer MetricsBase::changeFontSet(string const & name)
64 RefChanger<MetricsBase> rc = make_save(*this);
65 ColorCode oldcolor = font.color();
66 string const oldname = fontname;
68 if (isMathFont(name) || isMathFont(oldname))
70 augmentFont(font, name);
71 font.setSize(rc->old.font.size());
72 font.setStyle(rc->old.font.style());
74 && ((isTextFont(oldname) && oldcolor != Color_foreground)
75 || (isMathFont(oldname) && oldcolor != Color_math)))
76 font.setColor(oldcolor);
81 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
84 case Inset::UNDECIDED_MODE:
86 case Inset::TEXT_MODE:
87 return isMathFont(fontname) ? changeFontSet("textnormal") : Changer();
88 case Inset::MATH_MODE:
90 // \textit{\ensuremath{\text{a}}}
91 // should appear in italics
92 return isTextFont(fontname) ? changeFontSet("mathnormal"): Changer();
98 int MetricsBase::inPixels(Length const & len) const
101 if (len.unit() == Length::MU)
102 // mu is 1/18th of an em in the math symbol font
103 fi.setFamily(SYMBOL_FAMILY);
105 // Math style is only taken into account in the case of mu
106 fi.setStyle(LM_ST_TEXT);
107 return len.inPixels(textwidth, theFontMetrics(fi).em());
111 /////////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////////
117 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
118 MacroContext const & mc)
119 : base(bv, font, textwidth), macrocontext(mc)
123 /////////////////////////////////////////////////////////////////////////
127 /////////////////////////////////////////////////////////////////////////
129 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
130 : pain(painter), ltr_pos(false), change_(), selected(false),
131 do_spellcheck(true), full_repaint(true), background_color(Color_background)
137 void PainterInfo::draw(int x, int y, char_type c)
139 pain.text(x, y, c, base.font);
143 void PainterInfo::draw(int x, int y, docstring const & str)
145 pain.text(x, y, str, base.font);
149 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
151 ColorCode const color_bg = inset->backgroundColor(*this);
154 // This inset is in a selection
155 return Color_selection;
157 if (color_bg != Color_none)
158 // This inset has its own color
161 if (background_color == Color_none)
162 // This inset has no own color and does not inherit a color
163 return Color_background;
165 // This inset has no own color, but inherits a color
166 return background_color;
172 Color PainterInfo::textColor(Color const & color) const
174 if (change_.changed())
175 return change_.color();
177 return Color_selectiontext;
182 Changer MetricsBase::changeScript()
184 switch (font.style()) {
187 return font.changeStyle(LM_ST_SCRIPT);
189 case LM_ST_SCRIPTSCRIPT:
190 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
197 Changer MetricsBase::changeFrac()
199 switch (font.style()) {
201 return font.changeStyle(LM_ST_TEXT);
203 return font.changeStyle(LM_ST_SCRIPT);
205 case LM_ST_SCRIPTSCRIPT:
206 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
213 Changer MetricsBase::changeArray()
215 return (font.style() == LM_ST_DISPLAY) ? font.changeStyle(LM_ST_TEXT)