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