]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBig.cpp
091907e5171b8d416d38a724cbbac0158b391352
[lyx.git] / src / mathed / InsetMathBig.cpp
1 /**
2  * \file InsetMathBig.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 "InsetMathBig.h"
14
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
17
18 #include "MathSupport.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
21
22 #include "frontends/FontMetrics.h"
23
24 #include "support/docstream.h"
25 #include "support/lstrings.h"
26
27
28 namespace lyx {
29
30
31 InsetMathBig::InsetMathBig(docstring const & name, docstring const & delim)
32         : name_(name), delim_(delim)
33 {}
34
35
36 docstring InsetMathBig::name() const
37 {
38         return name_;
39 }
40
41
42 MathClass InsetMathBig::mathClass() const
43 {
44         /* The class of the delimiter depends on the type (l, m, r, nothing).
45          * For example, the definition of \bigl in LaTeX sources is
46          * \def\bigl{\mathopen\big}
47          */
48         switch(name_.back()) {
49         case 'l':
50                 return MC_OPEN;
51         case 'm':
52                 return MC_REL;
53         case 'r':
54                 return MC_CLOSE;
55         default:
56                 return MC_ORD;
57         }
58 }
59
60
61 Inset * InsetMathBig::clone() const
62 {
63         return new InsetMathBig(*this);
64 }
65
66
67 size_type InsetMathBig::size() const
68 {
69         // order: big Big bigg Bigg biggg Biggg
70         //        0   1   2    3    4     5
71         char_type const c = name_.back();
72         int const base_size = (c == 'l' || c == 'm' || c == 'r') ? 4 : 3;
73         return name_.front() == 'B' ?
74                 2 * (name_.size() - base_size) + 1:
75                 2 * (name_.size() - base_size);
76 }
77
78
79 double InsetMathBig::increase() const
80 {
81         // The formula used in amsmath.sty is
82         // 1.2 * (1.0 + size() * 0.5) - 1.0.
83         // We use a smaller step and a bigger offset because our base size
84         // is different.
85         return (size() + 1) * 0.3;
86 }
87
88
89 void InsetMathBig::metrics(MetricsInfo & mi, Dimension & dim) const
90 {
91         Changer dummy = mi.base.changeEnsureMath();
92         double const h = theFontMetrics(mi.base.font).ascent('I');
93         double const f = increase();
94         dim.wid = 6;
95         dim.asc = int(h + f * h);
96         dim.des = int(f * h);
97 }
98
99
100 docstring InsetMathBig::word() const
101 {
102         // mathed_draw_deco does not use the leading backslash, so remove it
103         // (but don't use ltrim if this is the backslash delimiter).
104         // Replace \| by \Vert (equivalent in LaTeX), since mathed_draw_deco
105         // would treat it as |.
106         if (delim_ == "\\|")
107                 return from_ascii("Vert");
108         if (delim_ == "\\\\")
109                 return from_ascii("\\");
110         return support::ltrim(delim_, "\\");
111 }
112
113
114 void InsetMathBig::draw(PainterInfo & pi, int x, int y) const
115 {
116         Changer dummy = pi.base.changeEnsureMath();
117         Dimension const dim = dimension(*pi.base.bv);
118         mathed_draw_deco(pi, x + 1, y - dim.ascent(), 4, dim.height(),
119                          word());
120 }
121
122
123 void InsetMathBig::write(TeXMathStream & os) const
124 {
125         MathEnsurer ensurer(os);
126         os << '\\' << name_ << delim_;
127         if (delim_[0] == '\\')
128                 os.pendingSpace(true);
129 }
130
131
132 void InsetMathBig::normalize(NormalStream & os) const
133 {
134         os << '[' << name_ << ' ' << delim_ << ']';
135 }
136
137
138 void InsetMathBig::mathmlize(MathMLStream & ms) const
139 {
140         ms << "<" << from_ascii(ms.namespacedTag("mo"))
141            << " form='prefix' fence='true' stretchy='true' symmetric='true'>"
142            << convertDelimToXMLEscape(delim_, ms.xmlMode())
143            << "</" << from_ascii(ms.namespacedTag("mo")) << ">";
144 }
145
146
147 void InsetMathBig::htmlize(HtmlStream & os) const
148 {
149         std::string name;
150         switch (size()) {
151         case 0: case 1: name = "big"; break;
152         case 2: case 3: name = "bigg"; break;
153         case 4: case 5: name = "biggg"; break;
154         default: name  = "big"; break;
155         }
156         os << MTag("span", "class='" + name + "symbol'")
157            << convertDelimToXMLEscape(delim_, false)
158            << ETag("span");
159 }
160
161
162 void InsetMathBig::infoize2(odocstream & os) const
163 {
164         os << name_;
165 }
166
167
168 bool InsetMathBig::isBigInsetDelim(docstring const & delim)
169 {
170         // mathed_draw_deco must handle these
171         static char const * const delimiters[] = {
172                 "(", ")", "\\{", "\\}", "\\lbrace", "\\rbrace", "[", "]",
173                 "|", "/", "\\slash", "\\|", "\\vert", "\\Vert", "'",
174                 "<", ">", "\\\\", "\\backslash",
175                 "\\langle", "\\lceil", "\\lfloor",
176                 "\\rangle", "\\rceil", "\\rfloor",
177                 "\\llbracket", "\\rrbracket",
178                 "\\downarrow", "\\Downarrow",
179                 "\\uparrow", "\\Uparrow",
180                 "\\updownarrow", "\\Updownarrow", ""
181         };
182         return support::findToken(delimiters, to_utf8(delim)) >= 0;
183 }
184
185
186 void InsetMathBig::validate(LaTeXFeatures & features) const
187 {
188         validate_math_word(features, word());
189         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
190                 features.addCSSSnippet(
191                         "span.bigsymbol{font-size: 150%;}\n"
192                         "span.biggsymbol{font-size: 200%;}\n"
193                         "span.bigggsymbol{font-size: 225%;}");
194 }
195
196
197 } // namespace lyx