]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.cpp
Do some caching of window title and related UI
[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 "BufferEncodings.h"
22 #include "LaTeXFeatures.h"
23 #include "TextPainter.h"
24
25 #include "frontends/FontMetrics.h"
26
27 #include "support/debug.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
30 #include "support/textutils.h"
31
32
33 namespace lyx {
34
35 extern bool has_math_fonts;
36
37
38 static bool isBinaryOp(char_type c)
39 {
40         return support::contains("+-<>=/*", static_cast<char>(c));
41 }
42
43
44 static bool slanted(char_type c)
45 {
46         return isAlphaASCII(c) || Encodings::isMathAlpha(c);
47 }
48
49
50 InsetMathChar::InsetMathChar(char_type c)
51         : char_(c), kerning_(0)
52 {}
53
54
55
56 Inset * InsetMathChar::clone() const
57 {
58         return new InsetMathChar(*this);
59 }
60
61
62 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
63 {
64 #if 1
65         if (char_ == '=' && has_math_fonts) {
66                 FontSetChanger dummy(mi.base, "cmr");
67                 dim = theFontMetrics(mi.base.font).dimension(char_);
68         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
69                 FontSetChanger dummy(mi.base, "cmm");
70                 dim = theFontMetrics(mi.base.font).dimension(char_);
71         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
72                 ShapeChanger dummy(mi.base.font, UP_SHAPE);
73                 dim = theFontMetrics(mi.base.font).dimension(char_);
74         } else {
75                 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
76                 dim = fm.dimension(char_);
77                 kerning_ = fm.rbearing(char_) - dim.wid;
78         }
79         int const em = mathed_font_em(mi.base.font);
80         if (isBinaryOp(char_))
81                 dim.wid += support::iround(0.5 * em);
82         else if (char_ == '\'')
83                 dim.wid += support::iround(0.1667 * em);
84 #else
85         whichFont(font_, code_, mi);
86         dim = theFontMetrics(font_).dimension(char_);
87         if (isBinaryOp(char_, code_))
88                 dim.wid += 2 * theFontMetrics(font_).width(' ');
89         lyxerr << "InsetMathChar::metrics: " << dim << endl;
90 #endif
91 }
92
93
94 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
95 {
96         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
97         int const em = mathed_font_em(pi.base.font);
98         if (isBinaryOp(char_))
99                 x += support::iround(0.25 * em);
100         else if (char_ == '\'')
101                 x += support::iround(0.0833 * em);
102 #if 1
103         if (char_ == '=' && has_math_fonts) {
104                 FontSetChanger dummy(pi.base, "cmr");
105                 pi.draw(x, y, char_);
106         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
107                 FontSetChanger dummy(pi.base, "cmm");
108                 pi.draw(x, y, char_);
109         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
110                 ShapeChanger dummy(pi.base.font, UP_SHAPE);
111                 pi.draw(x, y, char_);
112         } else {
113                 pi.draw(x, y, char_);
114         }
115 #else
116         drawChar(pain, font_, x, y, char_);
117 #endif
118 }
119
120
121 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
122 {
123         dim.wid = 1;
124         dim.asc = 1;
125         dim.des = 0;
126 }
127
128
129 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
130 {
131         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
132         pain.draw(x, y, char_);
133 }
134
135
136 void InsetMathChar::write(WriteStream & os) const
137 {
138         os.os().put(char_);
139 }
140
141
142 void InsetMathChar::validate(LaTeXFeatures & features) const
143 {
144         if (!isASCII(char_))
145                 BufferEncodings::validate(char_, features, true);
146 }
147
148
149 void InsetMathChar::normalize(NormalStream & os) const
150 {
151         os << "[char ";
152         os.os().put(char_);
153         os << " mathalpha]";
154 }
155
156
157 void InsetMathChar::octave(OctaveStream & os) const
158 {
159         os.os().put(char_);
160 }
161
162
163 // We have a bit of a problem here. MathML wants to know whether the
164 // character represents an "identifier" or an "operator", and we have
165 // no general way of telling. So we shall guess: If it's alpha or 
166 // mathalpha, then we'll treat it as an identifier, otherwise as an 
167 // operator.
168 // Worst case: We get bad spacing, or bad italics.
169 void InsetMathChar::mathmlize(MathStream & ms) const
170 {
171         std::string entity;
172         switch (char_) {
173                 case '<': entity = "&lt;"; break;
174                 case '>': entity = "&gt;"; break;
175                 case '&': entity = "&amp;"; break;
176                 case ' ': {
177                         ms << from_ascii("&nbsp;");
178                         return;
179                 }
180                 default: break;
181         }
182         
183         if (ms.inText()) {
184                 if (entity.empty())
185                         ms.os().put(char_);
186                 else 
187                         ms << from_ascii(entity);
188                 return;
189         }
190
191         if (!entity.empty()) {
192                 ms << "<mo>" << from_ascii(entity) << "</mo>";
193                 return;
194         }               
195
196         char const * type = 
197                 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
198                         ? "mi" : "mo";
199         // we don't use MTag and ETag because we do not want the spacing
200         ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";    
201 }
202
203
204 void InsetMathChar::htmlize(HtmlStream & ms) const
205 {
206         std::string entity;
207         switch (char_) {
208                 case '<': entity = "&lt;"; break;
209                 case '>': entity = "&gt;"; break;
210                 case '&': entity = "&amp;"; break;
211                 case ' ': entity = "&nbsp;"; break;
212                 default: break;
213         }
214         
215         bool have_entity = !entity.empty();
216         
217         if (ms.inText()) {
218                 if (have_entity)
219                         ms << from_ascii(entity);
220                 else
221                         ms.os().put(char_);
222                 return;
223         }
224         
225         if (have_entity) {
226                 // an operator, so give some space
227                 ms << ' ' << from_ascii(entity) << ' ';
228                 return;
229         }               
230
231         if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
232                 // we don't use MTag and ETag because we do not want the spacing
233                 ms << MTag("i") << char_type(char_) << ETag("i");
234         else
235                 // an operator, so give some space
236                 ms << " " << char_type(char_) << " ";
237 }
238
239
240 bool InsetMathChar::isRelOp() const
241 {
242         return char_ == '=' || char_ == '<' || char_ == '>';
243 }
244
245
246 } // namespace lyx