Index: pageitem_textframe.cpp
===================================================================
--- pageitem_textframe.cpp	(revision 13363)
+++ pageitem_textframe.cpp	(working copy)
@@ -500,14 +500,57 @@
 			EndX += 0.125;
 			pt1 = QPoint(qRound(ceil(EndX + insets.Right)), static_cast<int>(yPos+descent));
 			pt2 = QPoint(qRound(ceil(EndX + insets.Right)), static_cast<int>(ceil(yPos-ascent)));
-		} while 
-			(   (EndX + (legacy? lineCorr + insets.Right : 0) < colRight - morespace)
-			  && shape.contains(pf2.map(pt1))
-			  && shape.contains(pf2.map(pt2)) 
-			  );
-		
-		return EndX;
+		} while ( (EndX + (legacy? lineCorr + insets.Right : 0) < colRight - morespace)
+			&& shape.contains(pf2.map(pt1))
+			&& shape.contains(pf2.map(pt2))
+			);
+	//original endX calculated now
+
+
+	// if we aren't restricted further, we'll end at this maxX:
+	double maxX = colRight - morespace;
+	if (legacy) maxX -= lineCorr + insets.Right;
+
+	double StartX = floor(qMax(line.x, qMin(colRight,breakXPos) - 1));
+	int xPos = static_cast<int>(ceil(maxX + insets.Right));
+	int yDesc = static_cast<int>(yPos+descent);
+	int yAsc = static_cast<int>(ceil(yPos-ascent));
+
+	QPoint pt12 (xPos, yDesc);
+	QPoint pt22 (xPos, yAsc);
+
+	// simple and fast case: transformed line completely falls within range
+	// this will happen if we don't wrap around anything - ie. very often
+	QPolygon p;
+	p.append (pf2.map (QPoint (StartX, yAsc)));
+	p.append (pf2.map (QPoint (StartX, yDesc)));
+	p.append (pf2.map (pt12));
+	p.append (pf2.map (pt22));
+	// check if something gets in the way
+	QRegion lineI = shape.intersected (p.boundingRect());
+	// if the intersection only has 1 rectangle, then nothing gets in the way
+	if (lineI.numRects() == 1)
+	{
+		int steps = static_cast<int>((maxX - StartX - insets.Right - 2)/0.125);
+		if (steps > 0)
+			StartX += steps * 0.125;
 	}
+
+	// qDebug()<<"endOfLine: going for the hard case";
+	double EndX2;
+	double Interval = 0.125;
+	EndX2 = StartX;
+	do {
+		EndX2 += Interval;
+		int xPos = static_cast<int>(ceil(EndX2 + insets.Right));
+		pt12.setX (xPos);
+		pt22.setX (xPos);
+	} while ((EndX2 < maxX) && shape.contains(pf2.map(pt12)) && shape.contains(pf2.map(pt22)));
+
+	qDebug()<<EndX<<EndX2<<StartX<<maxX;
+	if (EndX!=EndX2) qDebug()<<"DIFFERING ENDX!!!!!";
+	return EndX;
+}
 	
 	
 	double getLineAscent(const StoryText& itemText)
