]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFont.cpp
Use SetMode() to manage text and math mode.
[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         // Make sure amssymb is put in preamble if Blackboard Bold or
96         // Fraktur used:
97         if (key_->name == "mathfrak" || key_->name == "mathbb")
98                 features.require("amssymb");
99         if (key_->name == "text" || key_->name == "textnormal"
100             || (key_->name.length() == 6 && key_->name.substr(0, 4) == "text"))
101                 features.require("amstext");
102         if (key_->name == "textipa")
103                 features.require("tipa");
104         if (key_->name == "ce" || key_->name == "cf")
105                 features.require("mhchem");
106 }
107
108
109 // The fonts we want to support are listed in lib/symbols
110 void InsetMathFont::mathmlize(MathStream & os) const
111 {
112         // FIXME These are not quite right, because they do not nest
113         // correctly. A proper fix would presumably involve tracking
114         // the fonts already in effect.
115         std::string variant;
116         docstring const & tag = key_->name;
117         if (tag == "mathnormal" || tag == "mathrm"
118             || tag == "text" || tag == "textnormal"
119             || tag == "textrm" || tag == "textup"
120             || tag == "textmd")
121                 variant = "normal";
122         else if (tag == "frak" || tag == "mathfrak")
123                 variant = "fraktur";
124         else if (tag == "mathbb" || tag == "mathbf"
125                  || tag == "textbf")
126                 variant = "bold";
127         else if (tag == "mathcal")
128                 variant == "script";
129         else if (tag == "mathit" || tag == "textsl"
130                  || tag == "emph")
131                 variant = "italic";
132         else if (tag == "mathsf" || tag == "textit"
133                  || tag == "textsf")
134                 variant = "sans-serif";
135         else if (tag == "mathtt" || tag == "texttt")
136                 variant = "monospace";
137         // no support at present for textipa, textsc, noun
138         
139         // FIXME We need some kind of "mode tracker", so we can
140         // just output verbatim text in some cases.
141         docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
142         bool const textmode = (beg == "text");
143         if (!variant.empty()) {
144                 os << "<mstyle mathvariant='" << from_utf8(variant) << "'>";
145                 SetMode sm(os, textmode);
146                 os << cell(0);
147                 os << "</mstyle>";
148         }
149 }
150
151
152 void InsetMathFont::infoize(odocstream & os) const
153 {
154         os << "Font: " << key_->name;
155 }
156
157
158 } // namespace lyx