Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 173 additions & 30 deletions reader/uiframe/DocSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ extern "C" {
}
DWIDGET_USE_NAMESPACE

namespace {
constexpr double kXpsLogicalDpi = 96.0;
constexpr double kPointLogicalDpi = 72.0; //PDF/DOCX/DjVu 等页面逻辑尺寸为印刷点(1/72 英寸)
constexpr int kFallbackPrintDpi = 300;
constexpr int kMaxPrintPixelsPerSide = 10000;
//DTK 异步打印会一次性持有整个页范围的页面位图,大文档按页数分级限制渲染 DPI,避免内存耗尽
constexpr int kHugePrintDocPageCount = 200;
constexpr int kLargePrintDocPageCount = 50;
constexpr int kHugePrintDocDpiCeiling = 150;
constexpr int kLargePrintDocDpiCeiling = 200;
}
QReadWriteLock DocSheet::g_lock;
QStringList DocSheet::g_uuidList;
QList<DocSheet *> DocSheet::g_sheetList;
Expand Down Expand Up @@ -440,7 +451,77 @@ void DocSheet::setScaleFactor(qreal scaleFactor)
QImage DocSheet::getImage(int index, int width, int height, const QRect &slice)
{
return m_renderer->getImage(index, width, height, slice);
;
}

QSize DocSheet::calculatePrintTargetSize(int pageIndex, const QPrinter &printer, const QRectF &pageRect) const
{
// XPS 页面逻辑尺寸按 96 DPI 计,PDF/DOCX/DjVu 等按印刷点(72 DPI)计
double logicalDpi = kPointLogicalDpi;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · DjVu print resolution is miscalculated

When a DjVu page’s native resolution is not 72 DPI, djVu page dimensions are stored in pixels at each page’s m_resolution, but calculatePrintTargetSize treats them as 72-DPI points. The rendered bitmap therefore has the wrong pixel dimensions for the printer, so DjVu pages print at an unintended resolution and can be blurry or unnecessarily large.

Use each DjVu page’s stored DPI when converting its pixel dimensions to the requested print bitmap size.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 459:

**DjVu print resolution is miscalculated**

When a DjVu page’s native resolution is not 72 DPI, djVu page dimensions are stored in pixels at each page’s `m_resolution`, but `calculatePrintTargetSize` treats them as 72-DPI points. The rendered bitmap therefore has the wrong pixel dimensions for the printer, so DjVu pages print at an unintended resolution and can be blurry or unnecessarily large.

Use each DjVu page’s stored DPI when converting its pixel dimensions to the requested print bitmap size.

#ifdef XPS_SUPPORT_ENABLED
if (m_fileType == Dr::XPS) {
logicalDpi = kXpsLogicalDpi;
}
#endif

const QSizeF logicalSize = m_renderer ? m_renderer->getPageSize(pageIndex) : QSizeF();
if (logicalSize.isEmpty() || logicalSize.width() <= 0.0 || logicalSize.height() <= 0.0) {
qWarning() << "Unable to determine logical page size for high DPI print. index:" << pageIndex;
return QSize();
}

int printerDpi = printer.resolution();
if (printerDpi <= 0) {
qWarning() << "Printer resolution is invalid, fallback to default DPI:" << kFallbackPrintDpi;
printerDpi = kFallbackPrintDpi;
} else if (printerDpi < kFallbackPrintDpi) {
// 打印预览的 DPrinter 为 ScreenResolution(72DPI),按其取图会导致打印模糊
qWarning() << "Printer resolution" << printerDpi << "too low, raise to" << kFallbackPrintDpi;
printerDpi = kFallbackPrintDpi;
}

//大文档限制渲染 DPI 上限,防止打印时整册高分辨率位图同时驻留内存
const int totalPages = m_renderer ? m_renderer->getPageCount() : 0;
int dpiCeiling = kFallbackPrintDpi;
if (totalPages > kHugePrintDocPageCount) {
dpiCeiling = kHugePrintDocDpiCeiling;
} else if (totalPages > kLargePrintDocPageCount) {
Comment on lines +485 to +487

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Large print jobs exhaust memory

When a sufficiently large document or page range is retained by the asynchronous preview path, the per-page DPI tiers do not bound aggregate bitmap memory, and N-up pages are sized against the full sheet rather than their smaller slots. Retaining the requested range therefore exhausts memory.

Bound total retained bitmap memory and size rendered pages to their actual print slots.

Also at reader/uiframe/DocSheet.cpp:504-508.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at lines 485-487:

**Large print jobs exhaust memory**

When a sufficiently large document or page range is retained by the asynchronous preview path, the per-page DPI tiers do not bound aggregate bitmap memory, and N-up pages are sized against the full sheet rather than their smaller slots. Retaining the requested range therefore exhausts memory.

Bound total retained bitmap memory and size rendered pages to their actual print slots.

Also at `reader/uiframe/DocSheet.cpp:504-508`.

dpiCeiling = kLargePrintDocDpiCeiling;
}
if (printerDpi > dpiCeiling) {
qWarning() << "Total pages" << totalPages << "exceeds tier, limit print DPI from" << printerDpi << "to" << dpiCeiling;
printerDpi = dpiCeiling;
}

double widthPixels = logicalSize.width() / logicalDpi * static_cast<double>(printerDpi);
double heightPixels = logicalSize.height() / logicalDpi * static_cast<double>(printerDpi);

if (widthPixels <= 0.0 || heightPixels <= 0.0) {
qWarning() << "Calculated print size is invalid:" << widthPixels << heightPixels;
return QSize();
}

if (pageRect.width() > 0.0 && pageRect.height() > 0.0) {
const double scale = qMin(pageRect.width() / widthPixels, pageRect.height() / heightPixels);
if (scale < 1.0) {
widthPixels *= scale;
heightPixels *= scale;
}
Comment on lines +504 to +508

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Preview pages lose print sharpness

When the preview printer reports 72 DPI and pageRect is measured at that device resolution, calculatePrintTargetSize raises the target to 300 DPI, but its scale calculation compares it with the smaller, unconverted pageRect and shrinks the bitmap back to screen resolution. getImageForPrint therefore supplies a low-resolution image and preview-driven output loses print detail.

Convert pageRect to the target DPI coordinate system before fitting the bitmap, or otherwise avoid scaling the 300-DPI target back to screen resolution.

Also at reader/uiframe/DocSheet.cpp:477-480.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at lines 504-508:

**Preview pages lose print sharpness**

When the preview printer reports 72 DPI and `pageRect` is measured at that device resolution, `calculatePrintTargetSize` raises the target to 300 DPI, but its scale calculation compares it with the smaller, unconverted `pageRect` and shrinks the bitmap back to screen resolution. `getImageForPrint` therefore supplies a low-resolution image and preview-driven output loses print detail.

Convert `pageRect` to the target DPI coordinate system before fitting the bitmap, or otherwise avoid scaling the 300-DPI target back to screen resolution.

Also at `reader/uiframe/DocSheet.cpp:477-480`.

}

const double maxDimension = qMax(widthPixels, heightPixels);
if (maxDimension > kMaxPrintPixelsPerSide) {
const double clampScale = static_cast<double>(kMaxPrintPixelsPerSide) / maxDimension;
widthPixels *= clampScale;
heightPixels *= clampScale;
qWarning() << "Requested print resolution too large, clamping to" << kMaxPrintPixelsPerSide << "pixels. index:" << pageIndex;
}

const int roundedWidth = qMax(1, qRound(widthPixels));
const int roundedHeight = qMax(1, qRound(heightPixels));

qDebug() << "Calculated print target size for page" << pageIndex << ":" << roundedWidth << "x" << roundedHeight << "@" << printerDpi << "DPI";

return QSize(roundedWidth, roundedHeight);
}

bool DocSheet::fileChanged()
Expand Down Expand Up @@ -757,23 +838,69 @@ void DocSheet::onPrintRequested(DPrinter *printer, const QVector<int> &pageRange

painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);

auto targetRectForSize = [&pageRect](const QSize &sourceSize) -> QRect {
if (!sourceSize.isValid() || sourceSize.isEmpty()) {
return QRect();
}

qreal targetWidth = pageRect.width();
qreal targetHeight = targetWidth * static_cast<qreal>(sourceSize.height()) / static_cast<qreal>(sourceSize.width());

if (targetHeight > pageRect.height()) {
targetHeight = pageRect.height();
targetWidth = targetHeight * static_cast<qreal>(sourceSize.width()) / static_cast<qreal>(sourceSize.height());
}

const int left = qRound((pageRect.width() - targetWidth) / 2.0);
const int top = qRound((pageRect.height() - targetHeight) / 2.0);
return QRect(left,
top,
qMax(1, qRound(targetWidth)),
qMax(1, qRound(targetHeight)));
};

for (int i = 0; i < pageRange.count(); ++i) {
if (pageRange[i] > pageCount() || pageRange[i] > m_browser->pages().count())
continue;

const QRectF boundingrect = m_browser->pages().at(pageRange[i] - 1)->boundingRect(); //文档页缩放后的原区域不受旋转影响
qreal printWidth = pageRect.width(); //适合打印的图片宽度
qreal printHeight = printWidth * boundingrect.height() / boundingrect.width(); //适合打印的图片高度
if (printHeight > pageRect.height()) {
printHeight = pageRect.height();
printWidth = printHeight * boundingrect.width() / boundingrect.height();
QImage image;
QRect targetRect;

//优先按打印机 DPI 渲染高清晰度位图(并列打印等设置走 paintRequested 路径,需保证打印清晰度)
const int zeroBasedIndex = pageRange[i] - 1;
const QSize requestedSize = calculatePrintTargetSize(zeroBasedIndex, *printer, pageRect);
if (!requestedSize.isValid()) {
qWarning() << "Falling back to view-based print size for page" << zeroBasedIndex;
} else {
image = loading.getImageForPrint(this, zeroBasedIndex, requestedSize);
if (!image.isNull())
targetRect = targetRectForSize(image.size());
}

if (image.isNull()) {
const QRectF boundingrect = m_browser->pages().at(pageRange[i] - 1)->boundingRect(); //文档页缩放后的原区域不受旋转影响
qreal printWidth = pageRect.width(); //适合打印的图片宽度
qreal printHeight = printWidth * boundingrect.height() / boundingrect.width(); //适合打印的图片高度
if (printHeight > pageRect.height()) {
printHeight = pageRect.height();
printWidth = printHeight * boundingrect.width() / boundingrect.height();
}
image = loading.getImage(this, pageRange[i] - 1, static_cast<int>(printWidth), static_cast<int>(printHeight));
targetRect = QRect((static_cast<int>(pageRect.width()) - image.width()) / 2,
(static_cast<int>(pageRect.height()) - image.height()) / 2,
image.width(), image.height());
}

if (image.isNull()) {
qWarning() << "Failed to render image for printing page" << pageRange[i];
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Failed pages shift printed output

When a page cannot be rendered by either path and later pages remain in the requested range, onPrintRequested continues before calling printer->newPage(), so the next rendered page is drawn on the failed page’s sheet and the printed sequence shifts or overlaps.

Advance the printer page when rendering fails, before continuing.

Also at reader/uiframe/DocSheet.cpp:897.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 896:

**Failed pages shift printed output**

When a page cannot be rendered by either path and later pages remain in the requested range, `onPrintRequested` continues before calling `printer->newPage()`, so the next rendered page is drawn on the failed page’s sheet and the printed sequence shifts or overlaps.

Advance the printer page when rendering fails, before continuing.

Also at `reader/uiframe/DocSheet.cpp:897`.

}

if (!targetRect.isValid()) {
targetRect = targetRectForSize(image.size());
}

QImage image = loading.getImage(this, pageRange[i] - 1, static_cast<int>(printWidth), static_cast<int>(printHeight));
painter.drawImage(QRect((static_cast<int>(pageRect.width()) - image.width()) / 2,
(static_cast<int>(pageRect.height()) - image.height()) / 2,
image.width(), image.height()),
image);
painter.drawImage(targetRect, image, image.rect());

if (i != pageRange.count() - 1)
printer->newPage();
Expand Down Expand Up @@ -1309,24 +1436,11 @@ void DocSheet::onPopPrintDialog()
//文件打印
preview->setAsynPreview(pageCount());
preview->setDocName(QFileInfo(filePath()).fileName());
if (Dr::DOCX == fileType()) { //旧版本和最新版本使用新接口,PDF文件直接传,解决打印模糊问题
preview->setPrintFromPath(openedFilePath());
}

//pdf若是Linearized类型的,需要另存为Normal类型,然后打印
if (Dr::PDF == fileType()) {
QString pdfPath = filePath();
// 使用 pdftocairo 转换为嵌入矢量pdf,解决打印乱码和模糊问题
QString tmpPdfPath = convertedFileDir() + "/pdftocairoPrint.pdf";
QProcess process;
process.start("pdftocairo", QStringList() << "-pdf" << pdfPath << tmpPdfPath);
process.waitForFinished();
if (process.exitCode() != 0) {
qWarning() << "pdftocairo failed:" << process.readAllStandardError();
return;
}
preview->setPrintFromPath(tmpPdfPath);
}
// 不使用 setPrintFromPath:该接口把打印交给打印插件按文件路径输出,DTK 会
// 因此禁用并列打印(N-up)以及方向、缩放、边距、水印等设置项。统一走
// paintRequested 由本应用渲染页面,并列打印等设置方可生效;原 PDF 经
// pdftocairo 转换后按路径打印是为解决模糊/乱码,现由 onPrintRequested
// 的高 DPI 位图渲染保证清晰度。
connect(preview, QOverload<DPrinter *, const QVector<int> &>::of(&DPrintPreviewDialog::paintRequested), this, QOverload<DPrinter *, const QVector<int> &>::of(&DocSheet::onPrintRequested));
#else
//图片打印
Expand Down Expand Up @@ -1394,6 +1508,35 @@ QImage DocSheet::LoadingWidget::getImage(DocSheet *doc, int index, int width, in
return image;
}

QImage DocSheet::LoadingWidget::getImageForPrint(DocSheet *doc, int index, const QSize &targetSize)
{
qDebug() << "getImageForPrint" << targetSize;
if (!doc) {
qWarning() << "DocSheet pointer is null when requesting print image";
return QImage();
}
if (!targetSize.isValid() || targetSize.width() <= 0 || targetSize.height() <= 0) {
qWarning() << "Invalid target size for print image" << targetSize;
return QImage();
}

QImage image;
QEventLoop loop;
const QSize requestSize = targetSize;
QThread *thread = QThread::create([=, &image]() {
image = doc->getImage(index, requestSize.width(), requestSize.height());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Closing a tab crashes printing

When the user closes the document tab while getImageForPrint is waiting for its worker, the worker dereferences doc in getImage while the nested event loop is running; closing the tab can destroy the DocSheet and its renderer first, causing a use-after-free and a crash.

Keep the sheet and renderer alive until the worker finishes, or cancel and join the worker before destroying them.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 1527:

**Closing a tab crashes printing**

When the user closes the document tab while `getImageForPrint` is waiting for its worker, the worker dereferences `doc` in `getImage` while the nested event loop is running; closing the tab can destroy the `DocSheet` and its renderer first, causing a use-after-free and a crash.

Keep the sheet and renderer alive until the worker finishes, or cancel and join the worker before destroying them.

});
QObject::connect(thread, &QThread::finished, &loop, &QEventLoop::quit);
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);

thread->start();

loop.exec(QEventLoop::ExcludeSocketNotifiers);

qDebug() << "getImageForPrint end";
return image;
}

void DocSheet::LoadingWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
Expand Down
6 changes: 6 additions & 0 deletions reader/uiframe/DocSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ private slots:
void onExtractPassword(const QString &password);

private:
QSize calculatePrintTargetSize(int pageIndex, const QPrinter &printer, const QRectF &pageRect) const;
/**
* @brief setAlive
* 设置当前sheet是否存活
Expand Down Expand Up @@ -878,6 +879,11 @@ private slots:
*/
QImage getImage(DocSheet *doc, int index, int width, int height);

/**
* @brief getImageForPrint 打印专用图像获取接口
*/
QImage getImageForPrint(DocSheet *doc, int index, const QSize &targetSize);

protected:
void paintEvent(QPaintEvent */*event*/) override;

Expand Down
Loading