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