]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
Get package things working with modules prior to UI patch.
[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 "TextPainter.h"
21
22 #include "support/lstrings.h"
23
24 #include "frontends/FontMetrics.h"
25
26
27 namespace lyx {
28
29 using std::auto_ptr;
30
31 extern bool has_math_fonts;
32
33 namespace {
34
35         bool isBinaryOp(char_type c)
36         {
37                 return support::contains("+-<>=/*", static_cast<char>(c));
38         }
39
40
41         bool slanted(char_type c)
42         {
43                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
44         }
45
46 }
47
48
49 InsetMathChar::InsetMathChar(char_type c)
50         : char_(c), kerning_(0)
51 {}
52
53
54
55 auto_ptr<Inset> InsetMathChar::doClone() const
56 {
57         return auto_ptr<Inset>(new InsetMathChar(*this));
58 }
59
60
61 bool InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
62 {
63 #if 1
64         if (char_ == '=' && has_math_fonts) {
65                 FontSetChanger dummy(mi.base, "cmr");
66                 dim = theFontMetrics(mi.base.font).dimension(char_);
67         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
68                 FontSetChanger dummy(mi.base, "cmm");
69                 dim = theFontMetrics(mi.base.font).dimension(char_);
70         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
71                 ShapeChanger dummy(mi.base.font, Font::UP_SHAPE);
72                 dim = theFontMetrics(mi.base.font).dimension(char_);
73         } else {
74                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
75                 dim = fm.dimension(char_);
76                 kerning_ = fm.rbearing(char_) - dim.wid;
77         }
78         int const em = mathed_char_width(mi.base.font, 'M');
79         if (isBinaryOp(char_))
80                 dim.wid += static_cast<int>(0.5*em+0.5);
81         else if (char_ == '\'')
82                 dim.wid += static_cast<int>(0.1667*em+0.5);
83 #else
84         whichFont(font_, code_, mi);
85         dim = theFontMetrics(font_).dimension(char_);
86         if (isBinaryOp(char_, code_))
87                 dim.wid += 2 * theFontMetrics(font_).width(' ');
88         lyxerr << "InsetMathChar::metrics: " << dim << endl;
89 #endif
90
91         if (dim_ == dim)
92                 return false;
93
94         dim_ = dim;
95         return true;
96 }
97
98
99 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
100 {
101         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
102         int const em = mathed_char_width(pi.base.font, 'M');
103         if (isBinaryOp(char_))
104                 x += static_cast<int>(0.25*em+0.5);
105         else if (char_ == '\'')
106                 x += static_cast<int>(0.0833*em+0.5);
107 #if 1
108         if (char_ == '=' && has_math_fonts) {
109                 FontSetChanger dummy(pi.base, "cmr");
110                 pi.draw(x, y, char_);
111         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
112                 FontSetChanger dummy(pi.base, "cmm");
113                 pi.draw(x, y, char_);
114         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
115                 ShapeChanger dummy(pi.base.font, Font::UP_SHAPE);
116                 pi.draw(x, y, char_);
117         } else {
118                 pi.draw(x, y, char_);
119         }
120 #else
121         drawChar(pain, font_, x, y, char_);
122 #endif
123 }
124
125
126 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
127 {
128         dim.wid = 1;
129         dim.asc = 1;
130         dim.des = 0;
131 }
132
133
134 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
135 {
136         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
137         pain.draw(x, y, char_);
138 }
139
140
141 void InsetMathChar::write(WriteStream & os) const
142 {
143         os.os().put(char_);
144 }
145
146
147 void InsetMathChar::normalize(NormalStream & os) const
148 {
149         os << "[char ";
150         os.os().put(char_);
151         os << " mathalpha]";
152 }
153
154
155 void InsetMathChar::octave(OctaveStream & os) const
156 {
157         os.os().put(char_);
158 }
159
160
161 void InsetMathChar::mathmlize(MathStream & ms) const
162 {
163         switch (char_) {
164                 case '<': ms << "&lt;"; break;
165                 case '>': ms << "&gt;"; break;
166                 case '&': ms << "&amp;"; break;
167                 default: ms.os().put(char_); break;
168         }
169 }
170
171
172 bool InsetMathChar::isRelOp() const
173 {
174         return char_ == '=' || char_ == '<' || char_ == '>';
175 }
176
177
178 } // namespace lyx