From a97b097768b2d4df69015070040dfe3858ff82e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Uwe=20St=C3=B6hr?= Date: Sun, 5 Apr 2009 19:52:53 +0000 Subject: [PATCH] major code cleanup for fractions: - InsetMathFrac.cpp/h: - InsetCFrac, InsetDFrac, and InsetTFrac are now included as kind of InsetMathFrac - InsetDBinom and InsetTBinum are now included as kind of InsetMathBinom - support for the optional argument of \cfrac (cursor position still needs to be fixed, see post in mailing list) - stdtoolbars.inc: - add entries for \cfrac - remove entry for \atop since atop is outdated and only in LaTeX2e for compatibility reasons (according to the LaTeX companion, you get warnings from amsmath in the LaTeX log when using atop) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29114 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ui/stdtoolbars.inc | 7 +- src/mathed/InsetMathFrac.cpp | 516 +++++++++++++---------------------- src/mathed/InsetMathFrac.h | 112 +------- src/mathed/MathFactory.cpp | 24 +- src/mathed/MathParser.cpp | 15 + 5 files changed, 227 insertions(+), 447 deletions(-) diff --git a/lib/ui/stdtoolbars.inc b/lib/ui/stdtoolbars.inc index ae324b98eb..550f1b266b 100644 --- a/lib/ui/stdtoolbars.inc +++ b/lib/ui/stdtoolbars.inc @@ -302,14 +302,15 @@ ToolbarSet Toolbar "frac-square" "Fractions" Item "Standard \\frac" "math-insert \frac" - Item "No horizontal line \\atop" "math-insert \atop" Item "Nice fraction (3/4) \\nicefrac" "math-insert \nicefrac" Item "Unit (km) \\unit" "math-insert \unitone" Item "Unit (864 m) \\unit" "math-insert \unittwo" Item "Unit fraction (km/h) \\unitfrac" "math-insert \unitfrac" Item "Unit fraction (20 km/h) \\unitfrac" "math-insert \unitfracthree" - Item "Text fraction (amsmath) \\tfrac" "math-insert \tfrac" - Item "Display fraction (amsmath) \\dfrac" "math-insert \dfrac" + Item "Text fraction \\tfrac" "math-insert \tfrac" + Item "Display fraction \\dfrac" "math-insert \dfrac" + Item "Continued fraction \\cfrac" "math-insert \cfrac" + Item "Continued fraction (aligned) \\cfrac" "math-insert \cfracthree" Item "Binomial \\binom" "math-insert \binom" Item "Text binomial \\tbinom" "math-insert \tbinom" Item "Display binomial \\dbinom" "math-insert \dbinom" diff --git a/src/mathed/InsetMathFrac.cpp b/src/mathed/InsetMathFrac.cpp index 558bbcddf6..66c75ba4bc 100644 --- a/src/mathed/InsetMathFrac.cpp +++ b/src/mathed/InsetMathFrac.cpp @@ -85,7 +85,8 @@ InsetMathFrac const * InsetMathFrac::asFracInset() const bool InsetMathFrac::idxForward(Cursor & cur) const { InsetMath::idx_type target = 0; - if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3) + || (kind_ == CFRAC && nargs() == 3)) { if (nargs() == 3) target = 0; else if (nargs() == 2) @@ -103,7 +104,8 @@ bool InsetMathFrac::idxForward(Cursor & cur) const bool InsetMathFrac::idxBackward(Cursor & cur) const { InsetMath::idx_type target = 0; - if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3) + || (kind_ == CFRAC && nargs() == 3)) { if (nargs() == 3) target = 2; else if (nargs() == 2) @@ -157,13 +159,53 @@ void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const dim.wid = dim0.width() + dim1.wid + 5; dim.asc = dim0.height() + 5; dim.des = dim1.height() - 5; + } else if (kind_ == CFRAC) { + if (nargs() == 2) { + // cfrac is always in display size + StyleChanger dummy2(mi.base, LM_ST_DISPLAY); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + dim.wid = max(dim0.wid, dim1.wid) + 2; + dim.asc = dim0.height() + 2 + 5; + dim.des = dim1.height() + 2 - 5; + } else { + // cfrac is always in display size + StyleChanger dummy2(mi.base, LM_ST_DISPLAY); + // use text font for the optional argument + FontSetChanger dummy3(mi.base, "textnormal"); + cell(2).metrics(mi, dim2); + // return to math font + FontSetChanger dummy4(mi.base, "mathnormal"); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + dim.wid = 2 + max(dim0.wid, dim1.wid); + dim.asc = dim0.height() + 2 + 5; + dim.des = dim1.height() + 2 - 5; + } } else if (kind_ == UNITFRAC) { ShapeChanger dummy2(mi.base.font, UP_SHAPE); dim.wid = dim0.width() + dim1.wid + 5; dim.asc = dim0.height() + 5; dim.des = dim1.height() - 5; + } else if (kind_ == DFRAC) { + // dfrac is in always in display size + StyleChanger dummy2(mi.base, LM_ST_DISPLAY); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + dim.wid = max(dim0.wid, dim1.wid) + 2; + dim.asc = dim0.height() + 2 + 5; + dim.des = dim1.height() + 2 - 5; + } else if (kind_ == TFRAC) { + // tfrac is in always in text size + StyleChanger dummy2(mi.base, LM_ST_SCRIPT); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + dim.wid = max(dim0.wid, dim1.wid) + 2; + dim.asc = dim0.height() + 2 + 5; + dim.des = dim1.height() + 2 - 5; } else { - dim.wid = max(dim0.width(), dim1.wid) + 2; + // FRAC + dim.wid = max(dim0.wid, dim1.wid) + 2; dim.asc = dim0.height() + 2 + 5; dim.des = dim1.height() + 2 - 5; } @@ -212,12 +254,57 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const y - dim0.des - 5); cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2); + } else if (kind_ == CFRAC) { + if (nargs() == 2) { + // cfrac is always in display size + StyleChanger dummy2(pi.base, LM_ST_DISPLAY); + Dimension const dim0 = cell(0).dimension(*pi.base.bv); + Dimension const dim1 = cell(1).dimension(*pi.base.bv); + int m = x + dim.wid / 2; + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); + } else { + // cfrac is always in display size + StyleChanger dummy2(pi.base, LM_ST_DISPLAY); + // use text font for the optional argument + FontSetChanger dummy3(pi.base, "textnormal"); + Dimension const dim2 = cell(2).dimension(*pi.base.bv); + int w = mathed_char_width(pi.base.font, '['); + drawStrBlack(pi, x, y, from_ascii("[")); + x += w; + cell(2).draw(pi, x + 1, y); + x += dim2.wid; + drawStrBlack(pi, x, y, from_ascii("]")); + x += w; + // return to math font + FontSetChanger dummy4(pi.base, "mathnormal"); + Dimension const dim0 = cell(0).dimension(*pi.base.bv); + Dimension const dim1 = cell(1).dimension(*pi.base.bv); + int m = x + dim.wid / 2; + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); + } + } else if (kind_ == DFRAC) { + // dfrac is in always in display size + StyleChanger dummy2(pi.base, LM_ST_DISPLAY); + //Dimension const dim = dimension(*pi.base.bv); + Dimension const dim0 = cell(0).dimension(*pi.base.bv); + Dimension const dim1 = cell(1).dimension(*pi.base.bv); + int m = x + dim.wid / 2; + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); + } else if (kind_ == TFRAC) { + // tfrac is in always in text size + StyleChanger dummy2(pi.base, LM_ST_SCRIPT); + Dimension const dim0 = cell(0).dimension(*pi.base.bv); + Dimension const dim1 = cell(1).dimension(*pi.base.bv); + int m = x + dim.wid / 2; + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); } else { - // Classical fraction - cell(0).draw(pi, m - dim0.width() / 2, - y - dim0.des - 2 - 5); - cell(1).draw(pi, m - dim1.wid / 2, - y + dim1.asc + 2 - 5); + // FRAC + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); } } if (kind_ == NICEFRAC || kind_ == UNITFRAC) { @@ -225,13 +312,13 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const int xx = x; if (nargs() == 3) xx += cell(2).dimension(*pi.base.bv).wid + 5; - pi.pain.line(xx + dim0.wid, y + dim.des - 2, xx + dim0.wid + 5, y - dim.asc + 2, Color_math); } - if (kind_ == FRAC || kind_ == OVER) + if (kind_ == FRAC || kind_ == CFRAC || kind_ == DFRAC + || kind_ == TFRAC || kind_ == OVER) pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math); drawMarkers(pi, x, y); @@ -276,7 +363,15 @@ void InsetMathFrac::write(WriteStream & os) const os << "\\frac{" << cell(0) << "}{" << cell(1) << '}'; break; case FRAC: + case DFRAC: + case TFRAC: case NICEFRAC: + case CFRAC: + if (nargs() == 2) + InsetMathNest::write(os); + else + os << "\\cfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}'; + break; case UNITFRAC: if (nargs() == 2) InsetMathNest::write(os); @@ -298,6 +393,12 @@ docstring InsetMathFrac::name() const switch (kind_) { case FRAC: return from_ascii("frac"); + case CFRAC: + return from_ascii("cfrac"); + case DFRAC: + return from_ascii("dfrac"); + case TFRAC: + return from_ascii("tfrac"); case OVER: return from_ascii("over"); case NICEFRAC: @@ -340,7 +441,17 @@ void InsetMathFrac::octave(OctaveStream & os) const void InsetMathFrac::mathmlize(MathStream & os) const { - os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac"); + switch (kind_) { + case FRAC: + os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac"); + break; + case DFRAC: + os << MTag("mdfrac") << cell(0) << cell(1) << ETag("mdfrac"); + break; + case TFRAC: + os << MTag("mtfrac") << cell(0) << cell(1) << ETag("mtfrac"); + break; + } } @@ -348,177 +459,8 @@ void InsetMathFrac::validate(LaTeXFeatures & features) const { if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT) features.require("units"); - InsetMathNest::validate(features); -} - - -///////////////////////////////////////////////////////////////////// -// -// InsetMathDFrac -// -///////////////////////////////////////////////////////////////////// - - -Inset * InsetMathDFrac::clone() const -{ - return new InsetMathDFrac(*this); -} - - -void InsetMathDFrac::metrics(MetricsInfo & mi, Dimension & dim) const -{ - Dimension dim0, dim1; - cell(0).metrics(mi, dim0); - cell(1).metrics(mi, dim1); - dim.wid = max(dim0.wid, dim1.wid) + 2; - dim.asc = dim0.height() + 2 + 5; - dim.des = dim1.height() + 2 - 5; -} - - -void InsetMathDFrac::draw(PainterInfo & pi, int x, int y) const -{ - Dimension const dim = dimension(*pi.base.bv); - Dimension const & dim0 = cell(0).dimension(*pi.base.bv); - Dimension const & dim1 = cell(1).dimension(*pi.base.bv); - int m = x + dim.wid / 2; - cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); - cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); - pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math); - setPosCache(pi, x, y); -} - - -docstring InsetMathDFrac::name() const -{ - return from_ascii("dfrac"); -} - - -void InsetMathDFrac::mathmlize(MathStream & os) const -{ - os << MTag("mdfrac") << cell(0) << cell(1) << ETag("mdfrac"); -} - - -void InsetMathDFrac::validate(LaTeXFeatures & features) const -{ - features.require("amsmath"); - InsetMathNest::validate(features); -} - - -///////////////////////////////////////////////////////////////////// -// -// InsetMathTFrac -// -///////////////////////////////////////////////////////////////////// - - -Inset * InsetMathTFrac::clone() const -{ - return new InsetMathTFrac(*this); -} - - -void InsetMathTFrac::metrics(MetricsInfo & mi, Dimension & dim) const -{ - StyleChanger dummy(mi.base, LM_ST_SCRIPT); - Dimension dim0; - cell(0).metrics(mi, dim0); - Dimension dim1; - cell(1).metrics(mi, dim1); - dim.wid = max(dim0.width(), dim1.width()) + 2; - dim.asc = dim0.height() + 2 + 5; - dim.des = dim1.height() + 2 - 5; -} - - -void InsetMathTFrac::draw(PainterInfo & pi, int x, int y) const -{ - StyleChanger dummy(pi.base, LM_ST_SCRIPT); - Dimension const dim = dimension(*pi.base.bv); - Dimension const & dim0 = cell(0).dimension(*pi.base.bv); - Dimension const & dim1 = cell(1).dimension(*pi.base.bv); - int m = x + dim.wid / 2; - cell(0).draw(pi, m - dim0.width() / 2, y - dim0.descent() - 2 - 5); - cell(1).draw(pi, m - dim1.width() / 2, y + dim1.ascent() + 2 - 5); - pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math); - setPosCache(pi, x, y); -} - - -docstring InsetMathTFrac::name() const -{ - return from_ascii("tfrac"); -} - - -void InsetMathTFrac::mathmlize(MathStream & os) const -{ - os << MTag("mtfrac") << cell(0) << cell(1) << ETag("mtfrac"); -} - - -void InsetMathTFrac::validate(LaTeXFeatures & features) const -{ - features.require("amsmath"); - InsetMathNest::validate(features); -} - - -///////////////////////////////////////////////////////////////////// -// -// InsetMathCFrac -// -///////////////////////////////////////////////////////////////////// - - -Inset * InsetMathCFrac::clone() const -{ - return new InsetMathCFrac(*this); -} - - -void InsetMathCFrac::metrics(MetricsInfo & mi, Dimension & dim) const -{ - Dimension dim0, dim1; - cell(0).metrics(mi, dim0); - cell(1).metrics(mi, dim1); - dim.wid = max(dim0.wid, dim1.wid) + 2; - dim.asc = dim0.height() + 2 + 5; - dim.des = dim1.height() + 2 - 5; -} - - -void InsetMathCFrac::draw(PainterInfo & pi, int x, int y) const -{ - Dimension const dim = dimension(*pi.base.bv); - Dimension const & dim0 = cell(0).dimension(*pi.base.bv); - Dimension const & dim1 = cell(1).dimension(*pi.base.bv); - int m = x + dim.wid / 2; - cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5); - cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5); - pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math); - setPosCache(pi, x, y); -} - - -docstring InsetMathCFrac::name() const -{ - return from_ascii("cfrac"); -} - - -void InsetMathCFrac::mathmlize(MathStream & os) const -{ - os << MTag("mcfrac") << cell(0) << cell(1) << ETag("mcfrac"); -} - - -void InsetMathCFrac::validate(LaTeXFeatures & features) const -{ - features.require("amsmath"); + if (kind_ == CFRAC || kind_ == DFRAC || kind_ == TFRAC) + features.require("amsmath"); InsetMathNest::validate(features); } @@ -554,13 +496,26 @@ int InsetMathBinom::dw(int height) const void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const { - FracChanger dummy(mi.base); Dimension dim0, dim1; - cell(0).metrics(mi, dim0); - cell(1).metrics(mi, dim1); + + // FIXME: for an unknown reason the cells must be set directly + // after the StyleChanger and cannot be set after the if case + if (kind_ == DBINOM) { + StyleChanger dummy(mi.base, LM_ST_DISPLAY); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + } else if (kind_ == TBINOM) { + StyleChanger dummy(mi.base, LM_ST_SCRIPT); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + } else { + FracChanger dummy(mi.base); + cell(0).metrics(mi, dim0); + cell(1).metrics(mi, dim1); + } dim.asc = dim0.height() + 4 + 5; dim.des = dim1.height() + 4 - 5; - dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4; + dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4; metricsMarkers2(dim); } @@ -570,17 +525,33 @@ void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const Dimension const dim = dimension(*pi.base.bv); Dimension const & dim0 = cell(0).dimension(*pi.base.bv); Dimension const & dim1 = cell(1).dimension(*pi.base.bv); + // define the binom brackets docstring const bra = kind_ == BRACE ? from_ascii("{") : - kind_ == BRACK ? from_ascii("[") : from_ascii("("); + kind_ == BRACK ? from_ascii("[") : from_ascii("("); docstring const ket = kind_ == BRACE ? from_ascii("}") : - kind_ == BRACK ? from_ascii("]") : from_ascii(")"); + kind_ == BRACK ? from_ascii("]") : from_ascii(")"); + int m = x + dim.width() / 2; - FracChanger dummy(pi.base); - cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5); - cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); - mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), bra); - mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(), - dw(dim.height()), dim.height(), ket); + // FIXME: for an unknown reason the cells must be drawn directly + // after the StyleChanger and cannot be drawn after the if case + if (kind_ == DBINOM) { + StyleChanger dummy(pi.base, LM_ST_DISPLAY); + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); + } else if (kind_ == TBINOM) { + StyleChanger dummy(pi.base, LM_ST_SCRIPT); + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); + } else { + FracChanger dummy2(pi.base); + cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5); + cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); + } + // draw the brackets and the marker + mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), + dim.height(), bra); + mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), + y - dim.ascent(), dw(dim.height()), dim.height(), ket); drawMarkers2(pi, x, y); } @@ -598,6 +569,12 @@ void InsetMathBinom::write(WriteStream & os) const case BINOM: os << "\\binom{" << cell(0) << "}{" << cell(1) << '}'; break; + case DBINOM: + os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}'; + break; + case TBINOM: + os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}'; + break; case CHOOSE: os << '{' << cell(0) << " \\choose " << cell(1) << '}'; break; @@ -617,147 +594,30 @@ void InsetMathBinom::normalize(NormalStream & os) const } -void InsetMathBinom::validate(LaTeXFeatures & features) const -{ - if (kind_ == BINOM) - features.require("binom"); - InsetMathNest::validate(features); -} - - -///////////////////////////////////////////////////////////////////// -// -// InsetMathDBinom -// -///////////////////////////////////////////////////////////////////// - -Inset * InsetMathDBinom::clone() const -{ - return new InsetMathDBinom(*this); -} - - -int InsetMathDBinom::dw(int height) const -{ - int w = height / 5; - if (w > 15) - w = 15; - if (w < 6) - w = 6; - return w; -} - - -void InsetMathDBinom::metrics(MetricsInfo & mi, Dimension & dim) const -{ - Dimension dim0, dim1; - cell(0).metrics(mi, dim0); - cell(1).metrics(mi, dim1); - dim.asc = dim0.height() + 4 + 5; - dim.des = dim1.height() + 4 - 5; - dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4; - metricsMarkers2(dim); -} - - -void InsetMathDBinom::draw(PainterInfo & pi, int x, int y) const -{ - Dimension const dim = dimension(*pi.base.bv); - Dimension const & dim0 = cell(0).dimension(*pi.base.bv); - Dimension const & dim1 = cell(1).dimension(*pi.base.bv); - int m = x + dim.width() / 2; - cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5); - cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); - mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("(")); - mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(), - dw(dim.height()), dim.height(), from_ascii(")")); - drawMarkers2(pi, x, y); -} - - -docstring InsetMathDBinom::name() const +void InsetMathBinom::mathmlize(MathStream & os) const { - return from_ascii("dbinom"); + switch (kind_) { + case BINOM: + os << MTag("mbinom") << cell(0) << cell(1) << ETag("mbinom"); + break; + case DBINOM: + os << MTag("mdbinom") << cell(0) << cell(1) << ETag("mdbinom"); + break; + case TBINOM: + os << MTag("mtbinom") << cell(0) << cell(1) << ETag("mtbinom"); + break; + } } -void InsetMathDBinom::mathmlize(MathStream & os) const -{ - os << MTag("mdbinom") << cell(0) << cell(1) << ETag("mdbinom"); -} -void InsetMathDBinom::validate(LaTeXFeatures & features) const +void InsetMathBinom::validate(LaTeXFeatures & features) const { - features.require("amsmath"); + if (kind_ == BINOM) + features.require("binom"); + if (kind_ == DBINOM || kind_ == TBINOM) + features.require("amsmath"); InsetMathNest::validate(features); } -///////////////////////////////////////////////////////////////////// -// -// InsetMathTBinom -// -///////////////////////////////////////////////////////////////////// - -Inset * InsetMathTBinom::clone() const -{ - return new InsetMathTBinom(*this); -} - - -int InsetMathTBinom::dw(int height) const -{ - int w = height / 5; - if (w > 15) - w = 15; - if (w < 6) - w = 6; - return w; -} - - -void InsetMathTBinom::metrics(MetricsInfo & mi, Dimension & dim) const -{ - StyleChanger dummy(mi.base, LM_ST_SCRIPT); - Dimension dim0, dim1; - cell(0).metrics(mi, dim0); - cell(1).metrics(mi, dim1); - dim.asc = dim0.height() + 4 + 5; - dim.des = dim1.height() + 4 - 5; - dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4; - metricsMarkers2(dim); -} - - -void InsetMathTBinom::draw(PainterInfo & pi, int x, int y) const -{ - StyleChanger dummy(pi.base, LM_ST_SCRIPT); - Dimension const dim = dimension(*pi.base.bv); - Dimension const & dim0 = cell(0).dimension(*pi.base.bv); - Dimension const & dim1 = cell(1).dimension(*pi.base.bv); - int m = x + dim.width() / 2; - cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5); - cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5); - mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("(")); - mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(), - dw(dim.height()), dim.height(), from_ascii(")")); - drawMarkers2(pi, x, y); -} - - -docstring InsetMathTBinom::name() const -{ - return from_ascii("tbinom"); -} - -void InsetMathTBinom::mathmlize(MathStream & os) const -{ - os << MTag("mtbinom") << cell(0) << cell(1) << ETag("mtbinom"); -} - -void InsetMathTBinom::validate(LaTeXFeatures & features) const -{ - features.require("amsmath"); - InsetMathNest::validate(features); -} - } // namespace lyx diff --git a/src/mathed/InsetMathFrac.h b/src/mathed/InsetMathFrac.h index 59c198691b..3254405c35 100644 --- a/src/mathed/InsetMathFrac.h +++ b/src/mathed/InsetMathFrac.h @@ -40,13 +40,15 @@ public: /// enum Kind { FRAC, + CFRAC, + DFRAC, + TFRAC, OVER, ATOP, NICEFRAC, UNITFRAC, UNIT }; - /// explicit InsetMathFrac(Kind kind = FRAC, idx_type ncells = 2); /// @@ -69,7 +71,6 @@ public: docstring name() const; /// bool extraBraces() const; - /// void write(WriteStream & os) const; /// @@ -89,65 +90,6 @@ public: }; -/// \dfrac support -class InsetMathDFrac : public InsetMathFrac { -public: - /// - InsetMathDFrac() {} - /// - void metrics(MetricsInfo & mi, Dimension & dim) const; - /// - void draw(PainterInfo &, int x, int y) const; - /// - docstring name() const; - /// - void mathmlize(MathStream &) const; - /// - void validate(LaTeXFeatures & features) const; -private: - Inset * clone() const; -}; - - -/// \tfrac support -class InsetMathTFrac : public InsetMathFrac { -public: - /// - InsetMathTFrac() {} - /// - void metrics(MetricsInfo & mi, Dimension & dim) const; - /// - void draw(PainterInfo &, int x, int y) const; - /// - docstring name() const; - /// - void mathmlize(MathStream &) const; - /// - void validate(LaTeXFeatures & features) const; -private: - Inset * clone() const; -}; - - -/// \cfrac support -class InsetMathCFrac : public InsetMathFrac { -public: - /// - InsetMathCFrac() {} - /// - void metrics(MetricsInfo & mi, Dimension & dim) const; - /// - void draw(PainterInfo &, int x, int y) const; - /// - docstring name() const; - /// - void mathmlize(MathStream &) const; - /// - void validate(LaTeXFeatures & features) const; -private: - Inset * clone() const; -}; - /// Binom like objects class InsetMathBinom : public InsetMathFracBase { @@ -155,11 +97,12 @@ public: /// enum Kind { BINOM, + DBINOM, + TBINOM, CHOOSE, BRACE, BRACK }; - /// explicit InsetMathBinom(Kind kind = BINOM); /// @@ -176,28 +119,6 @@ public: /// bool extraBraces() const; /// - void validate(LaTeXFeatures & features) const; -private: - Inset * clone() const; - /// - int dw(int height) const; - /// - Kind kind_; -}; - - -/// \dbinom support -class InsetMathDBinom : public InsetMathFracBase { -public: - /// - InsetMathDBinom() {} - /// - void metrics(MetricsInfo & mi, Dimension & dim) const; - /// - void draw(PainterInfo &, int x, int y) const; - /// - docstring name() const; - /// void mathmlize(MathStream &) const; /// void validate(LaTeXFeatures & features) const; @@ -205,31 +126,12 @@ private: Inset * clone() const; /// int dw(int height) const; -}; - - -/// \tbinom support -class InsetMathTBinom : public InsetMathFracBase { -public: - /// - InsetMathTBinom() {} - /// - void metrics(MetricsInfo & mi, Dimension & dim) const; - /// - void draw(PainterInfo &, int x, int y) const; /// - docstring name() const; - /// - void mathmlize(MathStream &) const; - /// - void validate(LaTeXFeatures & features) const; -private: - Inset * clone() const; - /// - int dw(int height) const; + Kind kind_; }; + } // namespace lyx #endif diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 16b6a2e28d..52ba762f67 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -410,6 +410,10 @@ MathAtom createInsetMath(docstring const & s) return MathAtom(new InsetMathStackrel); if (s == "binom") return MathAtom(new InsetMathBinom(InsetMathBinom::BINOM)); + if (s == "dbinom") + return MathAtom(new InsetMathBinom(InsetMathBinom::DBINOM)); + if (s == "tbinom") + return MathAtom(new InsetMathBinom(InsetMathBinom::TBINOM)); if (s == "choose") return MathAtom(new InsetMathBinom(InsetMathBinom::CHOOSE)); if (s == "brace") @@ -418,19 +422,27 @@ MathAtom createInsetMath(docstring const & s) return MathAtom(new InsetMathBinom(InsetMathBinom::BRACK)); if (s == "frac") return MathAtom(new InsetMathFrac); + if (s == "cfrac") + return MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC)); + if (s == "dfrac") + return MathAtom(new InsetMathFrac(InsetMathFrac::DFRAC)); + if (s == "tfrac") + return MathAtom(new InsetMathFrac(InsetMathFrac::TFRAC)); if (s == "over") return MathAtom(new InsetMathFrac(InsetMathFrac::OVER)); if (s == "nicefrac") return MathAtom(new InsetMathFrac(InsetMathFrac::NICEFRAC)); if (s == "unitfrac") return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)); - // This string value is only for math toolbar use. Not a LaTeX name + // These string values are only for math toolbar use, no LaTeX names if (s == "unitfracthree") return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)); if (s == "unitone") return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)); if (s == "unittwo") return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)); + if (s == "cfracthree") + return MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC, 3)); //if (s == "infer") // return MathAtom(new MathInferInset); if (s == "atop") @@ -447,16 +459,6 @@ MathAtom createInsetMath(docstring const & s) return MathAtom(new InsetMathColor(true)); if (s == "textcolor") return MathAtom(new InsetMathColor(false)); - if (s == "cfrac") - return MathAtom(new InsetMathCFrac); - if (s == "dfrac") - return MathAtom(new InsetMathDFrac); - if (s == "tfrac") - return MathAtom(new InsetMathTFrac); - if (s == "dbinom") - return MathAtom(new InsetMathDBinom); - if (s == "tbinom") - return MathAtom(new InsetMathTBinom); if (s == "hphantom") return MathAtom(new InsetMathPhantom(InsetMathPhantom::hphantom)); if (s == "phantom") diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index c16e3cfe06..7456fce668 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -1302,6 +1302,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode); } } + else if (t.cs() == "unitfrac") { // Here allowed formats are \unitfrac[val]{num}{denom} MathData ar; @@ -1316,6 +1317,20 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode); } + else if (t.cs() == "cfrac") { + // Here allowed formats are \cfrac[pos]{num}{denom} + MathData ar; + parse(ar, FLAG_OPTION, mode); + if (ar.size()) { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC, 3))); + cell->back().nucleus()->cell(2) = ar; + } else { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC))); + } + parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode); + parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode); + } + else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") { cell->push_back(createInsetMath(t.cs())); parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode); -- 2.39.2