Andre Poenitz wrote:

>> I wonder if we really need to cache those num_foo_fills.
> 
> I don't think so.
> 
>> I think that it wont be much slower to compute them on demand (they
>> are used in three places : cursorX, getColumnNearX and rowPainter).
> 
> And all of them are 'close to the user' i.e. most likely uncritical...

Right. Attached an implementation of this idea. Much cleaner IMO, and the
same result.

[ 8 files changed, 83 insertions(+), 147 deletions(-) ]

Naming is probably bogus: I've introduced a row_metrics struct in lyxtext.h
holding all fill_foo and row.x (no need to cache it neither... it is used
always simultaneously with the other three). (naming, header,
namespace...?)

Then prepareToPrint could be renamed to something more descriptive like
computeRowMetrics... or computeRowXMetrics (it's all x related)

As with the previous one, help in testing is welcomed.

Alfredo
? ChangeLog-old
? PosIterator.C-save
? PosIterator.h-save
? bfri.C
? textcursor.C-save
? textcursor.h-save
? insets/insetcollapsable-save.C
? insets/insettext-save.C
Index: Bidi.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/Bidi.C,v
retrieving revision 1.3
diff -u -p -u -r1.3 Bidi.C
--- Bidi.C	13 Nov 2003 13:43:35 -0000	1.3
+++ Bidi.C	27 Feb 2004 22:31:46 -0000
@@ -52,7 +52,7 @@ bool Bidi::same_direction() const
 
 
 void Bidi::computeTables(Paragraph const & par,
-	Buffer const & buf, Row & row)
+	Buffer const & buf, Row const & row)
 {
 	same_direction_ = true;
 	if (!lyxrc.rtl_support) {
Index: Bidi.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/Bidi.h,v
retrieving revision 1.1
diff -u -p -u -r1.1 Bidi.h
--- Bidi.h	23 Oct 2003 13:28:44 -0000	1.1
+++ Bidi.h	27 Feb 2004 22:31:46 -0000
@@ -45,7 +45,7 @@ struct  Bidi {
 	bool same_direction() const;
 	///
 	void computeTables(Paragraph const & par,
-			   Buffer const &, Row & row);
+			   Buffer const &, Row const & row);
 private: 
 	/// 
 	bool same_direction_;
Index: lyxrow.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxrow.C,v
retrieving revision 1.37
diff -u -p -u -r1.37 lyxrow.C
--- lyxrow.C	27 Feb 2004 09:18:01 -0000	1.37
+++ lyxrow.C	27 Feb 2004 22:31:47 -0000
@@ -24,15 +24,13 @@ using lyx::pos_type;
 
 Row::Row()
 	: pos_(0), end_(0), height_(0), width_(0), y_offset_(0),
-	  ascent_of_text_(0), baseline_(0),
-	  x_(0), fill_separator_(0), fill_hfill_(0), fill_label_hfill_(0)
+	  ascent_of_text_(0), baseline_(0)
 {}
 
 
 Row::Row(pos_type pos)
 	: pos_(pos), end_(0), height_(0), width_(0), y_offset_(0),
-	  ascent_of_text_(0), baseline_(0),
-	  x_(0), fill_separator_(0), fill_hfill_(0), fill_label_hfill_(0)
+	  ascent_of_text_(0), baseline_(0)
 {}
 
 
@@ -105,54 +103,6 @@ void Row::baseline(unsigned int b)
 unsigned int Row::baseline() const
 {
 	return baseline_;
-}
-
-
-float Row::x() const
-{
-	return x_;
-}
-
-
-void Row::x(float f)
-{
-	x_ = f;
-}
-
-
-float Row::fill_separator() const
-{
-	return fill_separator_;
-}
-
-
-void Row::fill_separator(float f)
-{
-	fill_separator_ = f;
-}
-
-
-float Row::fill_hfill() const
-{
-	return fill_hfill_;
-}
-
-
-void Row::fill_hfill(float f)
-{
-	fill_hfill_ = f;
-}
-
-
-float Row::fill_label_hfill() const
-{
-	return fill_label_hfill_;
-}
-
-
-void Row::fill_label_hfill(float f)
-{
-	fill_label_hfill_ = f;
 }
 
 
Index: lyxrow.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxrow.h,v
retrieving revision 1.40
diff -u -p -u -r1.40 lyxrow.h
--- lyxrow.h	27 Feb 2004 09:18:01 -0000	1.40
+++ lyxrow.h	27 Feb 2004 22:31:47 -0000
@@ -58,24 +58,9 @@ public:
 	unsigned int y_offset() const { return y_offset_; }
 	/// cache the y position
 	void y_offset(unsigned int newy) { y_offset_ = newy; }
-	///
-	float x() const;
-	///
-	void x(float);
-	///
-	float fill_separator() const;
-	///
-	void fill_separator(float);
-	///
-	float fill_hfill() const;
-	///
-	void fill_hfill(float);
-	///
-	float fill_label_hfill() const;
-	///
-	void fill_label_hfill(float);
 	/// current debugging only
 	void dump(const char * = "") const;
+
 private:
 	/// first pos covered by this row
 	lyx::pos_type pos_;
@@ -93,14 +78,6 @@ private:
 	unsigned int top_of_text_;
 	///
 	unsigned int baseline_;
-	/// offet from left border
-	float x_;
-	///
-	float fill_separator_;
-	///
-	float fill_hfill_;
-	///
-	float fill_label_hfill_;
 };
 
 #endif
Index: lyxtext.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtext.h,v
retrieving revision 1.295
diff -u -p -u -r1.295 lyxtext.h
--- lyxtext.h	27 Feb 2004 09:18:01 -0000	1.295
+++ lyxtext.h	27 Feb 2004 22:31:48 -0000
@@ -46,6 +46,18 @@ class UpdatableInset;
 class VSpace;
 
 
+struct row_metrics {
+	///
+	double separator;
+	///
+	double hfill;
+	///
+	double label_hfill;
+	///
+	double x;
+};
+
+
 /// This class encapsulates the main text data and operations in LyX
 class LyXText {
 public:
@@ -320,7 +332,8 @@ public:
 
 	/** this calculates the specified parameters. needed when setting
 	 * the cursor and when creating a visible row */
-	void prepareToPrint(ParagraphList::iterator pit, Row & row) const;
+	row_metrics
+	prepareToPrint(ParagraphList::iterator pit, Row const & row) const;
 
 	/// access to our paragraphs
 	ParagraphList & paragraphs() const;
Index: rowpainter.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/rowpainter.C,v
retrieving revision 1.120
diff -u -p -u -r1.120 rowpainter.C
--- rowpainter.C	27 Feb 2004 09:18:02 -0000	1.120
+++ rowpainter.C	27 Feb 2004 22:31:49 -0000
@@ -120,16 +120,16 @@ RowPainter::RowPainter(BufferView const 
      ParagraphList::iterator pit, RowList::iterator rit,
      int xo, int yo)
 	: bv_(bv), pain_(bv_.painter()), text_(text), rit_(rit), row_(*rit),
-	  pit_(pit), xo_(xo), yo_(yo), x_(row_.x()),
-		width_(text_.textWidth()),
-		separator_(row_.fill_separator()),
-		hfill_(row_.fill_hfill()),
-		label_hfill_(row_.fill_label_hfill())
+	  pit_(pit), xo_(xo), yo_(yo), width_(text_.width)
 {
 	//lyxerr << "RowPainter: x: " << x_ << " xo: " << xo << " yo: " << yo
 	//	<< " pit->y: " << pit_->y
 	//	<< " row: " << (pit_->size() ? pit_->getChar(row_.pos()) : 'X') << endl;
-	x_ += xo_;
+	row_metrics m = text_.prepareToPrint(pit, row_);
+	x_ = m.x + xo_;
+	separator_ = m.separator;
+	hfill_ = m.hfill;
+	label_hfill_ = m.label_hfill;
 
 	// background has already been cleared.
 	if (&text_ == bv_.text())
Index: text.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text.C,v
retrieving revision 1.542
diff -u -p -u -r1.542 text.C
--- text.C	27 Feb 2004 13:06:16 -0000	1.542
+++ text.C	27 Feb 2004 22:31:52 -0000
@@ -1020,19 +1020,23 @@ void LyXText::charInserted()
 }
 
 
-void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
+row_metrics
+LyXText::prepareToPrint(ParagraphList::iterator pit, Row const & row) const
 {
-	double w = textWidth() - row.width();
-	double fill_hfill = 0;
-	double fill_label_hfill = 0;
-	double fill_separator = 0;
-	double x = 0;
+	row_metrics result;
+
+	double w = width - row.width();
+
+	result.hfill = 0;
+	result.label_hfill = 0;
+	result.separator = 0;
+	result.x = 0;
 
 	bool const is_rtl = isRTL(*pit);
 	if (is_rtl)
-		x = rightMargin(*pit);
+		result.x = rightMargin(*pit);
 	else
-		x = leftMargin(pit, row.pos());
+		result.x = leftMargin(pit, row.pos());
 
 	// is there a manual margin with a manual label
 	LyXLayout_ptr const & layout = pit->layout();
@@ -1051,7 +1055,7 @@ void LyXText::prepareToPrint(ParagraphLi
 			++nlh;
 
 		if (nlh && !pit->getLabelWidthString().empty())
-			fill_label_hfill = labelFill(pit, row) / double(nlh);
+			result.label_hfill = labelFill(pit, row) / double(nlh);
 	}
 
 	// are there any hfills in the row?
@@ -1059,7 +1063,7 @@ void LyXText::prepareToPrint(ParagraphLi
 
 	if (nh) {
 		if (w > 0)
-			fill_hfill = w / nh;
+			result.hfill = w / nh;
 	// we don't have to look at the alignment if it is ALIGN_LEFT and
 	// if the row is already larger then the permitted width as then
 	// we force the LEFT_ALIGN'edness!
@@ -1100,17 +1104,17 @@ void LyXText::prepareToPrint(ParagraphLi
 			    && !pit->isNewline(row.endpos() - 1)
 			    && !disp_inset
 				) {
-				fill_separator = w / ns;
+				result.separator = w / ns;
 			} else if (is_rtl) {
-				x += w;
+				result.x += w;
 			}
 			break;
 		}
 		case LYX_ALIGN_RIGHT:
-			x += w;
+			result.x += w;
 			break;
 		case LYX_ALIGN_CENTER:
-			x += w / 2;
+			result.x += w / 2;
 			break;
 		}
 	}
@@ -1123,16 +1127,13 @@ void LyXText::prepareToPrint(ParagraphLi
 		if (body_pos > 0
 		    && (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
 		{
-			x += font_metrics::width(layout->labelsep, getLabelFont(pit));
+			result.x += font_metrics::width(layout->labelsep, getLabelFont(pit));
 			if (body_pos <= end)
-				x += fill_label_hfill;
+				result.x += result.label_hfill;
 		}
 	}
 
-	row.fill_hfill(fill_hfill);
-	row.fill_label_hfill(fill_label_hfill);
-	row.fill_separator(fill_separator);
-	row.x(x);
+	return result;
 }
 
 
@@ -1593,7 +1594,6 @@ void LyXText::redoParagraphInternal(Para
 		Row row(z);
 		rowBreakPoint(pit, row);
 		setRowWidth(pit, row);
-		prepareToPrint(pit, row);
 		setHeightOfRow(pit, row);
 		row.y_offset(pit->height);
 		pit->rows.push_back(row);
@@ -1832,13 +1832,12 @@ int LyXText::cursorX(CursorSlice const &
 	ParagraphList::iterator pit = getPar(cur);
 	if (pit->rows.empty())
 		return xo_;
-	Row const & row         = *pit->getRow(cur.pos());
-	pos_type pos            = cur.pos();
-	pos_type cursor_vpos    = 0;
-	double x                = row.x();
-	double fill_separator   = row.fill_separator();
-	double fill_hfill       = row.fill_hfill();
-	double fill_label_hfill = row.fill_label_hfill();
+	Row const & row = *pit->getRow(cur.pos());
+	pos_type pos = cur.pos();
+	pos_type cursor_vpos = 0;
+
+	row_metrics m = prepareToPrint(pit, row);
+
 	pos_type const row_pos  = row.pos();
 	pos_type const end      = row.endpos();
 
@@ -1863,27 +1862,27 @@ int LyXText::cursorX(CursorSlice const &
 	for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
 		pos_type pos = bidi.vis2log(vpos);
 		if (body_pos > 0 && pos == body_pos - 1) {
-			x += fill_label_hfill
+			m.x += m.label_hfill
 				+ font_metrics::width(pit->layout()->labelsep,
 						      getLabelFont(pit));
 			if (pit->isLineSeparator(body_pos - 1))
-				x -= singleWidth(pit, body_pos - 1);
+				m.x -= singleWidth(pit, body_pos - 1);
 		}
 
 		if (hfillExpansion(*pit, row, pos)) {
-			x += singleWidth(pit, pos);
+			m.x += singleWidth(pit, pos);
 			if (pos >= body_pos)
-				x += fill_hfill;
+				m.x += m.hfill;
 			else
-				x += fill_label_hfill;
+				m.x += m.label_hfill;
 		} else if (pit->isSeparator(pos)) {
-			x += singleWidth(pit, pos);
+			m.x += singleWidth(pit, pos);
 			if (pos >= body_pos)
-				x += fill_separator;
+				m.x += m.separator;
 		} else
-			x += singleWidth(pit, pos);
+			m.x += singleWidth(pit, pos);
 	}
-	return xo_ + int(x);
+	return xo_ + int(m.x);
 }
 
 
Index: text2.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text2.C,v
retrieving revision 1.550
diff -u -p -u -r1.550 text2.C
--- text2.C	20 Feb 2004 17:19:51 -0000	1.550
+++ text2.C	27 Feb 2004 22:31:54 -0000
@@ -87,7 +87,7 @@ void LyXText::init(BufferView * bv)
 	for (ParagraphList::iterator pit = beg; pit != end; ++pit)
 		pit->rows.clear();
 
-	width = 0;
+	width = bv->workWidth();
 	height = 0;
 
 	current_font = getFont(beg, 0);
@@ -1197,11 +1197,8 @@ pos_type LyXText::getColumnNearX(Paragra
 	Row const & row, int & x, bool & boundary) const
 {
 	x -= xo_;
-	double tmpx             = row.x();
-	double fill_separator   = row.fill_separator();
-	double fill_hfill       = row.fill_hfill();
-	double fill_label_hfill = row.fill_label_hfill();
-
+	row_metrics r = prepareToPrint(pit, row);
+	
 	pos_type vc = row.pos();
 	pos_type end = row.endpos();
 	pos_type c = 0;
@@ -1210,7 +1207,7 @@ pos_type LyXText::getColumnNearX(Paragra
 	bool left_side = false;
 
 	pos_type body_pos = pit->beginOfBody();
-	double last_tmpx = tmpx;
+	double last_tmpx = r.x;
 
 	if (body_pos > 0 &&
 	    (body_pos > end || !pit->isLineSeparator(body_pos - 1)))
@@ -1218,38 +1215,38 @@ pos_type LyXText::getColumnNearX(Paragra
 
 	// check for empty row
 	if (vc == end) {
-		x = int(tmpx) + xo_;
+		x = int(r.x) + xo_;
 		return 0;
 	}
 
-	while (vc < end && tmpx <= x) {
+	while (vc < end && r.x <= x) {
 		c = bidi.vis2log(vc);
-		last_tmpx = tmpx;
+		last_tmpx = r.x;
 		if (body_pos > 0 && c == body_pos - 1) {
-			tmpx += fill_label_hfill +
+			r.x += r.label_hfill +
 				font_metrics::width(layout->labelsep, getLabelFont(pit));
 			if (pit->isLineSeparator(body_pos - 1))
-				tmpx -= singleWidth(pit, body_pos - 1);
+				r.x -= singleWidth(pit, body_pos - 1);
 		}
 
 		if (hfillExpansion(*pit, row, c)) {
-			tmpx += singleWidth(pit, c);
+			r.x += singleWidth(pit, c);
 			if (c >= body_pos)
-				tmpx += fill_hfill;
+				r.x += r.hfill;
 			else
-				tmpx += fill_label_hfill;
+				r.x += r.label_hfill;
 		} else if (pit->isSeparator(c)) {
-			tmpx += singleWidth(pit, c);
+			r.x += singleWidth(pit, c);
 			if (c >= body_pos)
-				tmpx += fill_separator;
+				r.x += r.separator;
 		} else {
-			tmpx += singleWidth(pit, c);
+			r.x += singleWidth(pit, c);
 		}
 		++vc;
 	}
 
-	if ((tmpx + last_tmpx) / 2 > x) {
-		tmpx = last_tmpx;
+	if ((r.x + last_tmpx) / 2 > x) {
+		r.x = last_tmpx;
 		left_side = true;
 	}
 
@@ -1264,8 +1261,8 @@ pos_type LyXText::getColumnNearX(Paragra
 	// the value of rtl.
 	bool const rtl = lastrow ? isRTL(*pit) : false;
 	if (lastrow &&
-		 ((rtl  &&  left_side && vc == row.pos() && x < tmpx - 5) ||
-		  (!rtl && !left_side && vc == end  && x > tmpx + 5)))
+		 ((rtl  &&  left_side && vc == row.pos() && x < r.x - 5) ||
+		  (!rtl && !left_side && vc == end  && x > r.x + 5)))
 		c = end;
 	else if (vc == row.pos()) {
 		c = bidi.vis2log(vc);
@@ -1282,13 +1279,13 @@ pos_type LyXText::getColumnNearX(Paragra
 
 	if (row.pos() < end && c >= end && pit->isNewline(end - 1)) {
 		if (bidi.level(end -1) % 2 == 0)
-			tmpx -= singleWidth(pit, end - 1);
+			r.x -= singleWidth(pit, end - 1);
 		else
-			tmpx += singleWidth(pit, end - 1);
+			r.x += singleWidth(pit, end - 1);
 		c = end - 1;
 	}
 
-	x = int(tmpx) + xo_;
+	x = int(r.x) + xo_;
 	return c - row.pos();
 }
 

Reply via email to