]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathChar.C
Account for real width of nucleus when positioning superscripts in mathed
[features.git] / src / mathed / InsetMathChar.C
1 /**
2  * \file InsetMathChar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathChar.h"
15 #include "MathSupport.h"
16 #include "MathStream.h"
17
18 #include "debug.h"
19 #include "dimension.h"
20 #include "support/lstrings.h"
21 #include "TextPainter.h"
22
23 #include "frontends/FontMetrics.h"
24
25
26 namespace lyx {
27
28 using std::auto_ptr;
29
30 extern bool has_math_fonts;
31
32 namespace {
33
34         bool isBinaryOp(char_type c)
35         {
36                 return support::contains("+-<>=/*", static_cast<char>(c));
37         }
38
39
40         bool slanted(char_type c)
41         {
42                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
43         }
44
45 }
46
47
48 InsetMathChar::InsetMathChar(char_type c)
49         : char_(c), kerning_(0)
50 {}
51
52
53
54 auto_ptr<InsetBase> InsetMathChar::doClone() const
55 {
56         return auto_ptr<InsetBase>(new InsetMathChar(*this));
57 }
58
59
60 bool InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
61 {
62 #if 1
63         if (char_ == '=' && has_math_fonts) {
64                 FontSetChanger dummy(mi.base, "cmr");
65                 dim = theFontMetrics(mi.base.font).dimension(char_);
66         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
67                 FontSetChanger dummy(mi.base, "cmm");
68                 dim = theFontMetrics(mi.base.font).dimension(char_);
69         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
70                 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
71                 dim = theFontMetrics(mi.base.font).dimension(char_);
72         } else {
73                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
74                 dim = fm.dimension(char_);
75                 kerning_ = fm.rbearing(char_) - dim.wid;
76         }
77         int const em = mathed_char_width(mi.base.font, 'M');
78         if (isBinaryOp(char_))
79                 dim.wid += static_cast<int>(0.5*em+0.5);
80         else if (char_ == '\'')
81                 dim.wid += static_cast<int>(0.1667*em+0.5);
82 #else
83         whichFont(font_, code_, mi);
84         dim = theFontMetrics(font_).dimension(char_);
85         if (isBinaryOp(char_, code_))
86                 width_ += 2 * theFontMetrics(font_).width(' ');
87         lyxerr << "InsetMathChar::metrics: " << dim << endl;
88 #endif
89         width_ = dim.wid;
90         if (dim_ == dim)
91                 return false;
92         dim_ = dim;
93         return true;
94 }
95
96
97 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
98 {
99         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
100         int const em = mathed_char_width(pi.base.font, 'M');
101         if (isBinaryOp(char_))
102                 x += static_cast<int>(0.25*em+0.5);
103         else if (char_ == '\'')
104                 x += static_cast<int>(0.0833*em+0.5);
105 #if 1
106         if (char_ == '=' && has_math_fonts) {
107                 FontSetChanger dummy(pi.base, "cmr");
108                 pi.draw(x, y, char_);
109         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
110                 FontSetChanger dummy(pi.base, "cmm");
111                 pi.draw(x, y, char_);
112         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
113                 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
114                 pi.draw(x, y, char_);
115         } else {
116                 pi.draw(x, y, char_);
117         }
118 #else
119         drawChar(pain, font_, x, y, char_);
120 #endif
121 }
122
123
124 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
125 {
126         dim.wid = 1;
127         dim.asc = 1;
128         dim.des = 0;
129 }
130
131
132 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
133 {
134         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
135         pain.draw(x, y, char_);
136 }
137
138
139 void InsetMathChar::write(WriteStream & os) const
140 {
141         os.os().put(char_);
142 }
143
144
145 void InsetMathChar::normalize(NormalStream & os) const
146 {
147         os << "[char ";
148         os.os().put(char_);
149         os << " mathalpha]";
150 }
151
152
153 void InsetMathChar::octave(OctaveStream & os) const
154 {
155         os.os().put(char_);
156 }
157
158
159 void InsetMathChar::mathmlize(MathStream & ms) const
160 {
161         switch (char_) {
162                 case '<': ms << "&lt;"; break;
163                 case '>': ms << "&gt;"; break;
164                 case '&': ms << "&amp;"; break;
165                 default: ms.os().put(char_); break;
166         }
167 }
168
169
170 bool InsetMathChar::isRelOp() const
171 {
172         return char_ == '=' || char_ == '<' || char_ == '>';
173 }
174
175
176 } // namespace lyx