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