]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
several fixes concerning font size in scripts/fractions/etc
[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 using std::max;
14
15
16 MathScriptInset::MathScriptInset()
17         : MathNestInset(3), limits_(0)
18 {
19         script_[0] = false;
20         script_[1] = false;
21 }
22
23
24 MathScriptInset::MathScriptInset(bool up)
25         : MathNestInset(3), limits_(0)
26 {
27         script_[0] = !up;
28         script_[1] = up;
29 }
30
31
32 MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
33         : MathNestInset(3), limits_(0)
34 {
35         script_[0] = !up;
36         script_[1] = up;
37         cell(2).push_back(at);
38 }
39
40
41
42 MathInset * MathScriptInset::clone() const
43 {
44         return new MathScriptInset(*this);
45 }
46
47
48 MathScriptInset const * MathScriptInset::asScriptInset() const
49 {
50         return this;
51 }
52
53
54 MathScriptInset * MathScriptInset::asScriptInset()
55 {
56         return this;
57 }
58
59
60 bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
61 {
62         idx = 2;
63         pos = 0;
64         return true;
65 }
66
67
68 bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
69 {
70         idx = 2;
71         pos = nuc().size();
72         return true;
73 }
74
75
76 MathArray const & MathScriptInset::down() const
77 {
78         return cell(0);
79 }
80
81
82 MathArray & MathScriptInset::down()
83 {
84         return cell(0);
85 }
86
87
88 MathArray const & MathScriptInset::up() const
89 {
90         return cell(1);
91 }
92
93
94 MathArray & MathScriptInset::up()
95 {
96         return cell(1);
97 }
98
99
100 void MathScriptInset::ensure(bool up)
101 {
102         script_[up] = true;
103 }
104
105
106 MathArray const & MathScriptInset::nuc() const
107 {
108         return cell(2);
109 }
110
111
112 MathArray & MathScriptInset::nuc()
113 {
114         return cell(2);
115 }
116
117
118 int MathScriptInset::dy0() const
119 {
120         int nd = ndes();
121         if (!hasDown())
122                 return nd;
123         int des = down().ascent();
124         if (hasLimits())
125                 des += nd + 2;
126         else
127                 des = max(des, nd);
128         return des;
129 }
130
131
132 int MathScriptInset::dy1() const
133 {
134         int na = nasc();
135         if (!hasUp())
136                 return na;
137         int asc = up().descent();
138         if (hasLimits())
139                 asc += na + 2;
140         else
141                 asc = max(asc, na);
142         asc = max(asc, 5);
143         return asc;
144 }
145
146
147 int MathScriptInset::dx0() const
148 {
149         lyx::Assert(hasDown());
150         return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
151 }
152
153
154 int MathScriptInset::dx1() const
155 {
156         lyx::Assert(hasUp());
157         return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
158 }
159
160
161 int MathScriptInset::dxx() const
162 {
163         return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
164 }
165
166
167 int MathScriptInset::nwid() const
168 {
169         return nuc().size() ? nuc().width() : 2;
170 }
171
172
173 int MathScriptInset::nasc() const
174 {
175         return nuc().size() ? nuc().ascent() : 5;
176 }
177
178
179 int MathScriptInset::ndes() const
180 {
181         return nuc().size() ? nuc().descent() : 0;
182 }
183
184
185 void MathScriptInset::metrics(MathMetricsInfo & mi) const
186 {
187         cell(2).metrics(mi);
188         MathScriptChanger dummy(mi.base);
189         cell(0).metrics(mi);
190         cell(1).metrics(mi);
191         dim_.w = 0;
192         if (hasLimits()) {
193                 dim_.w = nwid();
194                 if (hasUp())
195                         dim_.w = max(dim_.w, up().width());
196                 if (hasDown())
197                         dim_.w = max(dim_.w, down().width());
198         } else {
199                 if (hasUp())
200                         dim_.w = max(dim_.w, up().width());
201                 if (hasDown())
202                         dim_.w = max(dim_.w, down().width());
203                 dim_.w += nwid();
204         }
205         dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
206         dim_.d = dy0() + (hasDown() ? down().descent() : 0);
207         metricsMarkers2();
208 }
209
210
211 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
212 {
213         if (nuc().size())
214                 nuc().draw(pi, x + dxx(), y);
215         else if (editing())
216                 drawStr(pi, pi.base.font, x + dxx(), y, ".");
217         MathScriptChanger dummy(pi.base);
218         if (hasUp())
219                 up().draw(pi, x + dx1(), y - dy1());
220         if (hasDown())
221                 down().draw(pi, x + dx0(), y + dy0());
222         drawMarkers2(pi, x, y);
223 }
224
225
226 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
227 {
228         if (hasUp())
229                 up().metricsT(mi);
230         if (hasDown())
231                 down().metricsT(mi);
232         nuc().metricsT(mi);
233 }
234
235
236 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
237 {
238         if (nuc().size())
239                 nuc().drawT(pain, x + dxx(), y);
240         if (hasUp())
241                 up().drawT(pain, x + dx1(), y - dy1());
242         if (hasDown())
243                 down().drawT(pain, x + dx0(), y + dy0());
244 }
245
246
247
248 bool MathScriptInset::hasLimits() const
249 {
250         // obvious cases
251         if (limits_ == 1)
252                 return true;
253         if (limits_ == -1)
254                 return false;
255
256         // we can only display limits if the nucleus wants some
257         if (!nuc().size())
258                 return false;
259         if (!nuc().back()->isScriptable())
260                 return false;
261
262         // per default \int has limits beside the \int even in displayed formulas
263         if (nuc().back()->asSymbolInset())
264                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
265                         return false;
266
267         // assume "real" limits for everything else
268         return true;
269 }
270
271
272 void MathScriptInset::removeEmptyScripts()
273 {
274         for (int i = 0; i <= 1; ++i)
275                 if (script_[i] && cell(i).size() == 0) {
276                         cell(i).clear();
277                         script_[i] = false;
278                 }
279 }
280
281
282 void MathScriptInset::removeScript(bool up)
283 {
284         cell(up).clear();
285         script_[up] = false;
286 }
287
288
289 bool MathScriptInset::has(bool up) const
290 {
291         return script_[up];
292 }
293
294
295 bool MathScriptInset::empty() const
296 {
297         return !script_[0] && !script_[1] && cell(2).empty();
298 }
299
300
301 bool MathScriptInset::hasUp() const
302 {
303         return script_[1];
304 }
305
306
307 bool MathScriptInset::hasDown() const
308 {
309         return script_[0];
310 }
311
312
313 bool MathScriptInset::idxRight(idx_type &, pos_type &) const
314 {
315         return false;
316 }
317
318
319 bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
320 {
321         return false;
322 }
323
324
325 bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
326         int) const
327 {
328         if ((idx == 1 && up) || (idx == 0 && !up))
329                 return false;
330
331         // in nuclues?
332         if (idx == 2) {
333                 idx = up;
334                 pos = 0;
335         } else {
336                 idx = 2;
337                 pos = cell(2).size();   
338         }
339         return true;
340 }
341
342
343 void MathScriptInset::write(WriteStream & os) const
344 {
345         if (nuc().size()) {
346                 os << nuc();
347                 if (nuc().back()->takesLimits()) {
348                         if (limits_ == -1)
349                                 os << "\\nolimits ";
350                         if (limits_ == 1)
351                                 os << "\\limits ";
352                 }
353         } else {
354                 if (os.firstitem())
355                         lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
356                 else
357                         os << "{}";
358         }
359
360         if (hasDown() && down().size())
361                 os << "_{" << down() << '}';
362
363         if (hasUp() && up().size())
364                 os << "^{" << up() << '}';
365
366         if (lock_ && !os.latex())
367                 os << "\\lyxlock ";
368 }
369
370
371 void MathScriptInset::normalize(NormalStream & os) const
372 {
373         bool d = hasDown() && down().size();
374         bool u = hasUp() && up().size();
375
376         if (u && d)
377                 os << "[subsup ";
378         else if (u)
379                 os << "[sup ";
380         else if (d)
381                 os << "[sub ";
382
383         if (nuc().size())
384                 os << nuc() << ' ';
385         else
386                 os << "[par]";
387
388         if (u && d)
389                 os << down() << ' ' << up() << ']';
390         else if (d)
391                 os << down() << ']';
392         else if (u)
393                 os << up() << ']';
394 }
395
396
397 void MathScriptInset::maplize(MapleStream & os) const
398 {
399         if (nuc().size())
400                 os << nuc();
401         if (hasDown() && down().size())
402                 os << '[' << down() << ']';
403         if (hasUp() && up().size())
404                 os << "^(" << up() << ')';
405 }
406
407
408 void MathScriptInset::mathematicize(MathematicaStream & os) const
409 {
410         bool d = hasDown() && down().size();
411         bool u = hasUp() && up().size();
412
413         if (nuc().size()) {
414                 if (d) 
415                         os << "Subscript[" << nuc();
416                 else
417                         os << nuc();
418         }
419
420         if (u)
421                 os << "^(" << up() << ")";
422
423         if (nuc().size())
424                 if (d)
425                         os << "," << down() << "]"; 
426 }
427
428
429 void MathScriptInset::mathmlize( MathMLStream & os) const
430 {
431         bool d = hasDown() && down().size();
432         bool u = hasUp() && up().size();
433
434         if (u && d)
435                 os << MTag("msubsup");
436         else if (u)
437                 os << MTag("msup");
438         else if (d)
439                 os << MTag("msub");
440
441         if (nuc().size())
442                 os << nuc();
443         else
444                 os << "<mrow/>";
445
446         if (u && d)
447                 os << down() << up() << ETag("msubsup");
448         else if (u)
449                 os << up() << ETag("msup");
450         else if (d)
451                 os << down() << ETag("msub");
452 }
453
454
455 void MathScriptInset::octavize(OctaveStream & os) const
456 {
457         if (nuc().size())
458                 os << nuc();
459         if (hasDown() && down().size())
460                 os << '[' << down() << ']';
461         if (hasUp() && up().size())
462                 os << "^(" << up() << ')';
463 }
464
465
466 void MathScriptInset::infoize(std::ostream & os) const
467 {
468         os << "Scripts";
469         if (limits_)
470                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
471 }