]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFont.cpp
7a1a18d919ee82569e965ad6a7a4aac573941ee0
[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         return key_->extra == "forcetext";
62 }
63
64
65 void InsetMathFont::write(TeXMathStream & os) const
66 {
67         // Close the mode changing command inserted during export if
68         // we are going to output another mode changing command that
69         // actually doesn't change mode. This avoids exporting things
70         // such as \ensuremath{a\mathit{b}} or \textit{a\text{b}} and
71         // produce instead \ensuremath{a}\mathit{b} and \textit{a}\text{b}.
72         if (os.pendingBrace()
73             && ((currentMode() == TEXT_MODE && os.textMode())
74                     || (currentMode() == MATH_MODE && !os.textMode()))) {
75                 os.os() << '}';
76                 os.pendingBrace(false);
77                 os.textMode(!os.textMode());
78         }
79         InsetMathNest::write(os);
80 }
81
82
83 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
84 {
85         Changer dummy = mi.base.changeFontSet(font());
86         cell(0).metrics(mi, dim);
87 }
88
89
90 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
91 {
92         Changer dummy = pi.base.changeFontSet(font());
93         cell(0).draw(pi, x, y);
94 }
95
96
97 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
98 {
99         // FIXME: BROKEN!
100         Dimension dim;
101         cell(0).metricsT(mi, dim);
102 }
103
104
105 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
106 {
107         cell(0).drawT(pain, x, y);
108 }
109
110
111 docstring InsetMathFont::name() const
112 {
113         return key_->name;
114 }
115
116
117 void InsetMathFont::validate(LaTeXFeatures & features) const
118 {
119         InsetMathNest::validate(features);
120         std::string fontname = font();
121         if (features.runparams().isLaTeX()) {
122                 // Make sure amssymb is put in preamble if Blackboard Bold or
123                 // Fraktur used:
124                 if (fontname == "mathfrak" || fontname == "mathbb")
125                         features.require("amssymb");
126                 if (fontname == "text" || fontname == "textnormal"
127                     || (fontname.length() == 6 && fontname.substr(0, 4) == "text"))
128                         features.require("amstext");
129                 if (fontname == "mathscr")
130                         features.require("mathrsfs");
131                 if (fontname == "textipa")
132                         features.require("tipa");
133                 if (fontname == "ce" || fontname == "cf")
134                         features.require("mhchem");
135                 if (fontname == "mathds")
136                         features.require("dsfont");
137         } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
138                 features.addCSSSnippet(
139                         "span.normal{font: normal normal normal inherit serif;}\n"
140                         "span.fraktur{font: normal normal normal inherit cursive;}\n"
141                         "span.bold{font: normal normal bold inherit serif;}\n"
142                         "span.script{font: normal normal normal inherit cursive;}\n"
143                         "span.italic{font: italic normal normal inherit serif;}\n"
144                         "span.sans{font: normal normal normal inherit sans-serif;}\n"
145                         "span.monospace{font: normal normal normal inherit monospace;}\n"
146                         "span.noun{font: normal small-caps normal inherit normal;}");
147         }
148 }
149
150
151 // The fonts we want to support are listed in lib/symbols
152 void InsetMathFont::htmlize(HtmlStream & os) const
153 {
154         // FIXME These are not quite right, because they do not nest
155         // correctly. A proper fix would presumably involve tracking
156         // the fonts already in effect.
157         std::string variant;
158         docstring const & tag = key_->name;
159         if (tag == "mathnormal" || tag == "mathrm"
160             || tag == "text" || tag == "textnormal"
161             || tag == "textrm" || tag == "textup"
162             || tag == "textmd")
163                 variant = "normal";
164         else if (tag == "frak" || tag == "mathfrak")
165                 variant = "fraktur";
166         else if (tag == "mathbf" || tag == "textbf")
167                 variant = "bold";
168         else if (tag == "mathbb" || tag == "mathbbm"
169                  || tag == "mathds")
170                 variant = "double-struck";
171         else if (tag == "mathcal")
172                 variant = "script";
173         else if (tag == "mathit" || tag == "textsl"
174                  || tag == "emph" || tag == "textit")
175                 variant = "italic";
176         else if (tag == "mathsf" || tag == "textsf")
177                 variant = "sans";
178         else if (tag == "mathtt" || tag == "texttt")
179                 variant = "monospace";
180         else if (tag == "textipa" || tag == "textsc" || tag == "noun")
181                 variant = "noun";
182
183         docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
184         if (!variant.empty()) {
185                 os << MTag("span", "class='" + variant + "'")
186                    << cell(0)
187                    << ETag("span");
188         } else
189                 os << cell(0);
190 }
191
192
193 // The fonts we want to support are listed in lib/symbols
194 void InsetMathFont::mathmlize(MathMLStream & ms) const
195 {
196         // FIXME These are not quite right, because they do not nest
197         // correctly. A proper fix would presumably involve tracking
198         // the fonts already in effect.
199         std::string variant;
200         docstring const & tag = key_->name;
201         if (tag == "mathnormal" || tag == "mathrm")
202                 variant = "normal";
203         else if (tag == "frak" || tag == "mathfrak")
204                 variant = "fraktur";
205         else if (tag == "mathbf" || tag == "textbf")
206                 variant = "bold";
207         else if (tag == "mathbb" || tag == "mathbbm" || tag == "mathds")
208                 variant = "double-struck";
209         else if (tag == "mathcal")
210                 variant = "script";
211         else if (tag == "mathit" || tag == "textsl" || tag == "emph" ||
212                         tag == "textit")
213                 variant = "italic";
214         else if (tag == "mathsf" || tag == "textsf")
215                 variant = "sans-serif";
216         else if (tag == "mathtt" || tag == "texttt")
217                 variant = "monospace";
218         // no support at present for textipa, textsc, noun
219
220         if (tag == "text" || tag == "textnormal" || tag == "textrm" ||
221                         tag == "textup" || tag == "textmd") {
222                 SetMode textmode(ms, true);
223                 ms << MTagInline("mtext");
224                 ms << cell(0);
225                 ms << ETagInline("mtext");
226         } else if (!variant.empty()) {
227                 ms << MTag("mstyle", "mathvariant='" + variant + "'");
228                 ms << cell(0);
229                 ms << ETag("mstyle");
230         } else {
231                 ms << cell(0);
232         }
233 }
234
235
236 void InsetMathFont::infoize(odocstream & os) const
237 {
238         os << bformat(_("Font: %1$s"), key_->name);
239 }
240
241
242 } // namespace lyx