]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFont.cpp
Fix bug #8782.
[lyx.git] / src / mathed / InsetMathFont.cpp
1 /**
2  * \file InsetMathFont.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathFont.h"
14
15 #include "LaTeXFeatures.h"
16 #include "MathData.h"
17 #include "MathStream.h"
18 #include "MathParser.h"
19 #include "MetricsInfo.h"
20
21 #include "support/gettext.h"
22 #include "support/lassert.h"
23 #include "support/lstrings.h"
24
25 #include <ostream>
26
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 InsetMathFont::InsetMathFont(Buffer * buf, latexkeys const * key)
32         : InsetMathNest(buf, 1), key_(key)
33 {}
34
35
36 Inset * InsetMathFont::clone() const
37 {
38         return new InsetMathFont(*this);
39 }
40
41
42 std::string InsetMathFont::font() const
43 {
44         LASSERT(isAscii(key_->name), return "mathnormal");
45         return to_ascii(key_->name);
46 }
47
48
49 InsetMath::mode_type InsetMathFont::currentMode() const
50 {
51         if (key_->extra == "mathmode")
52                 return MATH_MODE;
53         if (key_->extra == "textmode")
54                 return TEXT_MODE;
55         return UNDECIDED_MODE;
56 }
57
58
59 bool InsetMathFont::lockedMode() const
60 {
61         if (key_->extra == "forcetext")
62                 return true;
63         return false;
64 }
65
66
67 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
68 {
69         Changer dummy = mi.base.changeFontSet(font());
70         cell(0).metrics(mi, dim);
71         metricsMarkers(dim);
72 }
73
74
75 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
76 {
77         Changer dummy = pi.base.changeFontSet(font());
78         cell(0).draw(pi, x + 1, y);
79         drawMarkers(pi, x, y);
80         setPosCache(pi, x, y);
81 }
82
83
84 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
85 {
86         // FIXME: BROKEN!
87         Dimension dim;
88         cell(0).metricsT(mi, dim);
89 }
90
91
92 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
93 {
94         cell(0).drawT(pain, x, y);
95 }
96
97
98 docstring InsetMathFont::name() const
99 {
100         return key_->name;
101 }
102
103
104 void InsetMathFont::validate(LaTeXFeatures & features) const
105 {
106         InsetMathNest::validate(features);
107         std::string fontname = font();
108         if (features.runparams().isLaTeX()) {
109                 // Make sure amssymb is put in preamble if Blackboard Bold or
110                 // Fraktur used:
111                 if (fontname == "mathfrak" || fontname == "mathbb")
112                         features.require("amssymb");
113                 if (fontname == "text" || fontname == "textnormal"
114                     || (fontname.length() == 6 && fontname.substr(0, 4) == "text"))
115                         features.require("amstext");
116                 if (fontname == "mathscr")
117                         features.require("mathrsfs");
118                 if (fontname == "textipa")
119                         features.require("tipa");
120                 if (fontname == "ce" || fontname == "cf")
121                         features.require("mhchem");
122         } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
123                 features.addCSSSnippet(
124                         "span.normal{font: normal normal normal inherit serif;}\n"
125                         "span.fraktur{font: normal normal normal inherit cursive;}\n"
126                         "span.bold{font: normal normal bold inherit serif;}\n"
127                         "span.script{font: normal normal normal inherit cursive;}\n"
128                         "span.italic{font: italic normal normal inherit serif;}\n"
129                         "span.sans{font: normal normal normal inherit sans-serif;}\n"
130                         "span.monospace{font: normal normal normal inherit monospace;}\n"
131                         "span.noun{font: normal small-caps normal inherit normal;}");
132         }
133 }
134
135
136 // The fonts we want to support are listed in lib/symbols
137 void InsetMathFont::htmlize(HtmlStream & os) const
138 {
139         // FIXME These are not quite right, because they do not nest
140         // correctly. A proper fix would presumably involve tracking
141         // the fonts already in effect.
142         std::string variant;
143         docstring const & tag = key_->name;
144         if (tag == "mathnormal" || tag == "mathrm"
145             || tag == "text" || tag == "textnormal"
146             || tag == "textrm" || tag == "textup"
147             || tag == "textmd")
148                 variant = "normal";
149         else if (tag == "frak" || tag == "mathfrak")
150                 variant = "fraktur";
151         else if (tag == "mathbb" || tag == "mathbf"
152                  || tag == "textbf")
153                 variant = "bold";
154         else if (tag == "mathcal")
155                 variant = "script";
156         else if (tag == "mathit" || tag == "textsl"
157                  || tag == "emph" || tag == "textit")
158                 variant = "italic";
159         else if (tag == "mathsf" || tag == "textsf")
160                 variant = "sans";
161         else if (tag == "mathtt" || tag == "texttt")
162                 variant = "monospace";
163         else if (tag == "textipa" || tag == "textsc" || tag == "noun")
164                 variant = "noun";
165
166         docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
167         if (!variant.empty()) {
168                 os << MTag("span", "class='" + variant + "'")
169                    << cell(0)
170                    << ETag("span");
171         } else
172                 os << cell(0);
173 }
174
175
176 // The fonts we want to support are listed in lib/symbols
177 void InsetMathFont::mathmlize(MathStream & os) const
178 {
179         // FIXME These are not quite right, because they do not nest
180         // correctly. A proper fix would presumably involve tracking
181         // the fonts already in effect.
182         std::string variant;
183         docstring const & tag = key_->name;
184         if (tag == "mathnormal" || tag == "mathrm"
185             || tag == "text" || tag == "textnormal"
186             || tag == "textrm" || tag == "textup"
187             || tag == "textmd")
188                 variant = "normal";
189         else if (tag == "frak" || tag == "mathfrak")
190                 variant = "fraktur";
191         else if (tag == "mathbb" || tag == "mathbf"
192                  || tag == "textbf")
193                 variant = "bold";
194         else if (tag == "mathcal")
195                 variant = "script";
196         else if (tag == "mathit" || tag == "textsl"
197                  || tag == "emph" || tag == "textit")
198                 variant = "italic";
199         else if (tag == "mathsf" || tag == "textsf")
200                 variant = "sans-serif";
201         else if (tag == "mathtt" || tag == "texttt")
202                 variant = "monospace";
203         // no support at present for textipa, textsc, noun
204
205         if (!variant.empty()) {
206                 if (tag == "mathbb") {
207                         os << MTag("mstyle", "class='mathbb' mathvariant='" + variant + "'")
208                            << cell(0)
209                            << ETag("mstyle");
210                 } else {
211                         os << MTag("mstyle", "mathvariant='" + variant + "'")
212                                  << cell(0)
213                                  << ETag("mstyle");
214                 }
215         } else
216                 os << cell(0);
217 }
218
219
220 void InsetMathFont::infoize(odocstream & os) const
221 {
222         os << bformat(_("Font: %1$s"), key_->name);
223 }
224
225
226 } // namespace lyx