2 * \file InsetMathChar.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathChar.h"
16 #include "MathSupport.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
20 #include "Dimension.h"
22 #include "LaTeXFeatures.h"
23 #include "TextPainter.h"
25 #include "frontends/FontMetrics.h"
27 #include "support/debug.h"
28 #include "support/lstrings.h"
29 #include "support/textutils.h"
34 extern bool has_math_fonts
;
37 static bool isBinaryOp(char_type c
)
39 return support::contains("+-<>=/*", static_cast<char>(c
));
43 static bool slanted(char_type c
)
45 return isAlphaASCII(c
) || Encodings::isMathAlpha(c
);
49 InsetMathChar::InsetMathChar(char_type c
)
50 : char_(c
), kerning_(0)
55 Inset
* InsetMathChar::clone() const
57 return new InsetMathChar(*this);
61 void InsetMathChar::metrics(MetricsInfo
& mi
, Dimension
& dim
) const
64 if (char_
== '=' && has_math_fonts
) {
65 FontSetChanger
dummy(mi
.base
, "cmr");
66 dim
= theFontMetrics(mi
.base
.font
).dimension(char_
);
67 } else if ((char_
== '>' || char_
== '<') && has_math_fonts
) {
68 FontSetChanger
dummy(mi
.base
, "cmm");
69 dim
= theFontMetrics(mi
.base
.font
).dimension(char_
);
70 } else if (!slanted(char_
) && mi
.base
.fontname
== "mathnormal") {
71 ShapeChanger
dummy(mi
.base
.font
, UP_SHAPE
);
72 dim
= theFontMetrics(mi
.base
.font
).dimension(char_
);
74 frontend::FontMetrics
const & fm
= theFontMetrics(mi
.base
.font
);
75 dim
= fm
.dimension(char_
);
76 kerning_
= fm
.rbearing(char_
) - dim
.wid
;
78 int const em
= mathed_char_width(mi
.base
.font
, 'M');
79 if (isBinaryOp(char_
))
80 dim
.wid
+= static_cast<int>(0.5*em
+0.5);
81 else if (char_
== '\'')
82 dim
.wid
+= static_cast<int>(0.1667*em
+0.5);
84 whichFont(font_
, code_
, mi
);
85 dim
= theFontMetrics(font_
).dimension(char_
);
86 if (isBinaryOp(char_
, code_
))
87 dim
.wid
+= 2 * theFontMetrics(font_
).width(' ');
88 lyxerr
<< "InsetMathChar::metrics: " << dim
<< endl
;
93 void InsetMathChar::draw(PainterInfo
& pi
, int x
, int y
) const
95 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
96 int const em
= mathed_char_width(pi
.base
.font
, 'M');
97 if (isBinaryOp(char_
))
98 x
+= static_cast<int>(0.25*em
+0.5);
99 else if (char_
== '\'')
100 x
+= static_cast<int>(0.0833*em
+0.5);
102 if (char_
== '=' && has_math_fonts
) {
103 FontSetChanger
dummy(pi
.base
, "cmr");
104 pi
.draw(x
, y
, char_
);
105 } else if ((char_
== '>' || char_
== '<') && has_math_fonts
) {
106 FontSetChanger
dummy(pi
.base
, "cmm");
107 pi
.draw(x
, y
, char_
);
108 } else if (!slanted(char_
) && pi
.base
.fontname
== "mathnormal") {
109 ShapeChanger
dummy(pi
.base
.font
, UP_SHAPE
);
110 pi
.draw(x
, y
, char_
);
112 pi
.draw(x
, y
, char_
);
115 drawChar(pain
, font_
, x
, y
, char_
);
120 void InsetMathChar::metricsT(TextMetricsInfo
const &, Dimension
& dim
) const
128 void InsetMathChar::drawT(TextPainter
& pain
, int x
, int y
) const
130 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
131 pain
.draw(x
, y
, char_
);
135 void InsetMathChar::write(WriteStream
& os
) const
141 void InsetMathChar::validate(LaTeXFeatures
& features
) const
144 encodings
.validate(char_
, features
, true);
148 void InsetMathChar::normalize(NormalStream
& os
) const
156 void InsetMathChar::octave(OctaveStream
& os
) const
162 void InsetMathChar::mathmlize(MathStream
& ms
) const
165 case '<': ms
<< "<"; break;
166 case '>': ms
<< ">"; break;
167 case '&': ms
<< "&"; break;
168 default: ms
.os().put(char_
); break;
173 bool InsetMathChar::isRelOp() const
175 return char_
== '=' || char_
== '<' || char_
== '>';