From dd60fa8ccbc203482c803c941a44b84e82dc8dee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Mon, 11 Feb 2002 08:19:02 +0000 Subject: [PATCH] fix parser bug; some stuff for hadling xymatrix (not 'activated' yet) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3514 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/math_gridinset.C | 75 ++++++++++++++--------- src/mathed/math_gridinset.h | 11 ++++ src/mathed/math_inset.h | 2 + src/mathed/math_metricsinfo.h | 9 ++- src/mathed/math_nestinset.C | 8 ++- src/mathed/math_parser.C | 42 +++++++++++-- src/mathed/math_xdata.h | 4 ++ src/mathed/math_xyarrowinset.C | 103 ++++++++++++++++++++++++++++++-- src/mathed/math_xyarrowinset.h | 22 ++++++- src/mathed/math_xymatrixinset.C | 12 ++++ src/mathed/math_xymatrixinset.h | 8 ++- 11 files changed, 250 insertions(+), 46 deletions(-) diff --git a/src/mathed/math_gridinset.C b/src/mathed/math_gridinset.C index 91613a49b1..a766dac670 100644 --- a/src/mathed/math_gridinset.C +++ b/src/mathed/math_gridinset.C @@ -12,18 +12,6 @@ namespace { -/// -int const COLSEP = 6; -/// -int const ROWSEP = 6; -/// -int const HLINESEP = 3; -/// -int const VLINESEP = 3; -/// -int const BORDER = 2; - - string verboseHLine(int n) { string res; @@ -239,7 +227,7 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const rowinfo_[row].ascent_ = asc; rowinfo_[row].descent_ = desc; } - rowinfo_[0].ascent_ += HLINESEP * rowinfo_[0].lines_; + rowinfo_[0].ascent_ += hlinesep() * rowinfo_[0].lines_; rowinfo_[nrows()].ascent_ = 0; rowinfo_[nrows()].descent_ = 0; @@ -250,8 +238,8 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const rowinfo_[row - 1].offset_ + rowinfo_[row - 1].descent_ + rowinfo_[row - 1].skipPixels() + - ROWSEP + - rowinfo_[row].lines_ * HLINESEP + + rowsep() + + rowinfo_[row].lines_ * hlinesep() + rowinfo_[row].ascent_; } @@ -281,31 +269,31 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const colinfo_[ncols()].width_ = 0; // compute horizontal offsets - colinfo_[0].offset_ = BORDER; + colinfo_[0].offset_ = border(); for (col_type col = 1; col <= ncols(); ++col) { colinfo_[col].offset_ = colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + colinfo_[col - 1].skip_ + - COLSEP + - colinfo_[col].lines_ * VLINESEP; + colsep() + + colinfo_[col].lines_ * vlinesep(); } width_ = colinfo_[ncols() - 1].offset_ + colinfo_[ncols() - 1].width_ - + VLINESEP * colinfo_[ncols()].lines_ - + BORDER; + + vlinesep() * colinfo_[ncols()].lines_ + + border(); ascent_ = - rowinfo_[0].offset_ + rowinfo_[0].ascent_ - + HLINESEP * rowinfo_[0].lines_ - + BORDER; + + hlinesep() * rowinfo_[0].lines_ + + border(); descent_ = rowinfo_[nrows() - 1].offset_ + rowinfo_[nrows() - 1].descent_ - + HLINESEP * rowinfo_[nrows()].lines_ - + BORDER; + + hlinesep() * rowinfo_[nrows()].lines_ + + border(); /* @@ -319,7 +307,7 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const ws_[0] = 7 * workwidth / 8; // Adjust local tabs - width = COLSEP; + width = colsep(); for (cxrow = row_.begin(); cxrow; ++cxrow) { int rg = COLSEP; int lf = 0; @@ -351,9 +339,9 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const } int const ww = (isvoid) ? lf : lf + cxrow->getTab(i); cxrow->setTab(i, lf + rg); - rg = ws_[i] - ww + COLSEP; + rg = ws_[i] - ww + colsep(); if (cxrow == row_.begin()) - width += ws_[i] + COLSEP; + width += ws_[i] + colsep(); } cxrow->setBaseline(cxrow->getBaseline() - ascent); } @@ -369,14 +357,14 @@ void MathGridInset::draw(Painter & pain, int x, int y) const for (row_type row = 0; row <= nrows(); ++row) for (int i = 0; i < rowinfo_[row].lines_; ++i) { int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_ - - i * HLINESEP - HLINESEP/2 - ROWSEP/2; + - i * hlinesep() - hlinesep()/2 - rowsep()/2; pain.line(x + 1, yy, x + width_ - 1, yy); } for (col_type col = 0; col <= ncols(); ++col) for (int i = 0; i < colinfo_[col].lines_; ++i) { int xx = x + colinfo_[col].offset_ - - i * VLINESEP - VLINESEP/2 - COLSEP/2; + - i * vlinesep() - vlinesep()/2 - colsep()/2; pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1); } } @@ -704,3 +692,32 @@ void MathGridInset::write(WriteStream & os) const os << "\\\\" << s; } + +int MathGridInset::colsep() const +{ + return 6; +} + + +int MathGridInset::rowsep() const +{ + return 6; +} + + +int MathGridInset::hlinesep() const +{ + return 3; +} + + +int MathGridInset::vlinesep() const +{ + return 3; +} + + +int MathGridInset::border() const +{ + return 2; +} diff --git a/src/mathed/math_gridinset.h b/src/mathed/math_gridinset.h index 3f2747bdec..fc06d7c0b0 100644 --- a/src/mathed/math_gridinset.h +++ b/src/mathed/math_gridinset.h @@ -151,6 +151,17 @@ public: /// void setDefaults(); + /// + virtual int colsep() const; + /// + virtual int rowsep() const; + /// + virtual int hlinesep() const; + /// + virtual int vlinesep() const; + /// + virtual int border() const; + /// void write(WriteStream & os) const; /// diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 42957a9b6e..509eaee2ce 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -62,6 +62,7 @@ class MathStringInset; class MathSpaceInset; class MathSymbolInset; class MathUnknownInset; +class MathXYMatrixInset; class NormalStream; class OctaveStream; @@ -197,6 +198,7 @@ public: virtual MathStringInset * asStringInset() { return 0; } virtual MathSymbolInset const * asSymbolInset() const { return 0; } virtual MathUnknownInset const * asUnknownInset() const { return 0; } + virtual MathXYMatrixInset const* asXYMatrixInset() const{ return 0; } virtual UpdatableInset * asHyperActiveInset() const { return 0; } /// identifies things that can get scripts diff --git a/src/mathed/math_metricsinfo.h b/src/mathed/math_metricsinfo.h index ee67ca7186..9c2e720547 100644 --- a/src/mathed/math_metricsinfo.h +++ b/src/mathed/math_metricsinfo.h @@ -4,6 +4,7 @@ #include "lyxfont.h" class BufferView; +class MathNestInset; /// Standard Math Sizes (Math mode styles) @@ -22,11 +23,11 @@ enum MathStyles { struct MathMetricsInfo { /// MathMetricsInfo() - : view(0), font(), style(LM_ST_TEXT) + : view(0), font(), style(LM_ST_TEXT), inset(0) {} /// MathMetricsInfo(BufferView * v, LyXFont const & f, MathStyles s) - : view(v), font(f), style(s) + : view(v), font(f), style(s), inset(0) {} /// @@ -35,6 +36,10 @@ struct MathMetricsInfo { LyXFont font; /// MathStyles style; + /// used to pass some info down + MathNestInset const * inset; + /// + int idx; }; #endif diff --git a/src/mathed/math_nestinset.C b/src/mathed/math_nestinset.C index 59b974f523..7708cb185d 100644 --- a/src/mathed/math_nestinset.C +++ b/src/mathed/math_nestinset.C @@ -51,8 +51,12 @@ void MathNestInset::substitute(MathMacro const & m) void MathNestInset::metrics(MathMetricsInfo const & mi) const { - for (idx_type i = 0; i < nargs(); ++i) - xcell(i).metrics(mi); + MathMetricsInfo m = mi; + m.inset = this; + for (idx_type i = 0; i < nargs(); ++i) { + m.idx = i; + xcell(i).metrics(m); + } } diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 08d06ca3af..089e965a50 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -71,6 +71,7 @@ point to write some macros: #include "math_splitinset.h" #include "math_sqrtinset.h" #include "math_support.h" +#include "math_xyarrowinset.h" #include "lyxlex.h" #include "debug.h" @@ -268,6 +269,8 @@ private: bool parse_lines(MathAtom & t, bool numbered, bool outmost); /// parses {... & ... \\ ... & ... } bool parse_lines2(MathAtom & t); + /// dump contents to screen + void dump() const; private: /// @@ -421,6 +424,8 @@ char Parser::getChar() string Parser::getArg(char lf, char rg) { + skipSpaces(); + string result; char c = getChar(); @@ -507,18 +512,23 @@ void Parser::tokenize(string const & buffer) } } -#if 0 + //dump(); +} + + +void Parser::dump() const +{ lyxerr << "\nTokens: "; for (unsigned i = 0; i < tokens_.size(); ++i) lyxerr << tokens_[i]; lyxerr << "\n"; -#endif } void Parser::error(string const & msg) { lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl; + dump(); //exit(1); } @@ -528,6 +538,7 @@ bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost) { MathGridInset * p = t->asGridInset(); if (!p) { + dump(); lyxerr << "error in Parser::parse_lines() 1\n"; return false; } @@ -1076,11 +1087,30 @@ void Parser::parse_into1(MathArray & array, unsigned flags, MathTextCodes code) parse_lines2(array.back()); } - // Disabled #if 0 - else if (0 && t.cs() == "ar") { - array.push_back(createMathInset(t.cs())); - parse_lines2(array.back()); + // Disabled + else if (1 && t.cs() == "ar") { + MathXYArrowInset * p = new MathXYArrowInset; + + // try to read target + char c = getChar(); + if (c == '[') { + parse_into(p->cell(0), FLAG_BRACK_END); + //lyxerr << "read target: " << p->cell(0) << "\n"; + } else { + putback(); + } + + // try to read label + if (nextToken().cat() == catSuper || nextToken().cat() == catSub) { + p->up_ = nextToken().cat() == catSuper; + getToken(); + parse_into(p->cell(1), FLAG_ITEM); + //lyxerr << "read label: " << p->cell(1) << "\n"; + } + + array.push_back(MathAtom(p)); + //lyxerr << "read array: " << array << "\n"; } else if (t.cs() == "mbox") { diff --git a/src/mathed/math_xdata.h b/src/mathed/math_xdata.h index e4005c6316..a145732990 100644 --- a/src/mathed/math_xdata.h +++ b/src/mathed/math_xdata.h @@ -37,6 +37,10 @@ public: int xo() const { return xo_; } /// access to cached y coordinate of last drawing int yo() const { return yo_; } + /// access to cached x coordinate of mid point of last drawing + int xm() const { return xo_ + width_ / 2; } + /// access to cached y coordinate of mid point of last drawing + int ym() const { return yo_ + (descent_ - ascent_) / 2; } /// returns x coordinate of given position in the array int pos2x(size_type pos) const; /// returns position of given x coordinate diff --git a/src/mathed/math_xyarrowinset.C b/src/mathed/math_xyarrowinset.C index b582a2b58b..c2a21ed1a0 100644 --- a/src/mathed/math_xyarrowinset.C +++ b/src/mathed/math_xyarrowinset.C @@ -5,12 +5,17 @@ #endif #include "math_xyarrowinset.h" +#include "math_xymatrixinset.h" #include "math_mathmlstream.h" #include "math_streamstr.h" +#include "math_cursor.h" +#include "math_support.h" +#include "Painter.h" +#include "debug.h" MathXYArrowInset::MathXYArrowInset() - : MathNestInset(1) + : MathNestInset(2) {} @@ -20,17 +25,107 @@ MathInset * MathXYArrowInset::clone() const } +bool MathXYArrowInset::editing() const +{ + return mathcursor && mathcursor->isInside(this); +} + + +MathXYMatrixInset const * MathXYArrowInset::targetMatrix() const +{ + return mi_.inset ? mi_.inset->asXYMatrixInset() : 0; +} + + +MathXArray const & MathXYArrowInset::targetCell() const +{ + MathXYMatrixInset const * p = targetMatrix(); + int x = 0; + int y = 0; + MathArray const & t = cell(0); + for (MathArray::const_iterator it = t.begin(); it != t.end(); ++it) { + switch ((*it)->getChar()) { + case 'l': --x; break; + case 'r': ++x; break; + case 'u': --y; break; + case 'd': ++y; break; + } + } + //lyxerr << "target: x: " << x << " y: " << y << "\n"; + int n = mi_.idx + p->ncols() * y + x; + if (n < 0 || n >= int(p->nargs())) { + lyxerr << "source: n: " << mi_.idx << "\n"; + lyxerr << "target: n: " << n << " out of range\n"; + n = 0; + } + return p->xcell(n); +} + + +MathXArray const & MathXYArrowInset::sourceCell() const +{ + return targetMatrix()->xcell(mi_.idx); +} + + void MathXYArrowInset::metrics(MathMetricsInfo const & mi) const { + mi_ = mi; MathNestInset::metrics(mi); + + if (editing()) { + int w = mathed_string_width(LM_TC_TEXTRM, mi_, "target: "); + width_ = w + std::max(xcell(0).width(), xcell(1).width()); + ascent_ = xcell(0).ascent(); + descent_ = xcell(0).descent() + xcell(1).height() + 10; + } else { + width_ = 0; + ascent_ = 0; + descent_ = 0; + //mathed_string_dim(LM_TC_TEXTRM, mi_, "X", ascent_, descent_, width_); + } +} + + +void MathXYArrowInset::draw(Painter & pain, int x, int y) const +{ + metrics(mi_); + + if (editing()) { + + int lasc; + int ldes; + int lwid; + mathed_string_dim(LM_TC_TEXTRM, mi_, "target: ", lasc, ldes, lwid); + + xcell(0).draw(pain, x + lwid, y); + drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, y, "target"); + y += std::max(xcell(0).descent(), ldes) + 5; + + y += std::max(xcell(1).ascent(), lasc) + 5; + xcell(1).draw(pain, x + lwid, y); + drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, y, "label"); + + } else { + + //drawStr(pain, LM_TC_TEXTRM, mi_, x, y, "X"); + MathXArray const & s = sourceCell(); + MathXArray const & t = targetCell(); + pain.line(s.xm(), s.ym(), t.xm(), t.ym(), LColor::math); + xcell(1).draw(pain, (s.xm() + t.xm())/2, (s.ym() + t.ym())/2); + + } } void MathXYArrowInset::write(WriteStream & os) const { - os << "\\ar{"; - MathNestInset::write(os); - os << "}\n"; + os << "\\ar"; + if (cell(0).size()) + os << "[" << cell(0) << "]"; + if (cell(1).size()) + os << (up_ ? "^" : "_") << "{" << cell(1) << "}"; + os << " "; } diff --git a/src/mathed/math_xyarrowinset.h b/src/mathed/math_xyarrowinset.h index 45cde7f49c..e8fb3d5e98 100644 --- a/src/mathed/math_xyarrowinset.h +++ b/src/mathed/math_xyarrowinset.h @@ -8,6 +8,9 @@ #pragma interface #endif +// for the \ar stuff in \xymatrix + +class MathXYMatrixInset; class MathXYArrowInset : public MathNestInset { public: @@ -18,14 +21,31 @@ public: /// void metrics(MathMetricsInfo const & st) const; /// + void draw(Painter & pain, int x, int y) const; + /// MathXYArrowInset * asXYArrowInset() { return this; } /// - void normalize(); + bool editing() const; + /// + void normalize(); /// void write(WriteStream & os) const; /// void normalize(NormalStream &) const; + +public: + /// + MathXYMatrixInset const * targetMatrix() const; + /// + MathXArray const & targetCell() const; + /// + MathXArray const & sourceCell() const; + + /// + bool up_; + /// + mutable MathMetricsInfo mi_; }; #endif diff --git a/src/mathed/math_xymatrixinset.C b/src/mathed/math_xymatrixinset.C index 9606bfc8a2..c18052a8d9 100644 --- a/src/mathed/math_xymatrixinset.C +++ b/src/mathed/math_xymatrixinset.C @@ -20,6 +20,18 @@ MathInset * MathXYMatrixInset::clone() const } +int MathXYMatrixInset::colsep() const +{ + return 40; +} + + +int MathXYMatrixInset::rowsep() const +{ + return 40; +} + + void MathXYMatrixInset::metrics(MathMetricsInfo const & st) const { MathMetricsInfo mi = st; diff --git a/src/mathed/math_xymatrixinset.h b/src/mathed/math_xymatrixinset.h index bead82a3ee..1522fc4a01 100644 --- a/src/mathed/math_xymatrixinset.h +++ b/src/mathed/math_xymatrixinset.h @@ -18,10 +18,14 @@ public: /// void metrics(MathMetricsInfo const & st) const; /// - MathXYMatrixInset * asXYMatrixInset() { return this; } + MathXYMatrixInset const * asXYMatrixInset() const { return this; } /// - void normalize(); + virtual int colsep() const; + /// + virtual int rowsep() const; + /// + void normalize(); /// void write(WriteStream & os) const; /// -- 2.39.2