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