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
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Build-Depends:
cmake,
debhelper (>= 11),
pkg-config,
libdeepin-pdfium-dev (>= 2.0.0),
libdeepin-pdfium-dev (>= 1.5.8),
qt6-5compat-dev [!mipsel !mips64el] | libqt5x11extras5-dev,
qt6-base-dev [!mipsel !mips64el] | qtbase5-dev,
qt6-tools-dev-tools [!mipsel !mips64el] | qttools5-dev-tools,
Expand Down
54 changes: 47 additions & 7 deletions linglong.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ sources:
- kind: git
url: https://github.com/linuxdeepin/rofd.git
commit: af6d562a649df85487e91b88375894a6049ad4de
- kind: git
url: https://github.com/linuxdeepin/deepin-pdfium.git
commit: d9e73d2c1440e4a47fc0f76e1b97dc671f9a8adf

command:
- deepin-reader
Expand Down Expand Up @@ -55,6 +58,35 @@ build: |
ln -sf librofd_ffi.so.0 $PREFIX/lib/${TRIPLET}/librofd_ffi.so
install -m 0644 "$ROFD_SRC/crates/rofd-ffi/include/rofd.h" $PREFIX/include/rofd.h

# ================= 从源码构建 deepin-pdfium(PDF 引擎,随包分发) =================
# 不依赖发行版仓库的 libdeepin-pdfium 版本;安装到 $PREFIX 后,主工程
# pkg-config(PREFIX 优先)与运行时均使用包内置引擎。SONAME 仍为 .so.1
# (内嵌 pdfium STATIC 链接,导出的 FPDF C 接口供夜间模式图片对象探测使用)
DPDF_SRC=""
for d in /project/linglong/sources/deepin-pdfium /project/sources/deepin-pdfium /project/deepin-pdfium /source/deepin-pdfium; do
if [ -f "$d/CMakeLists.txt" ] && [ -f "$d/include/dpdfdoc.h" ]; then DPDF_SRC="$d"; break; fi
done
if [ -z "$DPDF_SRC" ]; then
DPDF_SRC=$(find /project -maxdepth 8 -type f -path "*/include/dpdfdoc.h" 2>/dev/null | head -1 | sed 's|/include/dpdfdoc.h$||')
fi
if [ ! -f "$DPDF_SRC/CMakeLists.txt" ]; then
echo "ERROR: 未找到 deepin-pdfium 源码,sources 拉取失败?检查构建机对 github.com 的访问权限" >&2
exit 1
fi
echo ">>> deepin-pdfium 源码: $DPDF_SRC (commit: $(git -C $DPDF_SRC rev-parse --short HEAD 2>/dev/null || echo unknown))"
# 增量构建会复用容器 /tmp,旧产物指纹可能失效,先清理;
# 子 shell 保持主工程 cwd 不变(主工程按相对路径 cd build && cmake ..)
rm -rf /tmp/dpdf-build
(
mkdir -p /tmp/dpdf-build && cd /tmp/dpdf-build
cmake "$DPDF_SRC" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DCMAKE_INSTALL_LIBDIR=lib/${TRIPLET}
make -j$(nproc)
make install
)

# ================= pandoc 数据目录 =================
# reader 以 --data-dir=${prefix}/share/pandoc/data 调用 pandoc;buildext.apt.depends 合并产物
# 时仅收集 bin/sbin/lib,share/ 会被丢弃,故在构建期从 /usr/share 复制随包分发
Expand Down Expand Up @@ -94,7 +126,9 @@ build: |
# libglib2.0-0、libjpeg62-turbo —— 不进 depends(depends 对 base 已有包是无效 no-op);
# - runtime 层提供 Qt/DTK 全套开发包(qt6-*-dev、libdtk6*-dev),经 /runtime 前缀探测,
# 不重复列入 build_depends;gcc/cmake/make 由 base 镜像自带,pkg-config 由 runtime 提供;
# - 旧列表中的 libspectre-dev/libicu-dev/libgtest-dev 工程内无对应探测,属冗余,剔除。
# - 旧列表中的 libspectre-dev/libgtest-dev 工程内无对应探测,属冗余,剔除
# (libicu-dev 已随 deepin-pdfium 源码编译恢复,见 build_depends;
# PDF 引擎改为源码自编译随包分发后,depends 不再依赖 apt 的 libdeepin-pdfium)。
buildext:
apt:
build_depends:
Expand All @@ -109,14 +143,20 @@ buildext:
- libgxps-dev # reader: XPS(libgxps cairo glib gobject freetype2)
- libcairo2-dev # reader: XPS/OFD 的 cairo 探测
- libglib2.0-dev # reader: glib-2.0/gobject-2.0 探测
- libfreetype6-dev # reader: XPS 的 freetype2 探测
- libjpeg-dev # reader: pkg_check_modules(libjpeg)
- libdeepin-pdfium-dev # reader/batch-print: pkg_check_modules(deepin-pdfium),系统库不再内嵌
- libfreetype6-dev # reader: XPS 的 freetype2 探测;deepin-pdfium 编译依赖
- libjpeg-dev # reader: pkg_check_modules(libjpeg);deepin-pdfium 编译依赖
- libdeepin-pdfium-dev # 兜底:自建引擎缺失时的降级探测(正常情况下 PREFIX 内置版本优先)
# deepin-pdfium 源码编译(见 build 段):内嵌 pdfium 的系统依赖
- libicu-dev # pdfium 源码直接包含 <unicode/uchar.h>
- libpng-dev # pdfium PNG 图像解码
- zlib1g-dev # pdfium FlateDecode
- liblcms2-dev # pdfium ICC 色彩管理
- libopenjp2-7-dev # pdfium JPEG2000 解码
- libchardet-dev # 文本编码嗅探
depends:
# base 与 runtime 均不提供、必须随包分发的运行时依赖
- pandoc # 文档转换子进程(libyaml-0-2/liblua5.3-0 等闭包由 apt 自动解析)
- libdjvulibre21 # reader DT_NEEDED libdjvulibre.so.21
- libgxps2 # reader DT_NEEDED libgxps.so.2
- libdeepin-pdfium # reader/batchprint DT_NEEDED libdeepin-pdfium.so.1(PDF 引擎,
# base/runtime 包清单均不含,其 libchardet 等闭包由 apt 自动解析)
- libchardet1 # libdeepin-pdfium 传递依赖 DT_NEEDED libchardet.so.1
- libchardet1 # 内置 libdeepin-pdfium DT_NEEDED libchardet.so.1
# (PDF 引擎已由源码自编译随包分发,不再依赖 apt 版)
12 changes: 6 additions & 6 deletions reader/document/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,6 @@ class Page: public QObject
virtual QSizeF sizeF() const = 0;
virtual int resolution() const { return 72; }
virtual QImage render(int width, int height, const QRect &slice = QRect()) const = 0;
/**
* @brief 图片对象包围盒(与 render(width,height) 整页输出像素对齐)
* 默认返回空(无对象信息的文档格式,夜间模式回退整页处理)
*/
virtual QVector<QRectF> imageObjectRects(int width, int height) const
{ Q_UNUSED(width) Q_UNUSED(height) return QVector<QRectF>(); }
// LCOV_EXCL_START
virtual Link getLinkAtPoint(const QPointF &) { return Link(); }
virtual QString text(const QRectF &rect) const = 0;
Expand Down Expand Up @@ -242,6 +236,12 @@ class Document: public QObject
* 用于在文件被移动/重命名后识别同一文档,返回空串表示无可用 ID
*/
virtual QString fileIdentifier() const { return QString(); }
/**
* @brief 图片对象包围盒(与 image(index,width,height) 整页输出像素对齐)
* 默认返回空(无对象信息的文档格式,夜间模式回退整页处理)
*/
virtual QVector<QRectF> imageObjectRects(int index, int width, int height) const
{ Q_UNUSED(index) Q_UNUSED(width) Q_UNUSED(height) return QVector<QRectF>(); }
};

class DocumentFactory
Expand Down
173 changes: 163 additions & 10 deletions reader/document/PDFModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,77 @@
#include "dpdfpage.h"
#include "dpdfdoc.h"
#include "Application.h"
#include "ddlog.h"

Check warning on line 10 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ddlog.h" not found.

#include <QDebug>

Check warning on line 12 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 13 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegularExpression>

Check warning on line 14 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegularExpression> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QScreen>

Check warning on line 15 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QScreen> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QThread>

Check warning on line 16 in reader/document/PDFModel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QThread> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <functional>

#define LOCK_DOCUMENT QMutexLocker docMutexLocker(m_docMutex);

// libdeepin-pdfium 导出的 PDFium C 接口(官方头文件未声明这些 C 符号,但已在
// libdeepin-pdfium.so 1.5.8 上核实导出,PDFium C API 长期保持稳定)。
// 仅用于图片对象包围盒探测:夜间模式对象蒙版需要页面上 IMAGE 对象的位置信息,
// 而 Qt 级封装在 1.5.x 上未提供该能力。
extern "C" {
typedef void *FPDF_DOCUMENT;
typedef void *FPDF_PAGE;
typedef void *FPDF_PAGEOBJECT;

FPDF_DOCUMENT FPDF_LoadDocument(const char *file_path, const char *password);
FPDF_PAGE FPDF_LoadPage(FPDF_DOCUMENT document, int page_index);
void FPDF_ClosePage(FPDF_PAGE page);
void FPDF_CloseDocument(FPDF_DOCUMENT document);
int FPDFPage_CountObjects(FPDF_PAGE page);
FPDF_PAGEOBJECT FPDFPage_GetObject(FPDF_PAGE page, int index);
int FPDFPageObj_GetType(FPDF_PAGEOBJECT page_object); // FPDF_PAGEOBJ_IMAGE == 3
int FPDFPageObj_GetBounds(FPDF_PAGEOBJECT page_object, float *left, float *bottom, float *right, float *top);
int FPDFPage_GetRotation(FPDF_PAGE page);
float FPDF_GetPageWidthF(FPDF_PAGE page);
float FPDF_GetPageHeightF(FPDF_PAGE page);
}

namespace {

// 系统 libdeepin-pdfium (1.5.x) 未提供 /ID 接口,这里直接解析 PDF 文件中的文档
// 标识符:trailer 字典或交叉引用流字典的 /ID[值1 值2],按规范值1 为永久 ID
// (增量保存、移动/重命名均不变)。两个值各自支持十六进制 <...> 与字面量
// (...) 两种语法。增量更新会把最新 trailer 追加到文件末尾,故扫描文件尾部
// 并取最后一次出现。解析失败(加密/损坏/非常规布局)返回空串,调用方回退
// 到内容指纹匹配。
QString extractPdfFileIdentifier(const QString &filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
return QString();

file.seek(qMax<qint64>(0, file.size() - 8192));
const QString text = QString::fromLatin1(file.read(8192));

// /ID 数组:[值1 值2],值可为 <hex> 或 (literal);字面量内允许 \x 转义
static const QRegularExpression re(
QLatin1String("/ID\\s*\\[\\s*"
"(?:<([0-9a-fA-F]+)>|\\(((?:\\\\.|[^\\\\)])*)\\))"
"\\s*"
"(?:<[0-9a-fA-F]+>|\\((?:\\\\.|[^\\\\)])*\\))"
"\\s*\\]"));
QString id;
QRegularExpressionMatchIterator it = re.globalMatch(text);
while (it.hasNext()) { // 取最后一次出现(最新增量更新的 trailer)
const QRegularExpressionMatch m = it.next();
// 值1:十六进制时归一化小写;字面量时保持原始字节(同一文件解析结果稳定)
const QString hex = m.captured(1);
id = hex.isNull() ? m.captured(2) : hex.toLower();
}
return id;
}

} // namespace

namespace deepin_reader {
PDFAnnotation::PDFAnnotation(DPdfAnnot *dannotation) : Annotation(),
m_dannotation(dannotation)
Expand Down Expand Up @@ -110,13 +171,6 @@
return result;
}

QVector<QRectF> PDFPage::imageObjectRects(int width, int height) const
{
LOCK_DOCUMENT

return m_page->imageObjectRects(width, height);
}

Link PDFPage::getLinkAtPoint(const QPointF &pos)
{
// qCDebug(appLog) << "PDFPage::getLinkAtPoint() - Getting link at point:" << pos;
Expand Down Expand Up @@ -423,8 +477,9 @@
return nullptr;
}

PDFDocument::PDFDocument(DPdfDoc *document) :
PDFDocument::PDFDocument(DPdfDoc *document, const QString &password) :
m_document(document)
, m_password(password)
{
qCInfo(appLog) << "Creating PDF document handler with page count:" << (document ? document->pageCount() : 0);

Expand Down Expand Up @@ -452,12 +507,108 @@

m_document = nullptr;

if (m_probeDoc) {
FPDF_CloseDocument(m_probeDoc);
m_probeDoc = nullptr;
}

m_docMutex->unlock();

delete m_docMutex;
qCDebug(appLog) << "PDF document resources released";
}

QVector<QRectF> PDFDocument::imageObjectRects(int index, int width, int height) const
{
QVector<QRectF> rects;

if (!m_document || width <= 0 || height <= 0)
return rects;

QMutexLocker locker(m_docMutex);

// 探测句柄懒加载:独立于 m_document 的第二个 FPDF 文档实例(库未暴露内部
// 句柄),整个文档生命周期内复用;加载失败记录标记避免反复尝试
if (!m_probeDoc && !m_probeFailed) {
const QByteArray pwdBytes = m_password.toUtf8();
m_probeDoc = FPDF_LoadDocument(m_document->filePath().toUtf8().constData(),
pwdBytes.isEmpty() ? nullptr : pwdBytes.constData());
if (nullptr == m_probeDoc) {
m_probeFailed = true;
qCWarning(appLog) << "Probe document load failed, image object detection disabled";
}
}

if (nullptr == m_probeDoc)
return rects;

// 与老的内嵌实现一致:每次 FPDF_LoadPage 解析内容,取完立即关闭,
// 避免与渲染线程的页面句柄生命周期冲突
FPDF_PAGE probePage = FPDF_LoadPage(m_probeDoc, index);

if (nullptr == probePage)
return rects;

const int rotation = FPDFPage_GetRotation(probePage);
const float rotatedW = FPDF_GetPageWidthF(probePage);
const float rotatedH = FPDF_GetPageHeightF(probePage);
// FPDF_GetPageWidthF/HeightF 为应用旋转后的尺寸,transRect 需要
// 未旋转的页面点尺寸
const qreal width_pt = (rotation % 2 == 1) ? rotatedH : rotatedW;
const qreal height_pt = (rotation % 2 == 1) ? rotatedW : rotatedH;

// 目标像素尺寸对应的缩放(与 image(index,width,height) 整页渲染对齐):
// 视图坐标为页面点尺寸(transRect 输出),整页线性映射到目标像素,
// 即 rect × width/width_pt。注意不可再叠加 dpi 因子——老实现中
// transPointToPixel(×dpi/72) 与 sizeF(×dpi/72) 上下抵消,净效果
// 就是点尺寸直接线性缩放;若分母单独保留 dpi 会导致矩形缩小偏移
const qreal sx = qreal(width) / width_pt;
const qreal sy = qreal(height) / height_pt;

const int count = FPDFPage_CountObjects(probePage);

for (int i = 0; i < count; ++i) {
FPDF_PAGEOBJECT obj = FPDFPage_GetObject(probePage, i);

if (nullptr == obj || 3 != FPDFPageObj_GetType(obj)) // 3 = FPDF_PAGEOBJ_IMAGE
continue;

float left = 0, bottom = 0, right = 0, top = 0;

if (!FPDFPageObj_GetBounds(obj, &left, &bottom, &right, &top))
continue;

// 坐标链:PDF 用户空间(y 向上)→ 旋转后视图坐标(y 向下)→ 目标像素,
// 映射逻辑与老的内嵌实现逐行对齐(transRect(rotation, FS_RECTF))
QRectF viewPt;
switch (rotation) {
case 1:
viewPt = QRectF(qreal(bottom), qreal(left),
qreal(top) - qreal(bottom), qreal(right) - qreal(left));
break;
case 2:
viewPt = QRectF(width_pt - qreal(right), qreal(bottom),
qreal(right) - qreal(left), qreal(top) - qreal(bottom));
break;
case 3:
viewPt = QRectF(height_pt - qreal(top), width_pt - qreal(right),
qreal(top) - qreal(bottom), qreal(right) - qreal(left));
break;
default:
viewPt = QRectF(qreal(left), height_pt - qreal(top),
qreal(right) - qreal(left), qreal(top) - qreal(bottom));
break;
}

rects.append(QRectF(viewPt.x() * sx, viewPt.y() * sy,
viewPt.width() * sx, viewPt.height() * sy).normalized());
}

FPDF_ClosePage(probePage);

return rects;
}

int PDFDocument::pageCount() const
{
qCDebug(appLog) << "Retrieving PDF document page count";
Expand Down Expand Up @@ -507,7 +658,9 @@
{
if (!m_document)
return QString();
return m_document->fileIdentifier();
// 系统 libdeepin-pdfium (1.5.x) 未提供 /ID 接口,改为在 reader 侧直接解析
// PDF 文件中的文档标识符(见 extractPdfFileIdentifier)
return extractPdfFileIdentifier(m_document->filePath());
}

bool PDFDocument::saveAs(const QString &filePath) const
Expand Down Expand Up @@ -575,7 +728,7 @@
if (document->status() == DPdfDoc::SUCCESS) {
qCInfo(appLog) << "PDF document loaded successfully";
error = Document::NoError;
return new PDFDocument(document);
return new PDFDocument(document, password);
} else if (document->status() == DPdfDoc::PASSWORD_ERROR) {
qCDebug(appLog) << "PDFDocument::loadDocument() - Document status: PASSWORD_ERROR";
if (password.isEmpty()) {
Expand Down
18 changes: 15 additions & 3 deletions reader/document/PDFModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class PDFPage : public Page

QImage render(int width, int height, const QRect &slice = QRect()) const override;

QVector<QRectF> imageObjectRects(int width, int height) const override;

Link getLinkAtPoint(const QPointF &point) override;

bool hasWidgetAnnots() const override;
Expand Down Expand Up @@ -100,7 +98,7 @@ class PDFDocument : public Document
{
Q_DISABLE_COPY(PDFDocument)
public:
explicit PDFDocument(DPdfDoc *document);
explicit PDFDocument(DPdfDoc *document, const QString &password = QString());

virtual ~PDFDocument() override;

Expand All @@ -122,6 +120,14 @@ class PDFDocument : public Document

QString fileIdentifier() const override;

/**
* @brief imageObjectRects 探测页面上图片对象的包围盒
* 通过 libdeepin-pdfium 导出的 PDFium C API 枚举页面对象实现,
* 结果与 image(index,width,height) 整页渲染输出像素一一对齐;
* 探测失败(加密/损坏等)返回空,夜间模式回退整页反色
*/
QVector<QRectF> imageObjectRects(int index, int width, int height) const;

static PDFDocument *loadDocument(const QString &filePath, const QString &password, deepin_reader::Document::Error &error);

private:
Expand All @@ -136,6 +142,12 @@ class PDFDocument : public Document
qreal m_xRes = 72;

qreal m_yRes = 72;

QString m_password; // 文档密码(探测句柄加载加密文档时使用)

mutable void *m_probeDoc = nullptr; // PDFium FPDF_DOCUMENT,图片对象探测句柄(懒加载)

mutable bool m_probeFailed = false; // 探测句柄加载失败标记,避免反复尝试
};
}

Expand Down
Loading
Loading