]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
0baad6a7d53f8a4fc03a69e0e44dc96bf6cd6e7f
[lyx.git] / src / mathed / InsetMathChar.cpp
1 /**
2  * \file InsetMathChar.cpp
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 (mi.base.font == font_cache_) {
63                 dim = dim_;
64                 return false;
65         }
66         font_cache_ = mi.base.font;
67
68 #if 1
69         if (char_ == '=' && has_math_fonts) {
70                 FontSetChanger dummy(mi.base, "cmr");
71                 dim = theFontMetrics(mi.base.font).dimension(char_);
72         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
73                 FontSetChanger dummy(mi.base, "cmm");
74                 dim = theFontMetrics(mi.base.font).dimension(char_);
75         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
76                 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
77                 dim = theFontMetrics(mi.base.font).dimension(char_);
78         } else {
79                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
80                 dim = fm.dimension(char_);
81                 kerning_ = fm.rbearing(char_) - dim.wid;
82         }
83         int const em = mathed_char_width(mi.base.font, 'M');
84         if (isBinaryOp(char_))
85                 dim.wid += static_cast<int>(0.5*em+0.5);
86         else if (char_ == '\'')
87                 dim.wid += static_cast<int>(0.1667*em+0.5);
88 #else
89         whichFont(font_, code_, mi);
90         dim = theFontMetrics(font_).dimension(char_);
91         if (isBinaryOp(char_, code_))
92                 dim.wid += 2 * theFontMetrics(font_).width(' ');
93         lyxerr << "InsetMathChar::metrics: " << dim << endl;
94 #endif
95         dim_ = dim;
96         return true;
97 }
98
99
100 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
101 {
102         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
103         int const em = mathed_char_width(pi.base.font, 'M');
104         if (isBinaryOp(char_))
105                 x += static_cast<int>(0.25*em+0.5);
106         else if (char_ == '\'')
107                 x += static_cast<int>(0.0833*em+0.5);
108 #if 1
109         if (char_ == '=' && has_math_fonts) {
110                 FontSetChanger dummy(pi.base, "cmr");
111                 pi.draw(x, y, char_);
112         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
113                 FontSetChanger dummy(pi.base, "cmm");
114                 pi.draw(x, y, char_);
115         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
116                 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
117                 pi.draw(x, y, char_);
118         } else {
119                 pi.draw(x, y, char_);
120         }
121 #else
122         drawChar(pain, font_, x, y, char_);
123 #endif
124 }
125
126
127 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
128 {
129         dim.wid = 1;
130         dim.asc = 1;
131         dim.des = 0;
132 }
133
134
135 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
136 {
137         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
138         pain.draw(x, y, char_);
139 }
140
141
142 void InsetMathChar::write(WriteStream & os) const
143 {
144         os.os().put(char_);
145 }
146
147
148 void InsetMathChar::normalize(NormalStream & os) const
149 {
150         os << "[char ";
151         os.os().put(char_);
152         os << " mathalpha]";
153 }
154
155
156 void InsetMathChar::octave(OctaveStream & os) const
157 {
158         os.os().put(char_);
159 }
160
161
162 void InsetMathChar::mathmlize(MathStream & ms) const
163 {
164         switch (char_) {
165                 case '<': ms << "&lt;"; break;
166                 case '>': ms << "&gt;"; break;
167                 case '&': ms << "&amp;"; break;
168                 default: ms.os().put(char_); break;
169         }
170 }
171
172
173 bool InsetMathChar::isRelOp() const
174 {
175         return char_ == '=' || char_ == '<' || char_ == '>';
176 }
177
178
179 } // namespace lyx