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