]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBig.cpp
Force a Buffer * argument to math insets constructor
[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(Buffer * buf, docstring const & name, docstring const & delim)
32         : InsetMath(buf), 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 << MTagInline("mo", "fence='true' stretchy='true' symmetric='true'")
141            << convertDelimToXMLEscape(delim_)
142            << ETagInline("mo");
143 }
144
145
146 void InsetMathBig::htmlize(HtmlStream & os) const
147 {
148         std::string name;
149         switch (size()) {
150         case 0: case 1: name = "big"; break;
151         case 2: case 3: name = "bigg"; break;
152         case 4: case 5: name = "biggg"; break;
153         default: name  = "big"; break;
154         }
155         os << MTag("span", "class='" + name + "symbol'")
156            << convertDelimToXMLEscape(delim_)
157            << ETag("span");
158 }
159
160
161 void InsetMathBig::infoize2(odocstream & os) const
162 {
163         os << name_;
164 }
165
166
167 bool InsetMathBig::isBigInsetDelim(docstring const & delim)
168 {
169         // mathed_draw_deco must handle these
170         static char const * const delimiters[] = {
171                 "(", ")", "\\{", "\\}", "\\lbrace", "\\rbrace", "[", "]",
172                 "|", "/", "\\slash", "\\|", "\\vert", "\\Vert", "'",
173                 "<", ">", "\\\\", "\\backslash",
174                 "\\langle", "\\lceil", "\\lfloor",
175                 "\\rangle", "\\rceil", "\\rfloor",
176                 "\\llbracket", "\\rrbracket",
177                 "\\downarrow", "\\Downarrow",
178                 "\\uparrow", "\\Uparrow",
179                 "\\updownarrow", "\\Updownarrow", ""
180         };
181         return support::findToken(delimiters, to_utf8(delim)) >= 0;
182 }
183
184
185 void InsetMathBig::validate(LaTeXFeatures & features) const
186 {
187         validate_math_word(features, word());
188         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
189                 features.addCSSSnippet(
190                         "span.bigsymbol{font-size: 150%;}\n"
191                         "span.biggsymbol{font-size: 200%;}\n"
192                         "span.bigggsymbol{font-size: 225%;}");
193 }
194
195
196 } // namespace lyx