]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
Display properly math characters that behave like symbols
[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
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         if (mathfont && isMathPunct())
125                 dim.wid += mathed_thinmuskip(mi.base.font);
126 }
127
128
129 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
130 {
131         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
132         if (isMathFont(pi.base.fontname)) {
133                 if (subst_) {
134                         // If the char has a substitute, draw the replacement symbol
135                         // instead, but only in math mode.
136                         mathedSymbolDraw(pi, x, y, subst_);
137                         return;
138                 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
139                         Changer dummy = pi.base.font.changeShape(UP_SHAPE);
140                         pi.draw(x, y, char_);
141                         return;
142                 }
143         }
144         pi.draw(x, y, char_);
145 }
146
147
148 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
149 {
150         dim.wid = 1;
151         dim.asc = 1;
152         dim.des = 0;
153 }
154
155
156 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
157 {
158         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
159         pain.draw(x, y, char_);
160 }
161
162
163 void InsetMathChar::write(WriteStream & os) const
164 {
165         os.os().put(char_);
166 }
167
168
169 void InsetMathChar::validate(LaTeXFeatures & features) const
170 {
171         if (!isASCII(char_))
172                 BufferEncodings::validate(char_, features, true);
173 }
174
175
176 void InsetMathChar::normalize(NormalStream & os) const
177 {
178         os << "[char ";
179         os.os().put(char_);
180         os << " mathalpha]";
181 }
182
183
184 void InsetMathChar::octave(OctaveStream & os) const
185 {
186         os.os().put(char_);
187 }
188
189
190 // We have a bit of a problem here. MathML wants to know whether the
191 // character represents an "identifier" or an "operator", and we have
192 // no general way of telling. So we shall guess: If it's alpha or 
193 // mathalpha, then we'll treat it as an identifier, otherwise as an 
194 // operator.
195 // Worst case: We get bad spacing, or bad italics.
196 void InsetMathChar::mathmlize(MathStream & ms) const
197 {
198         std::string entity;
199         switch (char_) {
200                 case '<': entity = "&lt;"; break;
201                 case '>': entity = "&gt;"; break;
202                 case '&': entity = "&amp;"; break;
203                 case ' ': {
204                         ms << from_ascii("&nbsp;");
205                         return;
206                 }
207                 default: break;
208         }
209         
210         if (ms.inText()) {
211                 if (entity.empty())
212                         ms.os().put(char_);
213                 else 
214                         ms << from_ascii(entity);
215                 return;
216         }
217
218         if (!entity.empty()) {
219                 ms << "<mo>" << from_ascii(entity) << "</mo>";
220                 return;
221         }               
222
223         char const * type = 
224                 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
225                         ? "mi" : "mo";
226         // we don't use MTag and ETag because we do not want the spacing
227         ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";    
228 }
229
230
231 void InsetMathChar::htmlize(HtmlStream & ms) const
232 {
233         std::string entity;
234         // Not taking subst_ into account here because the MathML output of
235         // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
236         switch (char_) {
237                 case '<': entity = "&lt;"; break;
238                 case '>': entity = "&gt;"; break;
239                 case '&': entity = "&amp;"; break;
240                 case ' ': entity = "&nbsp;"; break;
241                 default: break;
242         }
243         
244         bool have_entity = !entity.empty();
245         
246         if (ms.inText()) {
247                 if (have_entity)
248                         ms << from_ascii(entity);
249                 else
250                         ms.os().put(char_);
251                 return;
252         }
253         
254         if (have_entity) {
255                 // an operator, so give some space
256                 ms << ' ' << from_ascii(entity) << ' ';
257                 return;
258         }               
259
260         if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
261                 // we don't use MTag and ETag because we do not want the spacing
262                 ms << MTag("i") << char_type(char_) << ETag("i");
263         else
264                 // an operator, so give some space
265                 ms << " " << char_type(char_) << " ";
266 }
267
268
269 bool InsetMathChar::isMathBin() const
270 {
271         return subst_ && subst_->extra == "mathbin";
272 }
273
274
275 bool InsetMathChar::isMathRel() const
276 {
277         return subst_ && subst_->extra == "mathrel";
278 }
279
280
281 bool InsetMathChar::isMathPunct() const
282 {
283         return support::contains(",;", static_cast<char>(char_))
284                 || (subst_ && subst_->extra == "mathpunct");
285 }
286
287
288 } // namespace lyx