2 * \file InsetMathFont.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathFont.h"
15 #include "LaTeXFeatures.h"
17 #include "MathStream.h"
18 #include "MathParser.h"
19 #include "MetricsInfo.h"
21 #include "support/gettext.h"
22 #include "support/lassert.h"
23 #include "support/lstrings.h"
27 using namespace lyx::support;
31 InsetMathFont::InsetMathFont(Buffer * buf, latexkeys const * key)
32 : InsetMathNest(buf, 1), key_(key)
36 Inset * InsetMathFont::clone() const
38 return new InsetMathFont(*this);
42 std::string InsetMathFont::font() const
44 LASSERT(isAscii(key_->name), return "mathnormal");
45 return to_ascii(key_->name);
49 InsetMath::mode_type InsetMathFont::currentMode() const
51 if (key_->extra == "mathmode")
53 if (key_->extra == "textmode")
55 return UNDECIDED_MODE;
59 bool InsetMathFont::lockedMode() const
61 return key_->extra == "forcetext";
65 void InsetMathFont::write(TeXMathStream & os) const
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}.
73 && ((currentMode() == TEXT_MODE && os.textMode())
74 || (currentMode() == MATH_MODE && !os.textMode()))) {
76 os.pendingBrace(false);
77 os.textMode(!os.textMode());
79 InsetMathNest::write(os);
83 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
85 Changer dummy = mi.base.changeFontSet(font());
86 cell(0).metrics(mi, dim);
90 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
92 Changer dummy = pi.base.changeFontSet(font());
93 cell(0).draw(pi, x, y);
97 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
101 cell(0).metricsT(mi, dim);
105 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
107 cell(0).drawT(pain, x, y);
111 docstring InsetMathFont::name() const
117 void InsetMathFont::validate(LaTeXFeatures & features) const
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
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;}");
151 // The fonts we want to support are listed in lib/symbols
152 void InsetMathFont::htmlize(HtmlStream & os) const
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.
158 docstring const & tag = key_->name;
159 if (tag == "mathnormal" || tag == "mathrm"
160 || tag == "text" || tag == "textnormal"
161 || tag == "textrm" || tag == "textup"
164 else if (tag == "frak" || tag == "mathfrak")
166 else if (tag == "mathbf" || tag == "textbf")
168 else if (tag == "mathbb" || tag == "mathbbm"
170 variant = "double-struck";
171 else if (tag == "mathcal")
173 else if (tag == "mathit" || tag == "textsl"
174 || tag == "emph" || tag == "textit")
176 else if (tag == "mathsf" || tag == "textsf")
178 else if (tag == "mathtt" || tag == "texttt")
179 variant = "monospace";
180 else if (tag == "textipa" || tag == "textsc" || tag == "noun")
183 docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
184 if (!variant.empty()) {
185 os << MTag("span", "class='" + variant + "'")
193 // The fonts we want to support are listed in lib/symbols
194 void InsetMathFont::mathmlize(MathMLStream & ms) const
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.
200 docstring const & tag = key_->name;
201 if (tag == "mathnormal" || tag == "mathrm"
202 || tag == "text" || tag == "textnormal"
203 || tag == "textrm" || tag == "textup"
206 else if (tag == "frak" || tag == "mathfrak")
208 else if (tag == "mathbf" || tag == "textbf")
210 else if (tag == "mathbb" || tag == "mathbbm"
212 variant = "double-struck";
213 else if (tag == "mathcal")
215 else if (tag == "mathit" || tag == "textsl"
216 || tag == "emph" || tag == "textit")
218 else if (tag == "mathsf" || tag == "textsf")
219 variant = "sans-serif";
220 else if (tag == "mathtt" || tag == "texttt")
221 variant = "monospace";
222 // no support at present for textipa, textsc, noun
224 if (!variant.empty())
225 ms << MTag("mstyle", "mathvariant='" + variant + "'")
233 void InsetMathFont::infoize(odocstream & os) const
235 os << bformat(_("Font: %1$s"), key_->name);