From 0024f42724dd9f5eb20d6d5a14276fad4e744963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 12 Jul 2001 07:18:29 +0000 Subject: [PATCH] split super/subscript handling in new base class MathUpDownInset and MathScriptInset/MathBigOpInset git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2220 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/ChangeLog | 7 ++ src/mathed/Makefile.am | 2 + src/mathed/array.C | 47 +++----- src/mathed/array.h | 11 +- src/mathed/formulabase.C | 4 + src/mathed/math_accentinset.C | 2 +- src/mathed/math_accentinset.h | 2 +- src/mathed/math_bigopinset.C | 174 +++++++++++++---------------- src/mathed/math_bigopinset.h | 32 ++++-- src/mathed/math_cursor.C | 38 +++---- src/mathed/math_cursor.h | 4 +- src/mathed/math_decorationinset.C | 2 +- src/mathed/math_decorationinset.h | 2 +- src/mathed/math_deliminset.C | 2 +- src/mathed/math_deliminset.h | 2 +- src/mathed/math_dotsinset.C | 2 +- src/mathed/math_dotsinset.h | 2 +- src/mathed/math_fracinset.C | 2 +- src/mathed/math_fracinset.h | 2 +- src/mathed/math_funcinset.C | 2 +- src/mathed/math_funcinset.h | 2 +- src/mathed/math_grid.C | 3 +- src/mathed/math_grid.h | 2 +- src/mathed/math_inset.C | 15 +-- src/mathed/math_inset.h | 10 +- src/mathed/math_macro.C | 2 +- src/mathed/math_macro.h | 2 +- src/mathed/math_macroarg.C | 2 +- src/mathed/math_macroarg.h | 2 +- src/mathed/math_macrotable.C | 2 +- src/mathed/math_macrotemplate.C | 2 +- src/mathed/math_macrotemplate.h | 2 +- src/mathed/math_matrixinset.C | 4 +- src/mathed/math_matrixinset.h | 2 +- src/mathed/math_parser.C | 110 +++++++++--------- src/mathed/math_root.C | 2 +- src/mathed/math_root.h | 2 +- src/mathed/math_scriptinset.C | 153 +------------------------ src/mathed/math_scriptinset.h | 49 +------- src/mathed/math_sizeinset.C | 2 +- src/mathed/math_sizeinset.h | 2 +- src/mathed/math_spaceinset.C | 2 +- src/mathed/math_spaceinset.h | 2 +- src/mathed/math_sqrtinset.C | 4 +- src/mathed/math_sqrtinset.h | 2 +- src/mathed/math_updowninset.C | 178 ++++++++++++++++++++++++++++++ src/mathed/math_updowninset.h | 75 +++++++++++++ src/mathed/xarray.C | 33 +++--- src/mathed/xarray.h | 2 +- 49 files changed, 526 insertions(+), 485 deletions(-) create mode 100644 src/mathed/math_updowninset.C create mode 100644 src/mathed/math_updowninset.h diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 7cf5a8f7ba..5e61a363b8 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,4 +1,11 @@ +2001-07-12 André Pönitz + + * math_updowninset.[hC]: new base class for script and bigop insets + *.[hC]: subsequnet changes to all Metric() functions + + * math_parser.C: small changes (\sqrt0 is read properly now) + 2001-07-10 André Pönitz * math_accentinset.[hC]: rewrite diff --git a/src/mathed/Makefile.am b/src/mathed/Makefile.am index 34af4fca28..2f8dc71aab 100644 --- a/src/mathed/Makefile.am +++ b/src/mathed/Makefile.am @@ -66,6 +66,8 @@ libmathed_la_SOURCES = \ math_spaceinset.h \ math_sqrtinset.C \ math_sqrtinset.h \ + math_updowninset.C \ + math_updowninset.h \ math_utils.C \ math_utils.h \ matriz.C \ diff --git a/src/mathed/array.C b/src/mathed/array.C index 76e078e7b3..7141bb79dc 100644 --- a/src/mathed/array.C +++ b/src/mathed/array.C @@ -23,7 +23,7 @@ MathArray::~MathArray() { for (int pos = 0; pos < size(); next(pos)) if (MathIsInset(pos)) - delete GetInset(pos); + delete nextInset(pos); } @@ -32,7 +32,7 @@ MathArray::MathArray(MathArray const & array) { for (int pos = 0; pos < size(); next(pos)) if (isInset(pos)) - replace(pos, GetInset(pos)->clone()); + replace(pos, nextInset(pos)->clone()); } @@ -75,7 +75,7 @@ void MathArray::substitute(MathMacro const & m) MathArray tmp; for (int pos = 0; pos < size(); next(pos)) { if (isInset(pos)) - GetInset(pos)->substitute(tmp, m); + nextInset(pos)->substitute(tmp, m); else tmp.push_back(GetChar(pos), GetCode(pos)); } @@ -91,7 +91,7 @@ MathArray & MathArray::operator=(MathArray const & array) } -MathInset * MathArray::GetInset(int pos) const +MathInset * MathArray::nextInset(int pos) const { if (!isInset(pos)) return 0; @@ -100,6 +100,14 @@ MathInset * MathArray::GetInset(int pos) const return p; } +MathInset * MathArray::prevInset(int pos) const +{ + if (!pos) + return 0; + prev(pos); + return nextInset(pos); +} + byte MathArray::GetChar(int pos) const { return pos < size() ? bf_[pos + 1] : '\0'; @@ -157,7 +165,7 @@ void MathArray::insert(int pos, MathArray const & array) bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end()); for (int p = pos; p < pos + array.size(); next(p)) if (isInset(p)) - replace(p, GetInset(p)->clone()); + replace(p, nextInset(p)->clone()); } @@ -230,35 +238,12 @@ MathInset * MathArray::back_inset() const int pos = size(); prev(pos); if (isInset(pos)) - return GetInset(pos); + return nextInset(pos); } return 0; } -MathScriptInset * MathArray::prevScriptInset(int pos) const -{ - if (!pos) - return 0; - prev(pos); - - MathInset * inset = GetInset(pos); - if (inset && inset->isScriptInset()) - return static_cast(inset); - - return 0; -} - -MathScriptInset * MathArray::nextScriptInset(int pos) const -{ - MathInset * inset = GetInset(pos); - if (inset && inset->isScriptInset()) - return static_cast(inset); - - return 0; -} - - void MathArray::dump2(ostream & os) const { for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it) @@ -272,7 +257,7 @@ void MathArray::dump(ostream & os) const { for (int pos = 0; pos < size(); next(pos)) { if (isInset(pos)) - os << ""; + os << ""; else os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">"; } @@ -296,7 +281,7 @@ void MathArray::Write(ostream & os, bool fragile) const for (int pos = 0; pos < size(); next(pos)) { if (isInset(pos)) { - GetInset(pos)->Write(os, fragile); + nextInset(pos)->Write(os, fragile); } else { diff --git a/src/mathed/array.h b/src/mathed/array.h index c5ec80b787..4e8a351974 100644 --- a/src/mathed/array.h +++ b/src/mathed/array.h @@ -24,7 +24,6 @@ #include "LString.h" class MathInset; -class MathScriptInset; class MathMacro; class Painter; @@ -33,9 +32,7 @@ class Painter; #endif /** \class MathArray - \brief A resizable array. - - A general purpose resizable array. + \brief Low level container for math insets \author Alejandro Aguilar Sierra \author André Pönitz @@ -101,11 +98,9 @@ public: /// /// - MathInset * GetInset(int pos) const; + MathInset * nextInset(int pos) const; /// - MathScriptInset * prevScriptInset(int pos) const; - /// - MathScriptInset * nextScriptInset(int pos) const; + MathInset * prevInset(int pos) const; /// byte GetChar(int pos) const; /// read subsequent chars of the same kind. diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index b048f29c46..91e1fd6c2f 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -147,6 +147,7 @@ LyXFont WhichFont(short type, int size) case LM_TC_SPECIAL: //f = Math_Fonts[0]; break; case LM_TC_TEXTRM: + case LM_TC_TEX: case LM_TC_RM: f = Math_Fonts[6]; break; @@ -184,6 +185,9 @@ LyXFont WhichFont(short type, int size) if (type != LM_TC_TEXTRM) f.setColor(LColor::math); + if (type == LM_TC_TEX) + f.setColor(LColor::latex); + return f; } diff --git a/src/mathed/math_accentinset.C b/src/mathed/math_accentinset.C index 3bfb5ca2ff..2c12be4ad8 100644 --- a/src/mathed/math_accentinset.C +++ b/src/mathed/math_accentinset.C @@ -17,7 +17,7 @@ MathInset * MathAccentInset::clone() const return new MathAccentInset(*this); } -void MathAccentInset::Metrics(MathStyles st) +void MathAccentInset::Metrics(MathStyles st, int, int) { xcell(0).Metrics(st); ascent_ = xcell(0).ascent(); diff --git a/src/mathed/math_accentinset.h b/src/mathed/math_accentinset.h index e75f69fe0d..dc7a9f0717 100644 --- a/src/mathed/math_accentinset.h +++ b/src/mathed/math_accentinset.h @@ -20,7 +20,7 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// int getAccentCode() const; /// diff --git a/src/mathed/math_bigopinset.C b/src/mathed/math_bigopinset.C index 588a4cde9f..2da8735809 100644 --- a/src/mathed/math_bigopinset.C +++ b/src/mathed/math_bigopinset.C @@ -1,17 +1,15 @@ #include -#include - #include "math_bigopinset.h" -#include "LColor.h" #include "Painter.h" #include "mathed/support.h" #include "support/LOstream.h" + using std::ostream; MathBigopInset::MathBigopInset(string const & name, int id) - : MathScriptInset(false, true), lims_(0), sym_(id) + : MathUpDownInset(false, false), sym_(id), limits_(0) { SetName(name); } @@ -23,132 +21,116 @@ MathInset * MathBigopInset::clone() const } +int MathBigopInset::limits() const +{ + return limits_; +} + + +void MathBigopInset::limits(int limits) +{ + limits_ = limits; +} + + +bool MathBigopInset::hasLimits() const +{ + return limits_ == 1 || (limits_ == 0 && size() == LM_ST_DISPLAY); +} + void MathBigopInset::Write(ostream & os, bool fragile) const { //bool f = sym_ != LM_int && sym_ != LM_oint && size() == LM_ST_DISPLAY; os << '\\' << name(); - if (limits() == 1) - os << "\\limits "; - else if (limits() == -1) - os << "\\nolimits "; - else - os << ' '; - MathScriptInset::Write(os, fragile); + MathUpDownInset::Write(os, fragile); } void MathBigopInset::WriteNormal(ostream & os) const { - os << "[bigop " << name(); - if (limits() == 1) - os << "\\limits "; - else if (limits() == -1) - os << "\\nolimits "; - else - os << ' '; - MathScriptInset::WriteNormal(os); - os << "] "; + os << "[bigop " << name() << "] "; } -void MathBigopInset::Metrics(MathStyles st) + +void MathBigopInset::Metrics(MathStyles st, int, int) { - MathScriptInset::Metrics(st); + //cerr << "\nBigopDraw\n"; size(st); - string s; - short t; if (sym_ < 256 || sym_ == LM_oint) { - char const c = (sym_ == LM_oint) ? LM_int : sym_; - s += c; - t = LM_TC_BSYM; + ssym_ = string(); + ssym_ += (sym_ == LM_oint) ? LM_int : sym_; + code_ = LM_TC_BSYM; } else { - s = name(); - t = LM_TC_TEXTRM; + ssym_ = name(); + code_ = LM_TC_TEXTRM; } - int asc, des, wid; - mathed_string_dim(t, size(), s, asc, des, wid); + int wid; + mathed_string_dim(code_, size(), ssym_, ascent_, descent_, wid); if (sym_ == LM_oint) wid += 2; + //cerr << " asc: " << ascent_ << " des: " << descent_ + // << " wid: " << wid << "\n"; + //cerr << " hasLimits: " << hasLimits() << " up: " + // << up() << " down: " << down() << "\n"; + + width_ = wid; if (hasLimits()) { - ascent_ = asc + xcell(0).height() + 2; - descent_ = des + xcell(1).height() + 2; - width_ = std::max(width_, wid); + xcell(0).Metrics(st); + xcell(1).Metrics(st); + //cerr << " 0: ascent_: " << xcell(0).ascent() << " descent_: " << + // xcell(0).descent() << " width_: " << xcell(0).width() << "\n"; + //cerr << " 1: ascent_: " << xcell(1).ascent() << " descent_: " << + // xcell(1).descent() << " width_: " << xcell(1).width() << "\n"; + if (up()) { + ascent_ += xcell(0).height() + 1; + width_ = std::max(width_, xcell(0).width()); + dy0_ = - (ascent_ - xcell(0).ascent()); + } + if (down()) { + descent_ += xcell(1).height() + 1; + width_ = std::max(width_, xcell(1).width()); + dy1_ = descent_ - xcell(1).descent(); + } + dxx_ = (width_ - wid) / 2; + dx0_ = (width_ - xcell(0).width()) / 2; + dx1_ = (width_ - xcell(1).width()) / 2; + //cerr << " ascent_: " << ascent_ << " descent_: " + // << descent_ << " width_: " << width_ << "\n"; + //cerr << " dx0_: " << dx0_ << " dx1_: " << dx1_ + // << " dxx_: " << dxx_ << "\n"; + //cerr << " dy0_: " << dy0_ << " dy1_: " << dy1_ + // << "\n"; } else { - ascent_ = std::max(ascent_, asc); - descent_ = std::max(descent_, des); - width_ += wid; + MathUpDownInset::Metrics(st, ascent_, descent_); + width_ += wid; + dx0_ = wid; + dx1_ = wid; + dxx_ = 0; } - } void MathBigopInset::draw(Painter & pain, int x, int y) -{ +{ xo(x); yo(y); - string s; - short t; - - if (sym_ < 256 || sym_ == LM_oint) { - s += (sym_ == LM_oint) ? LM_int : sym_; - t = LM_TC_BSYM; - } else { - s = name(); - t = LM_TC_TEXTRM; - } + pain.text(x + dxx_, y, ssym_, mathed_get_font(code_, size())); + + if (up()) + xcell(0).draw(pain, x + dx0_, y + dy0_); + if (down()) + xcell(1).draw(pain, x + dx1_, y + dy1_); + if (sym_ == LM_oint) { - int wid; - int asc; - int des; - mathed_char_dim(t, size(), LM_int, asc, des, wid); - wid += 2; - pain.arc(x - 1, y - (asc - des) / 2, wid, wid, 0, 360 * 64, LColor::mathline); - } - - int asc, des, wid; - mathed_string_dim(t, size(), s, asc, des, wid); - - if (hasLimits()) { - int w = width(); - pain.text(x + (w - wid)/2, y, s, mathed_get_font(t, size())); - xcell(0).draw - (pain, x + (w - xcell(0).width())/2, y - asc - xcell(0).descent() - 1); - xcell(1).draw - (pain, x + (w - xcell(1).width())/2, y + des + xcell(1).ascent() + 1); - } else { - pain.text(x, y, s, mathed_get_font(t, size())); - MathScriptInset::draw(pain, x + wid, y); + int xx = x - 1; + int yy = y - (ascent_ - descent_) / 2; + pain.arc(xx, yy, width_, width_, 0, 360 * 64, LColor::mathline); } } -int MathBigopInset::limits() const -{ - return lims_; -} - - -void MathBigopInset::limits(int limit) -{ - lims_ = limit; -} - -bool MathBigopInset::hasLimits() const -{ - return limits() == 1 || (limits() == 0 && size() == LM_ST_DISPLAY); -} - - -void MathBigopInset::idxDelete(int & idx, bool & popit, bool & deleteit) -{ - if (idx == 0) - up(false); - else - down(false); - popit = true; - deleteit = true; -} diff --git a/src/mathed/math_bigopinset.h b/src/mathed/math_bigopinset.h index e0409aeb5a..c347e35166 100644 --- a/src/mathed/math_bigopinset.h +++ b/src/mathed/math_bigopinset.h @@ -2,35 +2,45 @@ #ifndef MATH_BIGOPINSET_H #define MATH_BIGOPINSET_H -#include "math_scriptinset.h" +#include "math_updowninset.h" /// big operators -class MathBigopInset : public MathScriptInset { +class MathBigopInset : public MathUpDownInset { public: /// MathBigopInset(string const &, int); /// MathInset * clone() const; /// - void draw(Painter &, int, int); - /// void Write(std::ostream &, bool fragile) const; /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// - int limits() const; - /// - bool hasLimits() const; + void draw(Painter &, int, int); /// void limits(int); /// - void idxDelete(int & idx, bool & popit, bool & deleteit); + int limits() const; + /// Identifies BigopInsets + bool isBigopInset() const { return true; } private: - /// 1: \limits, -1: \nolimits, 0: use default - int lims_; + /// + bool hasLimits() const; /// int sym_; + /// + string ssym_; + /// + short code_; + /// 1: \limits, -1: \nolimits, 0: use default + int limits_; + /// x offset for drawing the superscript + int dx0_; + /// x offset for drawing the subscript + int dx1_; + /// x offset for drawing the inner symbol + int dxx_; }; #endif diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 558f398acc..0228118367 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -306,9 +306,9 @@ void MathCursor::SetPos(int x, int y) lyxerr << "found idx: " << idx_ << " cursor: " << cursor_ << "\n"; MathInset * n = nextInset(); MathInset * p = prevInset(); - if (n && (n->isActive() || n->isScriptInset()) && n->covers(x, y)) + if (n && (n->isActive() || n->isUpDownInset()) && n->covers(x, y)) push(n, true); - else if (p && (p->isActive() || p->isScriptInset()) && p->covers(x, y)) { + else if (p && (p->isActive() || p->isUpDownInset()) && p->covers(x, y)) { array().prev(cursor_); push(p, false); } else @@ -536,7 +536,7 @@ bool MathCursor::toggleLimits() return false; MathInset * p = prevInset(); int old = p->limits(); - p->limits(old == -1 ? 1 : -1); + p->limits(old < 0 ? 1 : -1); return old != p->limits(); } @@ -554,29 +554,27 @@ void MathCursor::Interpret(string const & s) in_word_set(s) << " \n"; if (s[0] == '^') { - MathScriptInset * p = nearbyScriptInset(); + MathUpDownInset * p = nearbyUpDownInset(); if (!p) { - p = new MathScriptInset; + p = new MathScriptInset(true, false); insert(p); array().prev(cursor_); } push(p, true); - if (!p->up()) - p->up(true); + p->up(true); idx_ = 0; return; } if (s[0] == '_') { - MathScriptInset * p = nearbyScriptInset(); + MathUpDownInset * p = nearbyUpDownInset(); if (!p) { - p = new MathScriptInset; + p = new MathScriptInset(false, true); insert(p); array().prev(cursor_); } push(p, true); - if (!p->down()) - p->down(true); + p->down(true); idx_ = 1; return; } @@ -1064,28 +1062,30 @@ MathInset * MathCursor::prevInset() const int c = cursor_; if (!array().prev(c)) return 0; - return array().GetInset(c); + return array().nextInset(c); } MathInset * MathCursor::nextInset() const { normalize(); - return array().GetInset(cursor_); + return array().nextInset(cursor_); } -MathScriptInset * MathCursor::nearbyScriptInset() const +MathUpDownInset * MathCursor::nearbyUpDownInset() const { normalize(); - MathScriptInset * p = array().prevScriptInset(cursor_); - if (p) - return p; - return array().nextScriptInset(cursor_); + MathInset * p = array().prevInset(cursor_); + if (p && p->isUpDownInset()) + return static_cast(p); + p = array().nextInset(cursor_); + if (p && p->isUpDownInset()) + return static_cast(p); + return 0; } - MathArray & MathCursor::array() const { static MathArray dummy; diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index 3f316d96b7..22779c881d 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -25,7 +25,7 @@ class MathInset; class MathFuncInset; -class MathScriptInset; +class MathUpDownInset; class InsetFormulaBase; class MathArray; class MathXArray; @@ -217,7 +217,7 @@ private: /// MathInset * prevInset() const; /// - MathScriptInset * nearbyScriptInset() const; + MathUpDownInset * nearbyUpDownInset() const; /// MathFuncInset * imacro; diff --git a/src/mathed/math_decorationinset.C b/src/mathed/math_decorationinset.C index 1cef8d83c0..c76cd1d8fe 100644 --- a/src/mathed/math_decorationinset.C +++ b/src/mathed/math_decorationinset.C @@ -28,7 +28,7 @@ MathInset * MathDecorationInset::clone() const -void MathDecorationInset::Metrics(MathStyles st) +void MathDecorationInset::Metrics(MathStyles st, int, int) { xcell(0).Metrics(st); size_ = st; diff --git a/src/mathed/math_decorationinset.h b/src/mathed/math_decorationinset.h index 34b5f47438..6f189c9ab4 100644 --- a/src/mathed/math_decorationinset.h +++ b/src/mathed/math_decorationinset.h @@ -22,7 +22,7 @@ public: /// void Write(std::ostream &, bool fragile) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); private: /// int deco_; diff --git a/src/mathed/math_deliminset.C b/src/mathed/math_deliminset.C index bea4755336..44a0a0c403 100644 --- a/src/mathed/math_deliminset.C +++ b/src/mathed/math_deliminset.C @@ -89,7 +89,7 @@ int MathDelimInset::dw() const } -void MathDelimInset::Metrics(MathStyles st) +void MathDelimInset::Metrics(MathStyles st, int, int) { xcell(0).Metrics(st); size_ = st; diff --git a/src/mathed/math_deliminset.h b/src/mathed/math_deliminset.h index 8ab72b17d1..cd25fe6f1b 100644 --- a/src/mathed/math_deliminset.h +++ b/src/mathed/math_deliminset.h @@ -22,7 +22,7 @@ public: /// void Write(std::ostream &, bool fragile) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); private: int dw() const; /// diff --git a/src/mathed/math_dotsinset.C b/src/mathed/math_dotsinset.C index d173a3bb71..e08ef4f789 100644 --- a/src/mathed/math_dotsinset.C +++ b/src/mathed/math_dotsinset.C @@ -33,7 +33,7 @@ void MathDotsInset::draw(Painter & pain, int x, int y) } -void MathDotsInset::Metrics(MathStyles st) +void MathDotsInset::Metrics(MathStyles st, int, int) { size(st); mathed_char_dim(LM_TC_VAR, size(), 'M', ascent_, descent_, width_); diff --git a/src/mathed/math_dotsinset.h b/src/mathed/math_dotsinset.h index 7f1b29e276..d6d973a820 100644 --- a/src/mathed/math_dotsinset.h +++ b/src/mathed/math_dotsinset.h @@ -23,7 +23,7 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); protected: /// int dh_; diff --git a/src/mathed/math_fracinset.C b/src/mathed/math_fracinset.C index 6376eeab4d..7e6a166d9d 100644 --- a/src/mathed/math_fracinset.C +++ b/src/mathed/math_fracinset.C @@ -24,7 +24,7 @@ MathInset * MathFracInset::clone() const } -void MathFracInset::Metrics(MathStyles st) +void MathFracInset::Metrics(MathStyles st, int, int) { size_ = smallerStyleFrac(st); xcell(0).Metrics(size_); diff --git a/src/mathed/math_fracinset.h b/src/mathed/math_fracinset.h index 4a3e7ab8e8..10da11aae8 100644 --- a/src/mathed/math_fracinset.h +++ b/src/mathed/math_fracinset.h @@ -22,7 +22,7 @@ public: /// virtual void WriteNormal(std::ostream &) const; /// - virtual void Metrics(MathStyles st); + virtual void Metrics(MathStyles st, int asc = 0, int des = 0); /// virtual void draw(Painter &, int x, int baseline); /// diff --git a/src/mathed/math_funcinset.C b/src/mathed/math_funcinset.C index 22ba821089..c2e7f61df3 100644 --- a/src/mathed/math_funcinset.C +++ b/src/mathed/math_funcinset.C @@ -55,7 +55,7 @@ void MathFuncInset::WriteNormal(std::ostream & os) const } -void MathFuncInset::Metrics(MathStyles st) +void MathFuncInset::Metrics(MathStyles st, int, int) { LyXFont font = WhichFont(LM_TC_TEXTRM, size()); #ifndef NO_LATEX diff --git a/src/mathed/math_funcinset.h b/src/mathed/math_funcinset.h index 5c7dcf0c1e..9b386b274d 100644 --- a/src/mathed/math_funcinset.h +++ b/src/mathed/math_funcinset.h @@ -25,7 +25,7 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); private: /// bool lims_; diff --git a/src/mathed/math_grid.C b/src/mathed/math_grid.C index d6dad817c7..28bb8208d2 100644 --- a/src/mathed/math_grid.C +++ b/src/mathed/math_grid.C @@ -7,7 +7,6 @@ #include "math_grid.h" #include "support/LOstream.h" #include "debug.h" -#include "Painter.h" namespace { @@ -77,7 +76,7 @@ char MathGridInset::valign() const return v_align_; } -void MathGridInset::Metrics(MathStyles st) +void MathGridInset::Metrics(MathStyles st, int, int) { // let the cells adjust themselves MathInset::Metrics(st); diff --git a/src/mathed/math_grid.h b/src/mathed/math_grid.h index b3e14e36f4..9948bdc300 100644 --- a/src/mathed/math_grid.h +++ b/src/mathed/math_grid.h @@ -58,7 +58,7 @@ public: /// void Write(std::ostream &, bool fragile) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void draw(Painter &, int, int); /// diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index c17c9644f9..bc635a3a7b 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -70,12 +70,6 @@ void MathInset::limits(int) { } -bool MathInset::hasLimits() const -{ - return false; -} - - string const & MathInset::name() const { return name_; @@ -172,12 +166,6 @@ MathArray const & MathInset::cell(int i) const } -void MathInset::setData(MathArray const & a, int idx) -{ - cells_[idx].data_ = a; -} - - void MathInset::substitute(MathArray & array, MathMacro const & m) const { MathInset * p = clone(); @@ -186,7 +174,7 @@ void MathInset::substitute(MathArray & array, MathMacro const & m) const array.push_back(p); } -void MathInset::Metrics(MathStyles st) +void MathInset::Metrics(MathStyles st, int, int) { size_ = st; for (int i = 0; i < nargs(); ++i) @@ -373,4 +361,3 @@ bool MathInset::covers(int x, int y) const y >= yo_ - ascent_ && y <= yo_ + descent_; } - diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index f8c6e4c117..a2c53cc5e5 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -60,7 +60,7 @@ public: /// Appends itself with macro arguments substituted virtual void substitute(MathArray & array, MathMacro const & macro) const; /// Compute the size of the object - virtual void Metrics(MathStyles st) = 0; + virtual void Metrics(MathStyles st, int = 0, int = 0) = 0; /// virtual int ascent() const; /// @@ -70,8 +70,6 @@ public: /// virtual int height() const; /// - virtual bool hasLimits() const; - /// virtual int limits() const; /// virtual void limits(int); @@ -136,8 +134,6 @@ public: MathXArray & xcell(int); /// MathXArray const & xcell(int) const; - /// - void setData(MathArray const &, int); /// int xo() const; @@ -174,9 +170,11 @@ public: /// bool covers(int x, int y) const; /// Identifies ScriptInsets - virtual bool isScriptInset() const { return false; } + virtual bool isUpDownInset() const { return false; } /// Identifies AccentInsets virtual bool isAccentInset() const { return false; } + /// Identifies BigopInsets + virtual bool isBigopInset() const { return false; } /// virtual bool isActive() const { return nargs() > 0; } diff --git a/src/mathed/math_macro.C b/src/mathed/math_macro.C index fae542145c..988809f25f 100644 --- a/src/mathed/math_macro.C +++ b/src/mathed/math_macro.C @@ -44,7 +44,7 @@ MathInset * MathMacro::clone() const } -void MathMacro::Metrics(MathStyles st) +void MathMacro::Metrics(MathStyles st, int, int) { if (mathcursor && mathcursor->isInside(this)) { expanded_ = tmplate_->xcell(0); diff --git a/src/mathed/math_macro.h b/src/mathed/math_macro.h index 094efe14f9..1e82f96125 100644 --- a/src/mathed/math_macro.h +++ b/src/mathed/math_macro.h @@ -41,7 +41,7 @@ public: /// void draw(Painter &, int, int); /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// MathInset * clone() const; /// diff --git a/src/mathed/math_macroarg.C b/src/mathed/math_macroarg.C index beb04af035..553aaef9f5 100644 --- a/src/mathed/math_macroarg.C +++ b/src/mathed/math_macroarg.C @@ -38,7 +38,7 @@ void MathMacroArgument::draw(Painter & pain, int x, int y) } -void MathMacroArgument::Metrics(MathStyles st) +void MathMacroArgument::Metrics(MathStyles st, int, int) { char str[] = "#0"; str[1] += number_; diff --git a/src/mathed/math_macroarg.h b/src/mathed/math_macroarg.h index 594b7100e4..f089bf3c9e 100644 --- a/src/mathed/math_macroarg.h +++ b/src/mathed/math_macroarg.h @@ -18,7 +18,7 @@ public: /// MathInset * clone() const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void draw(Painter &, int x, int baseline); /// diff --git a/src/mathed/math_macrotable.C b/src/mathed/math_macrotable.C index 4f3368fa07..9f384dff82 100644 --- a/src/mathed/math_macrotable.C +++ b/src/mathed/math_macrotable.C @@ -114,7 +114,7 @@ void MathMacroTable::builtinMacros() { MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0); MathAccentInset * p = new MathAccentInset(LM_not); - p->cell(0).push_back('0', LM_TC_VAR); + p->cell(0).push_back('O', LM_TC_VAR); t->push_back(p); insertTemplate(t); } diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index 5277073473..e3846aa0ca 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -51,7 +51,7 @@ void MathMacroTemplate::Write(std::ostream & os, bool fragile) const } -void MathMacroTemplate::Metrics(MathStyles st) +void MathMacroTemplate::Metrics(MathStyles st, int, int) { xcell(0).Metrics(st); size_ = st; diff --git a/src/mathed/math_macrotemplate.h b/src/mathed/math_macrotemplate.h index 8f8d709c42..324af9b113 100644 --- a/src/mathed/math_macrotemplate.h +++ b/src/mathed/math_macrotemplate.h @@ -34,7 +34,7 @@ public: /// void draw(Painter &, int, int); /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); private: /// int numargs_; diff --git a/src/mathed/math_matrixinset.C b/src/mathed/math_matrixinset.C index d2c4629f7b..2314150915 100644 --- a/src/mathed/math_matrixinset.C +++ b/src/mathed/math_matrixinset.C @@ -97,7 +97,7 @@ MathInset * MathMatrixInset::clone() const } -void MathMatrixInset::Metrics(MathStyles /* st */) +void MathMatrixInset::Metrics(MathStyles /* st */, int, int) { size_ = (GetType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY; @@ -245,7 +245,7 @@ void MathMatrixInset::Validate1(LaTeXFeatures & features) MathIter it(cell()); while (it.OK() && !(features.binom && features.boldsymbol)) { - MathInset * p = it.GetInset(); + MathInset * p = it.nextInset(); if (p) { p = it.GetActiveInset(); if (p) { diff --git a/src/mathed/math_matrixinset.h b/src/mathed/math_matrixinset.h index 4795d07716..379555763f 100644 --- a/src/mathed/math_matrixinset.h +++ b/src/mathed/math_matrixinset.h @@ -29,7 +29,7 @@ public: /// void Write(std::ostream &, bool fragile) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void draw(Painter &, int, int); /// diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index a00cbb681b..ab8f50af33 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -15,6 +15,8 @@ * the GNU General Public Licence version 2 or later. */ +// {[( + #include #include @@ -38,6 +40,7 @@ #include "math_funcinset.h" #include "math_spaceinset.h" #include "math_sizeinset.h" +#include "math_scriptinset.h" #include "math_dotsinset.h" #include "math_fracinset.h" #include "math_deliminset.h" @@ -75,12 +78,10 @@ lexcode_enum lexcode[256]; char const * latex_special_chars = "#$%&_{}"; -/// Read TeX into data, flags give stop conditions -void mathed_parse(MathArray & data, unsigned flags); - - namespace { +void mathed_parse(MathArray & array, unsigned flags); + unsigned char getuchar(std::istream * is) { char c; @@ -93,17 +94,22 @@ const unsigned char LM_TK_CLOSE = '}'; enum { FLAG_BRACE = 1 << 0, // A { needed //} - FLAG_BRACE_OPT = 1 << 2, // Optional { - FLAG_BRACE_LAST = 1 << 3, // Last } ends the parsing process - FLAG_BRACK_ARG = 1 << 4, // Optional [ + FLAG_BRACE_OPT = 1 << 2, // Optional { //} + FLAG_BRACE_LAST = 1 << 3, // // { Last } ends the parsing process + FLAG_BRACK_ARG = 1 << 4, // Optional [ //] FLAG_RIGHT = 1 << 5, // Next right ends the parsing process FLAG_END = 1 << 6, // Next end ends the parsing process - FLAG_BRACE_FONT = 1 << 7, // Next } closes a font - FLAG_BRACK_END = 1 << 9, // Next ] ends the parsing process + FLAG_BRACE_FONT = 1 << 7, // // { Next } closes a font + FLAG_BRACK_END = 1 << 9, // // [ Next ] ends the parsing process FLAG_AMPERSAND = 1 << 10, // Next & ends the parsing process - FLAG_NEWLINE = 1 << 11 // Next \\ ends the parsing process + FLAG_NEWLINE = 1 << 11, // Next \\ ends the parsing process + + // Read a (possibly braced token) + FLAG_ITEM = FLAG_BRACE_OPT | FLAG_BRACE_LAST }; +} + /// union { @@ -114,6 +120,7 @@ union { } yylval; + string yytext; int yylineno; istream * yyis; @@ -302,9 +309,9 @@ int yylex() yylval.i = (i < 4) ? i : 0; return LM_TK_SPACE; } - if (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) { + if (lexcode[c] == LexAlpha) { yytext.erase(); - while (lexcode[c] == LexAlpha || lexcode[c] == LexDigit) { + while (lexcode[c] == LexAlpha) { yytext += c; c = getuchar(yyis); } @@ -333,25 +340,26 @@ int yylex() } -void handle_frac(MathArray & dat, string const & name) -{ - MathFracInset * p = new MathFracInset(name); - mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST); - mathed_parse(p->cell(1), FLAG_BRACE | FLAG_BRACE_LAST); - dat.push_back(p); -} - - -MathScriptInset * lastScriptInset(MathArray & array) +MathInset * lastUpDownInset(MathArray & array, bool up, bool down) { MathInset * p = array.back_inset(); - if (!p || !p->isScriptInset()) { - p = new MathScriptInset; + if (!p || !p->isUpDownInset()) { + p = new MathScriptInset(up, down); array.push_back(p); } - return static_cast(p); + MathUpDownInset * q = static_cast(p); + if (up) + q->up(true); + if (down) + q->down(down); + return p; } + +MathBigopInset * lastBigopInset(MathArray & array) +{ + MathInset * p = array.back_inset(); + return (p && p->isBigopInset()) ? static_cast(p) : 0; } @@ -480,10 +488,21 @@ MathInset * mathed_parse() } + +namespace { + +void handle_frac(MathArray & array, string const & name) +{ + MathFracInset * p = new MathFracInset(name); + mathed_parse(p->cell(0), FLAG_ITEM); + mathed_parse(p->cell(1), FLAG_ITEM); + array.push_back(p); +} + + void mathed_parse(MathArray & array, unsigned flags) { int t = yylex(); - int tprev = 0; bool panic = false; static int plevel = -1; yyvarcode = LM_TC_VAR; @@ -577,28 +596,16 @@ void mathed_parse(MathArray & array, unsigned flags) break; case '^': - { - MathArray ar; - mathed_parse(ar, FLAG_BRACE_OPT | FLAG_BRACE_LAST); - MathScriptInset * p = lastScriptInset(array); - p->setData(ar, 0); - p->up(true); + mathed_parse(lastUpDownInset(array, true, false)->cell(0), FLAG_ITEM); break; - } case '_': - { - MathArray ar; - mathed_parse(ar, FLAG_BRACE_OPT | FLAG_BRACE_LAST); - MathScriptInset * p = lastScriptInset(array); - p->setData(ar, 1); - p->down(true); + mathed_parse(lastUpDownInset(array, false, true)->cell(1), FLAG_ITEM); break; - } case LM_TK_LIMIT: { - MathScriptInset * p = lastScriptInset(array); + MathBigopInset * p = lastBigopInset(array); if (p) p->limits(yylval.l->id ? 1 : -1); break; @@ -669,15 +676,13 @@ void mathed_parse(MathArray & array, unsigned flags) { unsigned char c = getuchar(yyis); if (c == '[') { - MathRootInset * rt = new MathRootInset; - mathed_parse(rt->cell(0), FLAG_BRACK_END); - mathed_parse(rt->cell(1), FLAG_BRACE | FLAG_BRACE_LAST); - array.push_back(rt); + array.push_back(new MathRootInset); + mathed_parse(array.back_inset()->cell(0), FLAG_BRACK_END); + mathed_parse(array.back_inset()->cell(1), FLAG_ITEM); } else { yyis->putback(c); - MathSqrtInset * sq = new MathSqrtInset; - mathed_parse(sq->cell(0), FLAG_BRACE | FLAG_BRACE_LAST); - array.push_back(sq); + array.push_back(new MathSqrtInset); + mathed_parse(array.back_inset()->cell(0), FLAG_ITEM); } break; } @@ -700,7 +705,7 @@ void mathed_parse(MathArray & array, unsigned flags) rd = yylval.i; MathDelimInset * dl = new MathDelimInset(ld, rd); - dl->setData(ar, 0); + dl->cell(0) = ar; array.push_back(dl); break; } @@ -766,7 +771,7 @@ void mathed_parse(MathArray & array, unsigned flags) if (MathMacroTable::hasTemplate(yytext)) { MathMacro * m = MathMacroTable::cloneTemplate(yytext); for (int i = 0; i < m->nargs(); ++i) - mathed_parse(m->cell(i), FLAG_BRACE_OPT | FLAG_BRACE_LAST); + mathed_parse(m->cell(i), FLAG_ITEM); array.push_back(m); m->Metrics(LM_ST_TEXT); } else @@ -831,7 +836,6 @@ void mathed_parse(MathArray & array, unsigned flags) } // end of big switch - tprev = t; if (panic) { lyxerr << " Math Panic, expect problems!" << endl; // Search for the end command. @@ -849,6 +853,8 @@ void mathed_parse(MathArray & array, unsigned flags) --plevel; } +} + MathInset * mathed_parse(istream & is) { @@ -879,3 +885,5 @@ MathInset * mathed_parse(LyXLex & lex) return p; } + +//]}) diff --git a/src/mathed/math_root.C b/src/mathed/math_root.C index da68d2536e..19e3cbd442 100644 --- a/src/mathed/math_root.C +++ b/src/mathed/math_root.C @@ -34,7 +34,7 @@ MathInset * MathRootInset::clone() const } -void MathRootInset::Metrics(MathStyles st) +void MathRootInset::Metrics(MathStyles st, int, int) { MathInset::Metrics(st); size_ = st; diff --git a/src/mathed/math_root.h b/src/mathed/math_root.h index 1b1fb2e360..cae5cfb93a 100644 --- a/src/mathed/math_root.h +++ b/src/mathed/math_root.h @@ -39,7 +39,7 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// bool idxUp(int & idx, int & pos) const; /// diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index be9972a006..3cf77fbcd6 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -4,22 +4,12 @@ #pragma implementation #endif -#include - #include "math_scriptinset.h" -#include "LColor.h" -#include "Painter.h" -#include "debug.h" -#include "mathed/support.h" #include "support/LOstream.h" -MathScriptInset::MathScriptInset() - : MathInset(2), up_(false), down_(false) -{} - MathScriptInset::MathScriptInset(bool up, bool down) - : MathInset(2), up_(up), down_(down) + : MathUpDownInset(up, down) {} @@ -29,67 +19,6 @@ MathInset * MathScriptInset::clone() const } -void MathScriptInset::Metrics(MathStyles st) -{ - size_ = smallerStyleScript(st); - xcell(0).Metrics(size_); - xcell(1).Metrics(size_); - - width_ = std::max(xcell(0).width(), xcell(1).width()) + 2; - if (up()) - ascent_ = std::max(ascent_, xcell(0).height() + 9); - if (down()) - descent_ = std::max(descent_, xcell(1).height()); -} - - -bool MathScriptInset::up() const -{ - return up_; -} - -bool MathScriptInset::down() const -{ - return down_; -} - -void MathScriptInset::up(bool b) -{ - up_ = b; -} - -void MathScriptInset::down(bool b) -{ - down_ = b; -} - - -void MathScriptInset::draw(Painter & pain, int x, int y) -{ - xo(x); - yo(y); - if (up()) - xcell(0).draw(pain, x, y - xcell(0).descent() - 9); - if (down()) - xcell(1).draw(pain, x, y + xcell(1).ascent()); -} - - -void MathScriptInset::Write(std::ostream & os, bool fragile) const -{ - if (up()) { - os << "^{"; - cell(0).Write(os, fragile); - os << "}"; - } - if (down()) { - os << "_{"; - cell(1).Write(os, fragile); - os << "}"; - } -} - - void MathScriptInset::WriteNormal(std::ostream & os) const { if (up()) { @@ -104,86 +33,6 @@ void MathScriptInset::WriteNormal(std::ostream & os) const } } -bool MathScriptInset::idxRight(int &, int &) const -{ - return false; -} - -bool MathScriptInset::idxLeft(int &, int &) const -{ - return false; -} - - -bool MathScriptInset::idxUp(int & idx, int & pos) const -{ - if (idx == 0 || !up()) - return false; - idx = 0; - pos = 0; - return true; -} - -bool MathScriptInset::idxDown(int & idx, int & pos) const -{ - if (idx == 1 || !down()) - return false; - idx = 1; - pos = 0; - return true; -} - -bool MathScriptInset::idxFirst(int & idx, int & pos) const -{ - idx = up() ? 0 : 1; - pos = 0; - return true; -} - -bool MathScriptInset::idxLast(int & idx, int & pos) const -{ - idx = down() ? 1 : 0; - pos = cell(idx).size(); - return true; -} - - -bool MathScriptInset::idxFirstUp(int & idx, int & pos) const -{ - if (!up()) - return false; - idx = 0; - pos = 0; - return true; -} - -bool MathScriptInset::idxFirstDown(int & idx, int & pos) const -{ - if (!down()) - return false; - idx = 1; - pos = 0; - return true; -} - -bool MathScriptInset::idxLastUp(int & idx, int & pos) const -{ - if (!up()) - return false; - idx = 0; - pos = cell(idx).size(); - return true; -} - -bool MathScriptInset::idxLastDown(int & idx, int & pos) const -{ - if (!down()) - return false; - idx = 1; - pos = cell(idx).size(); - return true; -} - void MathScriptInset::idxDelete(int & idx, bool & popit, bool & deleteit) { diff --git a/src/mathed/math_scriptinset.h b/src/mathed/math_scriptinset.h index 2fa10b6e01..7f589e953b 100644 --- a/src/mathed/math_scriptinset.h +++ b/src/mathed/math_scriptinset.h @@ -2,7 +2,7 @@ #ifndef MATH_SCRIPTINSET_H #define MATH_SCRIPTINSET_H -#include "math_inset.h" +#include "math_updowninset.h" #ifdef __GNUG__ #pragma interface @@ -12,61 +12,18 @@ \author André Pönitz */ -class MathScriptInset : public MathInset { +class MathScriptInset : public MathUpDownInset { public: - /// - MathScriptInset(); /// MathScriptInset(bool up, bool down); /// MathInset * clone() const; /// - void Write(std::ostream &, bool fragile) const; - /// void WriteNormal(std::ostream &) const; - /// - void Metrics(MathStyles st); - /// - void draw(Painter &, int x, int baseline); - /// - bool idxUp(int & idx, int & pos) const; - /// - bool idxDown(int & idx, int & pos) const; - /// - bool idxLeft(int & idx, int & pos) const; - /// - bool idxRight(int & idx, int & pos) const; - /// - bool idxFirst(int & idx, int & pos) const; - /// - bool idxFirstUp(int & idx, int & pos) const; - /// - bool idxFirstDown(int & idx, int & pos) const; - /// - bool idxLast(int & idx, int & pos) const; - /// - bool idxLastUp(int & idx, int & pos) const; - /// - bool idxLastDown(int & idx, int & pos) const; - /// - bool up() const; - /// - bool down() const; - /// - void up(bool); - /// - void down(bool); - /// - bool isActive() const { return false; } /// Identifies ScriptInsets - bool isScriptInset() const { return true; } + bool isUpDownInset() const { return true; } /// void idxDelete(int & idx, bool & popit, bool & deleteit); -private: - /// - bool up_; - /// - bool down_; }; #endif diff --git a/src/mathed/math_sizeinset.C b/src/mathed/math_sizeinset.C index 707248a131..f6dc71f82c 100644 --- a/src/mathed/math_sizeinset.C +++ b/src/mathed/math_sizeinset.C @@ -45,7 +45,7 @@ void MathSizeInset::draw(Painter & pain, int x, int y) } -void MathSizeInset::Metrics(MathStyles /* st */) +void MathSizeInset::Metrics(MathStyles /* st */, int, int) { xcell(0).Metrics(style_); ascent_ = xcell(0).ascent_; diff --git a/src/mathed/math_sizeinset.h b/src/mathed/math_sizeinset.h index 98d209e5e1..d581be7320 100644 --- a/src/mathed/math_sizeinset.h +++ b/src/mathed/math_sizeinset.h @@ -20,7 +20,7 @@ public: /// virtual MathInset * clone() const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void draw(Painter &, int x, int baseline); /// diff --git a/src/mathed/math_spaceinset.C b/src/mathed/math_spaceinset.C index 85cb731460..8c6d91369c 100644 --- a/src/mathed/math_spaceinset.C +++ b/src/mathed/math_spaceinset.C @@ -55,7 +55,7 @@ void MathSpaceInset::WriteNormal(std::ostream & os) const } -void MathSpaceInset::Metrics(MathStyles st) +void MathSpaceInset::Metrics(MathStyles st, int, int) { size_ = st; width_ = space_ ? space_ * 2 : 2; diff --git a/src/mathed/math_spaceinset.h b/src/mathed/math_spaceinset.h index 9983f4945b..620f4d2bdd 100644 --- a/src/mathed/math_spaceinset.h +++ b/src/mathed/math_spaceinset.h @@ -23,7 +23,7 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void SetSpace(int sp); /// diff --git a/src/mathed/math_sqrtinset.C b/src/mathed/math_sqrtinset.C index b201df0545..e802a4e0c7 100644 --- a/src/mathed/math_sqrtinset.C +++ b/src/mathed/math_sqrtinset.C @@ -22,7 +22,7 @@ MathInset * MathSqrtInset::clone() const } -void MathSqrtInset::Metrics(MathStyles st) +void MathSqrtInset::Metrics(MathStyles st, int, int) { xcell(0).Metrics(st); size_ = st; @@ -51,7 +51,7 @@ void MathSqrtInset::draw(Painter & pain, int x, int y) void MathSqrtInset::Write(std::ostream & os, bool fragile) const { - os << '\\' << name_ << '{'; + os << "\\sqrt{"; cell(0).Write(os, fragile); os << '}'; } diff --git a/src/mathed/math_sqrtinset.h b/src/mathed/math_sqrtinset.h index 18019df49d..e3e153de39 100644 --- a/src/mathed/math_sqrtinset.h +++ b/src/mathed/math_sqrtinset.h @@ -24,6 +24,6 @@ public: /// void WriteNormal(std::ostream &) const; /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); }; #endif diff --git a/src/mathed/math_updowninset.C b/src/mathed/math_updowninset.C new file mode 100644 index 0000000000..57c2ba9522 --- /dev/null +++ b/src/mathed/math_updowninset.C @@ -0,0 +1,178 @@ +#include + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "math_updowninset.h" +#include "support/LOstream.h" + + +MathUpDownInset::MathUpDownInset() + : MathInset(2), up_(false), down_(false) +{} + +MathUpDownInset::MathUpDownInset(bool up, bool down) + : MathInset(2), up_(up), down_(down) +{} + + +MathInset * MathUpDownInset::clone() const +{ + return new MathUpDownInset(*this); +} + + +bool MathUpDownInset::up() const +{ + return up_; +} + +bool MathUpDownInset::down() const +{ + return down_; +} + +void MathUpDownInset::up(bool b) +{ + up_ = b; +} + +void MathUpDownInset::down(bool b) +{ + down_ = b; +} + + +bool MathUpDownInset::idxRight(int &, int &) const +{ + return false; +} + +bool MathUpDownInset::idxLeft(int &, int &) const +{ + return false; +} + + +bool MathUpDownInset::idxUp(int & idx, int & pos) const +{ + if (idx == 0 || !up()) + return false; + idx = 0; + pos = 0; + return true; +} + +bool MathUpDownInset::idxDown(int & idx, int & pos) const +{ + if (idx == 1 || !down()) + return false; + idx = 1; + pos = 0; + return true; +} + +bool MathUpDownInset::idxFirst(int & idx, int & pos) const +{ + idx = up() ? 0 : 1; + pos = 0; + return true; +} + +bool MathUpDownInset::idxLast(int & idx, int & pos) const +{ + idx = down() ? 1 : 0; + pos = cell(idx).size(); + return true; +} + + +bool MathUpDownInset::idxFirstUp(int & idx, int & pos) const +{ + if (!up()) + return false; + idx = 0; + pos = 0; + return true; +} + +bool MathUpDownInset::idxFirstDown(int & idx, int & pos) const +{ + if (!down()) + return false; + idx = 1; + pos = 0; + return true; +} + +bool MathUpDownInset::idxLastUp(int & idx, int & pos) const +{ + if (!up()) + return false; + idx = 0; + pos = cell(idx).size(); + return true; +} + +bool MathUpDownInset::idxLastDown(int & idx, int & pos) const +{ + if (!down()) + return false; + idx = 1; + pos = cell(idx).size(); + return true; +} + + +void MathUpDownInset::idxDelete(int & idx, bool & popit, bool & deleteit) +{ + if (idx == 0) + up(false); + else + down(false); + popit = true; + deleteit = !(up() || down()); +} + +void MathUpDownInset::Write(std::ostream & os, bool fragile) const +{ + if (up()) { + os << "^{"; + cell(0).Write(os, fragile); + os << "}"; + } + if (down()) { + os << "_{"; + cell(1).Write(os, fragile); + os << "}"; + } +} + +void MathUpDownInset::Metrics(MathStyles st, int asc, int des) +{ + if (up()) + xcell(0).Metrics(st); + if (down()) + xcell(1).Metrics(st); + + // we assume that asc, des, wid are the metrics of the item in front + // of this MathScriptInset + width_ = std::max(xcell(0).width(), xcell(1).width()); + ascent_ = up() ? xcell(0).height() + 9 : 0; + descent_ = down() ? xcell(1).height() : 0; + dy0_ = - asc - xcell(0).descent(); + dy1_ = des + xcell(1).ascent(); +} + + +void MathUpDownInset::draw(Painter & pain, int x, int y) +{ + xo(x); + yo(y); + if (up()) + xcell(0).draw(pain, x, y + dy0_); + if (down()) + xcell(1).draw(pain, x, y + dy1_); +} + diff --git a/src/mathed/math_updowninset.h b/src/mathed/math_updowninset.h new file mode 100644 index 0000000000..325f068482 --- /dev/null +++ b/src/mathed/math_updowninset.h @@ -0,0 +1,75 @@ +// -*- C++ -*- +#ifndef MATH_UPDOWNINSET_H +#define MATH_UPDOWNINSET_H + +#include "math_inset.h" + +#ifdef __GNUG__ +#pragma interface +#endif + +/** Abstract base class for super- and subscripts and mathop inset + \author André Pönitz + */ + +class MathUpDownInset : public MathInset { +public: + /// + MathUpDownInset(); + /// + MathUpDownInset(bool up, bool down); + /// + MathInset * clone() const; + /// + void Write(std::ostream &, bool fragile) const; + /// + void Metrics(MathStyles st, int asc = 0, int des = 0); + /// + void draw(Painter &, int x, int baseline); + /// + bool idxUp(int & idx, int & pos) const; + /// + bool idxDown(int & idx, int & pos) const; + /// + bool idxLeft(int & idx, int & pos) const; + /// + bool idxRight(int & idx, int & pos) const; + /// + bool idxFirst(int & idx, int & pos) const; + /// + bool idxFirstUp(int & idx, int & pos) const; + /// + bool idxFirstDown(int & idx, int & pos) const; + /// + bool idxLast(int & idx, int & pos) const; + /// + bool idxLastUp(int & idx, int & pos) const; + /// + bool idxLastDown(int & idx, int & pos) const; + /// + bool up() const; + /// + bool down() const; + /// + void up(bool); + /// + void down(bool); + /// + bool isActive() const { return false; } + /// Identifies ScriptInsets + bool isUpDownInset() const { return true; } + /// + void idxDelete(int & idx, bool & popit, bool & deleteit); +private: + /// + bool up_; + /// + bool down_; +protected: + /// + int dy0_; + /// + int dy1_; +}; + +#endif diff --git a/src/mathed/xarray.C b/src/mathed/xarray.C index 688ef4b83b..8d3c9c11a7 100644 --- a/src/mathed/xarray.C +++ b/src/mathed/xarray.C @@ -14,12 +14,13 @@ using std::max; using std::min; + MathXArray::MathXArray() : width_(0), ascent_(0), descent_(0), xo_(0), yo_(0), style_(LM_ST_TEXT) {} -void MathXArray::Metrics(MathStyles st) +void MathXArray::Metrics(MathStyles st, int, int) { if (data_.empty()) { mathed_char_dim(LM_TC_VAR, st, 'I', ascent_, descent_, width_); @@ -31,24 +32,28 @@ void MathXArray::Metrics(MathStyles st) width_ = 0; style_ = st; + // keep last values for scriptInset's need to look back + int asc = 0; + int des = 0; + int wid = 0; + mathed_char_height(LM_TC_VAR, st, 'I', asc, des); + for (int pos = 0; pos < data_.size(); data_.next(pos)) { - MathInset * p = data_.GetInset(pos); + MathInset * p = data_.nextInset(pos); if (p) { - p->Metrics(st); - ascent_ = max(ascent_, p->ascent()); - descent_ = max(descent_, p->descent()); - width_ += p->width(); + // only MathUpDownInsets will use the asc/des information... + p->Metrics(st, asc, des); + asc = p->ascent(); + des = p->descent(); + wid = p->width(); } else { char cx = data_.GetChar(pos); MathTextCodes fc = data_.GetCode(pos); - int asc; - int des; - int wid; mathed_char_dim(fc, style_, cx, asc, des, wid); - ascent_ = max(ascent_, asc); - descent_ = max(descent_, des); - width_ += wid; } + ascent_ = max(ascent_, asc); + descent_ = max(descent_, des); + width_ += wid; } } @@ -64,7 +69,7 @@ void MathXArray::draw(Painter & pain, int x, int y) } for (int pos = 0; pos < data_.size(); data_.next(pos)) { - MathInset * p = data_.GetInset(pos); + MathInset * p = data_.nextInset(pos); if (p) { p->draw(pain, x, y); x += p->width(); @@ -104,7 +109,7 @@ int MathXArray::width(int pos) const return 0; if (data_.isInset(pos)) - return data_.GetInset(pos)->width(); + return data_.nextInset(pos)->width(); else return mathed_char_width(data_.GetCode(pos), style_, data_.GetChar(pos)); } diff --git a/src/mathed/xarray.h b/src/mathed/xarray.h index 7bc0bc8269..abd9a3d3e9 100644 --- a/src/mathed/xarray.h +++ b/src/mathed/xarray.h @@ -18,7 +18,7 @@ public: /// MathXArray(); /// - void Metrics(MathStyles st); + void Metrics(MathStyles st, int asc = 0, int des = 0); /// void draw(Painter & pain, int x, int y);