Skip to content

feat: Improve XPS print quality by using printer DPI-aware rendering#205

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
LiHua000:xpsSupport
Nov 13, 2025
Merged

feat: Improve XPS print quality by using printer DPI-aware rendering#205
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
LiHua000:xpsSupport

Conversation

@LiHua000
Copy link
Contributor

@deepin-ci-robot
Copy link

deepin pr auto review

我来分析一下这段代码的改进意见:

  1. 代码逻辑和结构改进:
  • 在 DocSheet.cpp 中,targetRectForSize lambda 函数在两个地方重复定义,建议将其提取为类的私有成员函数,避免代码重复。
  • calculatePrintTargetSize 函数中的多个 return 语句可以考虑使用早期返回模式,但需要确保所有情况都有适当的错误处理。
  1. 性能优化:
  • 在 onPrintRequested 函数中,对于 XPS 文档的打印处理,可以考虑缓存 calculatePrintTargetSize 的计算结果,避免重复计算。
  • getImageForPrint 函数中的 QEventLoop 可能会阻塞 UI 线程,建议考虑使用异步方式处理图像生成。
  1. 安全性改进:
  • 在 calculatePrintTargetSize 函数中,虽然已经对 widthPixels 和 heightPixels 进行了有效性检查,但建议添加对打印机 DPI 的上限检查,防止极端值导致的问题。
  • 在处理图像尺寸时,建议添加对内存使用的预估和限制,防止大尺寸图像导致内存耗尽。
  1. 代码质量改进:
  • 建议为 kXpsLogicalDpi、kFallbackPrintDpi 等常量添加更详细的注释,说明这些值的来源和用途。
  • 在 getImageForPrint 函数中,thread 的创建和销毁逻辑可以考虑使用更现代的 Qt 并发框架,如 QThreadPool。
  1. 错误处理改进:
  • 在 onPrintRequested 函数中,当图像渲染失败时,除了记录警告日志外,可以考虑提供用户反馈。
  • 建议在 calculatePrintTargetSize 函数中添加对 printer 参数的有效性检查。
  1. 具体建议的代码改进:
class DocSheet {
private:
    // 添加为私有成员函数
    QRect calculateTargetRect(const QSize& sourceSize, const QRectF& pageRect) const {
        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)));
    }
};

// 在 calculatePrintTargetSize 中添加 DPI 上限检查
QSize DocSheet::calculatePrintTargetSize(int pageIndex, const QPrinter &printer, const QRectF &pageRect) const {
    if (!printer.isValid()) {
        qCWarning(appLog) << "Invalid printer object";
        return QSize();
    }
    
    // ... 现有代码 ...
    
    // 添加 DPI 上限检查
    constexpr int kMaxPrintDpi = 1200;
    if (printerDpi > kMaxPrintDpi) {
        qCWarning(appLog) << "Printer DPI too high, clamping to" << kMaxPrintDpi;
        printerDpi = kMaxPrintDpi;
    }
    
    // ... 其余代码 ...
}

这些改进将提高代码的可维护性、性能和安全性,同时保持代码的功能完整性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LiHua000, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@LiHua000
Copy link
Contributor Author

/forcemerge

@deepin-bot deepin-bot bot merged commit ab1c3d6 into linuxdeepin:master Nov 13, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants