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 "MetricsInfo.h"
17 #include "insets/Inset.h"
19 #include "mathed/MathSupport.h"
21 #include "frontends/FontMetrics.h"
22 #include "frontends/Painter.h"
24 #include "support/RefChanger.h"
31 /////////////////////////////////////////////////////////////////////////
35 /////////////////////////////////////////////////////////////////////////
37 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
38 : bv(b), font(move(f)), fontname("mathnormal"),
39 textwidth(w), macro_nesting(0),
40 solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
42 if (lyxrc.currentZoom >= 200) {
43 // derive the line thickness from zoom factor
44 // the zoom is given in percent
45 // (increase thickness at 250%, 450% etc.)
46 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
47 // adjust line_offset_ too
48 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
50 if (lyxrc.currentZoom >= 100) {
51 // derive the line thickness from zoom factor
52 // the zoom is given in percent
53 // (increase thickness at 150%, 250% etc.)
54 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
59 Changer MetricsBase::changeFontSet(string const & name)
61 RefChanger<MetricsBase> rc = make_save(*this);
62 ColorCode oldcolor = font.color();
63 string const oldname = fontname;
65 if (isMathFont(name) || isMathFont(oldname))
67 augmentFont(font, name);
68 font.setSize(rc->old.font.size());
69 font.setStyle(rc->old.font.style());
71 && ((isTextFont(oldname) && oldcolor != Color_foreground)
72 || (isMathFont(oldname) && oldcolor != Color_math)))
73 font.setColor(oldcolor);
74 #if __cplusplus >= 201402L
82 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
85 case Inset::UNDECIDED_MODE:
87 case Inset::TEXT_MODE:
88 return isMathFont(fontname) ? changeFontSet("textnormal") : Changer();
89 case Inset::MATH_MODE:
91 // \textit{\ensuremath{\text{a}}}
92 // should appear in italics
93 return isTextFont(fontname) ? changeFontSet("mathnormal"): Changer();
99 int MetricsBase::inPixels(Length const & len) const
102 if (len.unit() == Length::MU)
103 // mu is 1/18th of an em in the math symbol font
104 fi.setFamily(SYMBOL_FAMILY);
106 // Math style is only taken into account in the case of mu
107 fi.setStyle(TEXT_STYLE);
108 return len.inPixels(textwidth, theFontMetrics(fi).em());
112 /////////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////////
118 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
119 MacroContext const & mc, bool vm)
120 : base(bv, font, textwidth), macrocontext(mc), vmode(vm)
124 /////////////////////////////////////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////////
130 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
131 : pain(painter), ltr_pos(false), change(), selected(false),
132 do_spellcheck(true), full_repaint(true), background_color(Color_background),
139 void PainterInfo::draw(int x, int y, char_type c)
141 pain.text(x, y, c, base.font);
145 void PainterInfo::draw(int x, int y, docstring const & str)
147 pain.text(x, y, str, base.font);
151 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
154 // This inset is in a selection
155 return Color_selection;
157 // special handling for inset background
158 if (inset != nullptr) {
159 if (pain.develMode() && !inset->isBufferValid())
160 // This inset is in error
163 ColorCode const color_bg = inset->backgroundColor(*this);
164 if (color_bg != Color_none)
165 // This inset has its own color
169 if (background_color == Color_none)
170 // This inset has no own color and does not inherit a color
171 return Color_background;
173 // This inset has no own color, but inherits a color
174 return background_color;
178 Color PainterInfo::textColor(Color const & color) const
180 if (change.changed())
181 return change.color();
183 return Color_selectiontext;
188 Changer MetricsBase::changeScript()
190 switch (font.style()) {
193 return font.changeStyle(SCRIPT_STYLE);
195 case SCRIPTSCRIPT_STYLE:
196 return font.changeStyle(SCRIPTSCRIPT_STYLE);
206 Changer MetricsBase::changeFrac()
208 switch (font.style()) {
210 return font.changeStyle(TEXT_STYLE);
212 return font.changeStyle(SCRIPT_STYLE);
214 case SCRIPTSCRIPT_STYLE:
215 return font.changeStyle(SCRIPTSCRIPT_STYLE);
225 Changer MetricsBase::changeArray(bool small)
228 return font.changeStyle(SCRIPT_STYLE);
229 return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)