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