diff options
author | Nils Jeisecke <jeisecke@saltation.de> | 2015-12-16 17:20:49 +0100 |
---|---|---|
committer | Nils Jeisecke <nils.jeisecke@saltation.com> | 2019-08-08 05:52:11 +0200 |
commit | 4c2587b6bcb4054e21baa7d5fa5f1b280ccbe80b (patch) | |
tree | 9689c9090a40d7565e52c30f394b2f354d7b8b34 /src/gui/text/qtextdocumentlayout.cpp | |
parent | 81554aca81ec4e161ff9fa236e3d76fa950fec97 (diff) | |
download | qtbase-4c2587b6bcb4054e21baa7d5fa5f1b280ccbe80b.tar.gz |
QTextDocumentLayout: Refactor pagination logic for borders
The new helper class BorderPaginator encapsulates the existing pagination
logic for drawing correctly clipped borders on all pages a cell appears
on.
This will allow reuse of that logic for drawing CSS-style borders.
Change-Id: I47ed4a8802513aef30d97f14591c7d4716bfdbb8
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/gui/text/qtextdocumentlayout.cpp')
-rw-r--r-- | src/gui/text/qtextdocumentlayout.cpp | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp index 783a7b083d..6ca1e408ed 100644 --- a/src/gui/text/qtextdocumentlayout.cpp +++ b/src/gui/text/qtextdocumentlayout.cpp @@ -798,12 +798,45 @@ QFixed QTextDocumentLayoutPrivate::blockIndent(const QTextBlockFormat &blockForm return QFixed::fromReal(indent * scale * document->indentWidth()); } +struct BorderPaginator +{ + BorderPaginator(QTextDocument *document, const QRectF &rect, qreal topMarginAfterPageBreak, qreal bottomMargin, qreal border) : + pageHeight(document->pageSize().height()), + topPage(pageHeight > 0 ? static_cast<int>(rect.top() / pageHeight) : 0), + bottomPage(pageHeight > 0 ? static_cast<int>((rect.bottom() + border) / pageHeight) : 0), + rect(rect), + topMarginAfterPageBreak(topMarginAfterPageBreak), + bottomMargin(bottomMargin), border(border) + {} + + QRectF clipRect(int page) const + { + QRectF clipped = rect.toRect(); + + if (topPage != bottomPage) { + clipped.setTop(qMax(clipped.top(), page * pageHeight + topMarginAfterPageBreak - border)); + clipped.setBottom(qMin(clipped.bottom(), (page + 1) * pageHeight - bottomMargin)); + + if (clipped.bottom() <= clipped.top()) + return QRectF(); + } + + return clipped; + } + + qreal pageHeight; + int topPage; + int bottomPage; + QRectF rect; + qreal topMarginAfterPageBreak; + qreal bottomMargin; + qreal border; +}; + void QTextDocumentLayoutPrivate::drawBorder(QPainter *painter, const QRectF &rect, qreal topMargin, qreal bottomMargin, qreal border, const QBrush &brush, QTextFrameFormat::BorderStyle style) const { - const qreal pageHeight = document->pageSize().height(); - const int topPage = pageHeight > 0 ? static_cast<int>(rect.top() / pageHeight) : 0; - const int bottomPage = pageHeight > 0 ? static_cast<int>((rect.bottom() + border) / pageHeight) : 0; + BorderPaginator paginator(document, rect, topMargin, bottomMargin, border); #ifndef QT_NO_CSSPARSER QCss::BorderStyle cssStyle = static_cast<QCss::BorderStyle>(style + 1); @@ -814,16 +847,11 @@ void QTextDocumentLayoutPrivate::drawBorder(QPainter *painter, const QRectF &rec bool turn_off_antialiasing = !(painter->renderHints() & QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing); - for (int i = topPage; i <= bottomPage; ++i) { - QRectF clipped = rect.toRect(); - - if (topPage != bottomPage) { - clipped.setTop(qMax(clipped.top(), i * pageHeight + topMargin - border)); - clipped.setBottom(qMin(clipped.bottom(), (i + 1) * pageHeight - bottomMargin)); + for (int i = paginator.topPage; i <= paginator.bottomPage; ++i) { + QRectF clipped = paginator.clipRect(i); + if (!clipped.isValid()) + continue; - if (clipped.bottom() <= clipped.top()) - continue; - } #ifndef QT_NO_CSSPARSER qDrawEdge(painter, clipped.left(), clipped.top(), clipped.left() + border, clipped.bottom() + border, 0, 0, QCss::LeftEdge, cssStyle, brush); qDrawEdge(painter, clipped.left() + border, clipped.top(), clipped.right() + border, clipped.top() + border, 0, 0, QCss::TopEdge, cssStyle, brush); |