]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathChar.cpp
Introduce the notion of math class
[features.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 "MathParser.h"
17 #include "MathSupport.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
20
21 #include "Dimension.h"
22 #include "BufferEncodings.h"
23 #include "LaTeXFeatures.h"
24 #include "TextPainter.h"
25
26 #include "frontends/FontMetrics.h"
27
28 #include "support/debug.h"
29 #include "support/lstrings.h"
30 #include "support/textutils.h"
31
32
33 namespace lyx {
34
35 extern bool has_math_fonts;
36
37
38 namespace {
39
40 latexkeys const * makeSubstitute(char_type c)
41 {
42         std::string name;
43         switch (c) {
44         // Latex replaces ', *, -, and : with specific symbols. With unicode-math,
45         // these symbols are replaced respectively by ^U+2032, U+2217, U+2212 and
46         // U+2236 (the latter substitution can be turned off with a package
47         // option). Unicode-math also replaces ` with \backprime.
48                 // prime needs to be placed in superscript unless an opentype font is used.
49                 //case '\'':
50                 //name = "prime";
51                 //break;
52         case '*':
53                 name = "ast";
54                 break;
55         case '-':
56                 name = "lyxminus";// unicode-math: "minus"
57                 break;
58         case ':':
59                 name = "ordinarycolon";// unicode-math: "mathratio"
60                 break;
61         // The remaining replacements are not real character substitutions (from a
62         // unicode point of view) but are done here: 1. for cosmetic reasons, in the
63         // context of being stuck with CM fonts at the moment, to ensure consistency
64         // with related symbols: -, \leq, \geq, etc.  2. to get the proper spacing
65         // as defined in lib/symbols.
66         case '+':
67                 name = "lyxplus";//unicode-math: "mathplus"
68                 break;
69         case '>':
70                 name = "lyxgt";//unicode-math: "greater"
71                 break;
72         case '<':
73                 name = "lyxlt";//unicode-math: "less"
74                 break;
75         case '=':
76                 name = "lyxeqrel";//unicode-math: "equal"
77                 break;
78         //case ','://unicode-math: "mathcomma"
79         //case ';'://unicode-math: "mathsemicolon"
80         default:
81                 return nullptr;
82         }
83         return in_word_set(from_ascii(name));
84 }
85
86 } //anonymous namespace
87
88
89 static bool slanted(char_type c)
90 {
91         return isAlphaASCII(c) || Encodings::isMathAlpha(c);
92 }
93
94
95 InsetMathChar::InsetMathChar(char_type c)
96         : char_(c), kerning_(0), subst_(makeSubstitute(c))
97 {}
98
99
100
101 Inset * InsetMathChar::clone() const
102 {
103         return new InsetMathChar(*this);
104 }
105
106
107 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
108 {
109         bool const mathfont = isMathFont(mi.base.fontname);
110         if (mathfont && subst_) {
111                 // If the char has a substitute, draw the replacement symbol
112                 // instead, but only in math mode.
113                 mathedSymbolDim(mi, dim, subst_);
114                 kerning_ = mathed_char_kerning(mi.base.font, *subst_->draw.rbegin());
115                 return;
116         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
117                 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
118                 dim = theFontMetrics(mi.base.font).dimension(char_);
119         } else {
120                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
121                 dim = fm.dimension(char_);
122                 kerning_ = fm.rbearing(char_) - dim.wid;
123         }
124 }
125
126
127 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
128 {
129         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
130         if (isMathFont(pi.base.fontname)) {
131                 if (subst_) {
132                         // If the char has a substitute, draw the replacement symbol
133                         // instead, but only in math mode.
134                         mathedSymbolDraw(pi, x, y, subst_);
135                         return;
136                 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
137                         Changer dummy = pi.base.font.changeShape(UP_SHAPE);
138                         pi.draw(x, y, char_);
139                         return;
140                 }
141         }
142         pi.draw(x, y, char_);
143 }
144
145
146 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
147 {
148         dim.wid = 1;
149         dim.asc = 1;
150         dim.des = 0;
151 }
152
153
154 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
155 {
156         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
157         pain.draw(x, y, char_);
158 }
159
160
161 void InsetMathChar::write(WriteStream & os) const
162 {
163         os.os().put(char_);
164 }
165
166
167 void InsetMathChar::validate(LaTeXFeatures & features) const
168 {
169         if (!isASCII(char_))
170                 BufferEncodings::validate(char_, features, true);
171 }
172
173
174 void InsetMathChar::normalize(NormalStream & os) const
175 {
176         os << "[char ";
177         os.os().put(char_);
178         os << " mathalpha]";
179 }
180
181
182 void InsetMathChar::octave(OctaveStream & os) const
183 {
184         os.os().put(char_);
185 }
186
187
188 // We have a bit of a problem here. MathML wants to know whether the
189 // character represents an "identifier" or an "operator", and we have
190 // no general way of telling. So we shall guess: If it's alpha or 
191 // mathalpha, then we'll treat it as an identifier, otherwise as an 
192 // operator.
193 // Worst case: We get bad spacing, or bad italics.
194 void InsetMathChar::mathmlize(MathStream & ms) const
195 {
196         std::string entity;
197         switch (char_) {
198                 case '<': entity = "&lt;"; break;
199                 case '>': entity = "&gt;"; break;
200                 case '&': entity = "&amp;"; break;
201                 case ' ': {
202                         ms << from_ascii("&nbsp;");
203                         return;
204                 }
205                 default: break;
206         }
207         
208         if (ms.inText()) {
209                 if (entity.empty())
210                         ms.os().put(char_);
211                 else 
212                         ms << from_ascii(entity);
213                 return;
214         }
215
216         if (!entity.empty()) {
217                 ms << "<mo>" << from_ascii(entity) << "</mo>";
218                 return;
219         }               
220
221         char const * type = 
222                 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
223                         ? "mi" : "mo";
224         // we don't use MTag and ETag because we do not want the spacing
225         ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";    
226 }
227
228
229 void InsetMathChar::htmlize(HtmlStream & ms) const
230 {
231         std::string entity;
232         // Not taking subst_ into account here because the MathML output of
233         // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
234         switch (char_) {
235                 case '<': entity = "&lt;"; break;
236                 case '>': entity = "&gt;"; break;
237                 case '&': entity = "&amp;"; break;
238                 case ' ': entity = "&nbsp;"; break;
239                 default: break;
240         }
241         
242         bool have_entity = !entity.empty();
243         
244         if (ms.inText()) {
245                 if (have_entity)
246                         ms << from_ascii(entity);
247                 else
248                         ms.os().put(char_);
249                 return;
250         }
251         
252         if (have_entity) {
253                 // an operator, so give some space
254                 ms << ' ' << from_ascii(entity) << ' ';
255                 return;
256         }               
257
258         if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
259                 // we don't use MTag and ETag because we do not want the spacing
260                 ms << MTag("i") << char_type(char_) << ETag("i");
261         else
262                 // an operator, so give some space
263                 ms << " " << char_type(char_) << " ";
264 }
265
266
267 MathClass InsetMathChar::mathClass() const
268 {
269         // this information comes from fontmath.ltx in LaTeX source.
270         char const ch = static_cast<char>(char_);
271         if (subst_)
272                 return string_to_class(subst_->extra);
273         else if (support::contains(",;", ch))
274                 return MC_PUNCT;
275         else if (support::contains("([", ch))
276                 return MC_OPEN;
277         else if (support::contains(")]!?", ch))
278                 return MC_CLOSE;
279         else return MC_ORD;
280 }
281
282
283 } // namespace lyx