]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSpecialChar.cpp
Remove unneccessary uses of dynamic_cast from the code.
[lyx.git] / src / mathed / InsetMathSpecialChar.cpp
1 /**
2  * \file InsetMathSpecialChar.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Enrico Forestieri
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathSpecialChar.h"
14
15 #include "MathSupport.h"
16 #include "MathStream.h"
17 #include "MetricsInfo.h"
18
19 #include "Dimension.h"
20 #include "LaTeXFeatures.h"
21 #include "TextPainter.h"
22
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
25
26 #include "support/lassert.h"
27
28
29 namespace lyx {
30
31
32 InsetMathSpecialChar::InsetMathSpecialChar(docstring const & name)
33         : name_(name), kerning_(0)
34 {
35         if (name.size() != 1) {
36                 if (name == "textasciicircum" || name == "mathcircumflex")
37                         char_ = '^';
38                 else if (name == "textasciitilde")
39                         char_ = '~';
40                 else if (name == "textbackslash")
41                         char_ = '\\';
42                 else
43                         LASSERT(false, /**/);
44         } else
45                 char_ = name.at(0);
46 }
47
48
49
50 Inset * InsetMathSpecialChar::clone() const
51 {
52         return new InsetMathSpecialChar(*this);
53 }
54
55
56 void InsetMathSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
57 {
58         if (char_ == ' ') {
59                 dim.asc = 4;
60                 dim.des = 0;
61                 dim.wid = 10;
62         } else if (mi.base.fontname == "mathnormal") {
63                 ShapeChanger dummy(mi.base.font, UP_SHAPE);
64                 dim = theFontMetrics(mi.base.font).dimension(char_);
65         } else {
66                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
67                 dim = fm.dimension(char_);
68                 kerning_ = fm.rbearing(char_) - dim.wid;
69         }
70 }
71
72
73 void InsetMathSpecialChar::draw(PainterInfo & pi, int x, int y) const
74 {
75         if (char_ == ' ') {
76                 int xp[4];
77                 int yp[4];
78                 int w = 10;
79
80                 xp[0] = ++x;        yp[0] = y - 3;
81                 xp[1] = x;          yp[1] = y;
82                 xp[2] = x + w - 2;  yp[2] = y;
83                 xp[3] = x + w - 2;  yp[3] = y - 3;
84
85                 pi.pain.lines(xp, yp, 4, Color_special);
86         } else if (pi.base.fontname == "mathnormal") {
87                 ShapeChanger dummy(pi.base.font, UP_SHAPE);
88                 pi.draw(x, y, char_);
89         } else {
90                 pi.draw(x, y, char_);
91         }
92 }
93
94
95 void InsetMathSpecialChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
96 {
97         dim.wid = 1;
98         dim.asc = 1;
99         dim.des = 0;
100 }
101
102
103 void InsetMathSpecialChar::drawT(TextPainter & pain, int x, int y) const
104 {
105         pain.draw(x, y, char_);
106 }
107
108
109 void InsetMathSpecialChar::write(WriteStream & os) const
110 {
111         os << '\\' << name_;
112         if (name_.size() != 1)
113                 os.pendingSpace(true);
114 }
115
116
117 void InsetMathSpecialChar::validate(LaTeXFeatures & features) const
118 {
119         if (name_ == "mathcircumflex")
120                 features.require("mathcircumflex");
121 }
122
123
124 void InsetMathSpecialChar::normalize(NormalStream & os) const
125 {
126         os << "[char ";
127         os.os().put(char_);
128         os << " mathalpha]";
129 }
130
131
132 void InsetMathSpecialChar::maple(MapleStream & os) const
133 {
134         os.os().put(char_);
135 }
136
137
138 void InsetMathSpecialChar::mathematica(MathematicaStream & os) const
139 {
140         os.os().put(char_);
141 }
142
143
144 void InsetMathSpecialChar::octave(OctaveStream & os) const
145 {
146         os.os().put(char_);
147 }
148
149
150 void InsetMathSpecialChar::mathmlize(MathStream & ms) const
151 {
152         switch (char_) {
153         case '&':
154                 ms << "&amp;";
155                 break;
156         default:
157                 ms.os().put(char_);
158                 break;
159         }
160 }
161
162
163 void InsetMathSpecialChar::htmlize(HtmlStream & ms) const
164 {
165         switch (char_) {
166         case '&':
167                 ms << "&amp;";
168                 break;
169         default:
170                 ms.os().put(char_);
171                 break;
172         }
173 }
174
175
176 } // namespace lyx