From 4250f334de729360567ec9bd88aed06fbd823dc4 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Fri, 10 Mar 2023 15:14:28 +0100 Subject: tdf#152218 vcl: PDF/UA export: produce ID for footnote/endnote There is this mysterious requirement: 7.9 Notes and references Footnotes and endnotes shall be tagged with a Note tag. Each note tag shall have a unique entry in the ID key as described in ISO 32000-1:2008, 14.7.2, Table 323. Then also an IDTree is required; PDF 1.7, 10.6.1 Structure Hierarchy: (Required if any structure elements have element identifiers) The point of this is entirely unclear to me, it looks like neither poppler nor pdfium even have code to read the IDTree, and structure elements already always have an object id by which they can be referenced, so why add a second ID? Alas veraPDF complains if it is missing: Specification: ISO 14289-1:2014, Clause: 7.9, Test number: 1 Note tag shall have ID entry Change-Id: Ia8e52c46312c7dbb3bf7ef8635321200e5de24ae Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148645 Tested-by: Jenkins Reviewed-by: Michael Stahl --- vcl/inc/pdf/pdfwriter_impl.hxx | 5 +++ vcl/source/gdi/pdfwriter_impl.cxx | 80 +++++++++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 12 deletions(-) (limited to 'vcl') diff --git a/vcl/inc/pdf/pdfwriter_impl.hxx b/vcl/inc/pdf/pdfwriter_impl.hxx index 4e10204433cb..a16d57404207 100644 --- a/vcl/inc/pdf/pdfwriter_impl.hxx +++ b/vcl/inc/pdf/pdfwriter_impl.hxx @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -760,6 +761,8 @@ private: /* role map of struct tree root */ std::unordered_map< OString, OString > m_aRoleMap; + /* structure elements (object ids) that should have ID */ + std::unordered_set m_StructElemObjsWithID; /* contains all widgets used in the PDF */ @@ -993,6 +996,8 @@ i12626 sal_Int32 emitStructure( PDFStructureElement& rEle ); // writes structure parent tree sal_Int32 emitStructParentTree( sal_Int32 nTreeObject ); + // writes structure IDTree + sal_Int32 emitStructIDTree(sal_Int32 nTreeObject); // writes page tree and catalog bool emitCatalog(); // writes signature dictionary object diff --git a/vcl/source/gdi/pdfwriter_impl.cxx b/vcl/source/gdi/pdfwriter_impl.cxx index c5b873565611..9a78d6908a58 100644 --- a/vcl/source/gdi/pdfwriter_impl.cxx +++ b/vcl/source/gdi/pdfwriter_impl.cxx @@ -111,6 +111,18 @@ constexpr sal_Int32 pointToPixel(double pt) return sal_Int32(pt * fDivisor); } +void appendObjectID(sal_Int32 nObjectID, OStringBuffer & aLine) +{ + aLine.append(nObjectID); + aLine.append(" 0 obj\n"); +} + +void appendObjectReference(sal_Int32 nObjectID, OStringBuffer & aLine) +{ + aLine.append(nObjectID); + aLine.append(" 0 R "); +} + void appendHex(sal_Int8 nInt, OStringBuffer& rBuffer) { static const char pHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', @@ -1801,6 +1813,43 @@ sal_Int32 PDFWriterImpl::emitStructParentTree( sal_Int32 nObject ) return nObject; } +// every structure element already has a unique object id - just use it for ID +static OString GenerateID(sal_Int32 const nObjectId) +{ + return "id" + OString::number(nObjectId); +} + +sal_Int32 PDFWriterImpl::emitStructIDTree(sal_Int32 const nObject) +{ + // loosely following PDF 1.7, 10.6.5 Example of Logical Structure, Example 10.15 + if (nObject < 0) + { + return nObject; + } + // the name tree entries must be sorted lexicographically. + std::map ids; + for (auto n : m_StructElemObjsWithID) + { + ids.emplace(GenerateID(n), n); + } + OStringBuffer buf; + appendObjectID(nObject, buf); + buf.append("<>\nendobj\n\n"); + + CHECK_RETURN( updateObject(nObject) ); + CHECK_RETURN( writeBuffer(buf.getStr(), buf.getLength()) ); + + return nObject; +} + const char* PDFWriterImpl::getAttributeTag( PDFWriter::StructAttribute eAttr ) { static std::map< PDFWriter::StructAttribute, const char* > aAttributeStrings; @@ -2084,6 +2133,7 @@ sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle ) aLine.append( " 0 obj\n" "<>\n" ); } + if (!m_StructElemObjsWithID.empty()) + { + nIDTree = createObject(); + aLine.append("/IDTree "); + appendObjectReference(nIDTree, aLine); + aLine.append("\n"); + } } else { @@ -2114,6 +2171,11 @@ sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle ) aLine.append( rEle.m_aAlias ); else aLine.append( getStructureTag( rEle.m_eType ) ); + if (m_StructElemObjsWithID.find(rEle.m_nObject) != m_StructElemObjsWithID.end()) + { + aLine.append("\n/ID "); + appendLiteralStringEncrypt(GenerateID(rEle.m_nObject), rEle.m_nObject, aLine); + } aLine.append( "\n" "/P " ); aLine.append( m_aStructure[ rEle.m_nParentElement ].m_nObject ); @@ -2207,6 +2269,7 @@ sal_Int32 PDFWriterImpl::emitStructure( PDFStructureElement& rEle ) CHECK_RETURN( writeBuffer( aLine.getStr(), aLine.getLength() ) ); CHECK_RETURN( emitStructParentTree( nParentTree ) ); + CHECK_RETURN( emitStructIDTree(nIDTree) ); return rEle.m_nObject; } @@ -3811,18 +3874,6 @@ void appendAnnotationRect(tools::Rectangle const & rRectangle, OStringBuffer & a aLine.append("] "); } -void appendObjectID(sal_Int32 nObjectID, OStringBuffer & aLine) -{ - aLine.append(nObjectID); - aLine.append(" 0 obj\n"); -} - -void appendObjectReference(sal_Int32 nObjectID, OStringBuffer & aLine) -{ - aLine.append(nObjectID); - aLine.append(" 0 R "); -} - } // end anonymous namespace void PDFWriterImpl::emitTextAnnotationLine(OStringBuffer & aLine, PDFNoteEntry const & rNote) @@ -10672,6 +10723,11 @@ sal_Int32 PDFWriterImpl::beginStructureElement( PDFWriter::StructElement eType, rEle.m_nObject = createObject(); // update parent's kids list m_aStructure[ rEle.m_nParentElement ].m_aKids.emplace_back(rEle.m_nObject); + // ISO 14289-1:2014, Clause: 7.9 + if (rEle.m_eType == PDFWriter::Note) + { + m_StructElemObjsWithID.insert(rEle.m_nObject); + } } return nNewId; } -- cgit