]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
One more nullptr
[lyx.git] / src / MetricsInfo.cpp
1 /**
2  * \file MetricsInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MetricsInfo.h"
14
15 #include "LyXRC.h"
16
17 #include "insets/Inset.h"
18
19 #include "mathed/MathSupport.h"
20
21 #include "frontends/FontMetrics.h"
22 #include "frontends/Painter.h"
23
24 using namespace std;
25
26
27 namespace lyx {
28
29 /////////////////////////////////////////////////////////////////////////
30 //
31 // MetricsBase
32 //
33 /////////////////////////////////////////////////////////////////////////
34
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)
39 {
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;
47         }
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;
53         }
54 }
55
56
57 Changer MetricsBase::changeFontSet(string const & name)
58 {
59         RefChanger<MetricsBase> rc = make_save(*this);
60         ColorCode oldcolor = font.color();
61         string const oldname = fontname;
62         fontname = name;
63         if (isMathFont(name) || isMathFont(oldname))
64                 font = sane_font;
65         augmentFont(font, name);
66         font.setSize(rc->old.font.size());
67         font.setStyle(rc->old.font.style());
68         if (name != "lyxtex"
69             && ((isTextFont(oldname) && oldcolor != Color_foreground)
70                 || (isMathFont(oldname) && oldcolor != Color_math)))
71                 font.setColor(oldcolor);
72 #if __cplusplus >= 201402L
73         return rc;
74 #else
75         return move(rc);
76 #endif
77 }
78
79
80 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
81 {
82         switch (mode) {
83         case Inset::UNDECIDED_MODE:
84                 return noChange();
85         case Inset::TEXT_MODE:
86                 return isMathFont(fontname) ? changeFontSet("textnormal") : noChange();
87         case Inset::MATH_MODE:
88                 // FIXME:
89                 //   \textit{\ensuremath{\text{a}}}
90                 // should appear in italics
91                 return isTextFont(fontname) ? changeFontSet("mathnormal"): noChange();
92         }
93         return noChange();
94 }
95
96
97 int MetricsBase::inPixels(Length const & len) const
98 {
99         FontInfo fi = font;
100         if (len.unit() == Length::MU)
101                 // mu is 1/18th of an em in the math symbol font
102                 fi.setFamily(SYMBOL_FAMILY);
103         else
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());
107 }
108
109
110 /////////////////////////////////////////////////////////////////////////
111 //
112 // MetricsInfo
113 //
114 /////////////////////////////////////////////////////////////////////////
115
116 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
117                          MacroContext const & mc, bool vm)
118         : base(bv, font, textwidth), macrocontext(mc), vmode(vm)
119 {}
120
121
122 /////////////////////////////////////////////////////////////////////////
123 //
124 // PainterInfo
125 //
126 /////////////////////////////////////////////////////////////////////////
127
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),
131           leftx(0), rightx(0)
132 {
133         base.bv = bv;
134 }
135
136
137 void PainterInfo::draw(int x, int y, char_type c)
138 {
139         pain.text(x, y, c, base.font);
140 }
141
142
143 void PainterInfo::draw(int x, int y, docstring const & str)
144 {
145         pain.text(x, y, str, base.font);
146 }
147
148
149 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
150 {
151         if (selected && sel)
152                 // This inset is in a selection
153                 return Color_selection;
154
155         // special handling for inset background
156         if (inset != nullptr) {
157                 if (pain.develMode() && !inset->isBufferValid())
158                         // This inset is in error
159                         return Color_error;
160
161                 ColorCode const color_bg = inset->backgroundColor(*this);
162                 if (color_bg != Color_none)
163                         // This inset has its own color
164                         return color_bg;
165         }
166
167         if (background_color == Color_none)
168                 // This inset has no own color and does not inherit a color
169                 return Color_background;
170
171         // This inset has no own color, but inherits a color
172         return background_color;
173 }
174
175
176 Color PainterInfo::textColor(Color const & color) const
177 {
178         if (change.changed())
179                 return change.color();
180         if (selected)
181                 return Color_selectiontext;
182         return color;
183 }
184
185
186 Changer MetricsBase::changeScript()
187 {
188         switch (font.style()) {
189         case DISPLAY_STYLE:
190         case TEXT_STYLE:
191                 return font.changeStyle(SCRIPT_STYLE);
192         case SCRIPT_STYLE:
193         case SCRIPTSCRIPT_STYLE:
194                 return font.changeStyle(SCRIPTSCRIPT_STYLE);
195         case INHERIT_STYLE:
196         case IGNORE_STYLE:
197                 return noChange();
198         }
199         //remove Warning
200         return noChange();
201 }
202
203
204 Changer MetricsBase::changeFrac()
205 {
206         switch (font.style()) {
207         case DISPLAY_STYLE:
208                 return font.changeStyle(TEXT_STYLE);
209         case TEXT_STYLE:
210                 return font.changeStyle(SCRIPT_STYLE);
211         case SCRIPT_STYLE:
212         case SCRIPTSCRIPT_STYLE:
213                 return font.changeStyle(SCRIPTSCRIPT_STYLE);
214         case INHERIT_STYLE:
215         case IGNORE_STYLE:
216                 return noChange();
217         }
218         //remove Warning
219         return noChange();
220 }
221
222
223 Changer MetricsBase::changeArray(bool small)
224 {
225         if (small)
226                 return font.changeStyle(SCRIPT_STYLE);
227         return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)
228                 : noChange();
229 }
230
231
232 } // namespace lyx