From 5475c246abe8f84082e442c69de58199793d78e9 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Thu, 28 Aug 2025 18:09:25 +0300 Subject: [PATCH] Fix test Redact --- .../graphics/commands/AnnotField.cpp | 12 ---- DesktopEditor/graphics/commands/AnnotField.h | 5 -- PdfFile/OnlineOfficeBinToPdf.cpp | 3 +- PdfFile/PdfEditor.cpp | 71 +++++++++++++++++-- PdfFile/PdfReader.cpp | 9 ++- PdfFile/SrcWriter/RedactOutputDev.cpp | 68 ++++++++++++++---- 6 files changed, 124 insertions(+), 44 deletions(-) diff --git a/DesktopEditor/graphics/commands/AnnotField.cpp b/DesktopEditor/graphics/commands/AnnotField.cpp index 4de6cce6c2..4c0566fc59 100644 --- a/DesktopEditor/graphics/commands/AnnotField.cpp +++ b/DesktopEditor/graphics/commands/AnnotField.cpp @@ -1226,13 +1226,6 @@ bool CWidgetsInfo::Read(NSOnlineOfficeBinToPdf::CBufferReader* pReader, IMetafil CRedact::CRedact() : IAdvancedCommand(AdvancedCommandType::Redact) {} CRedact::~CRedact() {} int CRedact::GetFlag() const { return m_nFlag; } -void CRedact::GetBounds(double& dX1, double& dY1, double& dX2, double& dY2) -{ - dX1 = m_dX1; - dY1 = m_dY1; - dX2 = m_dX2; - dY2 = m_dY2; -} const std::vector& CRedact::GetQuadPoints() { return m_arrQuadPoints; } BYTE* CRedact::GetRender(LONG& nLen) { @@ -1252,11 +1245,6 @@ bool CRedact::Read(NSOnlineOfficeBinToPdf::CBufferReader* pReader, IMetafileToRe m_nFlag = pReader->ReadInt(); if (m_nFlag & (1 << 0)) { - m_dX1 = pReader->ReadDouble(); - m_dY1 = pReader->ReadDouble(); - m_dX2 = pReader->ReadDouble(); - m_dY2 = pReader->ReadDouble(); - m_nRenderLen = pReader->ReadInt() - 4; m_pRender = pReader->GetCurrentBuffer(); pReader->Skip(m_nRenderLen); diff --git a/DesktopEditor/graphics/commands/AnnotField.h b/DesktopEditor/graphics/commands/AnnotField.h index 86949b996d..f3acf3a5b8 100644 --- a/DesktopEditor/graphics/commands/AnnotField.h +++ b/DesktopEditor/graphics/commands/AnnotField.h @@ -638,7 +638,6 @@ public: virtual ~CRedact(); int GetFlag() const; - void GetBounds(double& dX1, double& dY1, double& dX2, double& dY2); const std::vector& GetQuadPoints(); BYTE* GetRender(LONG& nLen); @@ -646,10 +645,6 @@ public: private: int m_nFlag; - double m_dX1; - double m_dY1; - double m_dX2; - double m_dY2; LONG m_nRenderLen; BYTE* m_pRender; std::vector m_arrQuadPoints; diff --git a/PdfFile/OnlineOfficeBinToPdf.cpp b/PdfFile/OnlineOfficeBinToPdf.cpp index 77a6f2cc60..e79e8a0a26 100644 --- a/PdfFile/OnlineOfficeBinToPdf.cpp +++ b/PdfFile/OnlineOfficeBinToPdf.cpp @@ -247,8 +247,7 @@ namespace NSOnlineOfficeBinToPdf } case AddCommandType::SetType: { - int nType = oReader.ReadInt(); - pPdf->SetEditType(nType); + pPdf->SetEditType(1); break; } default: diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index 11c31f27f2..e2769bb56b 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -3528,13 +3528,70 @@ bool CPdfEditor::IsBase14(const std::wstring& wsFontName, bool& bBold, bool& bIt } void CPdfEditor::Redact(IAdvancedCommand* _pCommand) { - CRedact* pCommand = (CRedact*)_pCommand; PdfWriter::CDocument* pDoc = m_pWriter->GetDocument(); - Redact(pCommand->GetQuadPoints()); + int nOriginIndex = m_nEditPage; + if (m_nMode == Mode::WriteNew) + { + PdfWriter::CPageTree* pPageTree = pDoc->GetPageTree(); + PdfWriter::CObjectBase* pObj = pPageTree->GetObj(m_nEditPage); + PdfWriter::CFakePage* pFakePage = NULL; + if (pObj) + pFakePage = dynamic_cast(pObj); + if (pFakePage) + nOriginIndex = pFakePage->GetOriginIndex(); + } + PDFDoc* pPDFDocument = NULL; + int nPageIndex = m_pReader->GetPageIndex(nOriginIndex, &pPDFDocument); + if (nPageIndex < 0 || !pPDFDocument) + return; + Page* pPage = pPDFDocument->getCatalog()->getPage(nPageIndex); + PDFRectangle* cropBox = pPage->getCropBox(); + + CRedact* pCommand = (CRedact*)_pCommand; + std::vector arrQuads = pCommand->GetQuadPoints(); + for (int i = 0; i < arrQuads.size() / 4; ++i) + { + arrQuads[i * 4 + 0] += cropBox->x1; + double dQ = arrQuads[i * 4 + 1]; + arrQuads[i * 4 + 1] = cropBox->y2 - arrQuads[i * 4 + 3]; + arrQuads[i * 4 + 2] += cropBox->x1; + arrQuads[i * 4 + 3] = cropBox->y2 - dQ; + } + + Redact(arrQuads); int nFlags = pCommand->GetFlag(); if (nFlags & (1 << 0)) { + m_pWriter->SetTransform(1, 0, 0, 1, 0, 0); + LONG nLenRender = 0; + BYTE* pRender = pCommand->GetRender(nLenRender); + + BYTE* pMemory = pRender; + int ret = *((int*)pMemory); + pMemory += 4; + double R = ret / 100000.0; + ret = *((int*)pMemory); + pMemory += 4; + double G = ret / 100000.0; + ret = *((int*)pMemory); + double B = ret / 100000.0; + LONG lColor = (LONG)(((LONG)(R * 255)) | ((LONG)(G * 255) << 8) | ((LONG)(B * 255) << 16) | ((LONG)255 << 24)); + + for (int i = 0; i < arrQuads.size() / 4; ++i) + { + m_pWriter->PathCommandEnd(); + m_pWriter->put_BrushColor1(lColor); + m_pWriter->PathCommandMoveTo(PdfReader::PDFCoordsToMM(arrQuads[i * 4 + 0] - cropBox->x1), PdfReader::PDFCoordsToMM(cropBox->y2 - arrQuads[i * 4 + 1])); + m_pWriter->PathCommandLineTo(PdfReader::PDFCoordsToMM(arrQuads[i * 4 + 0] - cropBox->x1), PdfReader::PDFCoordsToMM(cropBox->y2 - arrQuads[i * 4 + 3])); + m_pWriter->PathCommandLineTo(PdfReader::PDFCoordsToMM(arrQuads[i * 4 + 2] - cropBox->x1), PdfReader::PDFCoordsToMM(cropBox->y2 - arrQuads[i * 4 + 3])); + m_pWriter->PathCommandLineTo(PdfReader::PDFCoordsToMM(arrQuads[i * 4 + 2] - cropBox->x1), PdfReader::PDFCoordsToMM(cropBox->y2 - arrQuads[i * 4 + 1])); + m_pWriter->PathCommandClose(); + m_pWriter->DrawPath(NULL, L"", c_nWindingFillMode); + m_pWriter->PathCommandEnd(); + } + + /* PdfWriter::CPage* pCurPage = m_pWriter->GetPage(); pDoc->FixEditPage(pCurPage); PdfWriter::CPage* pFakePage = new PdfWriter::CPage(pDoc); @@ -3546,9 +3603,6 @@ void CPdfEditor::Redact(IAdvancedCommand* _pCommand) pFakePage->SetStream(pCurPage->GetStream()); pFakePage->Add("Resources", pCurPage->Get("Resources")); - LONG nLenRender = 0; - BYTE* pRender = pCommand->GetRender(nLenRender); - IMetafileToRenderter* pCorrector = new IMetafileToRenderter(m_pWriter->GetRenderer()); NSOnlineOfficeBinToPdf::ConvertBufferToRenderer(pRender, nLenRender, pCorrector); RELEASEOBJECT(pCorrector); @@ -3556,6 +3610,7 @@ void CPdfEditor::Redact(IAdvancedCommand* _pCommand) m_pWriter->SetPage(pCurPage); pDoc->SetCurPage(pCurPage); RELEASEOBJECT(pFakePage); + */ } } void CPdfEditor::Redact(const std::vector& arrQuadPoints) @@ -3578,9 +3633,15 @@ void CPdfEditor::Redact(const std::vector& arrQuadPoints) return; #ifndef BUILDING_WASM_MODULE + globalParams->setDrawFormFields(gFalse); + globalParams->setDrawAnnotations(gFalse); + PdfWriter::RedactOutputDev oRedactOut(m_pWriter); oRedactOut.NewPDF(pPDFDocument->getXRef()); oRedactOut.SetRedact(arrQuadPoints); pPDFDocument->displayPage(&oRedactOut, nPageIndex, 72.0, 72.0, 0, gTrue, gFalse, gFalse); + + globalParams->setDrawFormFields(gTrue); + globalParams->setDrawAnnotations(gTrue); #endif } diff --git a/PdfFile/PdfReader.cpp b/PdfFile/PdfReader.cpp index a136980a41..f0570c2a7a 100644 --- a/PdfFile/PdfReader.cpp +++ b/PdfFile/PdfReader.cpp @@ -775,10 +775,10 @@ bool CPdfReader::RedactPage(int _nPageIndex, double* arrRedactBox, int nLengthX4 pRedact->m_nPageIndex = _nPageIndex; for (int i = 0; i < nLengthX4; ++i) { - pRedact->m_arrRedactBox.push_back(arrRedactBox[i + 0] + cropBox->x1); - pRedact->m_arrRedactBox.push_back(cropBox->y2 - arrRedactBox[i + 3]); - pRedact->m_arrRedactBox.push_back(arrRedactBox[i + 2] + cropBox->x1); - pRedact->m_arrRedactBox.push_back(cropBox->y2 - arrRedactBox[i + 1]); + pRedact->m_arrRedactBox.push_back(arrRedactBox[i * 4 + 0] + cropBox->x1); + pRedact->m_arrRedactBox.push_back(cropBox->y2 - arrRedactBox[i * 4 + 3]); + pRedact->m_arrRedactBox.push_back(arrRedactBox[i * 4 + 2] + cropBox->x1); + pRedact->m_arrRedactBox.push_back(cropBox->y2 - arrRedactBox[i * 4 + 1]); } pRedact->m_pChanges = pChanges; pRedact->m_nChangeLength = nLength; @@ -827,7 +827,6 @@ void CPdfReader::DrawPageOnRenderer(IRenderer* pRenderer, int _nPageIndex, bool* { if (m_vRedact[i]->m_nPageIndex == _nPageIndex) { - // TODO нужно сбросить все матрицы и т.п. BYTE* pMemory = m_vRedact[i]->m_pChanges; int ret = *((int*)pMemory); pMemory += 4; diff --git a/PdfFile/SrcWriter/RedactOutputDev.cpp b/PdfFile/SrcWriter/RedactOutputDev.cpp index 227d9a07b0..611ae69b83 100644 --- a/PdfFile/SrcWriter/RedactOutputDev.cpp +++ b/PdfFile/SrcWriter/RedactOutputDev.cpp @@ -39,8 +39,6 @@ #include "../lib/pathkit/include/core/SkPath.h" #include "../lib/pathkit/include/pathops/SkPathOps.h" -#include "../../DesktopEditor/graphics/GraphicsPath.h" - namespace PdfWriter { void Transform(double* pMatrix, double dUserX, double dUserY, double* pdDeviceX, double* pdDeviceY) @@ -389,21 +387,61 @@ void RedactOutputDev::drawChar(GfxState *pGState, double dX, double dY, double d { double* pCTM = pGState->getCTM(); double* pTm = pGState->getTextMat(); - double arrMatrix[6]; + double pNewTm[6], arrMatrix[6]; - arrMatrix[0] = pTm[0] * pCTM[0] + pTm[1] * pCTM[2]; - arrMatrix[1] = pTm[0] * pCTM[1] + pTm[1] * pCTM[3]; - arrMatrix[2] = pTm[2] * pCTM[0] + pTm[3] * pCTM[2]; - arrMatrix[3] = pTm[2] * pCTM[1] + pTm[3] * pCTM[3]; - arrMatrix[4] = pTm[4] * pCTM[0] + pTm[5] * pCTM[2] + pCTM[4]; - arrMatrix[5] = pTm[4] * pCTM[1] + pTm[5] * pCTM[3] + pCTM[5]; - - if (arrMatrix[0] != m_arrMatrix[0] || arrMatrix[1] != m_arrMatrix[1] || arrMatrix[2] != m_arrMatrix[2] || - arrMatrix[3] != m_arrMatrix[3] || arrMatrix[4] != m_arrMatrix[4] || arrMatrix[5] != m_arrMatrix[5]) + double dTextScale = std::min(sqrt(pTm[2] * pTm[2] + pTm[3] * pTm[3]), sqrt(pTm[0] * pTm[0] + pTm[1] * pTm[1])); + double dITextScale = 1 / dTextScale; + double dOldSize = 10.0; + m_pRenderer->get_FontSize(&dOldSize); + if (dOldSize * dTextScale > 0) { - double dShiftX = 0, dShiftY = 0; - DoTransform(arrMatrix, &dShiftX, &dShiftY, true); + m_pRenderer->put_FontSize(dOldSize * dTextScale); + + pNewTm[0] = pTm[0] * dITextScale * pGState->getHorizScaling(); + pNewTm[1] = pTm[1] * dITextScale * pGState->getHorizScaling(); + pNewTm[2] = pTm[2] * dITextScale; + pNewTm[3] = pTm[3] * dITextScale; + pNewTm[4] = dX - dOriginX; + pNewTm[5] = dY - dOriginY; } + else + { + m_pRenderer->put_FontSize(-dOldSize * dTextScale); + + pNewTm[0] = pTm[0] * dITextScale * pGState->getHorizScaling(); + pNewTm[1] = pTm[1] * dITextScale * pGState->getHorizScaling(); + pNewTm[2] = pTm[2] * dITextScale; + pNewTm[3] = pTm[3] * dITextScale; + pNewTm[4] = dX; + pNewTm[5] = dY; + } + + arrMatrix[0] = pNewTm[0] * pCTM[0] + pNewTm[1] * pCTM[2]; + arrMatrix[1] = pNewTm[0] * pCTM[1] + pNewTm[1] * pCTM[3]; + arrMatrix[2] = pNewTm[2] * pCTM[0] + pNewTm[3] * pCTM[2]; + arrMatrix[3] = pNewTm[2] * pCTM[1] + pNewTm[3] * pCTM[3]; + arrMatrix[4] = pNewTm[4] * pCTM[0] + pNewTm[5] * pCTM[2] + pCTM[4]; + arrMatrix[5] = pNewTm[4] * pCTM[1] + pNewTm[5] * pCTM[3] + pCTM[5]; + + double dSize = 1; + if (true) + { + double dNorma = std::min(sqrt(arrMatrix[0] * arrMatrix[0] + arrMatrix[1] * arrMatrix[1]), sqrt(arrMatrix[2] * arrMatrix[2] + arrMatrix[3] * arrMatrix[3])); + if (dNorma > 0 && dNorma != 1) + { + arrMatrix[0] /= dNorma; + arrMatrix[1] /= dNorma; + arrMatrix[2] /= dNorma; + arrMatrix[3] /= dNorma; + + m_pRenderer->get_FontSize(&dSize); + dSize *= dNorma; + m_pRenderer->put_FontSize(dSize); + } + } + + double dShiftX = 0, dShiftY = 0; + DoTransform(arrMatrix, &dShiftX, &dShiftY, true); double dDiff = dX + dDx / 2.0; for (int i = 0; i < m_arrQuadPoints.size(); i += 4) @@ -423,7 +461,7 @@ void RedactOutputDev::drawChar(GfxState *pGState, double dX, double dY, double d m_pRenderer->m_oCommandManager.SetTransform(m_arrMatrix[0], m_arrMatrix[1], m_arrMatrix[2], m_arrMatrix[3], m_arrMatrix[4], m_arrMatrix[5]); - CRendererTextCommand* pText = m_pRenderer->m_oCommandManager.AddText(pCodes, 2, dOriginX, dOriginY); + CRendererTextCommand* pText = m_pRenderer->m_oCommandManager.AddText(pCodes, 2, dShiftX, dShiftY); pText->SetName(m_pRenderer->m_oFont.GetName()); pText->SetSize(m_pRenderer->m_oFont.GetSize()); int nDColor2Size;