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