Index: Version135/Scribus/scribus/pageitem_textframe.cpp
===================================================================
--- Version135/Scribus/scribus/pageitem_textframe.cpp	(wersja 16630)
+++ Version135/Scribus/scribus/pageitem_textframe.cpp	(kopia robocza)
@@ -425,7 +425,7 @@
 	/// called when a possible break is passed
 	void rememberBreak(int index, double pos)
 	{
-		if (pos > colRight)
+		if (pos > colRight - insets.Right)
 		{
 			// only look for the first break behind the right edge
 			maxShrink = 0;
@@ -439,7 +439,7 @@
 				if (newLooseness >= oldLooseness)
 					return;
 			}
-			breakXPos = colRight;
+			breakXPos = colRight - insets.Right;
 		}
 		else
 		{
@@ -520,9 +520,10 @@
 
 		// if we aren't restricted further, we'll end at this maxX:
 		double maxX = colRight - morespace;
-		if (legacy) maxX -= (lineCorr + insets.Right);
+		if (legacy)
+			maxX -= (lineCorr + insets.Right);
 
-		double StartX = floor(qMax(line.x, qMin(colRight,breakXPos) - 1));
+		double StartX = floor(qMax(line.x, qMin(maxX, breakXPos)));
 		int xPos  = static_cast<int>(ceil(maxX + insets.Right));
 		int yDesc = static_cast<int>(yPos+line.descent);
 		int yAsc  = static_cast<int>(ceil(yPos-line.ascent));
@@ -542,7 +543,7 @@
 		// if the intersection only has 1 rectangle, then nothing gets in the way
 		if (lineI.numRects() == 1)
 		{
-			int xPos = qRound(ceil(StartX + 0.125 + insets.Right));
+			int xPos = qRound(ceil(StartX + insets.Right));
 			pt12.setX(xPos);
 			pt22.setX(xPos);
 			if (shape.contains(pf2.map(pt12)) && shape.contains(pf2.map(pt22)))
@@ -570,6 +571,7 @@
 			qDebug()<<EndX<<EndX2<<StartX<<maxX;
 			qDebug()<<"DIFFERING ENDX!!!!!";
 		}*/
+
 		return EndX2;
 	}
 	
@@ -746,8 +748,6 @@
 	
 	double glyphNatural = 0;
 	double spaceNatural = 0;
-	double glyphExtension;
-	double spaceExtension;
 
 	const ParagraphStyle& style(itemText.paragraphStyle(line.firstItem));
 
@@ -764,29 +764,37 @@
 		}
 	}
 
-	// decision: prio 1: stretch glyph;  prio 2: stretch spaces
+	double glyphExtension = 0;
+	double spaceExtension = 0;
+
+	if (line.width < glyphNatural + spaceNatural) //stretch
+		glyphExtension = qMax(line.width / (glyphNatural + spaceNatural), style.minGlyphExtension());
+	else //expand
+		glyphExtension = qMin(1 + (line.width / (glyphNatural + spaceNatural)), style.maxGlyphExtension());
+	if (spaceNatural > 0)
+		spaceExtension = (line.width - glyphNatural * glyphExtension ) / spaceNatural  - 1;
+
+//	// decision: prio 1: stretch glyph;  prio 2: stretch spaces
+//	double minGE = style.minGlyphExtension();
+//	if (line.width < spaceNatural + glyphNatural * minGE && spaceNatural > 0)
+//	{
+//		glyphExtension = style.minGlyphExtension() - 1;
+//		spaceExtension = (line.width - glyphNatural * (1+glyphExtension) ) / spaceNatural  - 1;
+//	}
+//	else if (line.width < spaceNatural + glyphNatural * style.maxGlyphExtension() && glyphNatural > 0)
+//	{
+//		spaceExtension = 0;
+//		glyphExtension = (line.width - spaceNatural) / glyphNatural  - 1;
+//	}
+//	else
+//	{
+//		glyphExtension = style.minGlyphExtension() - 1;
+//		if (spaceNatural > 0)
+//			spaceExtension = (line.width - glyphNatural * (1+glyphExtension) ) / spaceNatural  - 1;
+//		else
+//			spaceExtension = 0;
+//	}
 	
-	if (line.width < spaceNatural + glyphNatural * style.minGlyphExtension() && spaceNatural > 0)
-	{
-		glyphExtension = style.minGlyphExtension() - 1;
-		spaceExtension = (line.width - glyphNatural * (1+glyphExtension) ) / spaceNatural  - 1;
-	}
-	else if (line.width < spaceNatural + glyphNatural * style.maxGlyphExtension() && glyphNatural > 0)
-	{
-		spaceExtension = 0;
-		glyphExtension = (line.width - spaceNatural) / glyphNatural  - 1;
-	}
-	else
-	{
-		glyphExtension = style.maxGlyphExtension() - 1;
-		if (spaceNatural > 0)
-			spaceExtension = (line.width - glyphNatural * (1+glyphExtension) ) / spaceNatural  - 1;
-		else
-			spaceExtension = 0;
-	}
-	
-	double glyphScale = 1 + glyphExtension;
-	
 /*
 	qDebug() << QString("justify: line = %7 natural = %1 + %2 = %3 (%4); spaces + %5%%; min=%8; glyphs + %6%%; min=%9")
 		   .arg(spaceNatural).arg(glyphNatural).arg(spaceNatural+glyphNatural).arg(line.naturalWidth)
@@ -800,12 +808,12 @@
 		double wide = itemText.item(yof)->glyph.wide();
 		if (!SpecialChars::isExpandingSpace(itemText.text(yof)))
 		{
-			itemText.item(yof)->glyph.last()->xadvance += wide * glyphExtension;
+			itemText.item(yof)->glyph.last()->xadvance += wide * (glyphExtension -1);
 			GlyphLayout* glyph = &(itemText.item(yof)->glyph);
 			while (glyph)
 			{
-				glyph->xoffset *= glyphScale;
-				glyph->scaleH *= glyphScale;
+				glyph->xoffset *= glyphExtension;
+				glyph->scaleH *= glyphExtension;
 				glyph = glyph->more;
 			}
 		}
@@ -820,7 +828,8 @@
 /// called when linelength is known and line is not justified
 static void indentLine(StoryText& itemText, LineSpec& line, double leftIndent)
 {
-	if (line.naturalWidth > line.width)
+	//it is hack, but works well
+	if (line.naturalWidth + 0.125 >= line.width)
 	{
 		justifyLine(itemText, line);
 	}
