From f390780a3577392e1136aa8f01e0a29934cb6b3a Mon Sep 17 00:00:00 2001 From: Martin Vermeer Date: Tue, 18 Sep 2007 15:04:58 +0000 Subject: [PATCH] Units corrections, all Georg's noticed problems git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20337 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ui/stdtoolbars.inc | 5 +- src/mathed/InsetMathFrac.cpp | 115 ++++++++++++++++++++--------------- src/mathed/InsetMathFrac.h | 3 +- src/mathed/MathFactory.cpp | 7 ++- src/mathed/MathParser.cpp | 30 ++++++++- 5 files changed, 103 insertions(+), 57 deletions(-) diff --git a/lib/ui/stdtoolbars.inc b/lib/ui/stdtoolbars.inc index 0caab3fa1a..1e063263a8 100644 --- a/lib/ui/stdtoolbars.inc +++ b/lib/ui/stdtoolbars.inc @@ -274,9 +274,10 @@ ToolbarSet Item "Standard \\frac" "math-insert \frac" Item "No hor. line \\atop" "math-insert \atop" Item "Nice (3/4) \\nicefrac" "math-insert \nicefrac" + Item "Unit (km) \\unit" "math-insert \unitone" Item "Unit (864 m) \\unit" "math-insert \unit" - Item "Units (km/h) \\unitfrac" "math-insert \unitfrac" - Item "Units3 ( 20 km/h) \\unitfrac3" "math-insert \unitfracthree" + Item "Unitfrac (km/h) \\unitfrac" "math-insert \unitfrac" + Item "Unitfrac (20 km/h) \\unitfrac" "math-insert \unitfracthree" Item "Text frac (amsmath) \\tfrac" "math-insert \tfrac" Item "Display frac (amsmath) \\dfrac" "math-insert \dfrac" Item "Binomial \\choose" "math-insert \choose" diff --git a/src/mathed/InsetMathFrac.cpp b/src/mathed/InsetMathFrac.cpp index 59c0d58c59..5ebbb27e0d 100644 --- a/src/mathed/InsetMathFrac.cpp +++ b/src/mathed/InsetMathFrac.cpp @@ -49,11 +49,12 @@ InsetMathFrac const * InsetMathFrac::asFracInset() const bool InsetMathFrac::idxRight(Cursor & cur) const { InsetMath::idx_type target; - if (kind_ == UNITFRAC3) - target = 0; - else if (kind_ == UNIT) - target = 1; - else + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (nargs() == 3) + target = 0; + else if (nargs() == 2) + target = 1; + } else return false; if (cur.idx() == target) return false; @@ -66,11 +67,12 @@ bool InsetMathFrac::idxRight(Cursor & cur) const bool InsetMathFrac::idxLeft(Cursor & cur) const { InsetMath::idx_type target; - if (kind_ == UNITFRAC3) - target = 2; - else if (kind_ == UNIT) - target = 0; - else + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (nargs() == 3) + target = 2; + else if (nargs() == 2) + target = 0; + } else return false; if (cur.idx() == target) return false; @@ -82,22 +84,30 @@ bool InsetMathFrac::idxLeft(Cursor & cur) const bool InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const { - if (kind_ == UNIT) { - cell(0).metrics(mi); - ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE); - cell(1).metrics(mi); - dim.wid = cell(0).width() + cell(1).width() + 5; - dim.asc = std::max(cell(0).ascent(), cell(1).ascent()); - dim.des = std::max(cell(0).descent(), cell(1).descent()); - } else if (kind_ == UNITFRAC3) { - cell(2).metrics(mi); - ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE); - FracChanger dummy(mi.base); - cell(0).metrics(mi); - cell(1).metrics(mi); - dim.wid = cell(0).width() + cell(1).width() + cell(2).width() + 10; - dim.asc = std::max(cell(2).ascent(), cell(0).height() + 5); - dim.des = std::max(cell(2).descent(), cell(1).height() - 5); + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (nargs() == 1) { + ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE); + cell(0).metrics(mi); + dim.wid = cell(0).width()+ 3; + dim.asc = cell(0).ascent(); + dim.des = cell(0).descent(); + } else if (nargs() == 2) { + cell(0).metrics(mi); + ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE); + cell(1).metrics(mi); + dim.wid = cell(0).width() + cell(1).width() + 5; + dim.asc = std::max(cell(0).ascent(), cell(1).ascent()); + dim.des = std::max(cell(0).descent(), cell(1).descent()); + } else { + cell(2).metrics(mi); + ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE); + FracChanger dummy(mi.base); + cell(0).metrics(mi); + cell(1).metrics(mi); + dim.wid = cell(0).width() + cell(1).width() + cell(2).width() + 10; + dim.asc = std::max(cell(2).ascent(), cell(0).height() + 5); + dim.des = std::max(cell(2).descent(), cell(1).height() - 5); + } } else { FracChanger dummy(mi.base); cell(0).metrics(mi); @@ -129,19 +139,24 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const { setPosCache(pi, x, y); int m = x + dim_.wid / 2; - if (kind_ == UNIT) { - cell(0).draw(pi, x + 1, y); - ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE); - cell(1).draw(pi, x + cell(0).width() + 5, y); - } else if (kind_ == UNITFRAC3) { - cell(2).draw(pi, x + 1, y); - ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE); - FracChanger dummy(pi.base); - int xx = x + cell(2).width() + 5; - cell(0).draw(pi, xx + 2, - y - cell(0).descent() - 5); - cell(1).draw(pi, xx + cell(0).width() + 5, - y + cell(1).ascent() / 2); + if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) { + if (nargs() == 1) { + ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE); + cell(0).draw(pi, x + 1, y); + } else if (nargs() == 2) { + cell(0).draw(pi, x + 1, y); + ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE); + cell(1).draw(pi, x + cell(0).width() + 5, y); + } else { + cell(2).draw(pi, x + 1, y); + ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE); + FracChanger dummy(pi.base); + int xx = x + cell(2).width() + 5; + cell(0).draw(pi, xx + 2, + y - cell(0).descent() - 5); + cell(1).draw(pi, xx + cell(0).width() + 5, + y + cell(1).ascent() / 2); + } } else { FracChanger dummy(pi.base); if (kind_ == NICEFRAC) { @@ -163,10 +178,10 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const y + cell(1).ascent() + 2 - 5); } } - if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNITFRAC3) { + if (kind_ == NICEFRAC || kind_ == UNITFRAC) { // Diag line: int xx = x; - if (kind_ == UNITFRAC3) + if (nargs() == 3) xx += cell(2).width() + 5; pi.pain.line(xx + cell(0).width(), y + dim_.des - 2, @@ -215,13 +230,16 @@ void InsetMathFrac::write(WriteStream & os) const case FRAC: case NICEFRAC: case UNITFRAC: - InsetMathNest::write(os); + if (nargs() == 2) + InsetMathNest::write(os); + else + os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}'; break; case UNIT: - os << "\\unit[" << cell(0) << "]{" << cell(1) << '}'; - break; - case UNITFRAC3: - os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}'; + if (nargs() == 2) + os << "\\unit[" << cell(0) << "]{" << cell(1) << '}'; + else + os << "\\unit{" << cell(0) << '}'; break; } } @@ -238,8 +256,6 @@ docstring InsetMathFrac::name() const return from_ascii("nicefrac"); case UNITFRAC: return from_ascii("unitfrac"); - case UNITFRAC3: - return from_ascii("unitfracthree"); case UNIT: return from_ascii("unit"); case ATOP: @@ -282,8 +298,7 @@ void InsetMathFrac::mathmlize(MathStream & os) const void InsetMathFrac::validate(LaTeXFeatures & features) const { - if (kind_ == NICEFRAC || kind_ == UNITFRAC - || kind_ == UNIT || kind_ == UNITFRAC3) + if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT) features.require("units"); InsetMathNest::validate(features); } diff --git a/src/mathed/InsetMathFrac.h b/src/mathed/InsetMathFrac.h index 6668654b75..1f9a25526a 100644 --- a/src/mathed/InsetMathFrac.h +++ b/src/mathed/InsetMathFrac.h @@ -29,8 +29,7 @@ public: ATOP, NICEFRAC, UNITFRAC, - UNIT, - UNITFRAC3 + UNIT }; /// diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 16801643d4..2dc518f5f1 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -260,7 +260,7 @@ MathAtom createInsetMath(char const * const s) MathAtom createInsetMath(docstring const & s) { - //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl; + lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl; latexkeys const * l = in_word_set(s); if (l) { docstring const & inset = l->inset; @@ -372,8 +372,11 @@ MathAtom createInsetMath(docstring const & s) 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 if (s == "unitfracthree") - return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC3, 3)); + return MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)); + if (s == "unitone") + return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)); if (s == "unit") return MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)); //if (s == "infer") diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index 7044009806..0c8ee7765a 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -47,6 +47,7 @@ following hack as starting point to write some macros: #include "InsetMathComment.h" #include "InsetMathDelim.h" #include "InsetMathEnv.h" +#include "InsetMathFrac.h" #include "InsetMathKern.h" #include "MathMacro.h" #include "InsetMathPar.h" @@ -241,7 +242,7 @@ enum { FLAG_END = 1 << 3, // next \\end ends the parsing process FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing FLAG_TEXTMODE = 1 << 5, // we are in a box - FLAG_ITEM = 1 << 6, // read a (possibly braced token) + FLAG_ITEM = 1 << 6, // read a (possibly braced) token FLAG_LEAVE = 1 << 7, // leave the loop at the end FLAG_SIMPLE = 1 << 8, // next $ leaves the loop FLAG_EQUATION = 1 << 9, // next \] leaves the loop @@ -1097,6 +1098,33 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags, } } + else if (t.cs() == "unit") { + // Allowed formats \unit[val]{unit} + MathData ar; + parse(ar, FLAG_OPTION, mode); + if (ar.size()) { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT))); + cell->back().nucleus()->cell(0) = ar; + parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode); + } else { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1))); + parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode); + } + } + else if (t.cs() == "unitfrac") { + // Here allowed formats are \unitfrac[val]{num}{denom} + MathData ar; + parse(ar, FLAG_OPTION, mode); + if (ar.size()) { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3))); + cell->back().nucleus()->cell(2) = ar; + } else { + cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC))); + } + 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