]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
Avoid full metrics computation with Update:FitCursor
[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 #include <map>
25
26 using namespace std;
27
28
29 namespace lyx {
30
31 /////////////////////////////////////////////////////////////////////////
32 //
33 // MetricsBase
34 //
35 /////////////////////////////////////////////////////////////////////////
36
37 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
38         : bv(b), font(std::move(f)), fontname("mathnormal"),
39           textwidth(w), macro_nesting(0),
40           solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
41 {
42         if (lyxrc.currentZoom >= 200) {
43                 // derive the line thickness from zoom factor
44                 // the zoom is given in percent
45                 // (increase thickness at 250%, 450% etc.)
46                 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
47                 // adjust line_offset_ too
48                 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
49         }
50         if (lyxrc.currentZoom >= 100) {
51                 // derive the line thickness from zoom factor
52                 // the zoom is given in percent
53                 // (increase thickness at 150%, 250% etc.)
54                 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
55         }
56 }
57
58
59 Changer MetricsBase::changeFontSet(string const & name)
60 {
61         RefChanger<MetricsBase> rc = make_save(*this);
62         ColorCode oldcolor = font.color();
63         string const oldname = fontname;
64         fontname = name;
65         if (isMathFont(name) || isMathFont(oldname))
66                 font = isTextFont(name) ? outer_font : sane_font;
67         augmentFont(font, name);
68         if (isTextFont(name) && isMathFont(oldname))
69                 font.setSize(rc->old.outer_font.size());
70         else
71                 font.setSize(rc->old.font.size());
72         font.setStyle(rc->old.font.style());
73         if (name == "emph") {
74                 font.setColor(oldcolor);
75                 if (rc->old.font.shape() != UP_SHAPE)
76                         font.setShape(UP_SHAPE);
77                 else
78                         font.setShape(ITALIC_SHAPE);
79         } else if (name != "lyxtex"
80             && ((isTextFont(oldname) && oldcolor != Color_foreground)
81                 || (isMathFont(oldname) && oldcolor != Color_math)))
82                 font.setColor(oldcolor);
83 #if __cplusplus >= 201402L
84         return rc;
85 #else
86         /** In theory, this is not needed with C++11, and modern compilers
87          * will complain in C++11 mode, but gcc 4.9 requires this. */
88         return std::move(rc);
89 #endif
90 }
91
92
93 Changer MetricsBase::changeFontSize(string const & size, bool mathmode)
94 {
95         map<string, FontSize> sizes = {
96                 {"tiny", TINY_SIZE},
97                 {"scriptsize", SCRIPT_SIZE},
98                 {"footnotesize", FOOTNOTE_SIZE},
99                 {"small", SMALL_SIZE},
100                 {"normalsize", NORMAL_SIZE},
101                 {"large", LARGE_SIZE},
102                 {"Large", LARGER_SIZE},
103                 {"LARGE", LARGEST_SIZE},
104                 {"huge", HUGE_SIZE},
105                 {"Huge", HUGER_SIZE}
106         };
107         RefChanger<MetricsBase> rc = make_save(*this);
108         // In math mode we only record the size in outer_font
109         if (mathmode)
110                 outer_font.setSize(sizes[size]);
111         else
112                 font.setSize(sizes[size]);
113         return rc;
114 }
115
116
117 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
118 {
119         switch (mode) {
120         case Inset::UNDECIDED_MODE:
121                 return noChange();
122         case Inset::TEXT_MODE:
123                 return isMathFont(fontname) ? changeFontSet("textnormal") : noChange();
124         case Inset::MATH_MODE:
125                 // FIXME:
126                 //   \textit{\ensuremath{\text{a}}}
127                 // should appear in italics
128                 return isTextFont(fontname) ? changeFontSet("mathnormal"): noChange();
129         }
130         return noChange();
131 }
132
133
134 int MetricsBase::inPixels(Length const & len) const
135 {
136         FontInfo fi = font;
137         if (len.unit() == Length::MU)
138                 // mu is 1/18th of an em in the math symbol font
139                 fi.setFamily(SYMBOL_FAMILY);
140         else
141                 // Math style is only taken into account in the case of mu
142                 fi.setStyle(TEXT_STYLE);
143         return len.inPixels(textwidth, theFontMetrics(fi).em());
144 }
145
146
147 /////////////////////////////////////////////////////////////////////////
148 //
149 // MetricsInfo
150 //
151 /////////////////////////////////////////////////////////////////////////
152
153 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
154                          MacroContext const & mc, bool vm, bool tight)
155         : base(bv, font, textwidth), macrocontext(mc), vmode(vm), tight_insets(tight),
156           extrawidth(0)
157 {}
158
159
160 /////////////////////////////////////////////////////////////////////////
161 //
162 // PainterInfo
163 //
164 /////////////////////////////////////////////////////////////////////////
165
166 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
167         : pain(painter), ltr_pos(false), change(),
168           selected(false), selected_left(false), selected_right(false),
169           do_spellcheck(true), full_repaint(true), background_color(Color_background),
170           leftx(0), rightx(0)
171 {
172         base.bv = bv;
173 }
174
175
176 void PainterInfo::draw(int x, int y, char_type c)
177 {
178         pain.text(x, y, c, base.font);
179 }
180
181
182 void PainterInfo::draw(int x, int y, docstring const & str)
183 {
184         pain.text(x, y, str, base.font);
185 }
186
187
188 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
189 {
190         if (selected && sel)
191                 // This inset is in a selection
192                 return Color_selection;
193
194         // special handling for inset background
195         if (inset != nullptr) {
196                 if (pain.develMode() && !inset->isBufferValid()) {
197                         LYXERR0("Invalid or no buffer set in " << insetName(inset->lyxCode()));
198                         // This inset is in error
199                         return Color_error;
200                 }
201
202                 ColorCode const color_bg = inset->backgroundColor(*this);
203                 if (color_bg != Color_none)
204                         // This inset has its own color
205                         return color_bg;
206         }
207
208         if (background_color == Color_none)
209                 // This inset has no own color and does not inherit a color
210                 return Color_background;
211
212         // This inset has no own color, but inherits a color
213         return background_color;
214 }
215
216
217 Color PainterInfo::textColor(Color const & color) const
218 {
219         if (change.changed())
220                 return change.color();
221         if (selected)
222                 return Color_selectiontext;
223         return color;
224 }
225
226
227 Changer MetricsBase::changeScript()
228 {
229         switch (font.style()) {
230         case DISPLAY_STYLE:
231         case TEXT_STYLE:
232                 return font.changeStyle(SCRIPT_STYLE);
233         case SCRIPT_STYLE:
234         case SCRIPTSCRIPT_STYLE:
235                 return font.changeStyle(SCRIPTSCRIPT_STYLE);
236         case INHERIT_STYLE:
237         case IGNORE_STYLE:
238                 return noChange();
239         }
240         //remove Warning
241         return noChange();
242 }
243
244
245 Changer MetricsBase::changeFrac()
246 {
247         switch (font.style()) {
248         case DISPLAY_STYLE:
249                 return font.changeStyle(TEXT_STYLE);
250         case TEXT_STYLE:
251                 return font.changeStyle(SCRIPT_STYLE);
252         case SCRIPT_STYLE:
253         case SCRIPTSCRIPT_STYLE:
254                 return font.changeStyle(SCRIPTSCRIPT_STYLE);
255         case INHERIT_STYLE:
256         case IGNORE_STYLE:
257                 return noChange();
258         }
259         //remove Warning
260         return noChange();
261 }
262
263
264 Changer MetricsBase::changeArray(bool small)
265 {
266         if (small)
267                 return font.changeStyle(SCRIPT_STYLE);
268         return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)
269                 : noChange();
270 }
271
272
273 } // namespace lyx