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