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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ build
.promptx/
.spec-workflow/
.cursorindexingignore
.cursor/
5 changes: 1 addition & 4 deletions reader/browser/BrowserWord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ void BrowserWord::setSelectable(bool enable)

QRectF BrowserWord::boundingRect() const
{
// qCDebug(appLog) << "BrowserWord::boundingRect() - Calculating bounding rectangle";
QRectF rect = QRectF(m_word.boundingBox.x() * m_scaleFactor - 1, m_word.boundingBox.y() * m_scaleFactor - 1, m_word.boundingBox.width() * m_scaleFactor + 2, m_word.boundingBox.height() * m_scaleFactor + 2);
// qCDebug(appLog) << "BrowserWord::boundingRect() - Bounding rectangle:" << rect;
return rect;
return QRectF(m_word.boundingBox.x() * m_scaleFactor - 1, m_word.boundingBox.y() * m_scaleFactor - 1, m_word.boundingBox.width() * m_scaleFactor + 2, m_word.boundingBox.height() * m_scaleFactor + 2);
}

QRectF BrowserWord::boundingBox() const
Expand Down
77 changes: 74 additions & 3 deletions reader/document/XpsDocumentAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

#include "XpsDocumentAdapter.h"

#include "XpsTextExtractor.h"
#include "ddlog.h"

Check warning on line 9 in reader/document/XpsDocumentAdapter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ddlog.h" not found.

#include <QByteArray>

Check warning on line 11 in reader/document/XpsDocumentAdapter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include <algorithm>

Check warning on line 18 in reader/document/XpsDocumentAdapter.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#ifdef signals
#pragma push_macro("signals")
#undef signals
Expand Down Expand Up @@ -795,8 +798,63 @@

QString XpsPageAdapter::text(const QRectF &rect) const
{
Q_UNUSED(rect)
return QString();
if (!m_document || rect.isEmpty()) {
return QString();
}

// 直接调用文本提取器,避免通过非const的words()方法
QString filePath = m_document->filePath();
if (filePath.isEmpty()) {
qCWarning(appLog) << "XpsPageAdapter::text() - Empty file path";
return QString();
}

QList<Word> allWords = XpsTextExtractor::extractWords(filePath, m_pageIndex);
if (allWords.isEmpty()) {
return QString();
}

// 筛选在矩形区域内的words并按位置排序
QList<Word> selectedWords;
for (const Word &word : allWords) {
if (rect.intersects(word.boundingBox)) {
selectedWords.append(word);
}
}

if (selectedWords.isEmpty()) {
return QString();
}

// 按位置排序(从上到下,从左到右)
std::sort(selectedWords.begin(), selectedWords.end(), [](const Word &a, const Word &b) {
const double yThreshold = 5.0; // Y坐标容差
if (qAbs(a.boundingBox.y() - b.boundingBox.y()) > yThreshold) {
return a.boundingBox.y() < b.boundingBox.y();
}
return a.boundingBox.x() < b.boundingBox.x();
});

// 拼接文本,处理换行
QStringList textParts;
qreal lastY = selectedWords.first().boundingBox.y();
const double yThreshold = 5.0;

for (const Word &word : selectedWords) {
// 检测换行
if (qAbs(word.boundingBox.y() - lastY) > yThreshold && !textParts.isEmpty()) {
// 如果Y坐标变化较大,可能是新行,添加换行符(可选)
// 这里先不添加,让调用者处理
}
textParts.append(word.text);
lastY = word.boundingBox.y();
}

QString result = textParts.join(QStringLiteral(" "));

qCDebug(appLog) << "XpsPageAdapter::text() - Extracted text for rect" << rect << ":" << result;

return result.simplified();
}

QVector<PageSection> XpsPageAdapter::search(const QString &text, bool matchCase, bool wholeWords) const
Expand All @@ -814,7 +872,20 @@

QList<Word> XpsPageAdapter::words()
{
return {};
if (!m_document) {
qCWarning(appLog) << "XpsPageAdapter::words() - Invalid document";
return {};
}

// 获取文档文件路径
QString filePath = m_document->filePath();
if (filePath.isEmpty()) {
qCWarning(appLog) << "XpsPageAdapter::words() - Empty file path";
return {};
}

// 使用文本提取器提取文本
return XpsTextExtractor::extractWords(filePath, m_pageIndex);
}

} // namespace deepin_reader
Expand Down
2 changes: 2 additions & 0 deletions reader/document/XpsDocumentAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class XpsDocumentAdapter : public Document
QImage renderPage(int pageIndex, int width, int height, const QRect &slice) const;
QSizeF pageSize(int pageIndex) const;

QString filePath() const { return m_filePath; }

private:
class Handle;

Expand Down
Loading