]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFont.cpp
Cmake build: Adapt to compile after 0213e7b5c62f487bdddd95ff0b2de736f64b1dc3
[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/lstrings.h"
23
24 #include <ostream>
25
26 using namespace lyx::support;
27
28 namespace lyx {
29
30 InsetMathFont::InsetMathFont(Buffer * buf, latexkeys const * key)
31         : InsetMathNest(buf, 1), key_(key)
32 {}
33
34
35 Inset * InsetMathFont::clone() const
36 {
37         return new InsetMathFont(*this);
38 }
39
40
41 InsetMath::mode_type InsetMathFont::currentMode() const
42 {
43         if (key_->extra == "mathmode")
44                 return MATH_MODE;
45         if (key_->extra == "textmode")
46                 return TEXT_MODE;
47         return UNDECIDED_MODE;
48 }
49
50
51 bool InsetMathFont::lockedMode() const
52 {
53         if (key_->extra == "forcetext")
54                 return true;
55         return false;
56 }
57
58
59 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
60 {
61         FontSetChanger dummy(mi.base, key_->name);
62         cell(0).metrics(mi, dim);
63         metricsMarkers(dim);
64 }
65
66
67 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
68 {
69         FontSetChanger dummy(pi.base, key_->name);
70         cell(0).draw(pi, x + 1, y);
71         drawMarkers(pi, x, y);
72         setPosCache(pi, x, y);
73 }
74
75
76 void InsetMathFont::metricsT(TextMetricsInfo const & mi, Dimension &) const
77 {
78         // FIXME: BROKEN!
79         Dimension dim;
80         cell(0).metricsT(mi, dim);
81 }
82
83
84 void InsetMathFont::drawT(TextPainter & pain, int x, int y) const
85 {
86         cell(0).drawT(pain, x, y);
87 }
88
89
90 docstring InsetMathFont::name() const
91 {
92         return key_->name;
93 }
94
95
96 void InsetMathFont::validate(LaTeXFeatures & features) const
97 {
98         InsetMathNest::validate(features);
99         if (features.runparams().isLaTeX()) {
100                 // Make sure amssymb is put in preamble if Blackboard Bold or
101                 // Fraktur used:
102                 if (key_->name == "mathfrak" || key_->name == "mathbb")
103                         features.require("amssymb");
104                 if (key_->name == "text" || key_->name == "textnormal"
105                                 || (key_->name.length() == 6 && key_->name.substr(0, 4) == "text"))
106                         features.require("amstext");
107                 if (key_->name == "mathscr")
108                         features.require("mathrsfs");
109                 if (key_->name == "textipa")
110                         features.require("tipa");
111                 if (key_->name == "ce" || key_->name == "cf")
112                         features.require("mhchem");
113         } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
114                 features.addCSSSnippet(
115                         "span.normal{font: normal normal normal inherit serif;}\n"
116                         "span.fraktur{font: normal normal normal inherit cursive;}\n"
117                         "span.bold{font: normal normal bold inherit serif;}\n"
118                         "span.script{font: normal normal normal inherit cursive;}\n"
119                         "span.italic{font: italic normal normal inherit serif;}\n"
120                         "span.sans{font: normal normal normal inherit sans-serif;}\n"
121                         "span.monospace{font: normal normal normal inherit monospace;}\n"
122                         "span.noun{font: normal small-caps normal inherit normal;}");
123         }
124 }
125
126
127 // The fonts we want to support are listed in lib/symbols
128 void InsetMathFont::htmlize(HtmlStream & os) const
129 {
130         // FIXME These are not quite right, because they do not nest
131         // correctly. A proper fix would presumably involve tracking
132         // the fonts already in effect.
133         std::string variant;
134         docstring const & tag = key_->name;
135         if (tag == "mathnormal" || tag == "mathrm"
136             || tag == "text" || tag == "textnormal"
137             || tag == "textrm" || tag == "textup"
138             || tag == "textmd")
139                 variant = "normal";
140         else if (tag == "frak" || tag == "mathfrak")
141                 variant = "fraktur";
142         else if (tag == "mathbb" || tag == "mathbf"
143                  || tag == "textbf")
144                 variant = "bold";
145         else if (tag == "mathcal")
146                 variant = "script";
147         else if (tag == "mathit" || tag == "textsl"
148                  || tag == "emph" || tag == "textit")
149                 variant = "italic";
150         else if (tag == "mathsf" || tag == "textsf")
151                 variant = "sans";
152         else if (tag == "mathtt" || tag == "texttt")
153                 variant = "monospace";
154         else if (tag == "textipa" || tag == "textsc" || tag == "noun")
155                 variant = "noun";
156
157         docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
158         if (!variant.empty()) {
159                 os << MTag("span", "class='" + variant + "'")
160                    << cell(0)
161                    << ETag("span");
162         } else
163                 os << cell(0);
164 }
165
166
167 // The fonts we want to support are listed in lib/symbols
168 void InsetMathFont::mathmlize(MathStream & os) const
169 {
170         // FIXME These are not quite right, because they do not nest
171         // correctly. A proper fix would presumably involve tracking
172         // the fonts already in effect.
173         std::string variant;
174         docstring const & tag = key_->name;
175         if (tag == "mathnormal" || tag == "mathrm"
176             || tag == "text" || tag == "textnormal"
177             || tag == "textrm" || tag == "textup"
178             || tag == "textmd")
179                 variant = "normal";
180         else if (tag == "frak" || tag == "mathfrak")
181                 variant = "fraktur";
182         else if (tag == "mathbb" || tag == "mathbf"
183                  || tag == "textbf")
184                 variant = "bold";
185         else if (tag == "mathcal")
186                 variant = "script";
187         else if (tag == "mathit" || tag == "textsl"
188                  || tag == "emph" || tag == "textit")
189                 variant = "italic";
190         else if (tag == "mathsf" || tag == "textsf")
191                 variant = "sans-serif";
192         else if (tag == "mathtt" || tag == "texttt")
193                 variant = "monospace";
194         // no support at present for textipa, textsc, noun
195
196         if (!variant.empty()) {
197                 os << MTag("mstyle", "mathvariant='" + variant + "'")
198                    << cell(0)
199                    << ETag("mstyle");
200         } else
201                 os << cell(0);
202 }
203
204
205 void InsetMathFont::infoize(odocstream & os) const
206 {
207         os << bformat(_("Font: %1$s"), key_->name);
208 }
209
210
211 } // namespace lyx