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