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