]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSpecialChar.cpp
Get rid of superfluous conversions and else statement.
[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
25 #include "support/lassert.h"
26
27
28 namespace lyx {
29
30
31 InsetMathSpecialChar::InsetMathSpecialChar(docstring name)
32         : name_(name), kerning_(0)
33 {
34         if (name.size() != 1) {
35                 if (name == "textasciicircum" || name == "mathcircumflex")
36                         char_ = '^';
37                 else if (name == "textasciitilde")
38                         char_ = '~';
39                 else if (name == "textbackslash")
40                         char_ = '\\';
41                 else
42                         LASSERT(false, /**/);
43         } else
44                 char_ = name.at(0);
45 }
46
47
48
49 Inset * InsetMathSpecialChar::clone() const
50 {
51         return new InsetMathSpecialChar(*this);
52 }
53
54
55 void InsetMathSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
56 {
57         if (mi.base.fontname == "mathnormal") {
58                 ShapeChanger dummy(mi.base.font, UP_SHAPE);
59                 dim = theFontMetrics(mi.base.font).dimension(char_);
60         } else {
61                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
62                 dim = fm.dimension(char_);
63                 kerning_ = fm.rbearing(char_) - dim.wid;
64         }
65 }
66
67
68 void InsetMathSpecialChar::draw(PainterInfo & pi, int x, int y) const
69 {
70         if (pi.base.fontname == "mathnormal") {
71                 ShapeChanger dummy(pi.base.font, UP_SHAPE);
72                 pi.draw(x, y, char_);
73         } else {
74                 pi.draw(x, y, char_);
75         }
76 }
77
78
79 void InsetMathSpecialChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
80 {
81         dim.wid = 1;
82         dim.asc = 1;
83         dim.des = 0;
84 }
85
86
87 void InsetMathSpecialChar::drawT(TextPainter & pain, int x, int y) const
88 {
89         pain.draw(x, y, char_);
90 }
91
92
93 void InsetMathSpecialChar::write(WriteStream & os) const
94 {
95         os << '\\' << name_;
96         if (name_.size() != 1)
97                 os.pendingSpace(true);
98 }
99
100
101 void InsetMathSpecialChar::validate(LaTeXFeatures & features) const
102 {
103         if (name_ == "mathcircumflex")
104                 features.require("mathcircumflex");
105 }
106
107
108 void InsetMathSpecialChar::normalize(NormalStream & os) const
109 {
110         os << "[char ";
111         os.os().put(char_);
112         os << " mathalpha]";
113 }
114
115
116 void InsetMathSpecialChar::maple(MapleStream & os) const
117 {
118         os.os().put(char_);
119 }
120
121
122 void InsetMathSpecialChar::mathematica(MathematicaStream & os) const
123 {
124         os.os().put(char_);
125 }
126
127
128 void InsetMathSpecialChar::octave(OctaveStream & os) const
129 {
130         os.os().put(char_);
131 }
132
133
134 void InsetMathSpecialChar::mathmlize(MathStream & ms) const
135 {
136         switch (char_) {
137         case '&':
138                 ms << "&amp;";
139                 break;
140         default:
141                 ms.os().put(char_);
142                 break;
143         }
144 }
145
146
147 } // namespace lyx