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::metrics(MetricsInfo & mi, Dimension & dim) const
69 Changer dummy = mi.base.changeFontSet(font());
70 cell(0).metrics(mi, dim);
75 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
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);
84 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
88 cell(0).metricsT(mi, dim);
92 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
94 cell(0).drawT(pain, x, y);
98 docstring InsetMathFont::name() const
104 void InsetMathFont::validate(LaTeXFeatures & features) const
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
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;}");
136 // The fonts we want to support are listed in lib/symbols
137 void InsetMathFont::htmlize(HtmlStream & os) const
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.
143 docstring const & tag = key_->name;
144 if (tag == "mathnormal" || tag == "mathrm"
145 || tag == "text" || tag == "textnormal"
146 || tag == "textrm" || tag == "textup"
149 else if (tag == "frak" || tag == "mathfrak")
151 else if (tag == "mathbb" || tag == "mathbf"
154 else if (tag == "mathcal")
156 else if (tag == "mathit" || tag == "textsl"
157 || tag == "emph" || tag == "textit")
159 else if (tag == "mathsf" || tag == "textsf")
161 else if (tag == "mathtt" || tag == "texttt")
162 variant = "monospace";
163 else if (tag == "textipa" || tag == "textsc" || tag == "noun")
166 docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
167 if (!variant.empty()) {
168 os << MTag("span", "class='" + variant + "'")
176 // The fonts we want to support are listed in lib/symbols
177 void InsetMathFont::mathmlize(MathStream & os) const
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.
183 docstring const & tag = key_->name;
184 if (tag == "mathnormal" || tag == "mathrm"
185 || tag == "text" || tag == "textnormal"
186 || tag == "textrm" || tag == "textup"
189 else if (tag == "frak" || tag == "mathfrak")
191 else if (tag == "mathbb" || tag == "mathbf"
194 else if (tag == "mathcal")
196 else if (tag == "mathit" || tag == "textsl"
197 || tag == "emph" || tag == "textit")
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
205 if (!variant.empty()) {
206 if (tag == "mathbb") {
207 os << MTag("mstyle", "class='mathbb' mathvariant='" + variant + "'")
211 os << MTag("mstyle", "mathvariant='" + variant + "'")
220 void InsetMathFont::infoize(odocstream & os) const
222 os << bformat(_("Font: %1$s"), key_->name);