]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
InsetMathHull.cpp: whitespace
[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 "MathSupport.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
19
20 #include "Dimension.h"
21 #include "Encoding.h"
22 #include "LaTeXFeatures.h"
23 #include "TextPainter.h"
24
25 #include "frontends/FontMetrics.h"
26
27 #include "support/debug.h"
28 #include "support/lstrings.h"
29
30
31 namespace lyx {
32
33 extern bool has_math_fonts;
34
35
36 static bool isBinaryOp(char_type c)
37 {
38         return support::contains("+-<>=/*", static_cast<char>(c));
39 }
40
41
42 static bool slanted(char_type c)
43 {
44         return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
45 }
46
47
48 InsetMathChar::InsetMathChar(char_type c)
49         : char_(c), kerning_(0)
50 {}
51
52
53
54 Inset * InsetMathChar::clone() const
55 {
56         return new InsetMathChar(*this);
57 }
58
59
60 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
61 {
62 #if 1
63         if (char_ == '=' && has_math_fonts) {
64                 FontSetChanger dummy(mi.base, "cmr");
65                 dim = theFontMetrics(mi.base.font).dimension(char_);
66         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
67                 FontSetChanger dummy(mi.base, "cmm");
68                 dim = theFontMetrics(mi.base.font).dimension(char_);
69         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
70                 ShapeChanger dummy(mi.base.font, UP_SHAPE);
71                 dim = theFontMetrics(mi.base.font).dimension(char_);
72         } else {
73                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
74                 dim = fm.dimension(char_);
75                 kerning_ = fm.rbearing(char_) - dim.wid;
76         }
77         int const em = mathed_char_width(mi.base.font, 'M');
78         if (isBinaryOp(char_))
79                 dim.wid += static_cast<int>(0.5*em+0.5);
80         else if (char_ == '\'')
81                 dim.wid += static_cast<int>(0.1667*em+0.5);
82 #else
83         whichFont(font_, code_, mi);
84         dim = theFontMetrics(font_).dimension(char_);
85         if (isBinaryOp(char_, code_))
86                 dim.wid += 2 * theFontMetrics(font_).width(' ');
87         lyxerr << "InsetMathChar::metrics: " << dim << endl;
88 #endif
89 }
90
91
92 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
93 {
94         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
95         int const em = mathed_char_width(pi.base.font, 'M');
96         if (isBinaryOp(char_))
97                 x += static_cast<int>(0.25*em+0.5);
98         else if (char_ == '\'')
99                 x += static_cast<int>(0.0833*em+0.5);
100 #if 1
101         if (char_ == '=' && has_math_fonts) {
102                 FontSetChanger dummy(pi.base, "cmr");
103                 pi.draw(x, y, char_);
104         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
105                 FontSetChanger dummy(pi.base, "cmm");
106                 pi.draw(x, y, char_);
107         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
108                 ShapeChanger dummy(pi.base.font, UP_SHAPE);
109                 pi.draw(x, y, char_);
110         } else {
111                 pi.draw(x, y, char_);
112         }
113 #else
114         drawChar(pain, font_, x, y, char_);
115 #endif
116 }
117
118
119 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
120 {
121         dim.wid = 1;
122         dim.asc = 1;
123         dim.des = 0;
124 }
125
126
127 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
128 {
129         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
130         pain.draw(x, y, char_);
131 }
132
133
134 void InsetMathChar::write(WriteStream & os) const
135 {
136         os.os().put(char_);
137 }
138
139
140 void InsetMathChar::validate(LaTeXFeatures & features) const
141 {
142         if (char_ >= 0x80)
143                 encodings.validate(char_, features, true);
144 }
145
146
147 void InsetMathChar::normalize(NormalStream & os) const
148 {
149         os << "[char ";
150         os.os().put(char_);
151         os << " mathalpha]";
152 }
153
154
155 void InsetMathChar::octave(OctaveStream & os) const
156 {
157         os.os().put(char_);
158 }
159
160
161 void InsetMathChar::mathmlize(MathStream & ms) const
162 {
163         switch (char_) {
164                 case '<': ms << "&lt;"; break;
165                 case '>': ms << "&gt;"; break;
166                 case '&': ms << "&amp;"; break;
167                 default: ms.os().put(char_); break;
168         }
169 }
170
171
172 bool InsetMathChar::isRelOp() const
173 {
174         return char_ == '=' || char_ == '<' || char_ == '>';
175 }
176
177
178 } // namespace lyx