Index: Version135/Scribus/scribus/pageitem_textframe.cpp
===================================================================
--- Version135/Scribus/scribus/pageitem_textframe.cpp	(wersja 16630)
+++ Version135/Scribus/scribus/pageitem_textframe.cpp	(kopia robocza)
@@ -126,8 +126,12 @@
 	return  res;
 }
 
-QRegion PageItem_TextFrame::availableRegion(QRegion clip)
+QRegion PageItem_TextFrame::availableRegion(QRegion clip, QRegion * unavaiable)
 {
+	//hack to get empty unavaiable region
+	//on call unavaible = clip
+	if (unavaiable->xored(Clip).isEmpty())
+		unavaiable->setRects(NULL,0);
 	QRegion result(clip);
 	if (!isEmbedded)
 	{
@@ -156,7 +160,9 @@
 						continue;
 					if (docItem->textFlowAroundObject())
 					{
-						result = result.subtract(itemShape(docItem, Mp->xOffset() - Dp->xOffset(), Mp->yOffset() - Dp->yOffset()));
+						result = result.subtracted(itemShape(docItem, Mp->xOffset() - Dp->xOffset(), Mp->yOffset() - Dp->yOffset()));
+						if (unavaiable)
+							unavaiable->united(itemShape(docItem, Mp->xOffset() - Dp->xOffset(), Mp->yOffset() - Dp->yOffset()));
 					}
 					if (docItem->isGroupControl)
 						currentGroup = docItem->Groups.top();
@@ -192,7 +198,11 @@
 					if ((currentGroup != -1) && (docItem->Groups.count() != 0) && (currentGroup == docItem->Groups.top()))
 						continue;
 					if (docItem->textFlowAroundObject())
-						result = result.subtract(itemShape(docItem, 0, 0));
+					{
+						result = result.subtracted(itemShape(docItem, 0, 0));
+						if (unavaiable)
+							unavaiable->united(itemShape(docItem, 0, 0));
+					}
 					if (docItem->isGroupControl)
 						currentGroup = docItem->Groups.top();
 				}
@@ -425,7 +435,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 +449,7 @@
 				if (newLooseness >= oldLooseness)
 					return;
 			}
-			breakXPos = colRight;
+			breakXPos = colRight - insets.Right;
 		}
 		else
 		{
@@ -520,9 +530,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 +553,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 +581,7 @@
 			qDebug()<<EndX<<EndX2<<StartX<<maxX;
 			qDebug()<<"DIFFERING ENDX!!!!!";
 		}*/
+
 		return EndX2;
 	}
 	
@@ -734,8 +746,9 @@
 private:
 	double frameWidth;
 	double frameHeight;
+	double lineCorr;
+public:
 	MarginStruct insets;
-	double lineCorr;
 };
 
 
@@ -746,8 +759,6 @@
 	
 	double glyphNatural = 0;
 	double spaceNatural = 0;
-	double glyphExtension;
-	double spaceExtension;
 
 	const ParagraphStyle& style(itemText.paragraphStyle(line.firstItem));
 
@@ -764,29 +775,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 +819,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 +839,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);
 	}
@@ -916,9 +936,22 @@
 }
 
 
+void PageItem_TextFrame::checkInsets(QRegion * c, QPoint p, LineControl * current, MarginStruct * extra)
+{
+	//if point is in unavailable region
+	//right and left text distances should not working
+	if ( !c->isEmpty() && c->contains(p))
+	{
+		extra->Left = current->insets.Left = 0;
+		extra->Right = current->insets.Left = -1; //should be 0, but these value works better
+	}
+	else
+	{
+		extra->Left = current->insets.Left = Extra;
+		extra->Right = current->insets.Right = RExtra;
+	}
+}
 
-
-
 void PageItem_TextFrame::layout() 
 {
 // 	qDebug()<<"==Layout==" << itemName() ;
@@ -1015,14 +1048,16 @@
 	setShadow();
 	if ((itemText.length() != 0)) // || (NextBox != 0))
 	{
-		// determine layout area
-		QRegion cl = availableRegion(QRegion(pf2.map(Clip)));
+		// determine layout area and unavaiable area
+		//hack to set pointer to clNA (cl Not Avaiable) region
+		//without it clNA was unset
+		QRegion clNA = QRegion(pf2.map(Clip));
+		QRegion cl = availableRegion(QRegion(pf2.map(Clip)), &clNA);
 		if (cl.isEmpty())
 		{
 			MaxChars = firstInFrame();
 			goto NoRoom;
 		}
-		
 		if (imageFlippedH())
 		{
 			pf2.translate(Width, 0);
@@ -1075,6 +1110,7 @@
 		MaxChars = 0;
 		for (int a = firstInFrame(); a < itemText.length(); ++a)
 		{
+			checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 			hl = itemText.item(a);
 			if (a > 0 && itemText.text(a-1) == SpecialChars::PARSEP)
 				style = itemText.paragraphStyle(a);
@@ -1422,10 +1458,14 @@
 //				qDebug() << QString("linestart: %1 + %2 + %3 < %4").arg(current.xPos).arg(wide).arg(style.rightMargin()).arg(current.colRight);
 				double leftIndent = 0;
 				double xStep = legacy? 1 : 0.125;
-				while (!cl.contains(pf2.map(pt1)) || !cl.contains(pf2.map(pt2)) || current.isEndOfLine(wide + leftIndent + style.rightMargin()))
+				while (!cl.contains(pf2.map(pt1)) || !cl.contains(pf2.map(pt2)))
 				{
+					checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
+					if (!current.isEndOfLine(wide + leftIndent + style.rightMargin()))
+						break;
 					fBorder = true;
 					current.xPos += xStep;
+					checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 					if (current.isEndOfLine(wide + leftIndent + style.rightMargin()))
 					{
 //						qDebug() << QString("eocol %5? %1 + %2 + %3 + %4").arg(current.yPos).arg(current.startOfCol).arg(style.lineSpacingMode() == ParagraphStyle::BaselineGridLineSpacing).arg(style.lineSpacing()).arg(current.column);
@@ -1650,6 +1690,7 @@
 			}
 			
 			// test if end of line reached
+			checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 			if ((!cl.contains(pf2.map(pt1))) || (!cl.contains(pf2.map(pt2))) || (legacy && current.isEndOfLine(style.rightMargin())))
 				outs = true;
 			if (current.isEndOfCol())
@@ -1776,6 +1817,7 @@
 				{
 					// find end of line
 					current.breakLine(itemText, style, firstLineOffset(), a);
+					checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 					EndX = current.endOfLine(cl, pf2, style.rightMargin());
 					current.finishLine(EndX);
 					
@@ -1845,6 +1887,7 @@
 						
 						current.updateHeightMetrics(itemText);
 						current.updateLineOffset(itemText, style, firstLineOffset());
+						checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 						EndX = current.endOfLine(cl, pf2, style.rightMargin());
 						current.finishLine(EndX);
 
@@ -1928,6 +1971,7 @@
 //						qDebug() << QString("style nb @%6: %1 -- %2, %4/%5 char: %3").arg(style.leftMargin()).arg(style.rightMargin())
 //							   .arg(style.charStyle().asString()).arg(style.name()).arg(style.parent())
 //							   .arg(a);
+						checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 						EndX = current.endOfLine(cl, pf2, style.rightMargin());
 						current.finishLine(EndX);
 //						qDebug() << QString("no break pos: %1-%2 @ %3 wid %4 nat %5 endX %6")
@@ -1938,6 +1982,7 @@
 				}
 				if ( outs || SpecialChars::isBreak(hl->ch, (Cols > 1)) )
 				{
+					checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 					if (outs && !current.isEndOfLine(hl->glyph.wide() + style.rightMargin()))
 					{
 						if (( hl->ch == SpecialChars::PARSEP || hl->ch == SpecialChars::LINEBREAK) 
@@ -1950,8 +1995,10 @@
 						}
 						bool fromOut = true;
 						double BotOffset = desc+extra.Bottom+lineCorr;
+						checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 						while (!current.isEndOfLine(style.rightMargin()))
 						{
+							checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 							current.xPos++;
 							if (current.isEndOfLine(style.rightMargin()))
 							{
@@ -2132,6 +2179,7 @@
 		int a = itemText.length()-1;
 		hl = a >=0 ? itemText.item(a) : NULL;
 		current.breakLine(itemText, style, firstLineOffset(), a);
+		checkInsets(&clNA, pf2.map(QPoint(static_cast<int>(ceil(current.xPos)), static_cast<int>(current.yPos))), &current, &extra);
 		EndX = current.endOfLine(cl, pf2, style.rightMargin());
 		current.finishLine(EndX);
 
Index: Version135/Scribus/scribus/pageitem_textframe.h
===================================================================
--- Version135/Scribus/scribus/pageitem_textframe.h	(wersja 16630)
+++ Version135/Scribus/scribus/pageitem_textframe.h	(kopia robocza)
@@ -32,6 +32,8 @@
 
 class ScPainter;
 class ScribusDoc;
+struct MarginStruct;
+struct LineControl;
 
 #include "text/nlsconfig.h"
 
@@ -85,7 +87,7 @@
 	void drawOverflowMarker(ScPainter *p);
 	void drawUnderflowMarker(ScPainter *p);
 	void drawColumnBorders(ScPainter *p);
-	QRegion availableRegion(QRegion clip);
+	QRegion availableRegion(QRegion clip, QRegion * unavaiable);
 
 #ifdef NLS_PROTO
 	void DrawLineItem(ScPainter *p, double width,
@@ -103,7 +105,7 @@
 	
 private:
 	bool cursorBiasBackward;
-
+	void checkInsets(QRegion * c, QPoint p, LineControl * current, MarginStruct * extra);
 	void setShadow();
 	void restoreTextSelection(int selStart, int selLength);
 	QString currentShadow;
