]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
Fix text direction issue for InsetInfo in RTL context
[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         return move(rc);
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(LM_ST_TEXT);
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)
119         : base(bv, font, textwidth), macrocontext(mc)
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 {
133         base.bv = bv;
134 }
135
136
137 void PainterInfo::draw(int x, int y, char_type c)
138 {
139         pain.text(x, y, c, base.font);
140 }
141
142
143 void PainterInfo::draw(int x, int y, docstring const & str)
144 {
145         pain.text(x, y, str, base.font);
146 }
147
148
149 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
150 {
151         ColorCode const color_bg = inset->backgroundColor(*this);
152
153         if (selected && sel)
154                 // This inset is in a selection
155                 return Color_selection;
156         else {
157                 if (color_bg != Color_none)
158                         // This inset has its own color
159                         return color_bg;
160                 else {
161                         if (background_color == Color_none)
162                                 // This inset has no own color and does not inherit a color
163                                 return Color_background;
164                         else
165                                 // This inset has no own color, but inherits a color
166                                 return background_color;
167                 }
168         }
169 }
170
171
172 Color PainterInfo::textColor(Color const & color) const
173 {
174         if (change_.changed())
175                 return change_.color();
176         if (selected)
177                 return Color_selectiontext;
178         return color;
179 }
180
181
182 Changer MetricsBase::changeScript()
183 {
184         switch (font.style()) {
185         case LM_ST_DISPLAY:
186         case LM_ST_TEXT:
187                 return font.changeStyle(LM_ST_SCRIPT);
188         case LM_ST_SCRIPT:
189         case LM_ST_SCRIPTSCRIPT:
190                 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
191         }
192         //remove Warning
193         return Changer();
194 }
195
196
197 Changer MetricsBase::changeFrac()
198 {
199         switch (font.style()) {
200         case LM_ST_DISPLAY:
201                 return font.changeStyle(LM_ST_TEXT);
202         case LM_ST_TEXT:
203                 return font.changeStyle(LM_ST_SCRIPT);
204         case LM_ST_SCRIPT:
205         case LM_ST_SCRIPTSCRIPT:
206                 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
207         }
208         //remove Warning
209         return Changer();
210 }
211
212
213 Changer MetricsBase::changeArray()
214 {
215         return (font.style() == LM_ST_DISPLAY) ? font.changeStyle(LM_ST_TEXT)
216                 : Changer();
217 }
218
219
220 } // namespace lyx