From c8c42ccaf06ca988ec196d4d15ba1d1db5cd496c Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Thu, 23 Sep 2021 21:02:56 +0200 Subject: [PATCH] DocDiff application: page added/removed --- Pdf4QtLib/sources/pdfdiff.cpp | 86 ++++++++++++++++++++++++++++++++++- Pdf4QtLib/sources/pdfdiff.h | 19 +++++--- 2 files changed, 97 insertions(+), 8 deletions(-) diff --git a/Pdf4QtLib/sources/pdfdiff.cpp b/Pdf4QtLib/sources/pdfdiff.cpp index 9ba2f18..6045bbb 100644 --- a/Pdf4QtLib/sources/pdfdiff.cpp +++ b/Pdf4QtLib/sources/pdfdiff.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Jakub Melka +// Copyright (C) 2021 Jakub Melka // // This file is part of PDF4QT. // @@ -464,6 +464,68 @@ void PDFDiff::performCompare(const std::vector& leftPrepared const bool isAdded = flags.testFlag(AlgorithmLCS::Added); const bool isRemoved = flags.testFlag(AlgorithmLCS::Removed); + const bool isReplaced = flags.testFlag(AlgorithmLCS::Replaced); + + Q_ASSERT(isAdded || isRemoved || isReplaced); + + // There are two cases. Some page content was replaced, or either + // page range was added, or page range was removed. + if (isReplaced) + { + for (auto it = range.first; it != range.second; ++i) + { + const AlgorithmLCS::SequenceItem& item = *it; + if (item.isReplaced()) + { + const bool isTextComparedAsVectorGraphics = m_options.testFlag(CompareTextsAsVector); + const PDFDiffPageContext& leftPageContext = leftPreparedPages[item.index1]; + const PDFDiffPageContext& rightPageContext = rightPreparedPages[item.index2]; + + auto pageLeft = m_leftDocument->getCatalog()->getPage(leftPageContext.pageIndex); + auto pageRight = m_rightDocument->getCatalog()->getPage(rightPageContext.pageIndex); + PDFReal epsilon = (calculateEpsilonForPage(pageLeft) + calculateEpsilonForPage(pageRight)) * 0.5; + + PDFDiffHelper::Differences differences = PDFDiffHelper::calculateDifferences(leftPageContext.graphicPieces, rightPageContext.graphicPieces, epsilon); + + for (const PDFDiffHelper::GraphicPieceInfo& info : differences.left) + { + /* + switch (info.type) + { + + }*/ + } + } + + if (item.isAdded()) + { + const PDFDiffPageContext& rightPageContext = rightPreparedPages[item.index2]; + m_result.addPageAdded(rightPageContext.pageIndex); + } + if (item.isRemoved()) + { + const PDFDiffPageContext& leftPageContext = leftPreparedPages[item.index1]; + m_result.addPageRemoved(leftPageContext.pageIndex); + } + } + } + else + { + for (auto it = range.first; it != range.second; ++i) + { + const AlgorithmLCS::SequenceItem& item = *it; + Q_ASSERT(item.isAdded() || item.isRemoved()); + + if (item.isAdded()) + { + m_result.addPageAdded(rightPreparedPages[item.index2].pageIndex); + } + if (item.isRemoved()) + { + m_result.addPageRemoved(leftPreparedPages[item.index1].pageIndex); + } + } + } } } @@ -542,6 +604,28 @@ void PDFDiffResult::addPageMoved(PDFInteger pageIndex1, PDFInteger pageIndex2) m_differences.emplace_back(std::move(difference)); } +void PDFDiffResult::addPageAdded(PDFInteger pageIndex) +{ + Difference difference; + + difference.type = Type::PageAdded; + difference.pageIndex2 = pageIndex; + difference.message = PDFDiff::tr("Page no. %1 was added.").arg(pageIndex + 1); + + m_differences.emplace_back(std::move(difference)); +} + +void PDFDiffResult::addPageRemoved(PDFInteger pageIndex) +{ + Difference difference; + + difference.type = Type::PageMoved; + difference.pageIndex1 = pageIndex; + difference.message = PDFDiff::tr("Page no. %1 was removed.").arg(pageIndex + 1); + + m_differences.emplace_back(std::move(difference)); +} + PDFDiffHelper::Differences PDFDiffHelper::calculateDifferences(const GraphicPieceInfos& left, const GraphicPieceInfos& right, PDFReal epsilon) diff --git a/Pdf4QtLib/sources/pdfdiff.h b/Pdf4QtLib/sources/pdfdiff.h index 1e3d1a0..da3c1bd 100644 --- a/Pdf4QtLib/sources/pdfdiff.h +++ b/Pdf4QtLib/sources/pdfdiff.h @@ -42,7 +42,9 @@ public: enum class Type { Invalid, - PageMoved + PageMoved, + PageAdded, + PageRemoved }; struct Difference @@ -59,6 +61,8 @@ public: const PDFOperationResult& getResult() const { return m_result; } void addPageMoved(PDFInteger pageIndex1, PDFInteger pageIndex2); + void addPageAdded(PDFInteger pageIndex); + void addPageRemoved(PDFInteger pageIndex); private: Differences m_differences; @@ -79,12 +83,13 @@ public: enum Option { - None = 0x0000, - Asynchronous = 0x0001, ///< Compare document asynchronously - PC_Text = 0x0002, ///< Use text to compare pages (determine, which pages correspond to each other) - PC_VectorGraphics = 0x0004, ///< Use vector graphics to compare pages (determine, which pages correspond to each other) - PC_Images = 0x0008, ///< Use images to compare pages (determine, which pages correspond to each other) - PC_Mesh = 0x0010, ///< Use mesh to compare pages (determine, which pages correspond to each other) + None = 0x0000, + Asynchronous = 0x0001, ///< Compare document asynchronously + PC_Text = 0x0002, ///< Use text to compare pages (determine, which pages correspond to each other) + PC_VectorGraphics = 0x0004, ///< Use vector graphics to compare pages (determine, which pages correspond to each other) + PC_Images = 0x0008, ///< Use images to compare pages (determine, which pages correspond to each other) + PC_Mesh = 0x0010, ///< Use mesh to compare pages (determine, which pages correspond to each other) + CompareTextsAsVector = 0x0020, ///< Compare texts as vector graphics }; Q_DECLARE_FLAGS(Options, Option)