summaryrefslogtreecommitdiffstats
path: root/src/pdf
diff options
context:
space:
mode:
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/CMakeLists.txt70
-rw-r--r--src/pdf/configure.cmake3
-rw-r--r--src/pdf/configure/BUILD.root.gn.in25
-rw-r--r--src/pdf/doc/about_credits.tmpl1
-rw-r--r--src/pdf/doc/about_credits_entry.tmpl13
-rw-r--r--src/pdf/doc/images/pdfviewer.pngbin0 -> 264348 bytes
-rw-r--r--src/pdf/doc/images/singlepageviewer.webpbin0 -> 57680 bytes
-rw-r--r--src/pdf/doc/qtpdf.qdocconf8
-rw-r--r--src/pdf/doc/snippets/multipageview.qml3
-rw-r--r--src/pdf/doc/snippets/pdfpageview.qml12
-rw-r--r--src/pdf/doc/snippets/qtpdf_build_snippet.qdoc5
-rw-r--r--src/pdf/doc/src/qtpdf-examples.qdoc1
-rw-r--r--src/pdf/doc/src/qtpdf-index.qdoc20
-rw-r--r--src/pdf/doc/src/qtpdf-licensing.qdoc18
-rw-r--r--src/pdf/doc/src/qtpdf-module.qdoc5
-rw-r--r--src/pdf/doc/src/qtpdf-platformnotes.qdoc11
-rw-r--r--src/pdf/plugins/imageformats/pdf/CMakeLists.txt3
-rw-r--r--src/pdf/plugins/imageformats/pdf/qpdfiohandler.cpp10
-rw-r--r--src/pdf/plugins/imageformats/pdf/qpdfiohandler_p.h2
-rw-r--r--src/pdf/qpdfbookmarkmodel.cpp33
-rw-r--r--src/pdf/qpdfbookmarkmodel.h3
-rw-r--r--src/pdf/qpdfdocument.cpp179
-rw-r--r--src/pdf/qpdfdocument.h3
-rw-r--r--src/pdf/qpdfdocument_p.h37
-rw-r--r--src/pdf/qpdfdocumentrenderoptions.h8
-rw-r--r--src/pdf/qpdfdocumentrenderoptions.qdoc8
-rw-r--r--src/pdf/qpdflink.cpp14
-rw-r--r--src/pdf/qpdflink.h4
-rw-r--r--src/pdf/qpdflinkmodel.cpp72
-rw-r--r--src/pdf/qpdflinkmodel.h67
-rw-r--r--src/pdf/qpdflinkmodel_p.h56
-rw-r--r--src/pdf/qpdflinkmodel_p_p.h44
-rw-r--r--src/pdf/qpdfpagenavigator.cpp30
-rw-r--r--src/pdf/qpdfpagerenderer.cpp2
-rw-r--r--src/pdf/qpdfsearchmodel.cpp62
-rw-r--r--src/pdf/qpdfsearchmodel.h6
-rw-r--r--src/pdf/qpdfsearchmodel_p.h2
-rw-r--r--src/pdf/qtpdfglobal.h19
38 files changed, 566 insertions, 293 deletions
diff --git a/src/pdf/CMakeLists.txt b/src/pdf/CMakeLists.txt
index ed2da1032..4a54b816e 100644
--- a/src/pdf/CMakeLists.txt
+++ b/src/pdf/CMakeLists.txt
@@ -1,6 +1,9 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
cmake_minimum_required(VERSION 3.19)
find_package(Ninja 1.7.2 REQUIRED)
-find_package(Nodejs 12 REQUIRED)
+find_package(Nodejs 14.19 REQUIRED)
find_package(PkgConfig)
if(PkgConfig_FOUND)
create_pkg_config_host_wrapper(${CMAKE_CURRENT_BINARY_DIR})
@@ -19,7 +22,7 @@ qt_internal_add_module(Pdf
qpdfdocumentrenderoptions.h
qpdffile.cpp qpdffile_p.h
qpdflink.cpp qpdflink.h qpdflink_p.h
- qpdflinkmodel.cpp qpdflinkmodel_p.h qpdflinkmodel_p_p.h
+ qpdflinkmodel.cpp qpdflinkmodel.h qpdflinkmodel_p.h
qpdfpagenavigator.cpp qpdfpagenavigator.h
qpdfpagerenderer.cpp qpdfpagerenderer.h
qpdfsearchmodel.cpp qpdfsearchmodel.h qpdfsearchmodel_p.h
@@ -29,17 +32,21 @@ qt_internal_add_module(Pdf
../3rdparty/chromium
DEFINES
QT_BUILD_PDF_LIB
- NOMINMAX
LIBRARIES
Qt::CorePrivate
Qt::Network
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
+ GENERATE_CPP_EXPORTS
)
add_subdirectory(plugins/imageformats/pdf)
+get_install_config(config)
+get_architectures(archs)
+list(GET archs 0 arch)
+
##
# PDF DOCS
##
@@ -48,12 +55,22 @@ qt_internal_add_docs(Pdf
doc/qtpdf.qdocconf
)
+add_code_attributions_target(
+ TARGET generate_pdf_attributions
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pdf_attributions.qdoc
+ GN_TARGET :QtPdf
+ FILE_TEMPLATE doc/about_credits.tmpl
+ ENTRY_TEMPLATE doc/about_credits_entry.tmpl
+ BUILDDIR ${buildDir}/${config}/${arch}
+)
+add_dependencies(generate_pdf_attributions run_pdf_GnDone)
+add_dependencies(prepare_docs_Pdf generate_pdf_attributions)
##
# TOOLCHAIN SETUP
##
-if(LINUX)
+if(LINUX OR MINGW OR ANDROID)
setup_toolchains()
endif()
@@ -103,34 +120,50 @@ foreach(arch ${archs})
qt_libjpeg_config="${buildDir}/${config}/${arch}:qt_libjpeg_config"
qt_harfbuzz_config="${buildDir}/${config}/${arch}:qt_harfbuzz_config"
qt_freetype_config="${buildDir}/${config}/${arch}:qt_freetype_config"
+ enable_swiftshader=false
+ enable_swiftshader_vulkan=false
+ angle_enable_swiftshader=false
+ dawn_use_swiftshader=false
+ use_dawn=false
+ build_dawn_tests=false
enable_ipc_fuzzer=false
enable_remoting=false
enable_resource_allowlist_generation=false
+ enable_vr=false
enable_web_speech=false
chrome_pgo_phase=0
strip_absolute_paths_from_debug_symbols=false
+ use_perfetto_client_library=false
+ v8_enable_webassembly=false
)
- if(LINUX)
+ if(LINUX OR ANDROID)
list(APPEND gnArgArg
is_cfi=false
ozone_auto_platforms=false
- use_gnome_keyring=false)
+ enable_arcore=false
+ use_ml_inliner=false
+ )
extend_gn_list(gnArgArg
ARGS use_system_icu
CONDITION QT_FEATURE_webengine_system_icu
)
+ extend_gn_list(gnArgArg
+ ARGS use_system_libopenjpeg2
+ CONDITION QT_FEATURE_webengine_system_libopenjpeg2
+ )
endif()
if(MACOS)
list(APPEND gnArgArg angle_enable_vulkan=false)
endif()
if(IOS)
+ list(APPEND gnArgArg enable_base_tracing=false)
extend_gn_list(gnArgArg
ARGS enable_ios_bitcode
CONDITION QT_FEATURE_pdf_bitcode
)
endif()
- if(WIN32)
+ if(WIN32 OR ANDROID)
list(APPEND gnArgArg
ninja_use_custom_environment_files=false
safe_browsing_mode=0
@@ -166,10 +199,22 @@ foreach(arch ${archs})
CONDITION QT_FEATURE_pdf_xfa_tiff
)
extend_gn_list(gnArgArg
+ ARGS pdfium_use_system_zlib use_system_zlib
+ CONDITION QT_FEATURE_webengine_system_zlib
+ )
+ extend_gn_list(gnArgArg
+ ARGS pdfium_use_system_libpng use_system_libpng
+ CONDITION QT_FEATURE_webengine_system_libpng
+ )
+ extend_gn_list(gnArgArg
ARGS pdfium_use_qt_libpng
CONDITION QT_FEATURE_webengine_qt_libpng
)
extend_gn_list(gnArgArg
+ ARGS pdfium_use_system_libtiff
+ CONDITION QT_FEATURE_webengine_system_libtiff
+ )
+ extend_gn_list(gnArgArg
ARGS use_qt_libjpeg
CONDITION QT_FEATURE_webengine_qt_libjpeg
)
@@ -186,7 +231,7 @@ foreach(arch ${archs})
CMAKE_TARGET Pdf
NINJA_TARGETS QtPdf
GN_TARGET ${buildGn}
- GN_ARGS "${gnArgArg}"
+ GN_ARGS ${gnArgArg}
BUILDDIR ${buildDir}/${config}/${arch}
MODULE pdf
)
@@ -203,6 +248,13 @@ endforeach()
get_architectures(archs)
list(GET archs 0 arch)
target_include_directories(Pdf PRIVATE ${buildDir}/$<CONFIG>/${arch}/gen)
-add_gn_build_aritfacts_to_target(Pdf QtPdf pdf ${buildDir} TRUE)
+add_gn_build_artifacts_to_target(
+ CMAKE_TARGET Pdf
+ NINJA_TARGET QtPdf
+ MODULE pdf
+ BUILDDIR ${buildDir}
+ COMPLETE_STATIC TRUE
+ NINJA_STAMP QtPdf.stamp
+)
add_dependencies(Pdf run_pdf_NinjaDone)
diff --git a/src/pdf/configure.cmake b/src/pdf/configure.cmake
index 8d1a07183..ac4e4e25f 100644
--- a/src/pdf/configure.cmake
+++ b/src/pdf/configure.cmake
@@ -1,3 +1,6 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
qt_feature("pdf-v8" PRIVATE
LABEL "Support V8"
PURPOSE "Enables javascript support."
diff --git a/src/pdf/configure/BUILD.root.gn.in b/src/pdf/configure/BUILD.root.gn.in
index 5420e8581..e9f54ed6d 100644
--- a/src/pdf/configure/BUILD.root.gn.in
+++ b/src/pdf/configure/BUILD.root.gn.in
@@ -39,24 +39,35 @@ config("QtPdf_config") {
]
}
-config("cpp17_config") {
- # static initialized constexpr expressions must be compiled always as c++14 or always as c++17
- # and our qtwebengine core sources use them as c++17
+config("cpp20_config") {
+ # Chromium headers now use concepts and requires c++20
if (is_win) {
- cflags_cc = [ "/std:c++17" ]
+ cflags_cc = [ "/std:c++20" ]
} else {
- cflags_cc = [ "-std=c++17" ]
+ cflags_cc = [ "-std=c++20" ]
}
}
static_library("QtPdf") {
complete_static_lib = true
- rsp_types = [ "objects", "archives", "libs" ]
+ rsp_types = [ "objects", "archives", "libs", "ldir" ]
configs += [
- ":cpp17_config",
+ ":cpp20_config",
":QtPdf_config"
]
deps = [
"//third_party/pdfium"
]
+ if (is_msvc) {
+ libs = [
+ "dloadhelper.lib",
+ "winmm.lib",
+ "usp10.lib",
+ ]
+ }
+ if (is_mingw) {
+ libs = [
+ "winmm",
+ ]
+ }
}
diff --git a/src/pdf/doc/about_credits.tmpl b/src/pdf/doc/about_credits.tmpl
new file mode 100644
index 000000000..57fae9e78
--- /dev/null
+++ b/src/pdf/doc/about_credits.tmpl
@@ -0,0 +1 @@
+{{entries}}
diff --git a/src/pdf/doc/about_credits_entry.tmpl b/src/pdf/doc/about_credits_entry.tmpl
new file mode 100644
index 000000000..294198709
--- /dev/null
+++ b/src/pdf/doc/about_credits_entry.tmpl
@@ -0,0 +1,13 @@
+/*!
+\page qtpdf-3rdparty-{{name-sanitized}}.html
+\attribution
+\ingroup qtpdf-licensing
+\brief {{license-type}}
+\title {{name}}
+
+\l{{{url}}}{Project Homepage}
+
+\badcode
+{{license}}
+\endcode
+*/
diff --git a/src/pdf/doc/images/pdfviewer.png b/src/pdf/doc/images/pdfviewer.png
new file mode 100644
index 000000000..ac8a31ac0
--- /dev/null
+++ b/src/pdf/doc/images/pdfviewer.png
Binary files differ
diff --git a/src/pdf/doc/images/singlepageviewer.webp b/src/pdf/doc/images/singlepageviewer.webp
new file mode 100644
index 000000000..e429cb818
--- /dev/null
+++ b/src/pdf/doc/images/singlepageviewer.webp
Binary files differ
diff --git a/src/pdf/doc/qtpdf.qdocconf b/src/pdf/doc/qtpdf.qdocconf
index 6be1b46e0..d0340fe83 100644
--- a/src/pdf/doc/qtpdf.qdocconf
+++ b/src/pdf/doc/qtpdf.qdocconf
@@ -1,4 +1,5 @@
include($QT_INSTALL_DOCS/global/qt-module-defaults.qdocconf)
+include($QT_INSTALL_DOCS/config/exampleurl-qtwebengine.qdocconf)
project = QtPdf
description = Qt Pdf Reference Documentation
@@ -29,6 +30,8 @@ qhp.QtPdf.subprojects.examples.indexTitle = Qt PDF Examples
qhp.QtPdf.subprojects.examples.selectors = doc:example
qhp.QtPdf.subprojects.examples.sortPages = true
+manifestmeta.highlighted.names += "QtPdf/PDF Multipage Viewer Example"
+
depends += qtcore \
qtwidgets \
qtgui \
@@ -37,7 +40,8 @@ depends += qtcore \
qtdesigner \
qtquick \
qtquickcontrols \
- qtcmake
+ qtcmake \
+ qtsvg
headerdirs += ../ \
../../pdfwidgets
@@ -59,5 +63,5 @@ navigation.landingpage = "Qt PDF"
navigation.cppclassespage = "Qt PDF C++ Classes"
navigation.qmltypespage = "Qt Quick PDF QML Types"
-# Fail the documentation build if there are more warnings than the limit
+# Enforce zero documentation warnings
warninglimit = 0
diff --git a/src/pdf/doc/snippets/multipageview.qml b/src/pdf/doc/snippets/multipageview.qml
index ee695c03a..113444165 100644
--- a/src/pdf/doc/snippets/multipageview.qml
+++ b/src/pdf/doc/snippets/multipageview.qml
@@ -1,3 +1,6 @@
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
//! [0]
import QtQuick
import QtQuick.Pdf
diff --git a/src/pdf/doc/snippets/pdfpageview.qml b/src/pdf/doc/snippets/pdfpageview.qml
new file mode 100644
index 000000000..5e233961a
--- /dev/null
+++ b/src/pdf/doc/snippets/pdfpageview.qml
@@ -0,0 +1,12 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+//! [0]
+import QtQuick
+import QtQuick.Pdf
+
+PdfPageView {
+ document: PdfDocument { source: "my.pdf" }
+}
+//! [0]
+
diff --git a/src/pdf/doc/snippets/qtpdf_build_snippet.qdoc b/src/pdf/doc/snippets/qtpdf_build_snippet.qdoc
index 4be08b5a7..7d30ccdfd 100644
--- a/src/pdf/doc/snippets/qtpdf_build_snippet.qdoc
+++ b/src/pdf/doc/snippets/qtpdf_build_snippet.qdoc
@@ -4,8 +4,3 @@
//! [0]
QT += pdf
//! [0]
-
-
-//! [1]
-#include <QtPdf>
-//! [1]
diff --git a/src/pdf/doc/src/qtpdf-examples.qdoc b/src/pdf/doc/src/qtpdf-examples.qdoc
index 0ebad3c82..02dc23dc2 100644
--- a/src/pdf/doc/src/qtpdf-examples.qdoc
+++ b/src/pdf/doc/src/qtpdf-examples.qdoc
@@ -3,7 +3,6 @@
/*!
\group qtpdf-examples
- \ingroup all-examples
\title Qt PDF Examples
\brief Using the classes and types in the Qt PDF module.
diff --git a/src/pdf/doc/src/qtpdf-index.qdoc b/src/pdf/doc/src/qtpdf-index.qdoc
index 1e37565f5..b72619fbf 100644
--- a/src/pdf/doc/src/qtpdf-index.qdoc
+++ b/src/pdf/doc/src/qtpdf-index.qdoc
@@ -17,7 +17,7 @@
and holds the search results. The QPdfBookmarkModel class holds the
table of contents, if present. The QPdfLinkModel holds information
about hyperlinks on a page. The \l QPdfView widget is a complete
- PDF viewer, and the \l {PDF Viewer Example} shows how to use it.
+ PDF viewer, and the \l {PDF Viewer Widget Example} shows how to use it.
For Qt Quick applications, three kinds of full-featured viewer
components are provided. \l PdfMultiPageView should be your
@@ -48,11 +48,6 @@
\section2 Building with qmake
- To include the definitions of the module's classes, use the
- following directive:
-
- \snippet qtpdf_build_snippet.qdoc 1
-
To link against the module, add this line to your qmake project file:
\snippet qtpdf_build_snippet.qdoc 0
@@ -69,4 +64,17 @@
\li \l{Qt PDF C++ Classes}
\li \l{Qt Quick PDF QML Types}
\endlist
+
+ \section1 Articles and Guides
+ \list
+ \li {Qt PDF Platform Notes} {Platform Notes}
+ \endlist
+
+ \section1 Licenses and Attributions
+
+ Qt PDF is available under commercial licenses from \l{The Qt Company}.
+ In addition, it is available under the
+ \l{GNU Lesser General Public License, version 3}, or
+ the \l{GNU General Public License, version 2}.
+ See \l{Qt PDF Licensing} for further details about this module.
*/
diff --git a/src/pdf/doc/src/qtpdf-licensing.qdoc b/src/pdf/doc/src/qtpdf-licensing.qdoc
new file mode 100644
index 000000000..190ee8331
--- /dev/null
+++ b/src/pdf/doc/src/qtpdf-licensing.qdoc
@@ -0,0 +1,18 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
+
+/*!
+ \group qtpdf-licensing
+ \title Qt PDF Licensing
+
+ Qt PDF is available under commercial licenses from \l{The Qt Company}.
+ In addition, it is available under the
+ \l{GNU Lesser General Public License, version 3}, or
+ the \l{GNU General Public License, version 2}.
+ See \l{Qt Licensing} for further details.
+
+ The module includes a snapshot of PDFium. As such, users need to respect
+ the licenses of PDFium and third-party code included in it.
+
+ Third party licenses included in the sources are:
+*/
diff --git a/src/pdf/doc/src/qtpdf-module.qdoc b/src/pdf/doc/src/qtpdf-module.qdoc
index 6fe1bbcac..e2ca8e4ce 100644
--- a/src/pdf/doc/src/qtpdf-module.qdoc
+++ b/src/pdf/doc/src/qtpdf-module.qdoc
@@ -13,11 +13,6 @@
The Qt PDF module contains classes and functions for rendering
PDF documents.
- To include the definitions of the module's classes, use the
- following directive:
-
- \snippet qtpdf_build_snippet.qdoc 1
-
\if !defined(qtforpython)
To link against the module, add this line to your qmake project file:
diff --git a/src/pdf/doc/src/qtpdf-platformnotes.qdoc b/src/pdf/doc/src/qtpdf-platformnotes.qdoc
new file mode 100644
index 000000000..f50be120d
--- /dev/null
+++ b/src/pdf/doc/src/qtpdf-platformnotes.qdoc
@@ -0,0 +1,11 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
+
+/*!
+ \page qtpdf-platformnotes.html
+ \title Qt PDF Platform Notes
+
+ Building Qt PDF for Android is currently
+ \l{https://bugreports.qt.io/browse/QTBUG-83459} {not supported} on Windows host platforms.
+*/
+
diff --git a/src/pdf/plugins/imageformats/pdf/CMakeLists.txt b/src/pdf/plugins/imageformats/pdf/CMakeLists.txt
index 517d152c7..73a0b3144 100644
--- a/src/pdf/plugins/imageformats/pdf/CMakeLists.txt
+++ b/src/pdf/plugins/imageformats/pdf/CMakeLists.txt
@@ -1,3 +1,6 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
qt_internal_add_plugin(QPdfPlugin
OUTPUT_NAME qpdf
PLUGIN_TYPE imageformats
diff --git a/src/pdf/plugins/imageformats/pdf/qpdfiohandler.cpp b/src/pdf/plugins/imageformats/pdf/qpdfiohandler.cpp
index 195759028..bb3e7c929 100644
--- a/src/pdf/plugins/imageformats/pdf/qpdfiohandler.cpp
+++ b/src/pdf/plugins/imageformats/pdf/qpdfiohandler.cpp
@@ -62,7 +62,7 @@ int QPdfIOHandler::imageCount() const
bool QPdfIOHandler::read(QImage *image)
{
if (load(device())) {
- if (m_page >= m_doc->pageCount())
+ if (m_doc.isNull() || m_page >= m_doc->pageCount())
return false;
if (m_page < 0)
m_page = 0;
@@ -110,9 +110,11 @@ bool QPdfIOHandler::read(QImage *image)
options.setScaledSize(pageSize);
image->fill(m_backColor.rgba());
QPainter p(image);
- QImage pageImage = m_doc->render(m_page, finalSize, options);
- p.drawImage(0, 0, pageImage);
- p.end();
+ if (!m_doc.isNull()) {
+ QImage pageImage = m_doc->render(m_page, finalSize, options);
+ p.drawImage(0, 0, pageImage);
+ p.end();
+ }
}
return true;
}
diff --git a/src/pdf/plugins/imageformats/pdf/qpdfiohandler_p.h b/src/pdf/plugins/imageformats/pdf/qpdfiohandler_p.h
index 5918c0328..c4d8e0f9a 100644
--- a/src/pdf/plugins/imageformats/pdf/qpdfiohandler_p.h
+++ b/src/pdf/plugins/imageformats/pdf/qpdfiohandler_p.h
@@ -41,7 +41,7 @@ private:
bool load(QIODevice *device);
private:
- QPdfDocument *m_doc = nullptr;
+ QPointer<QPdfDocument> m_doc;
int m_page = -1;
QRect m_clipRect;
diff --git a/src/pdf/qpdfbookmarkmodel.cpp b/src/pdf/qpdfbookmarkmodel.cpp
index 50e1f88ba..93dbf5d1f 100644
--- a/src/pdf/qpdfbookmarkmodel.cpp
+++ b/src/pdf/qpdfbookmarkmodel.cpp
@@ -50,7 +50,7 @@ public:
int childCount() const
{
- return m_childNodes.count();
+ return m_childNodes.size();
}
int row() const
@@ -172,21 +172,11 @@ struct QPdfBookmarkModelPrivate
const int titleLength = int(FPDFBookmark_GetTitle(bookmark, nullptr, 0));
QList<char16_t> titleBuffer(titleLength);
- FPDFBookmark_GetTitle(bookmark, titleBuffer.data(), quint32(titleBuffer.length()));
+ FPDFBookmark_GetTitle(bookmark, titleBuffer.data(), quint32(titleBuffer.size()));
const FPDF_DEST dest = FPDFBookmark_GetDest(document, bookmark);
const int pageNumber = FPDFDest_GetDestPageIndex(document, dest);
- double pageHeight = 11.69 * 72; // A4 height
- {
- // get actual page height
- const QPdfMutexLocker lock;
- FPDF_PAGE pdfPage = FPDF_LoadPage(document, pageNumber);
- if (pdfPage)
- pageHeight = FPDF_GetPageHeight(pdfPage);
- else
- qCWarning(qLcBM) << "failed to load page" << pageNumber;
- }
-
+ const qreal pageHeight = m_document->pagePointSize(pageNumber).height();
FPDF_BOOL hasX, hasY, hasZoom;
FS_FLOAT x, y, zoom;
bool ok = FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y, &zoom);
@@ -219,6 +209,7 @@ struct QPdfBookmarkModelPrivate
QScopedPointer<BookmarkNode> m_rootNode;
QPointer<QPdfDocument> m_document;
+ QHash<int, QByteArray> m_roleNames;
};
@@ -243,7 +234,7 @@ struct QPdfBookmarkModelPrivate
\value Page The page number of the destination (int).
\value Location The position of the destination (QPointF).
\value Zoom The suggested zoom level (qreal).
- \omitvalue _Count
+ \omitvalue NRoles
*/
/*!
@@ -253,10 +244,10 @@ QPdfBookmarkModel::QPdfBookmarkModel(QObject *parent)
: QAbstractItemModel(parent), d(new QPdfBookmarkModelPrivate)
{
d->q = this;
- m_roleNames = QAbstractItemModel::roleNames();
+ d->m_roleNames = QAbstractItemModel::roleNames();
QMetaEnum rolesMetaEnum = metaObject()->enumerator(metaObject()->indexOfEnumerator("Role"));
- for (int r = Qt::UserRole; r < int(Role::_Count); ++r)
- m_roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower());
+ for (int r = Qt::UserRole; r < int(Role::NRoles); ++r)
+ d->m_roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower());
}
/*!
@@ -269,6 +260,10 @@ QPdfDocument* QPdfBookmarkModel::document() const
return d->m_document;
}
+/*!
+ \property QPdfBookmarkModel::document
+ \brief the PDF document in which bookmarks are to be found.
+*/
void QPdfBookmarkModel::setDocument(QPdfDocument *document)
{
if (d->m_document == document)
@@ -300,7 +295,7 @@ int QPdfBookmarkModel::columnCount(const QModelIndex &parent) const
*/
QHash<int, QByteArray> QPdfBookmarkModel::roleNames() const
{
- return m_roleNames;
+ return d->m_roleNames;
}
/*!
@@ -323,7 +318,7 @@ QVariant QPdfBookmarkModel::data(const QModelIndex &index, int role) const
return node->location();
case Role::Zoom:
return node->zoom();
- case Role::_Count:
+ case Role::NRoles:
break;
}
if (role == Qt::DisplayRole)
diff --git a/src/pdf/qpdfbookmarkmodel.h b/src/pdf/qpdfbookmarkmodel.h
index 616cb5379..5a3c24f84 100644
--- a/src/pdf/qpdfbookmarkmodel.h
+++ b/src/pdf/qpdfbookmarkmodel.h
@@ -26,7 +26,7 @@ public:
Page,
Location,
Zoom,
- _Count
+ NRoles
};
Q_ENUM(Role)
@@ -48,7 +48,6 @@ Q_SIGNALS:
void documentChanged(QPdfDocument *document);
private:
- QHash<int, QByteArray> m_roleNames;
std::unique_ptr<QPdfBookmarkModelPrivate> d;
Q_PRIVATE_SLOT(d, void _q_documentStatusChanged())
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp
index 26ca90e30..17fdb29b9 100644
--- a/src/pdf/qpdfdocument.cpp
+++ b/src/pdf/qpdfdocument.cpp
@@ -15,8 +15,11 @@
#include <QLoggingCategory>
#include <QMetaEnum>
#include <QMutex>
+#include <QPixmap>
#include <QVector2D>
+#include <QtCore/private/qtools_p.h>
+
QT_BEGIN_NAMESPACE
Q_GLOBAL_STATIC(QRecursiveMutex, pdfMutex)
@@ -37,9 +40,9 @@ public:
{
m_roleNames = QAbstractItemModel::roleNames();
QMetaEnum rolesMetaEnum = doc->metaObject()->enumerator(doc->metaObject()->indexOfEnumerator("PageModelRole"));
- for (int r = Qt::UserRole; r < int(QPdfDocument::PageModelRole::_Count); ++r) {
+ for (int r = Qt::UserRole; r < int(QPdfDocument::PageModelRole::NRoles); ++r) {
auto name = QByteArray(rolesMetaEnum.valueToKey(r));
- name[0] = tolower(name[0]);
+ name[0] = QtMiscUtils::toAsciiLower(name[0]);
m_roleNames.insert(r, name);
}
connect(doc, &QPdfDocument::statusChanged, this, [this](QPdfDocument::Status s) {
@@ -54,14 +57,23 @@ public:
{
if (!index.isValid())
return QVariant();
+
switch (QPdfDocument::PageModelRole(role)) {
case QPdfDocument::PageModelRole::Label:
return document()->pageLabel(index.row());
case QPdfDocument::PageModelRole::PointSize:
return document()->pagePointSize(index.row());
- case QPdfDocument::PageModelRole::_Count:
+ case QPdfDocument::PageModelRole::NRoles:
break;
}
+
+ switch (role) {
+ case Qt::DecorationRole:
+ return pageThumbnail(index.row());
+ case Qt::DisplayRole:
+ return document()->pageLabel(index.row());
+ }
+
return QVariant();
}
@@ -71,8 +83,24 @@ public:
private:
QPdfDocument *document() const { return static_cast<QPdfDocument *>(parent()); }
+ QPixmap pageThumbnail(int page) const
+ {
+ auto it = m_thumbnails.constFind(page);
+ if (it == m_thumbnails.constEnd()) {
+ auto doc = document();
+ auto size = doc->pagePointSize(page);
+ size.scale(128, 128, Qt::KeepAspectRatio);
+ // TODO use QPdfPageRenderer for threading?
+ auto image = document()->render(page, size.toSize());
+ QPixmap ret = QPixmap::fromImage(image);
+ m_thumbnails.insert(page, ret);
+ return ret;
+ }
+ return it.value();
+ }
QHash<int, QByteArray> m_roleNames;
+ mutable QHash<int, QPixmap> m_thumbnails;
};
QPdfDocumentPrivate::QPdfDocumentPrivate()
@@ -418,7 +446,7 @@ void QPdfDocumentPrivate::fpdf_AddSegment(_FX_DOWNLOADHINTS *pThis, size_t offse
Q_UNUSED(size);
}
-QString QPdfDocumentPrivate::getText(FPDF_TEXTPAGE textPage, int startIndex, int count)
+QString QPdfDocumentPrivate::getText(FPDF_TEXTPAGE textPage, int startIndex, int count) const
{
QList<ushort> buf(count + 1);
// TODO is that enough space in case one unicode character is more than one in utf-16?
@@ -427,23 +455,73 @@ QString QPdfDocumentPrivate::getText(FPDF_TEXTPAGE textPage, int startIndex, int
return QString::fromUtf16(reinterpret_cast<const char16_t *>(buf.constData()), len - 1);
}
-QPointF QPdfDocumentPrivate::getCharPosition(FPDF_TEXTPAGE textPage, double pageHeight, int charIndex)
+QPointF QPdfDocumentPrivate::getCharPosition(FPDF_PAGE pdfPage, FPDF_TEXTPAGE textPage, int charIndex) const
{
double x, y;
- int count = FPDFText_CountChars(textPage);
- bool ok = FPDFText_GetCharOrigin(textPage, qMin(count - 1, charIndex), &x, &y);
- if (!ok)
- return QPointF();
- return QPointF(x, pageHeight - y);
+ const int count = FPDFText_CountChars(textPage);
+ if (FPDFText_GetCharOrigin(textPage, qMin(count - 1, charIndex), &x, &y))
+ return mapPageToView(pdfPage, x, y);
+ return {};
}
-QRectF QPdfDocumentPrivate::getCharBox(FPDF_TEXTPAGE textPage, double pageHeight, int charIndex)
+QRectF QPdfDocumentPrivate::getCharBox(FPDF_PAGE pdfPage, FPDF_TEXTPAGE textPage, int charIndex) const
{
double l, t, r, b;
- bool ok = FPDFText_GetCharBox(textPage, charIndex, &l, &r, &b, &t);
- if (!ok)
- return QRectF();
- return QRectF(l, pageHeight - t, r - l, t - b);
+ if (FPDFText_GetCharBox(textPage, charIndex, &l, &r, &b, &t))
+ return mapPageToView(pdfPage, l, t, r, b);
+ return {};
+}
+
+/*! \internal
+ Convert the point \a x , \a y to the usual 1x (pixels = points)
+ 4th-quadrant "view" coordinate system relative to the top-left corner of
+ the rendered page. Some PDF files have internal transforms that make this
+ coordinate system different from "page coordinates", so we cannot just
+ subtract from page height to invert the y coordinates, in general.
+ */
+QPointF QPdfDocumentPrivate::mapPageToView(FPDF_PAGE pdfPage, double x, double y) const
+{
+ const auto pageHeight = FPDF_GetPageHeight(pdfPage);
+ const auto pageWidth = FPDF_GetPageWidth(pdfPage);
+ int rx, ry;
+ if (FPDF_PageToDevice(pdfPage, 0, 0, qRound(pageWidth), qRound(pageHeight), 0, x, y, &rx, &ry))
+ return QPointF(rx, ry);
+ return {};
+}
+
+/*! \internal
+ Convert the bounding box defined by \a left \a top \a right and \a bottom
+ to the usual 1x (pixels = points) 4th-quadrant "view" coordinate system
+ that we use for rendering things on top of the page image.
+ Some PDF files have internal transforms that make this coordinate
+ system different from "page coordinates", so we cannot just
+ subtract from page height to invert the y coordinates, in general.
+ */
+QRectF QPdfDocumentPrivate::mapPageToView(FPDF_PAGE pdfPage, double left, double top, double right, double bottom) const
+{
+ const auto pageHeight = FPDF_GetPageHeight(pdfPage);
+ const auto pageWidth = FPDF_GetPageWidth(pdfPage);
+ int xfmLeft, xfmTop, xfmRight, xfmBottom;
+ if ( FPDF_PageToDevice(pdfPage, 0, 0, qRound(pageWidth), qRound(pageHeight), 0, left, top, &xfmLeft, &xfmTop) &&
+ FPDF_PageToDevice(pdfPage, 0, 0, qRound(pageWidth), qRound(pageHeight), 0, right, bottom, &xfmRight, &xfmBottom) )
+ return QRectF(xfmLeft, xfmTop, xfmRight - xfmLeft, xfmBottom - xfmTop);
+ return {};
+}
+
+/*! \internal
+ Convert the point \a x , \a y \a from the usual 1x (pixels = points)
+ 4th-quadrant "view" coordinate system relative to the top-left corner of
+ the rendered page, to "page coordinates" suited to the given \a pdfPage,
+ which may have arbitrary internal transforms.
+ */
+QPointF QPdfDocumentPrivate::mapViewToPage(FPDF_PAGE pdfPage, QPointF position) const
+{
+ const auto pageHeight = FPDF_GetPageHeight(pdfPage);
+ const auto pageWidth = FPDF_GetPageWidth(pdfPage);
+ double rx, ry;
+ if (FPDF_DeviceToPage(pdfPage, 0, 0, qRound(pageWidth), qRound(pageHeight), 0, position.x(), position.y(), &rx, &ry))
+ return QPointF(rx, ry);
+ return {};
}
QPdfDocumentPrivate::TextPosition QPdfDocumentPrivate::hitTest(int page, QPointF position)
@@ -452,14 +530,14 @@ QPdfDocumentPrivate::TextPosition QPdfDocumentPrivate::hitTest(int page, QPointF
TextPosition result;
FPDF_PAGE pdfPage = FPDF_LoadPage(doc, page);
- double pageHeight = FPDF_GetPageHeight(pdfPage);
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
- int hitIndex = FPDFText_GetCharIndexAtPos(textPage, position.x(), pageHeight - position.y(),
+ const QPointF pagePos = mapViewToPage(pdfPage, position);
+ int hitIndex = FPDFText_GetCharIndexAtPos(textPage, pagePos.x(), pagePos.y(),
CharacterHitTolerance, CharacterHitTolerance);
if (hitIndex >= 0) {
- QPointF charPos = getCharPosition(textPage, pageHeight, hitIndex);
+ QPointF charPos = getCharPosition(pdfPage, textPage, hitIndex);
if (!charPos.isNull()) {
- QRectF charBox = getCharBox(textPage, pageHeight, hitIndex);
+ QRectF charBox = getCharBox(pdfPage, textPage, hitIndex);
// If the given position is past the end of the line, i.e. if the right edge of the found character's
// bounding box is closer to it than the left edge is, we say that we "hit" the next character index after
if (qAbs(charBox.right() - position.x()) < qAbs(charPos.x() - position.x())) {
@@ -639,7 +717,7 @@ QVariant QPdfDocument::metaData(MetaDataField field) const
const unsigned long len = FPDF_GetMetaText(d->doc, fieldName.constData(), nullptr, 0);
QList<ushort> buf(len);
- FPDF_GetMetaText(d->doc, fieldName.constData(), buf.data(), buf.length());
+ FPDF_GetMetaText(d->doc, fieldName.constData(), buf.data(), buf.size());
lock.unlock();
QString text = QString::fromUtf16(reinterpret_cast<const char16_t *>(buf.data()));
@@ -750,7 +828,7 @@ QSizeF QPdfDocument::pagePointSize(int page) const
\value Label The page number to be used for display purposes (QString).
\value PointSize The page size in points (1/72 of an inch) (QSizeF).
- \omitvalue _Count
+ \omitvalue NRoles
*/
/*!
@@ -781,6 +859,8 @@ QAbstractListModel *QPdfDocument::pageModel()
If the document does not have custom page numbering, this function returns
\c {page + 1}.
+
+ \sa pageIndexForLabel()
*/
QString QPdfDocument::pageLabel(int page)
{
@@ -795,6 +875,21 @@ QString QPdfDocument::pageLabel(int page)
}
/*!
+ Returns the index of the page that has the \a label, or \c -1 if not found.
+
+ \sa pageLabel()
+ \since 6.6
+*/
+int QPdfDocument::pageIndexForLabel(const QString &label)
+{
+ for (int i = 0; i < d->pageCount; ++i) {
+ if (pageLabel(i) == label)
+ return i;
+ }
+ return -1;
+}
+
+/*!
Renders the \a page into a QImage of size \a imageSize according to the
provided \a renderOptions.
@@ -822,22 +917,6 @@ QImage QPdfDocument::render(int page, QSize imageSize, QPdfDocumentRenderOptions
result.fill(Qt::transparent);
FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(result.width(), result.height(), FPDFBitmap_BGRA, result.bits(), result.bytesPerLine());
- int rotation = 0;
- switch (renderOptions.rotation()) {
- case QPdfDocumentRenderOptions::Rotation::Rotate0:
- rotation = 0;
- break;
- case QPdfDocumentRenderOptions::Rotation::Rotate90:
- rotation = 1;
- break;
- case QPdfDocumentRenderOptions::Rotation::Rotate180:
- rotation = 2;
- break;
- case QPdfDocumentRenderOptions::Rotation::Rotate270:
- rotation = 3;
- break;
- }
-
const QPdfDocumentRenderOptions::RenderFlags renderFlags = renderOptions.renderFlags();
int flags = 0;
if (renderFlags & QPdfDocumentRenderOptions::RenderFlag::Annotations)
@@ -883,6 +962,7 @@ QImage QPdfDocument::render(int page, QSize imageSize, QPdfDocumentRenderOptions
qCDebug(qLcDoc) << "page" << page << "region" << renderOptions.scaledClipRect()
<< "size" << imageSize << "took" << timer.elapsed() << "ms";
} else {
+ const auto rotation = QPdfDocumentPrivate::toFPDFRotation(renderOptions.rotation());
FPDF_RenderPageBitmap(bitmap, pdfPage, 0, 0, result.width(), result.height(), rotation, flags);
qCDebug(qLcDoc) << "page" << page << "size" << imageSize << "took" << timer.elapsed() << "ms";
}
@@ -901,11 +981,12 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
{
const QPdfMutexLocker lock;
FPDF_PAGE pdfPage = FPDF_LoadPage(d->doc, page);
- double pageHeight = FPDF_GetPageHeight(pdfPage);
+ const QPointF pageStart = d->mapViewToPage(pdfPage, start);
+ const QPointF pageEnd = d->mapViewToPage(pdfPage, end);
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
- int startIndex = FPDFText_GetCharIndexAtPos(textPage, start.x(), pageHeight - start.y(),
+ int startIndex = FPDFText_GetCharIndexAtPos(textPage, pageStart.x(), pageStart.y(),
CharacterHitTolerance, CharacterHitTolerance);
- int endIndex = FPDFText_GetCharIndexAtPos(textPage, end.x(), pageHeight - end.y(),
+ int endIndex = FPDFText_GetCharIndexAtPos(textPage, pageEnd.x(), pageEnd.y(),
CharacterHitTolerance, CharacterHitTolerance);
QPdfSelection result;
@@ -916,7 +997,7 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
// If the given end position is past the end of the line, i.e. if the right edge of the last character's
// bounding box is closer to it than the left edge is, then extend the char range by one
- QRectF endCharBox = d->getCharBox(textPage, pageHeight, endIndex);
+ QRectF endCharBox = d->getCharBox(pdfPage, textPage, endIndex);
if (qAbs(endCharBox.right() - end.x()) < qAbs(endCharBox.x() - end.x()))
++endIndex;
@@ -928,7 +1009,7 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
for (int i = 0; i < rectCount; ++i) {
double l, r, b, t;
FPDFText_GetRect(textPage, i, &l, &t, &r, &b);
- QRectF rect(l, pageHeight - t, r - l, t - b);
+ const QRectF rect = d->mapPageToView(pdfPage, l, t, r, b);
if (hull.isNull())
hull = rect;
else
@@ -958,7 +1039,6 @@ QPdfSelection QPdfDocument::getSelectionAtIndex(int page, int startIndex, int ma
return {};
const QPdfMutexLocker lock;
FPDF_PAGE pdfPage = FPDF_LoadPage(d->doc, page);
- double pageHeight = FPDF_GetPageHeight(pdfPage);
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
int pageCount = FPDFText_CountChars(textPage);
if (startIndex >= pageCount)
@@ -969,11 +1049,11 @@ QPdfSelection QPdfDocument::getSelectionAtIndex(int page, int startIndex, int ma
QString text;
if (maxLength > 0) {
text = d->getText(textPage, startIndex, maxLength);
- rectCount = FPDFText_CountRects(textPage, startIndex, text.length());
+ rectCount = FPDFText_CountRects(textPage, startIndex, text.size());
for (int i = 0; i < rectCount; ++i) {
double l, r, b, t;
FPDFText_GetRect(textPage, i, &l, &t, &r, &b);
- QRectF rect(l, pageHeight - t, r - l, t - b);
+ const QRectF rect = d->mapPageToView(pdfPage, l, t, r, b);
if (hull.isNull())
hull = rect;
else
@@ -982,14 +1062,14 @@ QPdfSelection QPdfDocument::getSelectionAtIndex(int page, int startIndex, int ma
}
}
if (bounds.isEmpty())
- hull = QRectF(d->getCharPosition(textPage, pageHeight, startIndex), QSizeF());
+ hull = QRectF(d->getCharPosition(pdfPage, textPage, startIndex), QSizeF());
qCDebug(qLcDoc) << "on page" << page << "at index" << startIndex << "maxLength" << maxLength
- << "got" << text.length() << "chars," << rectCount << "rects within" << hull;
+ << "got" << text.size() << "chars," << rectCount << "rects within" << hull;
FPDFText_ClosePage(textPage);
FPDF_ClosePage(pdfPage);
- return QPdfSelection(text, bounds, hull, startIndex, startIndex + text.length());
+ return QPdfSelection(text, bounds, hull, startIndex, startIndex + text.size());
}
/*!
@@ -999,7 +1079,6 @@ QPdfSelection QPdfDocument::getAllText(int page)
{
const QPdfMutexLocker lock;
FPDF_PAGE pdfPage = FPDF_LoadPage(d->doc, page);
- double pageHeight = FPDF_GetPageHeight(pdfPage);
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
int count = FPDFText_CountChars(textPage);
if (count < 1)
@@ -1011,7 +1090,7 @@ QPdfSelection QPdfDocument::getAllText(int page)
for (int i = 0; i < rectCount; ++i) {
double l, r, b, t;
FPDFText_GetRect(textPage, i, &l, &t, &r, &b);
- QRectF rect(l, pageHeight - t, r - l, t - b);
+ const QRectF rect = d->mapPageToView(pdfPage, l, t, r, b);
if (hull.isNull())
hull = rect;
else
diff --git a/src/pdf/qpdfdocument.h b/src/pdf/qpdfdocument.h
index 1a96d6e11..8355246ae 100644
--- a/src/pdf/qpdfdocument.h
+++ b/src/pdf/qpdfdocument.h
@@ -62,7 +62,7 @@ public:
enum class PageModelRole {
Label = Qt::UserRole,
PointSize,
- _Count
+ NRoles
};
Q_ENUM(PageModelRole)
@@ -89,6 +89,7 @@ public:
Q_INVOKABLE QSizeF pagePointSize(int page) const;
Q_INVOKABLE QString pageLabel(int page);
+ Q_INVOKABLE int pageIndexForLabel(const QString &label);
QAbstractListModel *pageModel();
diff --git a/src/pdf/qpdfdocument_p.h b/src/pdf/qpdfdocument_p.h
index 973dc1d4a..cdb76d16f 100644
--- a/src/pdf/qpdfdocument_p.h
+++ b/src/pdf/qpdfdocument_p.h
@@ -16,6 +16,7 @@
//
#include "qpdfdocument.h"
+#include "qtpdfexports.h"
#include "third_party/pdfium/public/fpdfview.h"
#include "third_party/pdfium/public/fpdf_dataavail.h"
@@ -37,7 +38,7 @@ public:
class QPdfPageModel;
-class Q_PDF_PRIVATE_EXPORT QPdfDocumentPrivate: public FPDF_FILEACCESS, public FX_FILEAVAIL, public FX_DOWNLOADHINTS
+class Q_PDF_EXPORT QPdfDocumentPrivate: public FPDF_FILEACCESS, public FX_FILEAVAIL, public FX_DOWNLOADHINTS
{
public:
QPdfDocumentPrivate();
@@ -77,9 +78,37 @@ public:
static int fpdf_GetBlock(void* param, unsigned long position, unsigned char* pBuf, unsigned long size);
static void fpdf_AddSegment(struct _FX_DOWNLOADHINTS* pThis, size_t offset, size_t size);
void updateLastError();
- QString getText(FPDF_TEXTPAGE textPage, int startIndex, int count);
- QPointF getCharPosition(FPDF_TEXTPAGE textPage, double pageHeight, int charIndex);
- QRectF getCharBox(FPDF_TEXTPAGE textPage, double pageHeight, int charIndex);
+ QString getText(FPDF_TEXTPAGE textPage, int startIndex, int count) const;
+ QPointF getCharPosition(FPDF_PAGE pdfPage, FPDF_TEXTPAGE textPage, int charIndex) const;
+ QRectF getCharBox(FPDF_PAGE pdfPage, FPDF_TEXTPAGE textPage, int charIndex) const;
+ QPointF mapPageToView(FPDF_PAGE pdfPage, double x, double y) const;
+ QRectF mapPageToView(FPDF_PAGE pdfPage, double left, double top, double right, double bottom) const;
+ QPointF mapViewToPage(FPDF_PAGE pdfPage, QPointF position) const;
+
+ // FPDF takes the rotation parameter as an int.
+ // This enum is mapping the int values defined in fpdfview.h:956.
+ // (not using enum class to ensure int convertability)
+ enum QFPDFRotation {
+ Normal = 0,
+ ClockWise90 = 1,
+ ClockWise180 = 2,
+ CounterClockWise90 = 3
+ };
+
+ static constexpr QFPDFRotation toFPDFRotation(QPdfDocumentRenderOptions::Rotation rotation)
+ {
+ switch (rotation) {
+ case QPdfDocumentRenderOptions::Rotation::None:
+ return QFPDFRotation::Normal;
+ case QPdfDocumentRenderOptions::Rotation::Clockwise90:
+ return QFPDFRotation::ClockWise90;
+ case QPdfDocumentRenderOptions::Rotation::Clockwise180:
+ return QFPDFRotation::ClockWise180;
+ case QPdfDocumentRenderOptions::Rotation::Clockwise270:
+ return QFPDFRotation::CounterClockWise90;
+ }
+ Q_UNREACHABLE();
+ }
struct TextPosition {
QPointF position;
diff --git a/src/pdf/qpdfdocumentrenderoptions.h b/src/pdf/qpdfdocumentrenderoptions.h
index 0c935d425..af074d976 100644
--- a/src/pdf/qpdfdocumentrenderoptions.h
+++ b/src/pdf/qpdfdocumentrenderoptions.h
@@ -15,10 +15,10 @@ class QPdfDocumentRenderOptions
{
public:
enum class Rotation {
- Rotate0,
- Rotate90,
- Rotate180,
- Rotate270
+ None,
+ Clockwise90,
+ Clockwise180,
+ Clockwise270
};
enum class RenderFlag {
diff --git a/src/pdf/qpdfdocumentrenderoptions.qdoc b/src/pdf/qpdfdocumentrenderoptions.qdoc
index 11b5fb044..ad8e7bfdb 100644
--- a/src/pdf/qpdfdocumentrenderoptions.qdoc
+++ b/src/pdf/qpdfdocumentrenderoptions.qdoc
@@ -20,10 +20,10 @@ QT_BEGIN_NAMESPACE
This enum describes the rotation of the page for rendering.
- \value Rotate0 Do not rotate (the default)
- \value Rotate90 Rotate 90 degrees clockwise
- \value Rotate180 Rotate 180 degrees
- \value Rotate270 Rotate 270 degrees clockwise
+ \value None Do not rotate (the default)
+ \value Clockwise90 Rotate 90 degrees clockwise
+ \value Clockwise180 Rotate 180 degrees
+ \value Clockwise270 Rotate 270 degrees clockwise
\sa QPdfDocument::render()
*/
diff --git a/src/pdf/qpdflink.cpp b/src/pdf/qpdflink.cpp
index 6429f9f75..0c2867086 100644
--- a/src/pdf/qpdflink.cpp
+++ b/src/pdf/qpdflink.cpp
@@ -5,6 +5,7 @@
#include "qpdflink_p.h"
#include "qpdflinkmodel_p.h"
#include <QGuiApplication>
+#include <QDebug>
QT_BEGIN_NAMESPACE
@@ -151,12 +152,11 @@ QList<QRectF> QPdfLink::rectangles() const
*/
QString QPdfLink::toString() const
{
- static const QString format = QPdfLinkModel::tr("page %1 location %2,%3 zoom %4");
- return d->page > 0 ? format.arg(QString::number(d->page),
- QString::number(d->location.x()),
- QString::number(d->location.y()),
- QString::number(d->zoom))
- : d->url.toString();
+ if (d->page <= 0)
+ return d->url.toString();
+ return QPdfLinkModel::tr("Page %1 location %2, %3 zoom %4")
+ .arg(d->page).arg(d->location.x(), 0, 'f', 1).arg(d->location.y(), 0, 'f', 1)
+ .arg(d->zoom, 0, 'f', 0);
}
/*!
@@ -168,6 +168,7 @@ void QPdfLink::copyToClipboard(QClipboard::Mode mode) const
QGuiApplication::clipboard()->setText(toString(), mode);
}
+#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QPdfLink &link)
{
QDebugStateSaver saver(dbg);
@@ -181,6 +182,7 @@ QDebug operator<<(QDebug dbg, const QPdfLink &link)
dbg << ')';
return dbg;
}
+#endif
QT_END_NAMESPACE
diff --git a/src/pdf/qpdflink.h b/src/pdf/qpdflink.h
index 637801512..63389afe6 100644
--- a/src/pdf/qpdflink.h
+++ b/src/pdf/qpdflink.h
@@ -5,7 +5,6 @@
#define QPDFLINK_H
#include <QtPdf/qtpdfglobal.h>
-#include <QtCore/qdebug.h>
#include <QtCore/qlist.h>
#include <QtCore/qobject.h>
#include <QtCore/qpoint.h>
@@ -15,6 +14,7 @@
QT_BEGIN_NAMESPACE
+class QDebug;
class QPdfLinkPrivate;
class QPdfLink
@@ -67,7 +67,9 @@ private: // storage
};
Q_DECLARE_SHARED(QPdfLink)
+#ifndef QT_NO_DEBUG_STREAM
Q_PDF_EXPORT QDebug operator<<(QDebug, const QPdfLink &);
+#endif
QT_END_NAMESPACE
diff --git a/src/pdf/qpdflinkmodel.cpp b/src/pdf/qpdflinkmodel.cpp
index 4fc9d8aea..0a8b1e812 100644
--- a/src/pdf/qpdflinkmodel.cpp
+++ b/src/pdf/qpdflinkmodel.cpp
@@ -2,8 +2,8 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qpdflink_p.h"
+#include "qpdflinkmodel.h"
#include "qpdflinkmodel_p.h"
-#include "qpdflinkmodel_p_p.h"
#include "qpdfdocument_p.h"
#include "third_party/pdfium/public/fpdf_doc.h"
@@ -18,7 +18,7 @@ Q_LOGGING_CATEGORY(qLcLink, "qt.pdf.links")
/*!
\class QPdfLinkModel
- \since 5.15
+ \since 6.6
\inmodule QtPdf
\inherits QAbstractListModel
@@ -37,18 +37,20 @@ Q_LOGGING_CATEGORY(qLcLink, "qt.pdf.links")
\value Page If the link is an internal link, the page number to which the link should jump; otherwise \c {-1}.
\value Location If the link is an internal link, the location on the page to which the link should jump.
\value Zoom If the link is an internal link, the suggested zoom level on the destination page.
- \omitvalue _Count
+ \omitvalue NRoles
*/
/*!
Constructs a new link model with parent object \a parent.
*/
QPdfLinkModel::QPdfLinkModel(QObject *parent)
- : QAbstractListModel(*(new QPdfLinkModelPrivate()), parent)
+ : QAbstractListModel(parent),
+ d_ptr{std::make_unique<QPdfLinkModelPrivate>(this)}
{
+ Q_D(QPdfLinkModel);
QMetaEnum rolesMetaEnum = metaObject()->enumerator(metaObject()->indexOfEnumerator("Role"));
- for (int r = Qt::UserRole; r < int(Role::_Count); ++r)
- m_roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower());
+ for (int r = Qt::UserRole; r < int(Role::NRoles); ++r)
+ d->roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower());
}
/*!
@@ -58,7 +60,8 @@ QPdfLinkModel::~QPdfLinkModel() {}
QHash<int, QByteArray> QPdfLinkModel::roleNames() const
{
- return m_roleNames;
+ Q_D(const QPdfLinkModel);
+ return d->roleNames;
}
/*!
@@ -68,7 +71,7 @@ int QPdfLinkModel::rowCount(const QModelIndex &parent) const
{
Q_D(const QPdfLinkModel);
Q_UNUSED(parent);
- return d->links.count();
+ return d->links.size();
}
/*!
@@ -91,7 +94,7 @@ QVariant QPdfLinkModel::data(const QModelIndex &index, int role) const
return link.location();
case Role::Zoom:
return link.zoom();
- case Role::_Count:
+ case Role::NRoles:
break;
}
if (role == Qt::DisplayRole)
@@ -101,7 +104,7 @@ QVariant QPdfLinkModel::data(const QModelIndex &index, int role) const
/*!
\property QPdfLinkModel::document
- \brief the document to load links from
+ \brief The document to load links from.
*/
QPdfDocument *QPdfLinkModel::document() const
{
@@ -127,7 +130,7 @@ void QPdfLinkModel::setDocument(QPdfDocument *document)
/*!
\property QPdfLinkModel::page
- \brief the page to load links from
+ \brief The page to load links from.
*/
int QPdfLinkModel::page() const
{
@@ -146,8 +149,22 @@ void QPdfLinkModel::setPage(int page)
d->update();
}
-QPdfLinkModelPrivate::QPdfLinkModelPrivate() : QAbstractItemModelPrivate()
+/*!
+ Returns a \l {QPdfLink::isValid()}{valid} link if found under the \a point
+ (given in units of points, 1/72 of an inch), or an invalid link if it is
+ not found. In other words, this function is useful for picking, to handle
+ mouse click or hover.
+*/
+QPdfLink QPdfLinkModel::linkAt(QPointF point) const
{
+ Q_D(const QPdfLinkModel);
+ for (const auto &link : std::as_const(d->links)) {
+ for (const auto &rect : link.rectangles()) {
+ if (rect.contains(point))
+ return link;
+ }
+ }
+ return {};
}
void QPdfLinkModelPrivate::update()
@@ -162,7 +179,6 @@ void QPdfLinkModelPrivate::update()
qCWarning(qLcLink) << "failed to load page" << page;
return;
}
- double pageHeight = FPDF_GetPageHeight(pdfPage);
q->beginResetModel();
links.clear();
@@ -187,8 +203,28 @@ void QPdfLinkModelPrivate::update()
std::swap(rect.bottom, rect.top);
QPdfLink linkData;
- linkData.d->rects << QRectF(rect.left, pageHeight - rect.top,
- rect.right - rect.left, rect.top - rect.bottom);
+ // Use quad points if present; otherwise use the rect.
+ if (int quadPointsCount = FPDFLink_CountQuadPoints(linkAnnot) > 0) {
+ for (int i = 0; i < quadPointsCount; ++i) {
+ FS_QUADPOINTSF point;
+ if (FPDFLink_GetQuadPoints(linkAnnot, i, &point)) {
+ // Quadpoints are counter clockwise from bottom left (x1, y1)
+ QPolygonF poly;
+ poly << QPointF(point.x1, point.y1);
+ poly << QPointF(point.x2, point.y2);
+ poly << QPointF(point.x3, point.y3);
+ poly << QPointF(point.x4, point.y4);
+ QRectF bounds = poly.boundingRect();
+ bounds = document->d->mapPageToView(pdfPage, bounds.left(), bounds.top(), bounds.right(), bounds.bottom());
+ qCDebug(qLcLink) << "quadpoints" << i << "of" << quadPointsCount << ":" << poly << "mapped bounds" << bounds;
+ linkData.d->rects << bounds;
+ // QPdfLink could store polygons rather than rects, to get the benefit of quadpoints;
+ // so far we didn't bother. It would be an API change, and we'd need to use Shapes in PdfLinkDelegate.qml
+ }
+ }
+ } else {
+ linkData.d->rects << document->d->mapPageToView(pdfPage, rect.left, rect.top, rect.right, rect.bottom);
+ }
FPDF_DEST dest = FPDFLink_GetDest(doc, linkAnnot);
FPDF_ACTION action = FPDFLink_GetAction(linkAnnot);
switch (FPDFAction_GetType(action)) {
@@ -196,7 +232,7 @@ void QPdfLinkModelPrivate::update()
case PDFACTION_GOTO: {
linkData.d->page = FPDFDest_GetDestPageIndex(doc, dest);
if (linkData.d->page < 0) {
- qCWarning(qLcLink) << "skipping link with invalid page number";
+ qCWarning(qLcLink) << "skipping link with invalid page number" << linkData.d->page;
continue; // while enumerating links
}
FPDF_BOOL hasX, hasY, hasZoom;
@@ -207,7 +243,7 @@ void QPdfLinkModelPrivate::update()
break; // at least we got a page number, so the link will jump there
}
if (hasX && hasY)
- linkData.d->location = QPointF(x, pageHeight - y);
+ linkData.d->location = document->d->mapPageToView(pdfPage, x, y);
if (hasZoom)
linkData.d->zoom = zoom;
break;
@@ -270,7 +306,7 @@ void QPdfLinkModelPrivate::update()
double left, top, right, bottom;
bool success = FPDFLink_GetRect(webLinks, i, r, &left, &top, &right, &bottom);
if (success) {
- linkData.d->rects << QRectF(left, pageHeight - top, right - left, top - bottom);
+ linkData.d->rects << document->d->mapPageToView(pdfPage, left, top, right, bottom);
links << linkData;
}
}
diff --git a/src/pdf/qpdflinkmodel.h b/src/pdf/qpdflinkmodel.h
new file mode 100644
index 000000000..be2ce890c
--- /dev/null
+++ b/src/pdf/qpdflinkmodel.h
@@ -0,0 +1,67 @@
+// Copyright (C) 2020 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#ifndef QPDFLINKMODEL_H
+#define QPDFLINKMODEL_H
+
+#include <QtPdf/qtpdfglobal.h>
+#include <QtPdf/qpdfdocument.h>
+#include <QtPdf/qpdflink.h>
+
+#include <QtCore/QAbstractListModel>
+
+#include <memory>
+
+QT_BEGIN_NAMESPACE
+
+class QPdfLinkModelPrivate;
+
+class Q_PDF_EXPORT QPdfLinkModel : public QAbstractListModel
+{
+ Q_OBJECT
+ Q_PROPERTY(QPdfDocument *document READ document WRITE setDocument NOTIFY documentChanged)
+ Q_PROPERTY(int page READ page WRITE setPage NOTIFY pageChanged)
+
+public:
+ enum class Role {
+ Link = Qt::UserRole,
+ Rectangle,
+ Url,
+ Page,
+ Location,
+ Zoom,
+ NRoles
+ };
+ Q_ENUM(Role)
+ explicit QPdfLinkModel(QObject *parent = nullptr);
+ ~QPdfLinkModel() override;
+
+ QPdfDocument *document() const;
+
+ QHash<int, QByteArray> roleNames() const override;
+ int rowCount(const QModelIndex &parent) const override;
+ QVariant data(const QModelIndex &index, int role) const override;
+
+ int page() const;
+
+ QPdfLink linkAt(QPointF point) const;
+
+public Q_SLOTS:
+ void setDocument(QPdfDocument *document);
+ void setPage(int page);
+
+Q_SIGNALS:
+ void documentChanged();
+ void pageChanged(int page);
+
+private Q_SLOTS:
+ void onStatusChanged(QPdfDocument::Status status);
+
+private:
+ Q_DECLARE_PRIVATE(QPdfLinkModel)
+ const std::unique_ptr<QPdfLinkModelPrivate> d_ptr;
+};
+
+QT_END_NAMESPACE
+
+#endif // QPDFLINKMODEL_H
diff --git a/src/pdf/qpdflinkmodel_p.h b/src/pdf/qpdflinkmodel_p.h
index 48c973d11..ba46a6e00 100644
--- a/src/pdf/qpdflinkmodel_p.h
+++ b/src/pdf/qpdflinkmodel_p.h
@@ -15,58 +15,26 @@
// We mean it.
//
-#include "qtpdfglobal.h"
-#include "qpdfdocument.h"
-
-#include <QObject>
-#include <QAbstractListModel>
+#include "qpdflinkmodel.h"
+#include <private/qabstractitemmodel_p.h>
QT_BEGIN_NAMESPACE
-class QPdfLinkModelPrivate;
-
-class Q_PDF_EXPORT QPdfLinkModel : public QAbstractListModel
+class QPdfLinkModelPrivate
{
- Q_OBJECT
- Q_PROPERTY(QPdfDocument *document READ document WRITE setDocument NOTIFY documentChanged)
- Q_PROPERTY(int page READ page WRITE setPage NOTIFY pageChanged)
+ QPdfLinkModel *q_ptr;
+ Q_DECLARE_PUBLIC(QPdfLinkModel)
public:
- enum class Role : int {
- Link = Qt::UserRole,
- Rectangle,
- Url,
- Page,
- Location,
- Zoom,
- _Count
- };
- Q_ENUM(Role)
- explicit QPdfLinkModel(QObject *parent = nullptr);
- ~QPdfLinkModel();
-
- QPdfDocument *document() const;
-
- QHash<int, QByteArray> roleNames() const override;
- int rowCount(const QModelIndex &parent) const override;
- QVariant data(const QModelIndex &index, int role) const override;
-
- int page() const;
-
-public Q_SLOTS:
- void setDocument(QPdfDocument *document);
- void setPage(int page);
-
-Q_SIGNALS:
- void documentChanged();
- void pageChanged(int page);
+ explicit QPdfLinkModelPrivate(QPdfLinkModel *qq)
+ : q_ptr(qq) {}
-private Q_SLOTS:
- void onStatusChanged(QPdfDocument::Status status);
+ void update();
-private:
- QHash<int, QByteArray> m_roleNames;
- Q_DECLARE_PRIVATE(QPdfLinkModel)
+ QHash<int, QByteArray> roleNames;
+ QPdfDocument *document = nullptr;
+ QList<QPdfLink> links;
+ int page = 0;
};
QT_END_NAMESPACE
diff --git a/src/pdf/qpdflinkmodel_p_p.h b/src/pdf/qpdflinkmodel_p_p.h
deleted file mode 100644
index ba553d41f..000000000
--- a/src/pdf/qpdflinkmodel_p_p.h
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright (C) 2020 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
-
-#ifndef QPDFLINKMODEL_P_P_H
-#define QPDFLINKMODEL_P_P_H
-
-//
-// W A R N I N G
-// -------------
-//
-// This file is not part of the Qt API. It exists purely as an
-// implementation detail. This header file may change from version to
-// version without notice, or even be removed.
-//
-// We mean it.
-//
-
-#include "qpdflinkmodel_p.h"
-#include "qpdflink.h"
-#include <private/qabstractitemmodel_p.h>
-
-#include "third_party/pdfium/public/fpdfview.h"
-
-#include <QUrl>
-
-QT_BEGIN_NAMESPACE
-
-class QPdfLinkModelPrivate: public QAbstractItemModelPrivate
-{
- Q_DECLARE_PUBLIC(QPdfLinkModel)
-
-public:
- QPdfLinkModelPrivate();
-
- void update();
-
- QPdfDocument *document = nullptr;
- QList<QPdfLink> links;
- int page = 0;
-};
-
-QT_END_NAMESPACE
-
-#endif // QPDFLINKMODEL_P_P_H
diff --git a/src/pdf/qpdfpagenavigator.cpp b/src/pdf/qpdfpagenavigator.cpp
index 9e807e5cd..e077e2184 100644
--- a/src/pdf/qpdfpagenavigator.cpp
+++ b/src/pdf/qpdfpagenavigator.cpp
@@ -62,7 +62,7 @@ QPdfPageNavigator::~QPdfPageNavigator()
*/
void QPdfPageNavigator::forward()
{
- if (d->currentHistoryIndex >= d->pageHistory.count() - 1)
+ if (d->currentHistoryIndex >= d->pageHistory.size() - 1)
return;
const bool backAvailableWas = backAvailable();
const bool forwardAvailableWas = forwardAvailable();
@@ -122,7 +122,7 @@ void QPdfPageNavigator::back()
*/
int QPdfPageNavigator::currentPage() const
{
- if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
return -1; // only until ctor or clear() runs
return d->pageHistory.at(d->currentHistoryIndex)->page;
}
@@ -136,7 +136,7 @@ int QPdfPageNavigator::currentPage() const
*/
QPointF QPdfPageNavigator::currentLocation() const
{
- if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
return QPointF();
return d->pageHistory.at(d->currentHistoryIndex)->location;
}
@@ -149,14 +149,14 @@ QPointF QPdfPageNavigator::currentLocation() const
*/
qreal QPdfPageNavigator::currentZoom() const
{
- if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
return 1;
return d->pageHistory.at(d->currentHistoryIndex)->zoom;
}
QPdfLink QPdfPageNavigator::currentLink() const
{
- if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
return QPdfLink();
return QPdfLink(d->pageHistory.at(d->currentHistoryIndex).data());
}
@@ -195,9 +195,9 @@ void QPdfPageNavigator::jump(QPdfLink destination)
const bool forwardAvailableWas = forwardAvailable();
if (!d->changing) {
if (d->currentHistoryIndex >= 0 && forwardAvailableWas)
- d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.count() - d->currentHistoryIndex - 1);
+ d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.size() - d->currentHistoryIndex - 1);
d->pageHistory.append(destination.d);
- d->currentHistoryIndex = d->pageHistory.count() - 1;
+ d->currentHistoryIndex = d->pageHistory.size() - 1;
}
if (zoomChange)
emit currentZoomChanged(currentZoom());
@@ -207,9 +207,9 @@ void QPdfPageNavigator::jump(QPdfLink destination)
emit currentLocationChanged(currentLocation());
if (d->changing)
return;
- if (!backAvailableWas)
+ if (backAvailableWas != backAvailable())
emit backAvailableChanged(backAvailable());
- if (forwardAvailableWas)
+ if (forwardAvailableWas != forwardAvailable())
emit forwardAvailableChanged(forwardAvailable());
emit jumped(currentLink());
qCDebug(qLcNav) << "push: index" << d->currentHistoryIndex << destination << "-> history" <<
@@ -251,9 +251,9 @@ void QPdfPageNavigator::jump(int page, const QPointF &location, qreal zoom)
const bool forwardAvailableWas = forwardAvailable();
if (!d->changing) {
if (d->currentHistoryIndex >= 0 && forwardAvailableWas)
- d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.count() - d->currentHistoryIndex - 1);
+ d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.size() - d->currentHistoryIndex - 1);
d->pageHistory.append(QExplicitlySharedDataPointer<QPdfLinkPrivate>(new QPdfLinkPrivate(page, location, zoom)));
- d->currentHistoryIndex = d->pageHistory.count() - 1;
+ d->currentHistoryIndex = d->pageHistory.size() - 1;
}
if (zoomChange)
emit currentZoomChanged(currentZoom());
@@ -263,9 +263,9 @@ void QPdfPageNavigator::jump(int page, const QPointF &location, qreal zoom)
emit currentLocationChanged(currentLocation());
if (d->changing)
return;
- if (!backAvailableWas)
+ if (backAvailableWas != backAvailable())
emit backAvailableChanged(backAvailable());
- if (forwardAvailableWas)
+ if (forwardAvailableWas != forwardAvailable())
emit forwardAvailableChanged(forwardAvailable());
emit jumped(currentLink());
qCDebug(qLcNav) << "push: index" << d->currentHistoryIndex << "page" << page
@@ -293,7 +293,7 @@ void QPdfPageNavigator::jump(int page, const QPointF &location, qreal zoom)
*/
void QPdfPageNavigator::update(int page, const QPointF &location, qreal zoom)
{
- if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.count())
+ if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
return;
int currentPageWas = currentPage();
QPointF currentLocationWas = currentLocation();
@@ -340,7 +340,7 @@ bool QPdfPageNavigator::backAvailable() const
*/
bool QPdfPageNavigator::forwardAvailable() const
{
- return d->currentHistoryIndex < d->pageHistory.count() - 1;
+ return d->currentHistoryIndex < d->pageHistory.size() - 1;
}
/*!
diff --git a/src/pdf/qpdfpagerenderer.cpp b/src/pdf/qpdfpagerenderer.cpp
index e46261817..771fc67ef 100644
--- a/src/pdf/qpdfpagerenderer.cpp
+++ b/src/pdf/qpdfpagerenderer.cpp
@@ -282,7 +282,7 @@ quint64 QPdfPageRenderer::requestPage(int pageNumber, QSize imageSize,
if (!d_ptr->m_document || d_ptr->m_document->status() != QPdfDocument::Status::Ready)
return 0;
- for (const auto &request : qAsConst(d_ptr->m_pendingRequests)) {
+ for (const auto &request : std::as_const(d_ptr->m_pendingRequests)) {
if (request.pageNumber == pageNumber
&& request.imageSize == imageSize
&& request.options == options)
diff --git a/src/pdf/qpdfsearchmodel.cpp b/src/pdf/qpdfsearchmodel.cpp
index bead3165f..a81ae77dc 100644
--- a/src/pdf/qpdfsearchmodel.cpp
+++ b/src/pdf/qpdfsearchmodel.cpp
@@ -3,12 +3,11 @@
#include "qpdfdocument_p.h"
#include "qpdflink.h"
-#include "qpdflink_p.h"
#include "qpdfsearchmodel.h"
#include "qpdfsearchmodel_p.h"
-#include "third_party/pdfium/public/fpdf_doc.h"
#include "third_party/pdfium/public/fpdf_text.h"
+#include "third_party/pdfium/public/fpdfview.h"
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qloggingcategory.h>
@@ -20,7 +19,6 @@ Q_LOGGING_CATEGORY(qLcS, "qt.pdf.search")
static const int UpdateTimerInterval = 100;
static const int ContextChars = 64;
-static const double CharacterHitTolerance = 6.0;
/*!
\class QPdfSearchModel
@@ -47,7 +45,7 @@ static const double CharacterHitTolerance = 6.0;
\value Location The position of the search result on the page (QPointF).
\value ContextBefore The adjacent text on the page, before the search string (QString).
\value ContextAfter The adjacent text on the page, after the search string (QString).
- \omitvalue _Count
+ \omitvalue NRoles
\sa QPdfLink
*/
@@ -59,13 +57,17 @@ QPdfSearchModel::QPdfSearchModel(QObject *parent)
: QAbstractListModel(*(new QPdfSearchModelPrivate()), parent)
{
QMetaEnum rolesMetaEnum = metaObject()->enumerator(metaObject()->indexOfEnumerator("Role"));
- for (int r = Qt::UserRole; r < int(Role::_Count); ++r) {
+ for (int r = Qt::UserRole; r < int(Role::NRoles); ++r) {
QByteArray roleName = QByteArray(rolesMetaEnum.valueToKey(r));
if (roleName.isEmpty())
continue;
roleName[0] = QChar::toLower(roleName[0]);
m_roleNames.insert(r, roleName);
}
+ connect(this, &QAbstractListModel::dataChanged, this, &QPdfSearchModel::countChanged);
+ connect(this, &QAbstractListModel::modelReset, this, &QPdfSearchModel::countChanged);
+ connect(this, &QAbstractListModel::rowsRemoved, this, &QPdfSearchModel::countChanged);
+ connect(this, &QAbstractListModel::rowsInserted, this, &QPdfSearchModel::countChanged);
}
/*!
@@ -113,7 +115,7 @@ QVariant QPdfSearchModel::data(const QModelIndex &index, int role) const
return d->searchResults[pi.page][pi.index].contextBefore();
case Role::ContextAfter:
return d->searchResults[pi.page][pi.index].contextAfter();
- case Role::_Count:
+ case Role::NRoles:
break;
}
if (role == Qt::DisplayRole) {
@@ -125,6 +127,16 @@ QVariant QPdfSearchModel::data(const QModelIndex &index, int role) const
return QVariant();
}
+/*!
+ \since 6.8
+ \property QPdfSearchModel::count
+ \brief the number of search results found
+*/
+int QPdfSearchModel::count() const
+{
+ return rowCount(QModelIndex());
+}
+
void QPdfSearchModel::updatePage(int page)
{
Q_D(QPdfSearchModel);
@@ -161,7 +173,7 @@ QList<QPdfLink> QPdfSearchModel::resultsOnPage(int page) const
{
Q_D(const QPdfSearchModel);
const_cast<QPdfSearchModelPrivate *>(d)->doSearch(page);
- if (d->searchResults.count() <= page)
+ if (d->searchResults.size() <= page)
return {};
return d->searchResults[page];
}
@@ -174,7 +186,7 @@ QPdfLink QPdfSearchModel::resultAtIndex(int index) const
{
Q_D(const QPdfSearchModel);
const auto pi = const_cast<QPdfSearchModelPrivate*>(d)->pageAndIndexForResult(index);
- if (pi.page < 0)
+ if (pi.page < 0 || index < 0)
return {};
return d->searchResults[pi.page][pi.index];
}
@@ -195,6 +207,10 @@ void QPdfSearchModel::setDocument(QPdfDocument *document)
if (d->document == document)
return;
+ disconnect(d->documentConnection);
+ d->documentConnection = connect(document, &QPdfDocument::pageCountChanged, this,
+ [this]() { d_func()->clearResults(); });
+
d->document = document;
d->clearResults();
emit documentChanged();
@@ -207,7 +223,7 @@ void QPdfSearchModel::timerEvent(QTimerEvent *event)
return;
if (!d->document || d->nextPageToUpdate >= d->document->pageCount()) {
if (d->document)
- qCDebug(qLcS) << "done updating search results on" << d->searchResults.count() << "pages";
+ qCDebug(qLcS) << "done updating search results on" << d->searchResults.size() << "pages";
killTimer(d->updateTimerId);
d->updateTimerId = -1;
}
@@ -234,7 +250,7 @@ void QPdfSearchModelPrivate::clearResults()
bool QPdfSearchModelPrivate::doSearch(int page)
{
- if (page < 0 || page >= pagesSearched.count() || searchString.isEmpty())
+ if (page < 0 || page >= pagesSearched.size() || searchString.isEmpty())
return false;
if (pagesSearched[page])
return true;
@@ -248,7 +264,6 @@ bool QPdfSearchModelPrivate::doSearch(int page)
qWarning() << "failed to load page" << page;
return false;
}
- double pageHeight = FPDF_GetPageHeight(pdfPage);
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(pdfPage);
if (!textPage) {
qWarning() << "failed to load text of page" << page;
@@ -257,6 +272,7 @@ bool QPdfSearchModelPrivate::doSearch(int page)
}
FPDF_SCHHANDLE sh = FPDFText_FindStart(textPage, searchString.utf16(), 0, 0);
QList<QPdfLink> newSearchResults;
+ constexpr double CharacterHitTolerance = 6.0;
while (FPDFText_FindNext(sh)) {
int idx = FPDFText_GetSchResultIndex(sh);
int count = FPDFText_GetSchCount(sh);
@@ -265,9 +281,12 @@ bool QPdfSearchModelPrivate::doSearch(int page)
int startIndex = -1;
int endIndex = -1;
for (int r = 0; r < rectCount; ++r) {
+ // get bounding box of search result in page coordinates
double left, top, right, bottom;
FPDFText_GetRect(textPage, r, &left, &top, &right, &bottom);
- rects << QRectF(left, pageHeight - top, right - left, top - bottom);
+ // deal with any internal PDF transforms and
+ // convert to the 1x (pixels = points) 4th-quadrant coordinate system
+ rects << document->d->mapPageToView(pdfPage, left, top, right, bottom);
if (r == 0) {
startIndex = FPDFText_GetCharIndexAtPos(textPage, left, top,
CharacterHitTolerance, CharacterHitTolerance);
@@ -276,7 +295,8 @@ bool QPdfSearchModelPrivate::doSearch(int page)
endIndex = FPDFText_GetCharIndexAtPos(textPage, right, top,
CharacterHitTolerance, CharacterHitTolerance);
}
- qCDebug(qLcS) << rects.last() << "char idx" << startIndex << "->" << endIndex;
+ qCDebug(qLcS) << rects.last() << "char idx" << startIndex << "->" << endIndex
+ << "from page rect" << left << top << right << bottom;
}
QString contextBefore, contextAfter;
if (startIndex >= 0 || endIndex >= 0) {
@@ -298,7 +318,7 @@ bool QPdfSearchModelPrivate::doSearch(int page)
if (si < 0)
qWarning() << "search string" << searchString << "not found in context" << context;
contextBefore = context.mid(0, si);
- contextAfter = context.mid(si + searchString.length());
+ contextAfter = context.mid(si + searchString.size());
}
}
if (!rects.isEmpty())
@@ -308,15 +328,15 @@ bool QPdfSearchModelPrivate::doSearch(int page)
FPDFText_ClosePage(textPage);
FPDF_ClosePage(pdfPage);
qCDebug(qLcS) << searchString << "took" << timer.elapsed() << "ms to find"
- << newSearchResults.count() << "results on page" << page;
+ << newSearchResults.size() << "results on page" << page;
pagesSearched[page] = true;
searchResults[page] = newSearchResults;
- if (newSearchResults.count() > 0) {
+ if (newSearchResults.size() > 0) {
int rowsBefore = rowsBeforePage(page);
- qCDebug(qLcS) << "from row" << rowsBefore << "rowCount" << rowCountSoFar << "increasing by" << newSearchResults.count();
- rowCountSoFar += newSearchResults.count();
- q->beginInsertRows(QModelIndex(), rowsBefore, rowsBefore + newSearchResults.count() - 1);
+ qCDebug(qLcS) << "from row" << rowsBefore << "rowCount" << rowCountSoFar << "increasing by" << newSearchResults.size();
+ rowCountSoFar += newSearchResults.size();
+ q->beginInsertRows(QModelIndex(), rowsBefore, rowsBefore + newSearchResults.size() - 1);
q->endInsertRows();
}
return true;
@@ -332,7 +352,7 @@ QPdfSearchModelPrivate::PageAndIndex QPdfSearchModelPrivate::pageAndIndexForResu
for (int page = 0; page < pageCount; ++page) {
if (!pagesSearched[page])
doSearch(page);
- totalSoFar += searchResults[page].count();
+ totalSoFar += searchResults[page].size();
if (totalSoFar > resultIndex)
return {page, resultIndex - previousTotalSoFar};
previousTotalSoFar = totalSoFar;
@@ -344,7 +364,7 @@ int QPdfSearchModelPrivate::rowsBeforePage(int page)
{
int ret = 0;
for (int i = 0; i < page; ++i)
- ret += searchResults[i].count();
+ ret += searchResults[i].size();
return ret;
}
diff --git a/src/pdf/qpdfsearchmodel.h b/src/pdf/qpdfsearchmodel.h
index 70342d691..04f8b9140 100644
--- a/src/pdf/qpdfsearchmodel.h
+++ b/src/pdf/qpdfsearchmodel.h
@@ -19,6 +19,7 @@ class Q_PDF_EXPORT QPdfSearchModel : public QAbstractListModel
Q_OBJECT
Q_PROPERTY(QPdfDocument *document READ document WRITE setDocument NOTIFY documentChanged)
Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY searchStringChanged)
+ Q_PROPERTY(int count READ count NOTIFY countChanged REVISION(6, 8) FINAL)
public:
enum class Role : int {
@@ -27,7 +28,7 @@ public:
Location,
ContextBefore,
ContextAfter,
- _Count
+ NRoles
};
Q_ENUM(Role)
QPdfSearchModel() : QPdfSearchModel(nullptr) {}
@@ -44,6 +45,8 @@ public:
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
+ int count() const;
+
public Q_SLOTS:
void setSearchString(const QString &searchString);
void setDocument(QPdfDocument *document);
@@ -51,6 +54,7 @@ public Q_SLOTS:
Q_SIGNALS:
void documentChanged();
void searchStringChanged();
+ Q_REVISION(6, 8) void countChanged();
protected:
void updatePage(int page);
diff --git a/src/pdf/qpdfsearchmodel_p.h b/src/pdf/qpdfsearchmodel_p.h
index 4922c81e9..5ffa08f5d 100644
--- a/src/pdf/qpdfsearchmodel_p.h
+++ b/src/pdf/qpdfsearchmodel_p.h
@@ -45,6 +45,8 @@ public:
int rowCountSoFar = 0;
int updateTimerId = -1;
int nextPageToUpdate = 0;
+
+ QMetaObject::Connection documentConnection;
};
QT_END_NAMESPACE
diff --git a/src/pdf/qtpdfglobal.h b/src/pdf/qtpdfglobal.h
index d38eafaab..2d0900029 100644
--- a/src/pdf/qtpdfglobal.h
+++ b/src/pdf/qtpdfglobal.h
@@ -5,24 +5,7 @@
#define QTPDFGLOBAL_H
#include <QtCore/qglobal.h>
-
-QT_BEGIN_NAMESPACE
-
-#ifndef Q_PDF_EXPORT
-# ifndef QT_STATIC
-# if defined(QT_BUILD_PDF_LIB)
-# define Q_PDF_EXPORT Q_DECL_EXPORT
-# else
-# define Q_PDF_EXPORT Q_DECL_IMPORT
-# endif
-# else
-# define Q_PDF_EXPORT
-# endif
-#endif
-
-#define Q_PDF_PRIVATE_EXPORT Q_PDF_EXPORT
-
-QT_END_NAMESPACE
+#include <QtPdf/qtpdfexports.h>
#endif // QTPDFGLOBAL_H