]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
New methods in LaTeXFeatures specifically for collection of CSS
[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 "Encoding.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 isBinaryOp(char_type c)
38 {
39         return support::contains("+-<>=/*", static_cast<char>(c));
40 }
41
42
43 static bool slanted(char_type c)
44 {
45         return isAlphaASCII(c) || Encodings::isMathAlpha(c);
46 }
47
48
49 InsetMathChar::InsetMathChar(char_type c)
50         : char_(c), kerning_(0)
51 {}
52
53
54
55 Inset * InsetMathChar::clone() const
56 {
57         return new InsetMathChar(*this);
58 }
59
60
61 void 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, 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
92
93 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
94 {
95         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
96         int const em = mathed_char_width(pi.base.font, 'M');
97         if (isBinaryOp(char_))
98                 x += static_cast<int>(0.25*em+0.5);
99         else if (char_ == '\'')
100                 x += static_cast<int>(0.0833*em+0.5);
101 #if 1
102         if (char_ == '=' && has_math_fonts) {
103                 FontSetChanger dummy(pi.base, "cmr");
104                 pi.draw(x, y, char_);
105         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
106                 FontSetChanger dummy(pi.base, "cmm");
107                 pi.draw(x, y, char_);
108         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
109                 ShapeChanger dummy(pi.base.font, UP_SHAPE);
110                 pi.draw(x, y, char_);
111         } else {
112                 pi.draw(x, y, char_);
113         }
114 #else
115         drawChar(pain, font_, x, y, char_);
116 #endif
117 }
118
119
120 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
121 {
122         dim.wid = 1;
123         dim.asc = 1;
124         dim.des = 0;
125 }
126
127
128 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
129 {
130         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
131         pain.draw(x, y, char_);
132 }
133
134
135 void InsetMathChar::write(WriteStream & os) const
136 {
137         os.os().put(char_);
138 }
139
140
141 void InsetMathChar::validate(LaTeXFeatures & features) const
142 {
143         if (char_ >= 0x80)
144                 encodings.validate(char_, features, true);
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 // We have a bit of a problem here. MathML wants to know whether the
163 // character represents an "identifier" or an "operator", and we have
164 // no general way of telling. So we shall guess: If it's alpha or 
165 // mathalpha, then we'll treat it as an identifier, otherwise as an 
166 // operator.
167 // Worst case: We get bad spacing, or bad italics.
168 void InsetMathChar::mathmlize(MathStream & ms) const
169 {
170         std::string entity;
171         switch (char_) {
172                 case '<': entity = "&lt;"; break;
173                 case '>': entity = "&gt;"; break;
174                 case '&': entity = "&amp;"; break;
175                 case ' ': {
176                         ms << from_ascii("&nbsp;");
177                         return;
178                 }
179                 default: break;
180         }
181         
182         if (ms.inText()) {
183                 if (entity.empty())
184                         ms.os().put(char_);
185                 else 
186                         ms << from_ascii(entity);
187                 return;
188         }
189
190         if (!entity.empty()) {
191                 ms << "<mo>" << from_ascii(entity) << "</mo>";
192                 return;
193         }               
194
195         char const * type = 
196                 (isalpha(char_) || Encodings::isMathAlpha(char_))
197                         ? "mi" : "mo";
198         // we don't use MTag and ETag because we do not want the spacing
199         ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";    
200 }
201
202
203 void InsetMathChar::htmlize(HtmlStream & ms) const
204 {
205         std::string entity;
206         switch (char_) {
207                 case '<': entity = "&lt;"; break;
208                 case '>': entity = "&gt;"; break;
209                 case '&': entity = "&amp;"; break;
210                 case ' ': entity = "&nbsp;"; break;
211                 default: break;
212         }
213         
214         bool have_entity = !entity.empty();
215         
216         if (ms.inText()) {
217                 if (have_entity)
218                         ms << from_ascii(entity);
219                 else
220                         ms.os().put(char_);
221                 return;
222         }
223         
224         if (have_entity) {
225                 // an operator, so give some space
226                 ms << ' ' << from_ascii(entity) << ' ';
227                 return;
228         }               
229
230         if (isalpha(char_) || Encodings::isMathAlpha(char_))
231                 // we don't use MTag and ETag because we do not want the spacing
232                 ms << MTag("i") << char_type(char_) << ETag("i");
233         else
234                 // an operator, so give some space
235                 ms << " " << char_type(char_) << " ";
236 }
237
238
239 bool InsetMathChar::isRelOp() const
240 {
241         return char_ == '=' || char_ == '<' || char_ == '>';
242 }
243
244
245 } // namespace lyx