]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathBig.cpp
Simplify Changers interface
[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 Inset * InsetMathBig::clone() const
43 {
44         return new InsetMathBig(*this);
45 }
46
47
48 InsetMathBig::size_type InsetMathBig::size() const
49 {
50         // order: big Big bigg Bigg biggg Biggg
51         //        0   1   2    3    4     5
52         char_type const c = name_[name_.size() - 1];
53         int const base_size = (c == 'l' || c == 'm' || c == 'r') ? 4 : 3;
54         return name_[0] == 'B' ?
55                 2 * (name_.size() - base_size) + 1:
56                 2 * (name_.size() - base_size);
57 }
58
59
60 double InsetMathBig::increase() const
61 {
62         // The formula used in amsmath.sty is
63         // 1.2 * (1.0 + size() * 0.5) - 1.0.
64         // We use a smaller step and a bigger offset because our base size
65         // is different.
66         return (size() + 1) * 0.3;
67 }
68
69
70 void InsetMathBig::metrics(MetricsInfo & mi, Dimension & dim) const
71 {
72         double const h = theFontMetrics(mi.base.font).ascent('I');
73         double const f = increase();
74         dim.wid = 6;
75         dim.asc = int(h + f * h);
76         dim.des = int(f * h);
77 }
78
79
80 void InsetMathBig::draw(PainterInfo & pi, int x, int y) const
81 {
82         Dimension const dim = dimension(*pi.base.bv);
83         // mathed_draw_deco does not use the leading backslash, so remove it
84         // (but don't use ltrim if this is the backslash delimiter).
85         // Replace \| by \Vert (equivalent in LaTeX), since mathed_draw_deco
86         // would treat it as |.
87         docstring const delim = (delim_ == "\\|") ?  from_ascii("Vert") :
88                 (delim_ == "\\\\") ? from_ascii("\\") : support::ltrim(delim_, "\\");
89         mathed_draw_deco(pi, x + 1, y - dim.ascent(), 4, dim.height(),
90                          delim);
91         setPosCache(pi, x, y);
92 }
93
94
95 void InsetMathBig::write(WriteStream & os) const
96 {
97         MathEnsurer ensurer(os);
98         os << '\\' << name_ << delim_;
99         if (delim_[0] == '\\')
100                 os.pendingSpace(true);
101 }
102
103
104 void InsetMathBig::normalize(NormalStream & os) const
105 {
106         os << '[' << name_ << ' ' << delim_ << ']';
107 }
108
109
110 void InsetMathBig::mathmlize(MathStream & os) const
111 {
112         os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>"
113            << convertDelimToXMLEscape(delim_)
114            << "</mo>";
115 }
116
117
118 void InsetMathBig::htmlize(HtmlStream & os) const
119 {
120         std::string name;
121         switch (size()) {
122         case 0: case 1: name = "big"; break;
123         case 2: case 3: name = "bigg"; break;
124         case 4: case 5: name = "biggg"; break;
125         default: name  = "big"; break;
126         }
127         os << MTag("span", "class='" + name + "symbol'")
128            << convertDelimToXMLEscape(delim_)
129            << ETag("span");
130 }
131
132
133 void InsetMathBig::infoize2(odocstream & os) const
134 {
135         os << name_;
136 }
137
138
139 bool InsetMathBig::isBigInsetDelim(docstring const & delim)
140 {
141         // mathed_draw_deco must handle these
142         static char const * const delimiters[] = {
143                 "(", ")", "\\{", "\\}", "\\lbrace", "\\rbrace", "[", "]",
144                 "|", "/", "\\slash", "\\|", "\\vert", "\\Vert", "'",
145                 "<", ">", "\\\\", "\\backslash",
146                 "\\langle", "\\lceil", "\\lfloor",
147                 "\\rangle", "\\rceil", "\\rfloor",
148                 "\\downarrow", "\\Downarrow",
149                 "\\uparrow", "\\Uparrow",
150                 "\\updownarrow", "\\Updownarrow", ""
151         };
152         return support::findToken(delimiters, to_utf8(delim)) >= 0;
153 }
154
155
156 void InsetMathBig::validate(LaTeXFeatures & features) const
157 {
158         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
159                 features.addCSSSnippet(
160                         "span.bigsymbol{font-size: 150%;}\n"
161                         "span.biggsymbol{font-size: 200%;}\n"
162                         "span.bigggsymbol{font-size: 225%;}");
163 }
164
165
166 } // namespace lyx