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.
14 #include "MetricsInfo.h"
16 #include "insets/Inset.h"
18 #include "mathed/MathSupport.h"
20 #include "frontends/FontMetrics.h"
21 #include "frontends/Painter.h"
23 #include "support/RefChanger.h"
30 /////////////////////////////////////////////////////////////////////////
34 /////////////////////////////////////////////////////////////////////////
36 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
37 : bv(b), font(move(f)), fontname("mathnormal"),
38 textwidth(w), macro_nesting(0),
39 solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
41 if (lyxrc.currentZoom >= 200) {
42 // derive the line thickness from zoom factor
43 // the zoom is given in percent
44 // (increase thickness at 250%, 450% etc.)
45 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
46 // adjust line_offset_ too
47 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
49 if (lyxrc.currentZoom >= 100) {
50 // derive the line thickness from zoom factor
51 // the zoom is given in percent
52 // (increase thickness at 150%, 250% etc.)
53 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
58 Changer MetricsBase::changeFontSet(string const & name)
60 RefChanger<MetricsBase> rc = make_save(*this);
61 ColorCode oldcolor = font.color();
62 string const oldname = fontname;
64 if (isMathFont(name) || isMathFont(oldname))
66 augmentFont(font, name);
67 font.setSize(rc->old.font.size());
68 font.setStyle(rc->old.font.style());
70 && ((isTextFont(oldname) && oldcolor != Color_foreground)
71 || (isMathFont(oldname) && oldcolor != Color_math)))
72 font.setColor(oldcolor);
73 #if __cplusplus >= 201402L
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(TEXT_STYLE);
107 return len.inPixels(textwidth, theFontMetrics(fi).em());
111 /////////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////////
117 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
118 MacroContext const & mc, bool vm)
119 : base(bv, font, textwidth), macrocontext(mc), vmode(vm)
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),
138 void PainterInfo::draw(int x, int y, char_type c)
140 pain.text(x, y, c, base.font);
144 void PainterInfo::draw(int x, int y, docstring const & str)
146 pain.text(x, y, str, base.font);
150 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
153 // This inset is in a selection
154 return Color_selection;
156 // special handling for inset background
157 if (inset != nullptr) {
158 if (pain.develMode() && !inset->isBufferValid())
159 // This inset is in error
162 ColorCode const color_bg = inset->backgroundColor(*this);
163 if (color_bg != Color_none)
164 // This inset has its own color
168 if (background_color == Color_none)
169 // This inset has no own color and does not inherit a color
170 return Color_background;
172 // This inset has no own color, but inherits a color
173 return background_color;
177 Color PainterInfo::textColor(Color const & color) const
179 if (change.changed())
180 return change.color();
182 return Color_selectiontext;
187 Changer MetricsBase::changeScript()
189 switch (font.style()) {
192 return font.changeStyle(SCRIPT_STYLE);
194 case SCRIPTSCRIPT_STYLE:
195 return font.changeStyle(SCRIPTSCRIPT_STYLE);
205 Changer MetricsBase::changeFrac()
207 switch (font.style()) {
209 return font.changeStyle(TEXT_STYLE);
211 return font.changeStyle(SCRIPT_STYLE);
213 case SCRIPTSCRIPT_STYLE:
214 return font.changeStyle(SCRIPTSCRIPT_STYLE);
224 Changer MetricsBase::changeArray(bool small)
227 return font.changeStyle(SCRIPT_STYLE);
228 return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)