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 font.setColor(oldcolor);
70 if (rc->old.font.shape() != UP_SHAPE)
71 font.setShape(UP_SHAPE);
73 font.setShape(ITALIC_SHAPE);
74 } else if (name != "lyxtex"
75 && ((isTextFont(oldname) && oldcolor != Color_foreground)
76 || (isMathFont(oldname) && oldcolor != Color_math)))
77 font.setColor(oldcolor);
78 #if __cplusplus >= 201402L
86 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
89 case Inset::UNDECIDED_MODE:
91 case Inset::TEXT_MODE:
92 return isMathFont(fontname) ? changeFontSet("textnormal") : noChange();
93 case Inset::MATH_MODE:
95 // \textit{\ensuremath{\text{a}}}
96 // should appear in italics
97 return isTextFont(fontname) ? changeFontSet("mathnormal"): noChange();
103 int MetricsBase::inPixels(Length const & len) const
106 if (len.unit() == Length::MU)
107 // mu is 1/18th of an em in the math symbol font
108 fi.setFamily(SYMBOL_FAMILY);
110 // Math style is only taken into account in the case of mu
111 fi.setStyle(TEXT_STYLE);
112 return len.inPixels(textwidth, theFontMetrics(fi).em());
116 /////////////////////////////////////////////////////////////////////////
120 /////////////////////////////////////////////////////////////////////////
122 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
123 MacroContext const & mc, bool vm, bool tight)
124 : base(bv, font, textwidth), macrocontext(mc), vmode(vm), tight_insets(tight)
128 /////////////////////////////////////////////////////////////////////////
132 /////////////////////////////////////////////////////////////////////////
134 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
135 : pain(painter), ltr_pos(false), change(),
136 selected(false), selected_left(false), selected_right(false),
137 do_spellcheck(true), full_repaint(true), background_color(Color_background),
144 void PainterInfo::draw(int x, int y, char_type c)
146 pain.text(x, y, c, base.font);
150 void PainterInfo::draw(int x, int y, docstring const & str)
152 pain.text(x, y, str, base.font);
156 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
159 // This inset is in a selection
160 return Color_selection;
162 // special handling for inset background
163 if (inset != nullptr) {
164 if (pain.develMode() && !inset->isBufferValid())
165 // This inset is in error
168 ColorCode const color_bg = inset->backgroundColor(*this);
169 if (color_bg != Color_none)
170 // This inset has its own color
174 if (background_color == Color_none)
175 // This inset has no own color and does not inherit a color
176 return Color_background;
178 // This inset has no own color, but inherits a color
179 return background_color;
183 Color PainterInfo::textColor(Color const & color) const
185 if (change.changed())
186 return change.color();
188 return Color_selectiontext;
193 Changer MetricsBase::changeScript()
195 switch (font.style()) {
198 return font.changeStyle(SCRIPT_STYLE);
200 case SCRIPTSCRIPT_STYLE:
201 return font.changeStyle(SCRIPTSCRIPT_STYLE);
211 Changer MetricsBase::changeFrac()
213 switch (font.style()) {
215 return font.changeStyle(TEXT_STYLE);
217 return font.changeStyle(SCRIPT_STYLE);
219 case SCRIPTSCRIPT_STYLE:
220 return font.changeStyle(SCRIPTSCRIPT_STYLE);
230 Changer MetricsBase::changeArray(bool small)
233 return font.changeStyle(SCRIPT_STYLE);
234 return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)