]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
whichFont down to 5.3%
[lyx.git] / src / mathed / math_scriptinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_scriptinset.h"
6 #include "math_support.h"
7 #include "math_symbolinset.h"
8 #include "math_mathmlstream.h"
9 #include "support/LAssert.h"
10 #include "debug.h"
11
12
13 MathScriptInset::MathScriptInset()
14         : MathNestInset(2), limits_(0)
15 {
16         script_[0] = false;
17         script_[1] = false;
18 }
19
20
21 MathScriptInset::MathScriptInset(bool up)
22         : MathNestInset(2), limits_(0)
23 {
24         script_[0] = !up;
25         script_[1] = up;
26 }
27
28
29 MathInset * MathScriptInset::clone() const
30 {
31         return new MathScriptInset(*this);
32 }
33
34
35 MathScriptInset const * MathScriptInset::asScriptInset() const
36 {
37         return this;
38 }
39
40
41 MathScriptInset * MathScriptInset::asScriptInset()
42 {
43         return this;
44 }
45
46
47 MathXArray const & MathScriptInset::up() const
48 {
49         return xcell(1);
50 }
51
52
53 MathXArray const & MathScriptInset::down() const
54 {
55         return xcell(0);
56 }
57
58
59 MathXArray & MathScriptInset::up()
60 {
61         return xcell(1);
62 }
63
64
65 MathXArray & MathScriptInset::down()
66 {
67         return xcell(0);
68 }
69
70
71 void MathScriptInset::ensure(bool up)
72 {
73         script_[up] = true;
74 }
75
76
77 int MathScriptInset::dy0(MathInset const * nuc) const
78 {
79         int nd = ndes(nuc);
80         if (!hasDown())
81                 return nd;
82         int des = down().ascent();
83         if (hasLimits(nuc))
84                 des += nd + 2;
85         else 
86                 des = std::max(des, nd);
87         return des;
88 }
89
90
91 int MathScriptInset::dy1(MathInset const * nuc) const
92 {
93         int na = nasc(nuc);
94         if (!hasUp())
95                 return na;
96         int asc = up().descent();
97         if (hasLimits(nuc))
98                 asc += na + 2;
99         else 
100                 asc = std::max(asc, na);
101         asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
102         return asc;
103 }
104
105
106 int MathScriptInset::dx0(MathInset const * nuc) const
107 {
108         lyx::Assert(hasDown());
109         return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
110 }
111
112
113 int MathScriptInset::dx1(MathInset const * nuc) const
114 {
115         lyx::Assert(hasUp());
116         return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
117 }
118
119
120 int MathScriptInset::dxx(MathInset const * nuc) const
121 {
122         //lyx::Assert(nuc());
123         return hasLimits(nuc)  ?  (width2(nuc) - nwid(nuc)) / 2  :  0;
124 }
125
126
127 int MathScriptInset::ascent2(MathInset const * nuc) const
128 {
129         return dy1(nuc) + (hasUp() ? up().ascent() : 0);
130 }
131
132
133 int MathScriptInset::descent2(MathInset const * nuc) const
134 {
135         return dy0(nuc) + (hasDown() ? down().descent() : 0);
136 }
137
138
139 int MathScriptInset::width2(MathInset const * nuc) const
140 {
141         int wid = 0;
142         if (hasLimits(nuc)) {
143                 wid = nwid(nuc);
144                 if (hasUp())
145                         wid = std::max(wid, up().width());
146                 if (hasDown())
147                         wid = std::max(wid, down().width());
148         } else {
149                 if (hasUp())
150                         wid = std::max(wid, up().width());
151                 if (hasDown())
152                         wid = std::max(wid, down().width());
153                 wid += nwid(nuc);
154         }
155         return wid;
156 }
157
158
159 int MathScriptInset::nwid(MathInset const * nuc) const
160 {
161         return nuc ?
162                 nuc->width() :
163                 mathed_char_width(LM_TC_TEX, mi_, '.');
164 }
165
166
167 int MathScriptInset::nasc(MathInset const * nuc) const
168 {
169         return nuc ? nuc->ascent()
170                 : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
171 }
172
173
174 int MathScriptInset::ndes(MathInset const * nuc) const
175 {
176         return nuc ? nuc->descent()
177                 : mathed_char_descent(LM_TC_VAR, mi_, 'I');
178 }
179
180
181 void MathScriptInset::metrics(MathMetricsInfo const & mi) const
182 {       
183         metrics(0, mi);
184 }
185
186
187 void MathScriptInset::metrics(MathInset const * nuc,
188         MathMetricsInfo const & mi) const
189 {       
190         mi_ = mi;
191         smallerStyleScript(mi_);
192         MathNestInset::metrics(mi_);
193         if (nuc)
194                 nuc->metrics(mi);
195         ascent_  = ascent2(nuc);
196         descent_ = descent2(nuc);
197         width_   = width2(nuc);
198 }
199
200
201 void MathScriptInset::draw(Painter & pain, int x, int y) const
202 {  
203         //lyxerr << "unexpected call to MathScriptInset::draw()\n";
204         draw(0, pain, x, y);
205 }
206
207
208 void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
209         int x, int y) const
210 {  
211         if (nuc)
212                 nuc->draw(pain, x + dxx(nuc), y);
213         else if (editing())
214                 drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
215
216         if (hasUp())
217                 up().draw(pain, x + dx1(nuc), y - dy1(nuc));
218
219         if (hasDown())
220                 down().draw(pain, x + dx0(nuc), y + dy0(nuc));
221 }
222
223
224 bool MathScriptInset::hasLimits(MathInset const * nuc) const
225 {
226         // obviuos cases
227         if (limits_ == 1)
228                 return true;
229         if (limits_ == -1)
230                 return false;
231
232         // we can only display limits if the nucleus wants some 
233         if (!nuc)
234                 return false;
235         if (!nuc->isScriptable())
236                 return false;
237         
238         // per default \int has limits beside the \int even in displayed formulas
239         if (nuc->asSymbolInset())
240                 if (nuc->asSymbolInset()->name().find("int") != string::npos)
241                         return false;
242
243         // assume "real" limits for everything else
244         return true;
245 }
246
247
248 void MathScriptInset::removeEmptyScripts()
249 {
250         for (int i = 0; i <= 1; ++i)
251                 if (script_[i] && !cell(i).size())
252                         script_[i] = false;
253 }
254
255
256 void MathScriptInset::removeScript(bool up)
257 {
258         cell(up).clear();
259         script_[up] = false;
260 }
261
262
263 bool MathScriptInset::has(bool up) const
264 {
265         return script_[up];
266 }
267
268
269 bool MathScriptInset::empty() const
270 {
271         return !script_[0] && !script_[1];
272 }
273
274
275 bool MathScriptInset::hasUp() const
276 {
277         return script_[1];
278 }
279
280
281 bool MathScriptInset::hasDown() const
282 {
283         return script_[0];
284 }
285
286
287 bool MathScriptInset::idxRight(MathInset::idx_type &,
288                                  MathInset::pos_type &) const
289 {
290         return false;
291 }
292
293
294 bool MathScriptInset::idxLeft(MathInset::idx_type &,
295                                 MathInset::pos_type &) const
296 {
297         return false;
298 }
299
300
301 void MathScriptInset::write(WriteStream & os) const
302 {  
303         //lyxerr << "unexpected call to MathScriptInset::write()\n";
304         write2(0, os);
305 }
306
307
308 void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
309 {
310         if (nuc) {
311                 os << nuc;
312                 if (nuc->takesLimits()) {
313                         if (limits_ == -1)
314                                 os << "\\nolimits ";
315                         if (limits_ == 1)
316                                 os << "\\limits ";
317                 }
318         } else
319                         if (os.firstitem())
320                                 lyxerr << "suppressing {} \n";
321                         else
322                                 os << "{}";
323
324         if (hasDown() && down().data_.size())
325                 os << "_{" << down().data_ << '}';
326
327         if (hasUp() && up().data_.size())
328                 os << "^{" << up().data_ << '}';
329 }
330
331
332 void MathScriptInset::normalize(NormalStream & os) const
333 {  
334         //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
335         normalize2(0, os);
336 }
337
338
339 void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
340 {
341         bool d = hasDown() && down().data_.size();
342         bool u = hasUp() && up().data_.size();
343
344         if (u) 
345                 os << "[sup ";
346         if (d)
347                 os << "[sub ";
348         
349         if (nuc)
350                 os << nuc << ' ';
351         else
352                 os << "[par]";
353
354         if (d)
355                 os << down().data_ << ']';
356         if (u) 
357                 os << up().data_ << ']';
358 }
359
360
361 void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
362 {
363         if (nuc)
364                 os << nuc;
365         if (hasDown() && down().data_.size())
366                 os << '[' << down().data_ << ']';
367         if (hasUp() && up().data_.size())
368                 os << "^(" << up().data_ << ')';
369 }
370
371
372 void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
373 {
374         bool d = hasDown() && down().data_.size();
375         bool u = hasUp() && up().data_.size();
376
377         if (u && d)
378                 os << MTag("msubsup");
379         else if (u)
380                 os << MTag("msup");
381         else if (d)
382                 os << MTag("msub");
383
384         if (nuc)
385                 os << nuc;
386         else
387                 os << "<mrow/>";
388
389         if (u && d)
390                 os << down().data_ << up().data_ << ETag("msubsup");
391         else if (u)
392                 os << up().data_ << ETag("msup");
393         else if (d)
394                 os << down().data_ << ETag("msub");
395 }
396
397
398 void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
399 {
400         if (nuc)
401                 os << nuc;
402         if (hasDown() && down().data_.size())
403                 os << '[' << down().data_ << ']';
404         if (hasUp() && up().data_.size())
405                 os << "^(" << up().data_ << ')';
406 }
407
408