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