From e70e507d01205cd2b1f98dbaa1ade487628e65ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 13 Feb 2002 13:15:15 +0000 Subject: [PATCH] support for AMS's \pmatrix and \bmatrix git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3530 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/Makefile.am | 2 + src/mathed/formulabase.C | 2 + src/mathed/formulabase.h | 2 +- src/mathed/math_amsarrayinset.C | 87 +++++++++++++++++++++++++++++++++ src/mathed/math_amsarrayinset.h | 44 +++++++++++++++++ src/mathed/math_parser.C | 33 ++++++++----- src/mathed/math_xdata.C | 20 ++++++++ src/mathed/math_xdata.h | 4 ++ 8 files changed, 182 insertions(+), 12 deletions(-) create mode 100644 src/mathed/math_amsarrayinset.C create mode 100644 src/mathed/math_amsarrayinset.h diff --git a/src/mathed/Makefile.am b/src/mathed/Makefile.am index 390f0ee883..4849bff9f6 100644 --- a/src/mathed/Makefile.am +++ b/src/mathed/Makefile.am @@ -14,6 +14,8 @@ libmathed_la_SOURCES = \ formula.h \ formulamacro.C \ formulamacro.h \ + math_amsarrayinset.C \ + math_amsarrayinset.h \ math_arrayinset.C \ math_arrayinset.h \ math_atom.C \ diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index c5ef86ec70..a1077977c3 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -401,6 +401,8 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action, case LFUN_RIGHT: result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT; + //lyxerr << "calling scroll 20\n"; + //scroll(bv, 20); updateLocal(bv, false); // write something to the minibuffer //bv->owner()->message(mathcursor->info()); diff --git a/src/mathed/formulabase.h b/src/mathed/formulabase.h index f2cac7066f..7e5b8f1813 100644 --- a/src/mathed/formulabase.h +++ b/src/mathed/formulabase.h @@ -30,7 +30,7 @@ class Buffer; class BufferView; class MathAtom; -/// +/// An abstract base class for all math related LyX insets class InsetFormulaBase : public UpdatableInset { public: /// diff --git a/src/mathed/math_amsarrayinset.C b/src/mathed/math_amsarrayinset.C new file mode 100644 index 0000000000..4fb7011b7c --- /dev/null +++ b/src/mathed/math_amsarrayinset.C @@ -0,0 +1,87 @@ +#include + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "math_amsarrayinset.h" +#include "math_mathmlstream.h" +#include "math_support.h" +#include "math_streamstr.h" +#include "math_support.h" +#include "Lsstream.h" + + +MathAMSArrayInset::MathAMSArrayInset(string const & name, int m, int n) + : MathGridInset(m, n), name_(name) +{} + + +MathAMSArrayInset::MathAMSArrayInset(string const & name) + : MathGridInset(1, 1), name_(name) +{} + + +MathInset * MathAMSArrayInset::clone() const +{ + return new MathAMSArrayInset(*this); +} + + +char const * MathAMSArrayInset::name_left() const +{ + if (name_ == "bmatrix") + return "["; + return "("; +} + + +char const * MathAMSArrayInset::name_right() const +{ + if (name_ == "bmatrix") + return "]"; + return ")"; +} + + +void MathAMSArrayInset::metrics(MathMetricsInfo const & st) const +{ + MathMetricsInfo mi = st; + if (mi.style == LM_ST_DISPLAY) + mi.style = LM_ST_TEXT; + MathGridInset::metrics(mi); + width_ += 12; +} + + +void MathAMSArrayInset::draw(Painter & pain, int x, int y) const +{ + MathGridInset::draw(pain, x + 6, y); + int yy = y - ascent_; + mathed_draw_deco(pain, x + 1, yy, 5, height(), name_left()); + mathed_draw_deco(pain, x + width_ - 6, yy, 5, height(), name_right()); +} + + +void MathAMSArrayInset::write(WriteStream & os) const +{ + os << "\\begin{" << name_ << "}"; + MathGridInset::write(os); + os << "\\end{" << name_ << "}\n"; +} + + +void MathAMSArrayInset::normalize(NormalStream & os) const +{ + os << "[" << name_ << " "; + MathGridInset::normalize(os); + os << "]"; +} + + +void MathAMSArrayInset::maplize(MapleStream & os) const +{ + os << name_ << "("; + MathGridInset::maplize(os); + os << ")"; +} diff --git a/src/mathed/math_amsarrayinset.h b/src/mathed/math_amsarrayinset.h new file mode 100644 index 0000000000..861f51d7da --- /dev/null +++ b/src/mathed/math_amsarrayinset.h @@ -0,0 +1,44 @@ +// -*- C++ -*- +#ifndef MATH_AMSARRAYINSET_H +#define MATH_AMSARRAYINSET_H + +#include "math_gridinset.h" + +#ifdef __GNUG__ +#pragma interface +#endif + + +class MathAMSArrayInset : public MathGridInset { +public: + /// + MathAMSArrayInset(string const & name_, int m, int n); + /// + MathAMSArrayInset(string const & name_); + /// + MathInset * clone() const; + /// + void metrics(MathMetricsInfo const & st) const; + /// + void draw(Painter & pain, int x, int y) const; + /// + MathAMSArrayInset * asAMSArrayInset() { return this; } + + /// + void write(WriteStream & os) const; + /// + void normalize(NormalStream &) const; + /// + void maplize(MapleStream &) const; + +private: + /// + char const * name_left() const; + /// + char const * name_right() const; + + /// + string name_; +}; + +#endif diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index b6baa3cf22..7faa4a7c88 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -52,6 +52,7 @@ point to write some macros: #include "math_parser.h" #include "math_inset.h" #include "math_arrayinset.h" +#include "math_amsarrayinset.h" #include "math_braceinset.h" #include "math_casesinset.h" #include "math_charinset.h" @@ -268,7 +269,7 @@ private: /// bool parse_lines(MathAtom & t, bool numbered, bool outmost); /// parses {... & ... \\ ... & ... } - bool parse_lines2(MathAtom & t); + bool parse_lines2(MathAtom & t, bool braced); /// dump contents to screen void dump() const; @@ -655,7 +656,7 @@ bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost) } -bool Parser::parse_lines2(MathAtom & t) +bool Parser::parse_lines2(MathAtom & t, bool braced) { MathGridInset * p = t->asGridInset(); if (!p) { @@ -663,8 +664,6 @@ bool Parser::parse_lines2(MathAtom & t) return false; } - skipBegin(); - for (int row = 0; true; ++row) { // reading a row for (MathInset::col_type col = 0; true; ++col) { @@ -694,12 +693,20 @@ bool Parser::parse_lines2(MathAtom & t) getToken(); } - // we are finished if the next token is an '}' - if (nextToken().cat() == catEnd) { - // skip the end-token - getToken(); - // leave the 'read a line'-loop - break; + // we are finished if the next token is the one we expected + // skip the end-token + // leave the 'read a line'-loop + if (braced) { + if (nextToken().cat() == catEnd) { + getToken(); + break; + } + } else { + if (nextToken().cs() == "end") { + getToken(); + getArg('{','}'); + break; + } } // otherwise, we have to start a new row @@ -1081,6 +1088,9 @@ void Parser::parse_into1(MathArray & array, unsigned flags, MathTextCodes code) } else if (name == "cases") { array.push_back(MathAtom(new MathCasesInset)); parse_lines(array.back(), false, false); + } else if (name == "pmatrix" || name == "bmatrix") { + array.push_back(MathAtom(new MathAMSArrayInset(name))); + parse_lines2(array.back(), false); } else lyxerr << "unknow math inset begin '" << name << "'\n"; } @@ -1117,7 +1127,8 @@ void Parser::parse_into1(MathArray & array, unsigned flags, MathTextCodes code) else if (t.cs() == "xymatrix") { array.push_back(createMathInset(t.cs())); - parse_lines2(array.back()); + skipBegin(); + parse_lines2(array.back(), true); } #if 0 diff --git a/src/mathed/math_xdata.C b/src/mathed/math_xdata.C index 9c2d059f13..8f4ba899bd 100644 --- a/src/mathed/math_xdata.C +++ b/src/mathed/math_xdata.C @@ -177,3 +177,23 @@ void MathXArray::findPos(MathPosFinder & f) const } } */ + +void MathXArray::center(int & x, int & y) const +{ + x = xo_ + width_ / 2; + y = yo_ + (descent_ - ascent_) / 2; +} + + +void MathXArray::towards(int & x, int & y) const +{ + int cx = 0; + int cy = 0; + center(cx, cy); + + double r = 1.0; + int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy); + + x = cx + int(r * (x - cx)); + y = cy + int(r * (y - cy)); +} diff --git a/src/mathed/math_xdata.h b/src/mathed/math_xdata.h index a145732990..ee5b1d73a9 100644 --- a/src/mathed/math_xdata.h +++ b/src/mathed/math_xdata.h @@ -61,6 +61,10 @@ public: void boundingBox(int & xlow, int & xhigh, int & ylow, int & yhigh); /// find best position to do things //void findPos(PosFinder &) const; + /// gives center coordinates + void center(int & x, int & y) const; + /// adjust (x,y) to point on boundary on a straight line from the center + void towards(int & x, int & y) const; /// begin iterator of the underlying MathArray const_iterator begin() const { return data_.begin(); } -- 2.39.2