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