diff --git a/src/printing/printpainter.cpp b/src/printing/printpainter.cpp --- a/src/printing/printpainter.cpp +++ b/src/printing/printpainter.cpp @@ -645,49 +645,62 @@ rangeptr->setLine(line); m_renderer->layoutLine(rangeptr, (int)pl.maxWidth, false); + // If the line is too long (too many 'viewlines') to fit the remaining vertical space, + // clip and adjust the painter position as necessary + int _lines = rangeptr->viewLineCount(); // number of "sublines" to paint. + int proceedLines = _lines; + int alreadyPrinted = 0; + + QRegion region; + + if (remainder) { + alreadyPrinted = _lines - remainder; + proceedLines = qMin((pl.maxHeight - y) / m_fontHeight, remainder); + + painter.translate(0, -(_lines - int(remainder)) * m_fontHeight + 1); + region = QRegion(QRect(0, (_lines - int(remainder)) * m_fontHeight + 1, pl.maxWidth, proceedLines * m_fontHeight)); //### drop the crosspatch in printerfriendly mode??? + remainder -= proceedLines; + } else if (y + m_fontHeight * _lines > pl.maxHeight) { + remainder = _lines - ((pl.maxHeight - y) / m_fontHeight); + region = QRegion(QRect(0, 0, pl.maxWidth, (_lines - int(remainder)) * m_fontHeight + 1)); //### drop the crosspatch in printerfriendly mode??? + } else if (!pl.selectionOnly) { + painter.setClipRegion(QRegion()); + painter.setClipping(false); + } else if (pl.selectionOnly && !(m_view && m_view->blockSelection())) { + region = QRegion(0, 0, pl.maxWidth, rangeptr->viewLineCount() * m_fontHeight); + } + // selectionOnly: clip non-selection parts and adjust painter position if needed int _xadjust = 0; if (pl.selectionOnly) { if (m_view && m_view->blockSelection()) { int _x = m_renderer->cursorToX(rangeptr->viewLine(0), pl.selectionRange.start()); int _x1 = m_renderer->cursorToX(rangeptr->viewLine(rangeptr->viewLineCount() - 1), pl.selectionRange.end()); _xadjust = _x; painter.translate(-_xadjust, 0); - painter.setClipRegion(QRegion(_x, 0, _x1 - _x, rangeptr->viewLineCount() * m_fontHeight)); + region = QRegion(_x, 0, _x1 - _x, rangeptr->viewLineCount() * m_fontHeight); } else if (line == pl.firstline || line == pl.lastline) { - QRegion region(0, 0, pl.maxWidth, rangeptr->viewLineCount() * m_fontHeight); - if (line == pl.firstline) { - region = region.subtracted(QRegion(0, 0, m_renderer->cursorToX(rangeptr->viewLine(0), pl.selectionRange.start()), m_fontHeight)); + for (int i = alreadyPrinted; i < _lines - remainder; ++i) { + int _x = m_renderer->cursorToX(rangeptr->viewLine(i), pl.selectionRange.start()); + if (_x > 0) { + region = region.subtracted(QRegion(0, i * m_fontHeight, _x, m_fontHeight)); + } + } } if (line == pl.lastline) { - int _x = m_renderer->cursorToX(rangeptr->viewLine(rangeptr->viewLineCount() - 1), pl.selectionRange.end()); - region = region.subtracted(QRegion(_x, 0, pl.maxWidth - _x, m_fontHeight)); + for (int i = alreadyPrinted; i < _lines - remainder; ++i) { + int _x = m_renderer->cursorToX(rangeptr->viewLine(i), pl.selectionRange.end()); + region = region.subtracted(QRegion(_x, i * m_fontHeight, pl.maxWidth - _x, m_fontHeight)); + } } - - painter.setClipRegion(region); } + painter.setClipRegion(region); } - - // If the line is too long (too many 'viewlines') to fit the remaining vertical space, - // clip and adjust the painter position as necessary - int _lines = rangeptr->viewLineCount(); // number of "sublines" to paint. - - int proceedLines = _lines; - if (remainder) { - proceedLines = qMin((pl.maxHeight - y) / m_fontHeight, remainder); - - painter.translate(0, -(_lines - int(remainder)) * m_fontHeight + 1); - painter.setClipRect(0, (_lines - int(remainder)) * m_fontHeight + 1, pl.maxWidth, proceedLines * m_fontHeight); //### drop the crosspatch in printerfriendly mode??? - remainder -= proceedLines; - } else if (y + m_fontHeight * _lines > pl.maxHeight) { - remainder = _lines - ((pl.maxHeight - y) / m_fontHeight); - painter.setClipRect(0, 0, pl.maxWidth, (_lines - int(remainder)) * m_fontHeight + 1); //### drop the crosspatch in printerfriendly mode??? - } else if (!pl.selectionOnly) { - painter.setClipRegion(QRegion()); - painter.setClipping(false); + else if (!region.isNull()) { + painter.setClipRegion(region); } m_renderer->paintTextLine(painter, rangeptr, 0, (int)pl.maxWidth);