From 4c2587b6bcb4054e21baa7d5fa5f1b280ccbe80b Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Wed, 16 Dec 2015 17:20:49 +0100 Subject: 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 --- src/gui/text/qtextdocumentlayout.cpp | 52 +++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'src/gui/text/qtextdocumentlayout.cpp') 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(rect.top() / pageHeight) : 0), + bottomPage(pageHeight > 0 ? static_cast((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(rect.top() / pageHeight) : 0; - const int bottomPage = pageHeight > 0 ? static_cast((rect.bottom() + border) / pageHeight) : 0; + BorderPaginator paginator(document, rect, topMargin, bottomMargin, border); #ifndef QT_NO_CSSPARSER QCss::BorderStyle cssStyle = static_cast(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); -- cgit v1.2.3