]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
813067e08a55bff863821a60885ed335032bd291
[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
16 #include "MathSupport.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
19
20 #include "Dimension.h"
21 #include "BufferEncodings.h"
22 #include "LaTeXFeatures.h"
23 #include "TextPainter.h"
24
25 #include "frontends/FontMetrics.h"
26
27 #include "support/debug.h"
28 #include "support/lstrings.h"
29 #include "support/textutils.h"
30
31
32 namespace lyx {
33
34 extern bool has_math_fonts;
35
36
37 static bool slanted(char_type c)
38 {
39         return isAlphaASCII(c) || Encodings::isMathAlpha(c);
40 }
41
42
43 InsetMathChar::InsetMathChar(char_type c)
44         : char_(c), kerning_(0)
45 {}
46
47
48
49 Inset * InsetMathChar::clone() const
50 {
51         return new InsetMathChar(*this);
52 }
53
54
55 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
56 {
57 #if 1
58         if (char_ == '=' && has_math_fonts) {
59                 Changer dummy = mi.base.changeFontSet("cmr");
60                 dim = theFontMetrics(mi.base.font).dimension(char_);
61         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
62                 Changer dummy = mi.base.changeFontSet("cmm");
63                 dim = theFontMetrics(mi.base.font).dimension(char_);
64         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
65                 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
66                 dim = theFontMetrics(mi.base.font).dimension(char_);
67         } else {
68                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
69                 dim = fm.dimension(char_);
70                 kerning_ = fm.rbearing(char_) - dim.wid;
71         }
72         if (isMathBin())
73                 dim.wid += 2 * mathed_medmuskip(mi.base.font);
74         else if (isMathRel())
75                 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
76         else if (isMathPunct())
77                 dim.wid += mathed_thinmuskip(mi.base.font);
78         else if (char_ == '\'')
79                 // FIXME: don't know where this is coming from
80                 dim.wid += mathed_thinmuskip(mi.base.font);
81 #else
82         whichFont(font_, code_, mi);
83         dim = theFontMetrics(font_).dimension(char_);
84         if (isBinaryOp(char_, code_))
85                 dim.wid += 2 * theFontMetrics(font_).width(' ');
86         lyxerr << "InsetMathChar::metrics: " << dim << endl;
87 #endif
88 }
89
90
91 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
92 {
93         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
94         if (isMathBin())
95                 x += mathed_medmuskip(pi.base.font);
96         else if (isMathRel())
97                 x += mathed_thickmuskip(pi.base.font);
98         else if (char_ == '\'')
99                 x += mathed_thinmuskip(pi.base.font) / 2;
100 #if 1
101         if (char_ == '=' && has_math_fonts) {
102                 Changer dummy = pi.base.changeFontSet("cmr");
103                 pi.draw(x, y, char_);
104         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
105                 Changer dummy = pi.base.changeFontSet("cmm");
106                 pi.draw(x, y, char_);
107         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
108                 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
109                 pi.draw(x, y, char_);
110         } else {
111                 pi.draw(x, y, char_);
112         }
113 #else
114         drawChar(pain, font_, x, y, char_);
115 #endif
116 }
117
118
119 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
120 {
121         dim.wid = 1;
122         dim.asc = 1;
123         dim.des = 0;
124 }
125
126
127 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
128 {
129         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
130         pain.draw(x, y, char_);
131 }
132
133
134 void InsetMathChar::write(WriteStream & os) const
135 {
136         os.os().put(char_);
137 }
138
139
140 void InsetMathChar::validate(LaTeXFeatures & features) const
141 {
142         if (!isASCII(char_))
143                 BufferEncodings::validate(char_, features, true);
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 // We have a bit of a problem here. MathML wants to know whether the
162 // character represents an "identifier" or an "operator", and we have
163 // no general way of telling. So we shall guess: If it's alpha or 
164 // mathalpha, then we'll treat it as an identifier, otherwise as an 
165 // operator.
166 // Worst case: We get bad spacing, or bad italics.
167 void InsetMathChar::mathmlize(MathStream & ms) const
168 {
169         std::string entity;
170         switch (char_) {
171                 case '<': entity = "&lt;"; break;
172                 case '>': entity = "&gt;"; break;
173                 case '&': entity = "&amp;"; break;
174                 case ' ': {
175                         ms << from_ascii("&nbsp;");
176                         return;
177                 }
178                 default: break;
179         }
180         
181         if (ms.inText()) {
182                 if (entity.empty())
183                         ms.os().put(char_);
184                 else 
185                         ms << from_ascii(entity);
186                 return;
187         }
188
189         if (!entity.empty()) {
190                 ms << "<mo>" << from_ascii(entity) << "</mo>";
191                 return;
192         }               
193
194         char const * type = 
195                 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
196                         ? "mi" : "mo";
197         // we don't use MTag and ETag because we do not want the spacing
198         ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";    
199 }
200
201
202 void InsetMathChar::htmlize(HtmlStream & ms) const
203 {
204         std::string entity;
205         switch (char_) {
206                 case '<': entity = "&lt;"; break;
207                 case '>': entity = "&gt;"; break;
208                 case '&': entity = "&amp;"; break;
209                 case ' ': entity = "&nbsp;"; break;
210                 default: break;
211         }
212         
213         bool have_entity = !entity.empty();
214         
215         if (ms.inText()) {
216                 if (have_entity)
217                         ms << from_ascii(entity);
218                 else
219                         ms.os().put(char_);
220                 return;
221         }
222         
223         if (have_entity) {
224                 // an operator, so give some space
225                 ms << ' ' << from_ascii(entity) << ' ';
226                 return;
227         }               
228
229         if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
230                 // we don't use MTag and ETag because we do not want the spacing
231                 ms << MTag("i") << char_type(char_) << ETag("i");
232         else
233                 // an operator, so give some space
234                 ms << " " << char_type(char_) << " ";
235 }
236
237
238 bool InsetMathChar::isMathBin() const
239 {
240         return support::contains("+-*", static_cast<char>(char_));
241 }
242
243
244 bool InsetMathChar::isMathRel() const
245 {
246         return support::contains("<>=:", static_cast<char>(char_));
247 }
248
249
250 bool InsetMathChar::isMathPunct() const
251 {
252         return support::contains(",;", static_cast<char>(char_));
253 }
254
255
256 } // namespace lyx