]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
8aed9481f75391acd679cbadc2bf2005af3a5cf6
[lyx.git] / src / mathed / InsetMathFrac.cpp
1 /**
2  * \file InsetMathFracBase.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  * \author Uwe Stöhr
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetMathFrac.h"
16
17 #include "Cursor.h"
18 #include "LaTeXFeatures.h"
19 #include "MathData.h"
20 #include "MathStream.h"
21 #include "MathSupport.h"
22 #include "MetricsInfo.h"
23 #include "TextPainter.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/lassert.h"
29
30 using namespace std;
31
32
33 namespace lyx {
34
35 /////////////////////////////////////////////////////////////////////
36 //
37 // InsetMathFracBase
38 //
39 /////////////////////////////////////////////////////////////////////
40
41
42 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
43         : InsetMathNest(buf, ncells)
44 {}
45
46
47 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
48 {
49         // If we only have one cell, target = 0, otherwise
50         // target = up ? 0 : 1, since upper cell has idx 0
51         InsetMath::idx_type target = nargs() > 1 ? !up : 0;
52         if (cur.idx() == target)
53                 return false;
54         cur.idx() = target;
55         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
56         return true;
57 }
58
59
60
61 /////////////////////////////////////////////////////////////////////
62 //
63 // InsetMathFrac
64 //
65 /////////////////////////////////////////////////////////////////////
66
67
68 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
69         : InsetMathFracBase(buf, ncells), kind_(kind)
70 {}
71
72
73 Inset * InsetMathFrac::clone() const
74 {
75         return new InsetMathFrac(*this);
76 }
77
78
79 InsetMathFrac * InsetMathFrac::asFracInset()
80 {
81         return kind_ == ATOP ? 0 : this;
82 }
83
84
85 InsetMathFrac const * InsetMathFrac::asFracInset() const
86 {
87         return kind_ == ATOP ? 0 : this;
88 }
89
90
91 bool InsetMathFrac::idxForward(Cursor & cur) const
92 {
93         InsetMath::idx_type target = 0;
94         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
95                 if (nargs() == 3)
96                         target = 0;
97                 else if (nargs() == 2)
98                         target = 1;
99         } else
100                 return false;
101         if (cur.idx() == target)
102                 return false;
103         cur.idx() = target;
104         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
105         return true;
106 }
107
108
109 bool InsetMathFrac::idxBackward(Cursor & cur) const
110 {
111         InsetMath::idx_type target = 0;
112         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
113                 if (nargs() == 3)
114                         target = 2;
115                 else if (nargs() == 2)
116                         target = 0;
117         } else
118                 return false;
119         if (cur.idx() == target)
120                 return false;
121         cur.idx() = target;
122         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
123         return true;
124 }
125
126
127 MathClass InsetMathFrac::mathClass() const
128 {
129         // Generalized fractions are of inner class (see The TeXbook, p. 292)
130         // But stuff from the unit/nicefrac packages are not real fractions.
131         MathClass mc = MC_ORD;
132         switch (kind_) {
133         case ATOP:
134         case OVER:
135         case FRAC:
136         case DFRAC:
137         case TFRAC:
138         case CFRAC:
139         case CFRACLEFT:
140         case CFRACRIGHT:
141                 mc = MC_INNER;
142                 break;
143         case NICEFRAC:
144         case UNITFRAC:
145         case UNIT:
146                 break;
147         }
148         return mc;
149 }
150
151
152
153 namespace {
154
155 // align frac to minus character
156 int dy_for_frac(MetricsBase & mb)
157 {
158         Changer dummy = mb.changeFontSet("mathnormal");
159         return theFontMetrics(mb.font).ascent('-') - 1;
160 }
161
162
163 // align the top of M in the cell with the top of M in the surrounding font
164 int dy_for_nicefrac(MetricsBase & mb)
165 {
166         // this is according to nicefrac.sty
167         int big_m = theFontMetrics(mb.font).ascent('M');
168         Changer dummy = mb.changeScript();
169         int small_m = theFontMetrics(mb.font).ascent('M');
170         return big_m - small_m;
171 }
172
173 } // anon namespace
174
175
176
177 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
178 {
179         Dimension dim0, dim1, dim2;
180
181         switch (kind_) {
182         case UNIT: {
183                 // \unitone, \unittwo
184                 dim.wid = 0;
185                 int unit_cell = 0;
186                 // is there an extra cell holding the value being given a dimension?
187                 // (this is \unittwo)
188                 if (nargs() == 2) {
189                         cell(0).metrics(mi, dim1);
190                         dim.wid += dim1.wid + 4;
191                         unit_cell = 1;
192                 }
193                 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
194                 cell(unit_cell).metrics(mi, dim0);
195                 dim.wid += dim0.width() + 1;
196                 dim.asc = max(dim0.asc, dim1.asc);
197                 dim.des = max(dim0.des, dim1.des);
198         }
199                 break;
200
201         case UNITFRAC:
202         case NICEFRAC: {
203                 // \unitfrac, \unitfracthree, \nicefrac
204                 dim.wid = 0;
205                 int const dy = dy_for_nicefrac(mi.base);
206                 // is there an extra cell holding the value being given a dimension?
207                 // (this is \unitfracthree)
208                 if (kind_ == UNITFRAC && nargs() == 3) {
209                         cell(2).metrics(mi, dim2);
210                         dim.wid += dim2.wid + 4;
211                 }
212                 Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
213                         : Changer();
214                 Changer dummy2 = mi.base.changeScript();
215                 cell(0).metrics(mi, dim0);
216                 cell(1).metrics(mi, dim1);
217                 dim.wid += dim0.wid + dim1.wid + 5;
218                 dim.asc = max(max(dim2.asc, dim0.asc + dy), dim1.asc);
219                 dim.des = max(max(dim2.des, dim0.des - dy), dim1.des);
220         }
221                 break;
222
223         case FRAC:
224         case CFRAC:
225         case CFRACLEFT:
226         case CFRACRIGHT:
227         case DFRAC:
228         case TFRAC:
229         case OVER:
230         case ATOP: {
231                 int const dy = dy_for_frac(mi.base);
232                 Changer dummy =
233                         // \tfrac is always in text size
234                         (kind_ == TFRAC) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
235                         // \cfrac and \dfrac are always in display size
236                         (kind_ == CFRAC
237                          || kind_ == CFRACLEFT
238                          || kind_ == CFRACRIGHT
239                          || kind_ == DFRAC) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
240                         // all others
241                                               mi.base.changeFrac();
242                 cell(0).metrics(mi, dim0);
243                 cell(1).metrics(mi, dim1);
244                 dim.wid = max(dim0.wid, dim1.wid) + 2;
245                 dim.asc = dim0.height() + dy/2 + dy;
246                 dim.des = max(0, dim1.height() + dy/2 - dy);
247         }
248         } //switch (kind_)
249         metricsMarkers(mi, dim);
250 }
251
252
253 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
254 {
255         setPosCache(pi, x, y);
256         Dimension const dim = dimension(*pi.base.bv);
257         Dimension const dim0 = cell(0).dimension(*pi.base.bv);
258         switch (kind_) {
259         case UNIT: {
260                 // \unitone, \unittwo
261                 int xx = x;
262                 int unit_cell = 0;
263                 // is there an extra cell holding the value being given a dimension?
264                 // (this is \unittwo)
265                 if (nargs() == 2) {
266                         cell(0).draw(pi, x + 1, y);
267                         xx += dim0.wid + 4;
268                         unit_cell = 1;
269                 }
270                 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
271                 cell(unit_cell).draw(pi, xx + 1, y);
272         }
273                 break;
274
275         case UNITFRAC:
276         case NICEFRAC: {
277                 // \unitfrac, \unitfracthree, \nicefrac
278                 int xx = x;
279                 int const dy = dy_for_nicefrac(pi.base);
280                 // is there an extra cell holding the value being given a dimension?
281                 // (this is \unitfracthree)
282                 if (kind_ == UNITFRAC && nargs() == 3) {
283                         cell(2).draw(pi, x + 1, y);
284                         xx += cell(2).dimension(*pi.base.bv).wid + 4;
285                 }
286                 Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
287                         : Changer();
288                 // nice fraction
289                 // FIXME:
290                 // * the solidus should be \kern-2mu/\kern-1mu.
291                 Changer dummy2 = pi.base.changeScript();
292                 cell(0).draw(pi, xx + 2, y - dy);
293                 cell(1).draw(pi, xx + dim0.wid + 5, y);
294                 // Diag line:
295                 pi.pain.line(xx + dim0.wid + 1, y + dim.des - 2,
296                              xx + dim0.wid + 6, y - dim.asc + 2,
297                              pi.base.font.color(), pi.pain.line_solid,
298                              pi.base.solidLineThickness());
299         }
300                 break;
301
302         case FRAC:
303         case CFRAC:
304         case CFRACLEFT:
305         case CFRACRIGHT:
306         case DFRAC:
307         case TFRAC:
308         case OVER:
309         case ATOP: {
310                 int const dy = dy_for_frac(pi.base);
311                 Changer dummy =
312                         // \tfrac is always in text size
313                         (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
314                         // \cfrac and \dfrac are always in display size
315                         (kind_ == CFRAC
316                          || kind_ == CFRACLEFT
317                          || kind_ == CFRACRIGHT
318                          || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
319                         // all others
320                                               pi.base.changeFrac();
321                 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
322                 int m = x + dim.wid / 2;
323                 int xx =
324                         // align left
325                         (kind_ == CFRACLEFT) ? x + 2 :
326                         // align right
327                         (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
328                         // center
329                                                 m - dim0.wid / 2;
330                 // take dy/2 for the spacing around the horizontal line. This is
331                 // arbitrary. In LaTeX it is more complicated to ensure that displayed
332                 // fractions line up next to each other.
333                 cell(0).draw(pi, xx, y - dim0.des - dy/2 - dy);
334                 // center
335                 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
336                 // horizontal line
337                 if (kind_ != ATOP)
338                         pi.pain.line(x + 1, y - dy,
339                                      x + dim.wid - 2, y - dy,
340                                      pi.base.font.color(), pi.pain.line_solid,
341                                      pi.base.solidLineThickness());
342         }
343         } //switch (kind_)
344         drawMarkers(pi, x, y);
345 }
346
347
348 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
349 {
350         Dimension dim0, dim1;
351         cell(0).metricsT(mi, dim0);
352         cell(1).metricsT(mi, dim1);
353         dim.wid = max(dim0.width(), dim1.wid);
354         dim.asc = dim0.height() + 1;
355         dim.des = dim1.height();
356 }
357
358
359 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
360 {
361         // FIXME: BROKEN!
362         /*
363         Dimension dim;
364         int m = x + dim.width() / 2;
365         cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
366         cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
367         // ASCII art: ignore niceties
368         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
369                 pain.horizontalLine(x, y, dim.width());
370         */
371 }
372
373
374 void InsetMathFrac::write(WriteStream & os) const
375 {
376         MathEnsurer ensurer(os);
377         switch (kind_) {
378         case ATOP:
379                 // \\atop is only for compatibility, \\binom is the
380                 // LaTeX2e successor
381                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
382                 break;
383         case OVER:
384                 // \\over is only for compatibility, normalize this to \\frac
385                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
386                 break;
387         case FRAC:
388         case DFRAC:
389         case TFRAC:
390         case NICEFRAC:
391         case CFRAC:
392         case UNITFRAC:
393                 if (nargs() == 2)
394                         InsetMathNest::write(os);
395                 else
396                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
397                 break;
398         case UNIT:
399                 if (nargs() == 2)
400                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
401                 else
402                         os << "\\unit{" << cell(0) << '}';
403                 break;
404         case CFRACLEFT:
405                 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
406                 break;
407         case CFRACRIGHT:
408                 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
409                 break;
410         }
411 }
412
413
414 docstring InsetMathFrac::name() const
415 {
416         switch (kind_) {
417         case FRAC:
418                 return from_ascii("frac");
419         case CFRAC:
420         case CFRACLEFT:
421         case CFRACRIGHT:
422                 return from_ascii("cfrac");
423         case DFRAC:
424                 return from_ascii("dfrac");
425         case TFRAC:
426                 return from_ascii("tfrac");
427         case OVER:
428                 return from_ascii("over");
429         case NICEFRAC:
430                 return from_ascii("nicefrac");
431         case UNITFRAC:
432                 return from_ascii("unitfrac");
433         case UNIT:
434                 return from_ascii("unit");
435         case ATOP:
436                 return from_ascii("atop");
437         }
438         // shut up stupid compiler
439         return docstring();
440 }
441
442
443 bool InsetMathFrac::extraBraces() const
444 {
445         return kind_ == ATOP || kind_ == OVER;
446 }
447
448
449 void InsetMathFrac::maple(MapleStream & os) const
450 {
451         if (nargs() != 2) {
452                 // Someone who knows about maple should fix this.
453                 LASSERT(false, return);
454         }
455         os << '(' << cell(0) << ")/(" << cell(1) << ')';
456 }
457
458
459 void InsetMathFrac::mathematica(MathematicaStream & os) const
460 {
461         if (nargs() != 2) {
462                 // Someone who knows about mathematica should fix this.
463                 LASSERT(false, return);
464         }
465         os << '(' << cell(0) << ")/(" << cell(1) << ')';
466 }
467
468
469 void InsetMathFrac::octave(OctaveStream & os) const
470 {
471         if (nargs() != 2) {
472                 // Someone who knows about octave should fix this.
473                 LASSERT(false, return);
474         }
475         os << '(' << cell(0) << ")/(" << cell(1) << ')';
476 }
477
478
479 void InsetMathFrac::mathmlize(MathStream & os) const
480 {
481         switch (kind_) {
482         case ATOP:
483                 os << MTag("mfrac", "linethickeness='0'")
484                    << MTag("mrow") << cell(0) << ETag("mrow")
485                          << MTag("mrow") << cell(1) << ETag("mrow")
486                          << ETag("mfrac");
487                 break;
488
489         // we do not presently distinguish these
490         case OVER:
491         case FRAC:
492         case DFRAC:
493         case TFRAC:
494         case CFRAC:
495         case CFRACLEFT:
496         case CFRACRIGHT:
497                 os << MTag("mfrac")
498                    << MTag("mrow") << cell(0) << ETag("mrow")
499                          << MTag("mrow") << cell(1) << ETag("mrow")
500                          << ETag("mfrac");
501                 break;
502
503         case NICEFRAC:
504                 os << MTag("mfrac", "bevelled='true'")
505                    << MTag("mrow") << cell(0) << ETag("mrow")
506                          << MTag("mrow") << cell(1) << ETag("mrow")
507                          << ETag("mfrac");
508                 break;
509
510         case UNITFRAC:
511                 if (nargs() == 3)
512                         os << cell(2);
513                 os << MTag("mfrac", "bevelled='true'")
514                    << MTag("mrow") << cell(0) << ETag("mrow")
515                          << MTag("mrow") << cell(1) << ETag("mrow")
516                          << ETag("mfrac");
517                 break;
518
519         case UNIT:
520                 // FIXME This is not right, because we still output mi, etc,
521                 // when we output the cell. So we need to prevent that somehow.
522                 if (nargs() == 2)
523                         os << cell(0) 
524                            << MTag("mstyle mathvariant='normal'") 
525                            << cell(1) 
526                            << ETag("mstyle");
527                 else
528                         os << MTag("mstyle mathvariant='normal'") 
529                            << cell(0)
530                            << ETag("mstyle");
531         }
532 }
533
534
535 void InsetMathFrac::htmlize(HtmlStream & os) const
536 {
537         switch (kind_) {
538         case ATOP:
539                 os << MTag("span", "class='frac'")
540                          << MTag("span", "class='numer'") << cell(0) << ETag("span")
541                          << MTag("span", "class='numer'") << cell(1) << ETag("span")
542                          << ETag("span");
543                 break;
544
545         // we do not presently distinguish these
546         case OVER:
547         case FRAC:
548         case DFRAC:
549         case TFRAC:
550         case CFRAC:
551         case CFRACLEFT:
552         case CFRACRIGHT:
553                 os << MTag("span", "class='frac'")
554                          << MTag("span", "class='numer'") << cell(0) << ETag("span")
555                          << MTag("span", "class='denom'") << cell(1) << ETag("span")
556                          << ETag("span");
557                 break;
558
559         case NICEFRAC:
560                 os << cell(0) << '/' << cell(1);
561                 break;
562
563         case UNITFRAC:
564                 if (nargs() == 3)
565                         os << cell(2) << ' ';
566                 os << cell(0) << '/' << cell(1);
567                 break;
568
569         case UNIT:
570                 // FIXME This is not right, because we still output i, etc,
571                 // when we output the cell. So we need to prevent that somehow.
572                 if (nargs() == 2)
573                         os << cell(0) 
574                            << MTag("span") 
575                            << cell(1) 
576                            << ETag("span");
577                 else
578                         os << MTag("span") 
579                            << cell(0)
580                            << ETag("span");
581         }
582 }
583
584
585 void InsetMathFrac::validate(LaTeXFeatures & features) const
586 {
587         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
588                 features.require("units");
589         if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
590                   || kind_ == DFRAC || kind_ == TFRAC)
591                 features.require("amsmath");
592         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
593                 // CSS adapted from eLyXer
594                 features.addCSSSnippet(
595                         "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
596                         "span.numer{display: block;}\n"
597                         "span.denom{display: block; border-top: thin solid #000040;}");
598         InsetMathNest::validate(features);
599 }
600
601
602 /////////////////////////////////////////////////////////////////////
603 //
604 // InsetMathBinom
605 //
606 /////////////////////////////////////////////////////////////////////
607
608
609 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
610         : InsetMathFracBase(buf), kind_(kind)
611 {}
612
613
614 Inset * InsetMathBinom::clone() const
615 {
616         return new InsetMathBinom(*this);
617 }
618
619
620 int InsetMathBinom::dw(int height) const
621 {
622         int w = height / 5;
623         if (w > 15)
624                 w = 15;
625         if (w < 6)
626                 w = 6;
627         return w;
628 }
629
630
631 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
632 {
633         Dimension dim0, dim1;
634         int const dy = dy_for_frac(mi.base);
635         Changer dummy =
636                 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
637                 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
638                                     mi.base.changeFrac();
639         cell(0).metrics(mi, dim0);
640         cell(1).metrics(mi, dim1);
641         dim.asc = dim0.height() + 1 + dy/2 + dy;
642         dim.des = max(0, dim1.height() + 1 + dy/2 - dy);
643         dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
644         metricsMarkers2(mi, dim);
645 }
646
647
648 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
649 {
650         Dimension const dim = dimension(*pi.base.bv);
651         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
652         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
653         int const dy = dy_for_frac(pi.base);
654         // define the binom brackets
655         docstring const bra = kind_ == BRACE ? from_ascii("{") :
656                 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
657         docstring const ket = kind_ == BRACE ? from_ascii("}") :
658                 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
659
660         int m = x + dim.width() / 2;
661         {
662                 Changer dummy =
663                         (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
664                         (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
665                                             pi.base.changeFrac();
666                 // take dy both for the vertical alignment and for the spacing between
667                 // cells
668                 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - dy/2 - dy);
669                 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
670         }
671         // draw the brackets and the marker
672         mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
673                 dim.height(), bra);
674         mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
675                 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
676         drawMarkers2(pi, x, y);
677 }
678
679
680 bool InsetMathBinom::extraBraces() const
681 {
682         return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
683 }
684
685
686 void InsetMathBinom::write(WriteStream & os) const
687 {
688         MathEnsurer ensurer(os);
689         switch (kind_) {
690         case BINOM:
691                 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
692                 break;
693         case DBINOM:
694                 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
695                 break;
696         case TBINOM:
697                 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
698                 break;
699         case CHOOSE:
700                 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
701                 break;
702         case BRACE:
703                 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
704                 break;
705         case BRACK:
706                 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
707                 break;
708         }
709 }
710
711
712 void InsetMathBinom::normalize(NormalStream & os) const
713 {
714         os << "[binom " << cell(0) << ' ' << cell(1) << ']';
715 }
716
717
718 void InsetMathBinom::mathmlize(MathStream & os) const
719 {
720         char ldelim = ' ';
721         char rdelim = ' ';
722         switch (kind_) {
723         case BINOM:
724         case TBINOM:
725         case DBINOM:
726         case CHOOSE:
727                 ldelim = '(';
728                 rdelim = ')';
729                 break;
730         case BRACE:
731                 ldelim = '{';
732                 rdelim = '}';
733                 break;
734         case BRACK:
735                 ldelim = '[';
736                 rdelim = ']';
737                 break;
738         }
739         os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
740            << "<mfrac linethickness='0'>"
741            << cell(0) << cell(1)
742            << "</mfrac>"
743            << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
744 }
745
746
747 void InsetMathBinom::htmlize(HtmlStream & os) const
748 {
749         char ldelim = ' ';
750         char rdelim = ' ';
751         switch (kind_) {
752         case BINOM:
753         case TBINOM:
754         case DBINOM:
755         case CHOOSE:
756                 ldelim = '(';
757                 rdelim = ')';
758                 break;
759         case BRACE:
760                 ldelim = '{';
761                 rdelim = '}';
762                 break;
763         case BRACK:
764                 ldelim = '[';
765                 rdelim = ']';
766                 break;
767         }
768         os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
769            << MTag("span", "class='binom'") << '\n'
770            << MTag("span") << cell(0) << ETag("span") << '\n'
771            << MTag("span") << cell(1) << ETag("span") << '\n'
772            << ETag("span") << '\n'
773                  << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
774 }
775
776
777 void InsetMathBinom::validate(LaTeXFeatures & features) const
778 {
779         if (features.runparams().isLaTeX()) {
780                 if (kind_ == BINOM)
781                         features.require("binom");
782                 if (kind_ == DBINOM || kind_ == TBINOM)
783                         features.require("amsmath");
784         } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
785                 features.addCSSSnippet(
786                         "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
787                         "span.binom span{display: block;}\n"
788                         "span.binomdelim{font-size: 2em;}");
789         InsetMathNest::validate(features);
790 }
791
792
793 } // namespace lyx