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