Fix page breaking with large images
Don't go into an infinite loop breaking pages, when an image is about
as large as the page. Correctly take top and bottom margins into account
when calculating whether the image could fit on one page.
Amends change 416b4cf685
.
Fixes: QTBUG-73730
Change-Id: Id311ddf05510be3b1d131702f4e17025a9861e58
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
parent
78c84c6353
commit
b4cc294347
@ -145,6 +145,9 @@ struct QTextLayoutStruct {
|
||||
inline QFixed absoluteY() const
|
||||
{ return frameY + y; }
|
||||
|
||||
inline QFixed contentHeight() const
|
||||
{ return pageHeight - pageBottomMargin - pageTopMargin; }
|
||||
|
||||
inline int currentPage() const
|
||||
{ return pageHeight == 0 ? 0 : (absoluteY() / pageHeight).truncate(); }
|
||||
|
||||
@ -2701,7 +2704,7 @@ void QTextDocumentLayoutPrivate::layoutBlock(const QTextBlock &bl, int blockPosi
|
||||
getLineHeightParams(blockFormat, line, scaling, &lineAdjustment, &lineBreakHeight, &lineHeight, &lineBottom);
|
||||
|
||||
while (layoutStruct->pageHeight > 0 && layoutStruct->absoluteY() + lineBreakHeight > layoutStruct->pageBottom &&
|
||||
layoutStruct->pageHeight >= lineBreakHeight) {
|
||||
layoutStruct->contentHeight() >= lineBreakHeight) {
|
||||
layoutStruct->newPage();
|
||||
|
||||
floatMargins(layoutStruct->y, layoutStruct, &left, &right);
|
||||
|
@ -59,6 +59,8 @@ private slots:
|
||||
void imageAtRightAlignedTab();
|
||||
void blockVisibility();
|
||||
|
||||
void largeImage();
|
||||
|
||||
private:
|
||||
QTextDocument *doc;
|
||||
};
|
||||
@ -347,5 +349,63 @@ void tst_QTextDocumentLayout::blockVisibility()
|
||||
QCOMPARE(doc->size(), halfSize);
|
||||
}
|
||||
|
||||
void tst_QTextDocumentLayout::largeImage()
|
||||
{
|
||||
auto img = QImage(400, 500, QImage::Format_ARGB32_Premultiplied);
|
||||
img.fill(Qt::black);
|
||||
|
||||
{
|
||||
QTextDocument document;
|
||||
|
||||
document.addResource(QTextDocument::ImageResource,
|
||||
QUrl("data://test.png"), QVariant(img));
|
||||
document.setPageSize({500, 504});
|
||||
|
||||
auto html = "<img src=\"data://test.png\">";
|
||||
document.setHtml(html);
|
||||
|
||||
QCOMPARE(document.pageCount(), 2);
|
||||
}
|
||||
|
||||
{
|
||||
QTextDocument document;
|
||||
|
||||
document.addResource(QTextDocument::ImageResource,
|
||||
QUrl("data://test.png"), QVariant(img));
|
||||
document.setPageSize({500, 508});
|
||||
|
||||
auto html = "<img src=\"data://test.png\">";
|
||||
document.setHtml(html);
|
||||
|
||||
QCOMPARE(document.pageCount(), 1);
|
||||
}
|
||||
|
||||
{
|
||||
QTextDocument document;
|
||||
|
||||
document.addResource(QTextDocument::ImageResource,
|
||||
QUrl("data://test.png"), QVariant(img));
|
||||
document.setPageSize({585, 250});
|
||||
|
||||
auto html = "<img src=\"data://test.png\">";
|
||||
document.setHtml(html);
|
||||
|
||||
QCOMPARE(document.pageCount(), 3);
|
||||
}
|
||||
|
||||
{
|
||||
QTextDocument document;
|
||||
|
||||
document.addResource(QTextDocument::ImageResource,
|
||||
QUrl("data://test.png"), QVariant(img));
|
||||
document.setPageSize({585, 258});
|
||||
|
||||
auto html = "<img src=\"data://test.png\">";
|
||||
document.setHtml(html);
|
||||
|
||||
QCOMPARE(document.pageCount(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QTextDocumentLayout)
|
||||
#include "tst_qtextdocumentlayout.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user