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"
29 /////////////////////////////////////////////////////////////////////////
33 /////////////////////////////////////////////////////////////////////////
35 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
36 : bv(b), font(move(f)), fontname("mathnormal"),
37 textwidth(w), macro_nesting(0),
38 solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
40 if (lyxrc.currentZoom >= 200) {
41 // derive the line thickness from zoom factor
42 // the zoom is given in percent
43 // (increase thickness at 250%, 450% etc.)
44 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
45 // adjust line_offset_ too
46 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
48 if (lyxrc.currentZoom >= 100) {
49 // derive the line thickness from zoom factor
50 // the zoom is given in percent
51 // (increase thickness at 150%, 250% etc.)
52 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
57 Changer MetricsBase::changeFontSet(string const & name)
59 RefChanger<MetricsBase> rc = make_save(*this);
60 ColorCode oldcolor = font.color();
61 string const oldname = fontname;
63 if (isMathFont(name) || isMathFont(oldname))
65 augmentFont(font, name);
66 font.setSize(rc->old.font.size());
67 font.setStyle(rc->old.font.style());
69 && ((isTextFont(oldname) && oldcolor != Color_foreground)
70 || (isMathFont(oldname) && oldcolor != Color_math)))
71 font.setColor(oldcolor);
72 #if __cplusplus >= 201402L
80 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
83 case Inset::UNDECIDED_MODE:
85 case Inset::TEXT_MODE:
86 return isMathFont(fontname) ? changeFontSet("textnormal") : noChange();
87 case Inset::MATH_MODE:
89 // \textit{\ensuremath{\text{a}}}
90 // should appear in italics
91 return isTextFont(fontname) ? changeFontSet("mathnormal"): noChange();
97 int MetricsBase::inPixels(Length const & len) const
100 if (len.unit() == Length::MU)
101 // mu is 1/18th of an em in the math symbol font
102 fi.setFamily(SYMBOL_FAMILY);
104 // Math style is only taken into account in the case of mu
105 fi.setStyle(TEXT_STYLE);
106 return len.inPixels(textwidth, theFontMetrics(fi).em());
110 /////////////////////////////////////////////////////////////////////////
114 /////////////////////////////////////////////////////////////////////////
116 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
117 MacroContext const & mc, bool vm)
118 : base(bv, font, textwidth), macrocontext(mc), vmode(vm)
122 /////////////////////////////////////////////////////////////////////////
126 /////////////////////////////////////////////////////////////////////////
128 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
129 : pain(painter), ltr_pos(false), change(), selected(false),
130 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
152 // This inset is in a selection
153 return Color_selection;
155 // special handling for inset background
156 if (inset != nullptr) {
157 if (pain.develMode() && !inset->isBufferValid())
158 // This inset is in error
161 ColorCode const color_bg = inset->backgroundColor(*this);
162 if (color_bg != Color_none)
163 // This inset has its own color
167 if (background_color == Color_none)
168 // This inset has no own color and does not inherit a color
169 return Color_background;
171 // This inset has no own color, but inherits a color
172 return background_color;
176 Color PainterInfo::textColor(Color const & color) const
178 if (change.changed())
179 return change.color();
181 return Color_selectiontext;
186 Changer MetricsBase::changeScript()
188 switch (font.style()) {
191 return font.changeStyle(SCRIPT_STYLE);
193 case SCRIPTSCRIPT_STYLE:
194 return font.changeStyle(SCRIPTSCRIPT_STYLE);
204 Changer MetricsBase::changeFrac()
206 switch (font.style()) {
208 return font.changeStyle(TEXT_STYLE);
210 return font.changeStyle(SCRIPT_STYLE);
212 case SCRIPTSCRIPT_STYLE:
213 return font.changeStyle(SCRIPTSCRIPT_STYLE);
223 Changer MetricsBase::changeArray(bool small)
226 return font.changeStyle(SCRIPT_STYLE);
227 return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)