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 if (key_->extra == "forcetext")
67 void InsetMathFont::write(WriteStream & os) const
69 // Close the mode changing command inserted during export if
70 // we are going to output another mode changing command that
71 // actually doesn't change mode. This avoids exporting things
72 // such as \ensuremath{a\mathit{b}} or \textit{a\text{b}} and
73 // produce instead \ensuremath{a}\mathit{b} and \textit{a}\text{b}.
75 && ((currentMode() == TEXT_MODE && os.textMode())
76 || (currentMode() == MATH_MODE && !os.textMode()))) {
78 os.pendingBrace(false);
79 os.textMode(!os.textMode());
81 InsetMathNest::write(os);
85 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
87 Changer dummy = mi.base.changeFontSet(font());
88 cell(0).metrics(mi, dim);
93 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
95 Changer dummy = pi.base.changeFontSet(font());
96 cell(0).draw(pi, x + 1, y);
97 drawMarkers(pi, x, y);
98 setPosCache(pi, x, y);
102 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
106 cell(0).metricsT(mi, dim);
110 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
112 cell(0).drawT(pain, x, y);
116 docstring InsetMathFont::name() const
122 void InsetMathFont::validate(LaTeXFeatures & features) const
124 InsetMathNest::validate(features);
125 std::string fontname = font();
126 if (features.runparams().isLaTeX()) {
127 // Make sure amssymb is put in preamble if Blackboard Bold or
129 if (fontname == "mathfrak" || fontname == "mathbb")
130 features.require("amssymb");
131 if (fontname == "text" || fontname == "textnormal"
132 || (fontname.length() == 6 && fontname.substr(0, 4) == "text"))
133 features.require("amstext");
134 if (fontname == "mathscr")
135 features.require("mathrsfs");
136 if (fontname == "textipa")
137 features.require("tipa");
138 if (fontname == "ce" || fontname == "cf")
139 features.require("mhchem");
140 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
141 features.addCSSSnippet(
142 "span.normal{font: normal normal normal inherit serif;}\n"
143 "span.fraktur{font: normal normal normal inherit cursive;}\n"
144 "span.bold{font: normal normal bold inherit serif;}\n"
145 "span.script{font: normal normal normal inherit cursive;}\n"
146 "span.italic{font: italic normal normal inherit serif;}\n"
147 "span.sans{font: normal normal normal inherit sans-serif;}\n"
148 "span.monospace{font: normal normal normal inherit monospace;}\n"
149 "span.noun{font: normal small-caps normal inherit normal;}");
154 // The fonts we want to support are listed in lib/symbols
155 void InsetMathFont::htmlize(HtmlStream & os) const
157 // FIXME These are not quite right, because they do not nest
158 // correctly. A proper fix would presumably involve tracking
159 // the fonts already in effect.
161 docstring const & tag = key_->name;
162 if (tag == "mathnormal" || tag == "mathrm"
163 || tag == "text" || tag == "textnormal"
164 || tag == "textrm" || tag == "textup"
167 else if (tag == "frak" || tag == "mathfrak")
169 else if (tag == "mathbb" || tag == "mathbf"
172 else if (tag == "mathcal")
174 else if (tag == "mathit" || tag == "textsl"
175 || tag == "emph" || tag == "textit")
177 else if (tag == "mathsf" || tag == "textsf")
179 else if (tag == "mathtt" || tag == "texttt")
180 variant = "monospace";
181 else if (tag == "textipa" || tag == "textsc" || tag == "noun")
184 docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
185 if (!variant.empty()) {
186 os << MTag("span", "class='" + variant + "'")
194 // The fonts we want to support are listed in lib/symbols
195 void InsetMathFont::mathmlize(MathStream & os) const
197 // FIXME These are not quite right, because they do not nest
198 // correctly. A proper fix would presumably involve tracking
199 // the fonts already in effect.
201 docstring const & tag = key_->name;
202 if (tag == "mathnormal" || tag == "mathrm"
203 || tag == "text" || tag == "textnormal"
204 || tag == "textrm" || tag == "textup"
207 else if (tag == "frak" || tag == "mathfrak")
209 else if (tag == "mathbb" || tag == "mathbf"
212 else if (tag == "mathcal")
214 else if (tag == "mathit" || tag == "textsl"
215 || tag == "emph" || tag == "textit")
217 else if (tag == "mathsf" || tag == "textsf")
218 variant = "sans-serif";
219 else if (tag == "mathtt" || tag == "texttt")
220 variant = "monospace";
221 // no support at present for textipa, textsc, noun
223 if (!variant.empty()) {
224 if (tag == "mathbb") {
225 os << MTag("mstyle", "class='mathbb' mathvariant='" + variant + "'")
229 os << MTag("mstyle", "mathvariant='" + variant + "'")
238 void InsetMathFont::infoize(odocstream & os) const
240 os << bformat(_("Font: %1$s"), key_->name);