]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.C
This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for...
[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/Application.h"
24 #include "frontends/FontLoader.h"
25 #include "frontends/FontMetrics.h"
26
27 using std::auto_ptr;
28
29 extern bool has_math_fonts;
30
31 namespace {
32
33         bool isBinaryOp(char c)
34         {
35                 return lyx::support::contains("+-<>=/*", c);
36         }
37
38
39         bool slanted(char c)
40         {
41                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
42         }
43
44 }
45
46
47 InsetMathChar::InsetMathChar(char c)
48         : char_(c)
49 {}
50
51
52
53 auto_ptr<InsetBase> InsetMathChar::doClone() const
54 {
55         return auto_ptr<InsetBase>(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                 mathed_char_dim(mi.base.font, char_, dim);
65         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
66                 FontSetChanger dummy(mi.base, "cmm");
67                 mathed_char_dim(mi.base.font, char_, dim);
68         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
69                 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
70                 mathed_char_dim(mi.base.font, char_, dim);
71         } else {
72                 mathed_char_dim(mi.base.font, char_, dim);
73         }
74         int const em = mathed_char_width(mi.base.font, 'M');
75         if (isBinaryOp(char_))
76                 dim.wid += static_cast<int>(0.5*em+0.5);
77         else if (char_ == '\'')
78                 dim.wid += static_cast<int>(0.1667*em+0.5);
79 #else
80         whichFont(font_, code_, mi);
81         mathed_char_dim(font_, char_, dim_);
82         if (isBinaryOp(char_, code_))
83                 width_ += 2 * theApp->fontLoader().metrics(font_).width(' ');
84         lyxerr << "InsetMathChar::metrics: " << dim << endl;
85 #endif
86         width_ = dim.wid;
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, LyXFont::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 << char_;
135 }
136
137
138 void InsetMathChar::normalize(NormalStream & os) const
139 {
140         os << "[char " << char_ << " mathalpha]";
141 }
142
143
144 void InsetMathChar::octave(OctaveStream & os) const
145 {
146         os << char_;
147 }
148
149
150 void InsetMathChar::mathmlize(MathMLStream & ms) const
151 {
152         switch (char_) {
153                 case '<': ms << "&lt;"; break;
154                 case '>': ms << "&gt;"; break;
155                 case '&': ms << "&amp;"; break;
156                 default: ms << char_; break;
157         }
158 }
159
160
161 bool InsetMathChar::isRelOp() const
162 {
163         return char_ == '=' || char_ == '<' || char_ == '>';
164 }