]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
g-brief loads babel internally. So don't load it ourselves.
[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/textutils.h"
30
31 #include <algorithm>
32
33 using namespace std;
34
35
36 namespace lyx {
37
38
39 namespace {
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         string const & f = mi.base.fontname;
110         if (isMathFont(f) && subst_) {
111                 // If the char has a substitute, draw the replacement symbol
112                 // instead, but only in math mode.
113                 kerning_ = mathedSymbolDim(mi.base, dim, subst_);
114         } else if (!slanted(char_) && f == "mathnormal") {
115                 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
116                 dim = theFontMetrics(mi.base.font).dimension(char_);
117                 kerning_ = 0;
118         } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
119                 Changer dummy1 = mi.base.changeFontSet("mathnormal");
120                 Changer dummy2 = Encodings::isMathAlpha(char_)
121                                 ? noChange()
122                                 : mi.base.font.changeShape(UP_SHAPE);
123                 dim = theFontMetrics(mi.base.font).dimension(char_);
124                 kerning_ = -mathed_char_kerning(mi.base.font, char_);
125         } else {
126                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
127                 dim = fm.dimension(char_);
128                 kerning_ = mathed_char_kerning(mi.base.font, char_);
129                 // cmmi has almost no left bearing: simulate this
130                 if (f == "mathnormal") {
131                         dim.wid += max(-fm.lbearing(char_), 0);
132                 }
133                 // Rule 17 from Appendix G
134                 // These are the fonts with fontdimen(2)==0.
135                 // To properly handle the case fontdimen(2)!=0 (that is for all other
136                 // math fonts), where the italic correction must also be converted into
137                 // kerning but only at the end of strings of characters with the same
138                 // font, one would need a more elaborate implementation in MathRow. For
139                 // now the case fontdimen(2)==0 is the most important.
140                 if (f == "mathnormal" || f == "mathscr" || f == "mathcal") {
141                         dim.wid += kerning_;
142                         // We use a negative value to tell InsetMathScript to move the
143                         // subscript leftwards instead of the superscript rightwards
144                         kerning_ = -kerning_;
145                 }
146         }
147 }
148
149
150 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
151 {
152         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
153         if (isMathFont(pi.base.fontname)) {
154                 if (subst_) {
155                         // If the char has a substitute, draw the replacement symbol
156                         // instead, but only in math mode.
157                         mathedSymbolDraw(pi, x, y, subst_);
158                         return;
159                 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
160                         Changer dummy = pi.base.font.changeShape(UP_SHAPE);
161                         pi.draw(x, y, char_);
162                         return;
163                 } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
164                         Changer dummy1 = pi.base.changeFontSet("mathnormal");
165                         Changer dummy2 = Encodings::isMathAlpha(char_)
166                                         ? noChange()
167                                         : pi.base.font.changeShape(UP_SHAPE);
168                         pi.draw(x, y, char_);
169                         return;
170                 }
171         }
172         // cmmi has almost no left bearing: simulate this
173         if (pi.base.fontname == "mathnormal") {
174                 x += max(-theFontMetrics(pi.base.font).lbearing(char_), 0);
175         }
176         pi.draw(x, y, char_);
177 }
178
179
180 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
181 {
182         dim.wid = 1;
183         dim.asc = 1;
184         dim.des = 0;
185 }
186
187
188 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
189 {
190         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
191         pain.draw(x, y, char_);
192 }
193
194
195 void InsetMathChar::write(TeXMathStream & os) const
196 {
197         if (os.latex() && os.pendingSpace()) {
198                 if (isAlphaASCII(char_))
199                         os.os() << ' ';
200                 os.pendingSpace(false);
201         }
202         os.os().put(char_);
203 }
204
205
206 void InsetMathChar::validate(LaTeXFeatures & features) const
207 {
208         if (!isASCII(char_))
209                 BufferEncodings::validate(char_, features, true);
210 }
211
212
213 void InsetMathChar::normalize(NormalStream & os) const
214 {
215         os << "[char ";
216         os.os().put(char_);
217         os << " mathalpha]";
218 }
219
220
221 void InsetMathChar::octave(OctaveStream & os) const
222 {
223         os.os().put(char_);
224 }
225
226
227 // We have a bit of a problem here. MathML wants to know whether the
228 // character represents an "identifier" or an "operator", and we have
229 // no general way of telling. So we shall guess: If it's alpha or
230 // mathalpha, then we'll treat it as an identifier, otherwise as an
231 // operator.
232 // Worst case: We get bad spacing, or bad italics.
233 // In any case, never let MathML stretch a single character when it
234 // is recognised as an operator, to match TeX' behaviour.
235 void InsetMathChar::mathmlize(MathMLStream & ms) const
236 {
237         std::string entity;
238         switch (char_) {
239                 case '<': entity = "&lt;"; break;
240                 case '>': entity = "&gt;"; break;
241                 case '&': entity = "&amp;"; break;
242                 case ' ': {
243                         ms << from_ascii("&#0160;");
244                         return;
245                 }
246                 default: break;
247         }
248
249         if (ms.inText()) {
250                 if (entity.empty())
251                         ms.os().put(char_);
252                 else
253                         ms << from_ascii(entity);
254                 return;
255         }
256
257         if (!entity.empty()) {
258                 ms << MTagInline("mo", "stretchy='false'")
259                    << from_ascii(entity)
260                    << ETagInline("mo");
261                 return;
262         }
263
264         char const * type =
265                 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
266                         ? "mi" : "mo";
267         ms << MTagInline(type, std::string(type) == "mo" ? "stretchy='false'" : "")
268            << char_type(char_)
269            << ETagInline(type);
270 }
271
272
273 void InsetMathChar::htmlize(HtmlStream & ms) const
274 {
275         std::string entity;
276         // Not taking subst_ into account here because the MathML output of
277         // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
278         switch (char_) {
279                 case '<': entity = "&lt;"; break;
280                 case '>': entity = "&gt;"; break;
281                 case '&': entity = "&amp;"; break;
282                 case ' ': entity = "&#160;"; break;
283                 default: break;
284         }
285
286         bool have_entity = !entity.empty();
287
288         if (ms.inText()) {
289                 if (have_entity)
290                         ms << from_ascii(entity);
291                 else
292                         ms.os().put(char_);
293                 return;
294         }
295
296         if (have_entity) {
297                 // an operator, so give some space
298                 ms << ' ' << from_ascii(entity) << ' ';
299                 return;
300         }
301
302         if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
303                 // we don't use MTag and ETag because we do not want the spacing
304                 ms << MTag("i") << char_type(char_) << ETag("i");
305         else
306                 // an operator, so give some space
307                 ms << " " << char_type(char_) << " ";
308 }
309
310
311 MathClass InsetMathChar::mathClass() const
312 {
313         // this information comes from fontmath.ltx in LaTeX source.
314         if (subst_)
315                 return string_to_class(subst_->extra);
316
317         if (!isASCII(char_))
318                 return MC_ORD;
319
320         switch (static_cast<char>(char_)) {
321         case ',':
322         case ';':
323                 return MC_PUNCT;
324         case '(':
325         case '[':
326                 return MC_OPEN;
327         case ')':
328         case ']':
329         case '!':
330         case '?':
331                 return MC_CLOSE;
332         default:
333                 return MC_ORD;
334         }
335 }
336
337
338 } // namespace lyx