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/Painter.h"
24 #include "support/docstring.h"
25 #include "support/lassert.h"
26 #include "support/RefChanger.h"
33 /////////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////////
39 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
40 : bv(b), font(move(f)), fontname("mathnormal"),
41 textwidth(w), macro_nesting(0),
42 solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
44 if (lyxrc.currentZoom >= 200) {
45 // derive the line thickness from zoom factor
46 // the zoom is given in percent
47 // (increase thickness at 250%, 450% etc.)
48 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
49 // adjust line_offset_ too
50 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
52 if (lyxrc.currentZoom >= 100) {
53 // derive the line thickness from zoom factor
54 // the zoom is given in percent
55 // (increase thickness at 150%, 250% etc.)
56 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
61 Changer MetricsBase::changeFontSet(string const & name)
63 RefChanger<MetricsBase> rc = make_save(*this);
64 ColorCode oldcolor = font.color();
65 string const oldname = fontname;
67 if (isMathFont(name) || isMathFont(oldname))
69 augmentFont(font, name);
70 font.setSize(rc->old.font.size());
71 font.setStyle(rc->old.font.style());
73 && ((isTextFont(oldname) && oldcolor != Color_foreground)
74 || (isMathFont(oldname) && oldcolor != Color_math)))
75 font.setColor(oldcolor);
76 #if __cplusplus >= 201402L
84 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
87 case Inset::UNDECIDED_MODE:
89 case Inset::TEXT_MODE:
90 return isMathFont(fontname) ? changeFontSet("textnormal") : Changer();
91 case Inset::MATH_MODE:
93 // \textit{\ensuremath{\text{a}}}
94 // should appear in italics
95 return isTextFont(fontname) ? changeFontSet("mathnormal"): Changer();
101 /////////////////////////////////////////////////////////////////////////
105 /////////////////////////////////////////////////////////////////////////
107 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
108 MacroContext const & mc)
109 : base(bv, font, textwidth), macrocontext(mc)
113 /////////////////////////////////////////////////////////////////////////
117 /////////////////////////////////////////////////////////////////////////
119 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
120 : pain(painter), ltr_pos(false), change_(), selected(false),
121 do_spellcheck(true), full_repaint(true), background_color(Color_background)
127 void PainterInfo::draw(int x, int y, char_type c)
129 pain.text(x, y, c, base.font);
133 void PainterInfo::draw(int x, int y, docstring const & str)
135 pain.text(x, y, str, base.font);
139 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
142 // This inset is in a selection
143 return Color_selection;
145 // special handling for inset background
146 if (inset != nullptr) {
147 ColorCode const color_bg = inset->backgroundColor(*this);
148 if (color_bg != Color_none)
149 // This inset has its own color
153 if (background_color == Color_none)
154 // This inset has no own color and does not inherit a color
155 return Color_background;
157 // This inset has no own color, but inherits a color
158 return background_color;
162 Color PainterInfo::textColor(Color const & color) const
164 if (change_.changed())
165 return change_.color();
167 return Color_selectiontext;
172 Changer MetricsBase::changeScript()
174 switch (font.style()) {
177 return font.changeStyle(LM_ST_SCRIPT);
179 case LM_ST_SCRIPTSCRIPT:
180 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
187 Changer MetricsBase::changeFrac()
189 switch (font.style()) {
191 return font.changeStyle(LM_ST_TEXT);
193 return font.changeStyle(LM_ST_SCRIPT);
195 case LM_ST_SCRIPTSCRIPT:
196 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
203 Changer MetricsBase::changeArray()
205 return (font.style() == LM_ST_DISPLAY) ? font.changeStyle(LM_ST_TEXT)