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