]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
6b768c91c2e159dfc1204730535b2a405102811d
[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() + 2 + dy;
246                 dim.des = max(0, dim1.height() + 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());
298         }
299                 break;
300
301         case FRAC:
302         case CFRAC:
303         case CFRACLEFT:
304         case CFRACRIGHT:
305         case DFRAC:
306         case TFRAC:
307         case OVER:
308         case ATOP: {
309                 int const dy = dy_for_frac(pi.base);
310                 Changer dummy =
311                         // \tfrac is always in text size
312                         (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
313                         // \cfrac and \dfrac are always in display size
314                         (kind_ == CFRAC
315                          || kind_ == CFRACLEFT
316                          || kind_ == CFRACRIGHT
317                          || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
318                         // all others
319                                               pi.base.changeFrac();
320                 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
321                 int m = x + dim.wid / 2;
322                 int xx =
323                         // align left
324                         (kind_ == CFRACLEFT) ? x + 2 :
325                         // align right
326                         (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
327                         // center
328                                                 m - dim0.wid / 2;
329                 cell(0).draw(pi, xx, y - dim0.des - 2 - dy);
330                 // center
331                 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - dy);
332                 // horizontal line
333                 if (kind_ != ATOP)
334                         pi.pain.line(x + 1, y - dy,
335                                      x + dim.wid - 2, y - dy, pi.base.font.color());
336         }
337         } //switch (kind_)
338         drawMarkers(pi, x, y);
339 }
340
341
342 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
343 {
344         Dimension dim0, dim1;
345         cell(0).metricsT(mi, dim0);
346         cell(1).metricsT(mi, dim1);
347         dim.wid = max(dim0.width(), dim1.wid);
348         dim.asc = dim0.height() + 1;
349         dim.des = dim1.height();
350 }
351
352
353 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
354 {
355         // FIXME: BROKEN!
356         /*
357         Dimension dim;
358         int m = x + dim.width() / 2;
359         cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
360         cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
361         // ASCII art: ignore niceties
362         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
363                 pain.horizontalLine(x, y, dim.width());
364         */
365 }
366
367
368 void InsetMathFrac::write(WriteStream & os) const
369 {
370         MathEnsurer ensurer(os);
371         switch (kind_) {
372         case ATOP:
373                 // \\atop is only for compatibility, \\binom is the
374                 // LaTeX2e successor
375                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
376                 break;
377         case OVER:
378                 // \\over is only for compatibility, normalize this to \\frac
379                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
380                 break;
381         case FRAC:
382         case DFRAC:
383         case TFRAC:
384         case NICEFRAC:
385         case CFRAC:
386         case UNITFRAC:
387                 if (nargs() == 2)
388                         InsetMathNest::write(os);
389                 else
390                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
391                 break;
392         case UNIT:
393                 if (nargs() == 2)
394                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
395                 else
396                         os << "\\unit{" << cell(0) << '}';
397                 break;
398         case CFRACLEFT:
399                 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
400                 break;
401         case CFRACRIGHT:
402                 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
403                 break;
404         }
405 }
406
407
408 docstring InsetMathFrac::name() const
409 {
410         switch (kind_) {
411         case FRAC:
412                 return from_ascii("frac");
413         case CFRAC:
414         case CFRACLEFT:
415         case CFRACRIGHT:
416                 return from_ascii("cfrac");
417         case DFRAC:
418                 return from_ascii("dfrac");
419         case TFRAC:
420                 return from_ascii("tfrac");
421         case OVER:
422                 return from_ascii("over");
423         case NICEFRAC:
424                 return from_ascii("nicefrac");
425         case UNITFRAC:
426                 return from_ascii("unitfrac");
427         case UNIT:
428                 return from_ascii("unit");
429         case ATOP:
430                 return from_ascii("atop");
431         }
432         // shut up stupid compiler
433         return docstring();
434 }
435
436
437 bool InsetMathFrac::extraBraces() const
438 {
439         return kind_ == ATOP || kind_ == OVER;
440 }
441
442
443 void InsetMathFrac::maple(MapleStream & os) const
444 {
445         if (nargs() != 2) {
446                 // Someone who knows about maple should fix this.
447                 LASSERT(false, return);
448         }
449         os << '(' << cell(0) << ")/(" << cell(1) << ')';
450 }
451
452
453 void InsetMathFrac::mathematica(MathematicaStream & os) const
454 {
455         if (nargs() != 2) {
456                 // Someone who knows about mathematica should fix this.
457                 LASSERT(false, return);
458         }
459         os << '(' << cell(0) << ")/(" << cell(1) << ')';
460 }
461
462
463 void InsetMathFrac::octave(OctaveStream & os) const
464 {
465         if (nargs() != 2) {
466                 // Someone who knows about octave should fix this.
467                 LASSERT(false, return);
468         }
469         os << '(' << cell(0) << ")/(" << cell(1) << ')';
470 }
471
472
473 void InsetMathFrac::mathmlize(MathStream & os) const
474 {
475         switch (kind_) {
476         case ATOP:
477                 os << MTag("mfrac", "linethickeness='0'")
478                    << MTag("mrow") << cell(0) << ETag("mrow")
479                          << MTag("mrow") << cell(1) << ETag("mrow")
480                          << ETag("mfrac");
481                 break;
482
483         // we do not presently distinguish these
484         case OVER:
485         case FRAC:
486         case DFRAC:
487         case TFRAC:
488         case CFRAC:
489         case CFRACLEFT:
490         case CFRACRIGHT:
491                 os << MTag("mfrac")
492                    << MTag("mrow") << cell(0) << ETag("mrow")
493                          << MTag("mrow") << cell(1) << ETag("mrow")
494                          << ETag("mfrac");
495                 break;
496
497         case NICEFRAC:
498                 os << MTag("mfrac", "bevelled='true'")
499                    << MTag("mrow") << cell(0) << ETag("mrow")
500                          << MTag("mrow") << cell(1) << ETag("mrow")
501                          << ETag("mfrac");
502                 break;
503
504         case UNITFRAC:
505                 if (nargs() == 3)
506                         os << cell(2);
507                 os << MTag("mfrac", "bevelled='true'")
508                    << MTag("mrow") << cell(0) << ETag("mrow")
509                          << MTag("mrow") << cell(1) << ETag("mrow")
510                          << ETag("mfrac");
511                 break;
512
513         case UNIT:
514                 // FIXME This is not right, because we still output mi, etc,
515                 // when we output the cell. So we need to prevent that somehow.
516                 if (nargs() == 2)
517                         os << cell(0) 
518                            << MTag("mstyle mathvariant='normal'") 
519                            << cell(1) 
520                            << ETag("mstyle");
521                 else
522                         os << MTag("mstyle mathvariant='normal'") 
523                            << cell(0)
524                            << ETag("mstyle");
525         }
526 }
527
528
529 void InsetMathFrac::htmlize(HtmlStream & os) const
530 {
531         switch (kind_) {
532         case ATOP:
533                 os << MTag("span", "class='frac'")
534                          << MTag("span", "class='numer'") << cell(0) << ETag("span")
535                          << MTag("span", "class='numer'") << cell(1) << ETag("span")
536                          << ETag("span");
537                 break;
538
539         // we do not presently distinguish these
540         case OVER:
541         case FRAC:
542         case DFRAC:
543         case TFRAC:
544         case CFRAC:
545         case CFRACLEFT:
546         case CFRACRIGHT:
547                 os << MTag("span", "class='frac'")
548                          << MTag("span", "class='numer'") << cell(0) << ETag("span")
549                          << MTag("span", "class='denom'") << cell(1) << ETag("span")
550                          << ETag("span");
551                 break;
552
553         case NICEFRAC:
554                 os << cell(0) << '/' << cell(1);
555                 break;
556
557         case UNITFRAC:
558                 if (nargs() == 3)
559                         os << cell(2) << ' ';
560                 os << cell(0) << '/' << cell(1);
561                 break;
562
563         case UNIT:
564                 // FIXME This is not right, because we still output i, etc,
565                 // when we output the cell. So we need to prevent that somehow.
566                 if (nargs() == 2)
567                         os << cell(0) 
568                            << MTag("span") 
569                            << cell(1) 
570                            << ETag("span");
571                 else
572                         os << MTag("span") 
573                            << cell(0)
574                            << ETag("span");
575         }
576 }
577
578
579 void InsetMathFrac::validate(LaTeXFeatures & features) const
580 {
581         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
582                 features.require("units");
583         if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
584                   || kind_ == DFRAC || kind_ == TFRAC)
585                 features.require("amsmath");
586         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
587                 // CSS adapted from eLyXer
588                 features.addCSSSnippet(
589                         "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
590                         "span.numer{display: block;}\n"
591                         "span.denom{display: block; border-top: thin solid #000040;}");
592         InsetMathNest::validate(features);
593 }
594
595
596 /////////////////////////////////////////////////////////////////////
597 //
598 // InsetMathBinom
599 //
600 /////////////////////////////////////////////////////////////////////
601
602
603 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
604         : InsetMathFracBase(buf), kind_(kind)
605 {}
606
607
608 Inset * InsetMathBinom::clone() const
609 {
610         return new InsetMathBinom(*this);
611 }
612
613
614 int InsetMathBinom::dw(int height) const
615 {
616         int w = height / 5;
617         if (w > 15)
618                 w = 15;
619         if (w < 6)
620                 w = 6;
621         return w;
622 }
623
624
625 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
626 {
627         Dimension dim0, dim1;
628         int const dy = dy_for_frac(mi.base);
629         Changer dummy =
630                 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
631                 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
632                                     mi.base.changeFrac();
633         cell(0).metrics(mi, dim0);
634         cell(1).metrics(mi, dim1);
635         dim.asc = dim0.height() + 4 + dy;
636         dim.des = max(0, dim1.height() + 4 - dy);
637         dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
638         metricsMarkers2(mi, dim);
639 }
640
641
642 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
643 {
644         Dimension const dim = dimension(*pi.base.bv);
645         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
646         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
647         int const dy = dy_for_frac(pi.base);
648         // define the binom brackets
649         docstring const bra = kind_ == BRACE ? from_ascii("{") :
650                 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
651         docstring const ket = kind_ == BRACE ? from_ascii("}") :
652                 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
653
654         int m = x + dim.width() / 2;
655         {
656                 Changer dummy =
657                         (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
658                         (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
659                                             pi.base.changeFrac();
660                 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - dy);
661                 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - dy);
662         }
663         // draw the brackets and the marker
664         mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
665                 dim.height(), bra);
666         mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
667                 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
668         drawMarkers2(pi, x, y);
669 }
670
671
672 bool InsetMathBinom::extraBraces() const
673 {
674         return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
675 }
676
677
678 void InsetMathBinom::write(WriteStream & os) const
679 {
680         MathEnsurer ensurer(os);
681         switch (kind_) {
682         case BINOM:
683                 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
684                 break;
685         case DBINOM:
686                 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
687                 break;
688         case TBINOM:
689                 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
690                 break;
691         case CHOOSE:
692                 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
693                 break;
694         case BRACE:
695                 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
696                 break;
697         case BRACK:
698                 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
699                 break;
700         }
701 }
702
703
704 void InsetMathBinom::normalize(NormalStream & os) const
705 {
706         os << "[binom " << cell(0) << ' ' << cell(1) << ']';
707 }
708
709
710 void InsetMathBinom::mathmlize(MathStream & os) const
711 {
712         char ldelim = ' ';
713         char rdelim = ' ';
714         switch (kind_) {
715         case BINOM:
716         case TBINOM:
717         case DBINOM:
718         case CHOOSE:
719                 ldelim = '(';
720                 rdelim = ')';
721                 break;
722         case BRACE:
723                 ldelim = '{';
724                 rdelim = '}';
725                 break;
726         case BRACK:
727                 ldelim = '[';
728                 rdelim = ']';
729                 break;
730         }
731         os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
732            << "<mfrac linethickness='0'>"
733            << cell(0) << cell(1)
734            << "</mfrac>"
735            << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
736 }
737
738
739 void InsetMathBinom::htmlize(HtmlStream & os) const
740 {
741         char ldelim = ' ';
742         char rdelim = ' ';
743         switch (kind_) {
744         case BINOM:
745         case TBINOM:
746         case DBINOM:
747         case CHOOSE:
748                 ldelim = '(';
749                 rdelim = ')';
750                 break;
751         case BRACE:
752                 ldelim = '{';
753                 rdelim = '}';
754                 break;
755         case BRACK:
756                 ldelim = '[';
757                 rdelim = ']';
758                 break;
759         }
760         os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
761            << MTag("span", "class='binom'") << '\n'
762            << MTag("span") << cell(0) << ETag("span") << '\n'
763            << MTag("span") << cell(1) << ETag("span") << '\n'
764            << ETag("span") << '\n'
765                  << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
766 }
767
768
769 void InsetMathBinom::validate(LaTeXFeatures & features) const
770 {
771         if (features.runparams().isLaTeX()) {
772                 if (kind_ == BINOM)
773                         features.require("binom");
774                 if (kind_ == DBINOM || kind_ == TBINOM)
775                         features.require("amsmath");
776         } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
777                 features.addCSSSnippet(
778                         "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
779                         "span.binom span{display: block;}\n"
780                         "span.binomdelim{font-size: 2em;}");
781         InsetMathNest::validate(features);
782 }
783
784
785 } // namespace lyx