]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
60a004573e2d3d61c1c4bc3d4fe71d077f32a8b0
[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/Painter.h"
23
24 #include "support/docstring.h"
25 #include "support/lassert.h"
26 #include "support/RefChanger.h"
27
28 using namespace std;
29
30
31 namespace lyx {
32
33 /////////////////////////////////////////////////////////////////////////
34 //
35 // MetricsBase
36 //
37 /////////////////////////////////////////////////////////////////////////
38
39 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
40         : bv(b), font(move(f)), fontname("mathnormal"),
41           textwidth(w), macro_nesting(0),
42           solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
43 {
44         if (lyxrc.currentZoom >= 200) {
45                 // derive the line thickness from zoom factor
46                 // the zoom is given in percent
47                 // (increase thickness at 250%, 450% etc.)
48                 solid_line_thickness_ = (lyxrc.currentZoom + 150) / 200;
49                 // adjust line_offset_ too
50                 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
51         }
52         if (lyxrc.currentZoom >= 100) {
53                 // derive the line thickness from zoom factor
54                 // the zoom is given in percent
55                 // (increase thickness at 150%, 250% etc.)
56                 dotted_line_thickness_ = (lyxrc.currentZoom + 50) / 100;
57         }
58 }
59
60
61 Changer MetricsBase::changeFontSet(string const & name)
62 {
63         RefChanger<MetricsBase> rc = make_save(*this);
64         ColorCode oldcolor = font.color();
65         string const oldname = fontname;
66         fontname = name;
67         if (isMathFont(name) || isMathFont(oldname))
68                 font = sane_font;
69         augmentFont(font, name);
70         font.setSize(rc->old.font.size());
71         font.setStyle(rc->old.font.style());
72         if (name != "lyxtex"
73             && ((isTextFont(oldname) && oldcolor != Color_foreground)
74                 || (isMathFont(oldname) && oldcolor != Color_math)))
75                 font.setColor(oldcolor);
76         return move(rc);
77 }
78
79
80 Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
81 {
82         switch (mode) {
83         case Inset::UNDECIDED_MODE:
84                 return Changer();
85         case Inset::TEXT_MODE:
86                 return isMathFont(fontname) ? changeFontSet("textnormal") : Changer();
87         case Inset::MATH_MODE:
88                 // FIXME:
89                 //   \textit{\ensuremath{\text{a}}}
90                 // should appear in italics
91                 return isTextFont(fontname) ? changeFontSet("mathnormal"): Changer();
92         }
93         return Changer();
94 }
95
96
97 /////////////////////////////////////////////////////////////////////////
98 //
99 // MetricsInfo
100 //
101 /////////////////////////////////////////////////////////////////////////
102
103 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
104                          MacroContext const & mc)
105         : base(bv, font, textwidth), macrocontext(mc)
106 {}
107
108
109 /////////////////////////////////////////////////////////////////////////
110 //
111 // PainterInfo
112 //
113 /////////////////////////////////////////////////////////////////////////
114
115 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
116         : pain(painter), ltr_pos(false), change_(), selected(false),
117         do_spellcheck(true), full_repaint(true), background_color(Color_background)
118 {
119         base.bv = bv;
120 }
121
122
123 void PainterInfo::draw(int x, int y, char_type c)
124 {
125         pain.text(x, y, c, base.font);
126 }
127
128
129 void PainterInfo::draw(int x, int y, docstring const & str)
130 {
131         pain.text(x, y, str, base.font);
132 }
133
134
135 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
136 {
137         ColorCode const color_bg = inset->backgroundColor(*this);
138
139         if (selected && sel)
140                 // This inset is in a selection
141                 return Color_selection;
142         else {
143                 if (color_bg != Color_none)
144                         // This inset has its own color
145                         return color_bg;
146                 else {
147                         if (background_color == Color_none)
148                                 // This inset has no own color and does not inherit a color
149                                 return Color_background;
150                         else
151                                 // This inset has no own color, but inherits a color
152                                 return background_color;
153                 }
154         }
155 }
156
157
158 Color PainterInfo::textColor(Color const & color) const
159 {
160         if (change_.changed())
161                 return change_.color();
162         if (selected)
163                 return Color_selectiontext;
164         return color;
165 }
166
167
168 Changer MetricsBase::changeScript()
169 {
170         switch (font.style()) {
171         case LM_ST_DISPLAY:
172         case LM_ST_TEXT:
173                 return font.changeStyle(LM_ST_SCRIPT);
174         case LM_ST_SCRIPT:
175         case LM_ST_SCRIPTSCRIPT:
176                 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
177         }
178         //remove Warning
179         return Changer();
180 }
181
182
183 Changer MetricsBase::changeFrac()
184 {
185         switch (font.style()) {
186         case LM_ST_DISPLAY:
187                 return font.changeStyle(LM_ST_TEXT);
188         case LM_ST_TEXT:
189                 return font.changeStyle(LM_ST_SCRIPT);
190         case LM_ST_SCRIPT:
191         case LM_ST_SCRIPTSCRIPT:
192                 return font.changeStyle(LM_ST_SCRIPTSCRIPT);
193         }
194         //remove Warning
195         return Changer();
196 }
197
198
199 Changer MetricsBase::changeArray()
200 {
201         return (font.style() == LM_ST_DISPLAY) ? font.changeStyle(LM_ST_TEXT)
202                 : Changer();
203 }
204
205
206 } // namespace lyx