]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
try to fix rounding errors
[lyx.git] / src / mathed / math_symbolinset.C
1 #include <config.h>
2
3 #include "math_symbolinset.h"
4 #include "math_mathmlstream.h"
5 #include "math_streamstr.h"
6 #include "math_support.h"
7 #include "math_parser.h"
8 #include "LaTeXFeatures.h"
9 #include "debug.h"
10
11
12 using std::ostream;
13
14
15 MathSymbolInset::MathSymbolInset(const latexkeys * l)
16         : sym_(l), h_(0)
17 {}
18
19
20 MathSymbolInset::MathSymbolInset(const char * name)
21         : sym_(in_word_set(name)), h_(0)
22 {}
23
24
25 MathSymbolInset::MathSymbolInset(string const & name)
26         : sym_(in_word_set(name.c_str())), h_(0)
27 {}
28
29
30
31 MathInset * MathSymbolInset::clone() const
32 {
33         return new MathSymbolInset(*this);
34 }
35
36
37 string MathSymbolInset::name() const
38 {
39         return sym_->name;
40 }
41
42
43 void MathSymbolInset::metrics(MathMetricsInfo & mi) const
44 {
45         //lyxerr << "metrics: symbol: '" << sym_->name
46         //      << "' in font: '" << sym_->inset
47         //      << "' drawn as: '" << sym_->draw
48         //      << "'\n";
49         MathFontSetChanger dummy(mi.base, sym_->inset.c_str());
50         mathed_string_dim(mi.base.font, sym_->draw, ascent_, descent_, width_);
51         if (sym_->inset == "cmex") {
52                 h_ = 4 * descent_ / 5;
53                 ascent_  += h_;
54                 descent_ -= h_;
55         }
56         if (isRelOp())
57                 width_ += 6;
58         scriptable_ = (mi.base.style == LM_ST_DISPLAY && sym_->inset == "cmex");
59 }
60
61
62 void MathSymbolInset::draw(MathPainterInfo & pi, int x, int y) const
63 {
64         //lyxerr << "metrics: symbol: '" << sym_->name
65         //      << "' in font: '" << sym_->inset
66         //      << "' drawn as: '" << sym_->draw
67         //      << "'\n";
68         if (isRelOp())
69                 x += 3;
70         MathFontSetChanger dummy(pi.base, sym_->inset.c_str());
71         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
72 }
73
74
75 bool MathSymbolInset::isRelOp() const
76 {
77         return sym_->extra == "mathrel";
78 }
79
80
81 bool MathSymbolInset::isScriptable() const
82 {
83         return scriptable_;
84 }
85
86
87 bool MathSymbolInset::takesLimits() const
88 {
89         return sym_->inset == "cmex" || sym_->inset == "lyxboldsymb";
90 }
91
92
93 void MathSymbolInset::validate(LaTeXFeatures & features) const
94 {
95         if (sym_->inset == "msa" || sym_->inset == "msb")
96                 features.require("amssymb");
97 }
98
99
100 void MathSymbolInset::normalize(NormalStream & os) const
101 {
102         os << "[symbol " << name() << "]";
103 }
104
105
106 void MathSymbolInset::maplize(MapleStream & os) const
107 {
108         if (name() == "cdot")
109                 os << '*';
110         else
111                 os << name();
112 }
113
114
115 char const * MathMLtype(string const & s)
116 {
117         if (s == "mathop")
118                 return "mo";
119         return "mi";
120 }
121
122
123 bool MathSymbolInset::match(MathInset * p) const
124 {
125         MathSymbolInset const * q = p->asSymbolInset();
126         return q && name() == q->name();
127 }
128
129
130 void MathSymbolInset::mathmlize(MathMLStream & os) const
131 {
132         char const * type = MathMLtype(sym_->extra);
133         os << '<' << type << "> ";
134         if (sym_->xmlname == "x") // unknown so far
135                 os << name();
136         else
137                 os << sym_->xmlname;
138         os << " </" << type << '>';
139 }
140
141
142 void MathSymbolInset::octavize(OctaveStream & os) const
143 {
144         if (name() == "cdot")
145                 os << '*';
146         else
147                 os << name();
148 }
149
150
151 void MathSymbolInset::write(WriteStream & os) const
152 {
153         os << '\\' << name() << ' ';
154 }
155
156
157 void MathSymbolInset::infoize(ostream & os) const
158 {
159         os << '\\' << name();
160 }