include/vcl/filter/pdfdocument.hxx | 67 +++++++++++++++++++++++++++++++++ vcl/source/filter/ipdf/pdfdocument.cxx | 28 ++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-)
New commits: commit 3262db463ce81722c6c4f10ec4bdecaa5e36011c Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Sat Nov 7 16:18:00 2020 +0100 Commit: Andras Timar <andras.ti...@collabora.com> CommitDate: Fri Nov 13 11:07:41 2020 +0100 pdf: add writeString for pdf elements for writing element content This adds a writeString virtual method to PDFElement and subclasses and implemnts them for each element. This is used to write the PDF object hierarchy back to a string buffer. Change-Id: I484c9cebd8ab9149029b925a47e68b6a4fdf9be1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105492 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> (cherry picked from commit fbf14b4e48c7677d5acf9a0ab91a3dc8d4ffc6fd) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105778 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Andras Timar <andras.ti...@collabora.com> diff --git a/include/vcl/filter/pdfdocument.hxx b/include/vcl/filter/pdfdocument.hxx index 5ab6c2044ac8..b1cf34856a95 100644 --- a/include/vcl/filter/pdfdocument.hxx +++ b/include/vcl/filter/pdfdocument.hxx @@ -17,6 +17,7 @@ #include <tools/stream.hxx> #include <vcl/dllapi.h> +#include <rtl/strbuf.hxx> #include <vcl/filter/pdfobjectcontainer.hxx> @@ -79,6 +80,8 @@ public: bool alreadyVisiting() const { return m_bVisiting; } void setParsing(bool bParsing) { m_bParsing = bParsing; } bool alreadyParsing() const { return m_bParsing; } + + virtual void writeString(OStringBuffer& rBuffer) = 0; }; /// Indirect object: something with a unique ID. @@ -149,6 +152,8 @@ public: SvMemoryStream* GetStreamBuffer() const; void SetStreamBuffer(std::unique_ptr<SvMemoryStream>& pStreamBuffer); PDFDocument& GetDocument(); + + void writeString(OStringBuffer& /*rBuffer*/) override { assert(false && "not implemented"); } }; /// Array object: a list. @@ -163,6 +168,17 @@ public: bool Read(SvStream& rStream) override; void PushBack(PDFElement* pElement); const std::vector<PDFElement*>& GetElements() const; + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("[ "); + for (auto& rElement : m_aElements) + { + rElement->writeString(rBuffer); + rBuffer.append(" "); + } + rBuffer.append("]"); + } }; /// Reference object: something with a unique ID. @@ -188,6 +204,14 @@ public: int GetGenerationValue() const; sal_uInt64 GetOffset() const; PDFNumberElement& GetObjectElement() const; + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append(sal_Int32(GetObjectValue())); + rBuffer.append(' '); + rBuffer.append(sal_Int32(GetGenerationValue())); + rBuffer.append(" R"); + } }; /// Stream object: a byte array with a known length. @@ -203,6 +227,13 @@ public: bool Read(SvStream& rStream) override; sal_uInt64 GetOffset() const; SvMemoryStream& GetMemory(); + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("stream\n"); + rBuffer.append(static_cast<const char*>(m_aMemory.GetData()), m_aMemory.GetSize()); + rBuffer.append("\nendstream\n"); + } }; /// Name object: a key string. @@ -218,6 +249,12 @@ public: const OString& GetValue() const; sal_uInt64 GetLocation() const; static sal_uInt64 GetLength() { return 0; } + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("/"); + rBuffer.append(m_aValue); + } }; /// Dictionary object: a set key-value pairs. @@ -249,6 +286,20 @@ public: PDFObjectElement* LookupObject(const OString& rDictionaryKey); /// Looks up an element which is contained in this dictionary. PDFElement* LookupElement(const OString& rDictionaryKey); + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("<< "); + for (auto& rPair : m_aItems) + { + rBuffer.append("/"); + rBuffer.append(rPair.first); + rBuffer.append(" "); + rPair.second->writeString(rBuffer); + rBuffer.append(" "); + } + rBuffer.append(">>"); + } }; enum class TokenizeMode @@ -312,6 +363,13 @@ class VCL_DLLPUBLIC PDFHexStringElement final : public PDFElement public: bool Read(SvStream& rStream) override; const OString& GetValue() const; + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("<"); + rBuffer.append(m_aValue); + rBuffer.append(">"); + } }; /// Literal string: in (asdf) form. @@ -322,6 +380,13 @@ class VCL_DLLPUBLIC PDFLiteralStringElement final : public PDFElement public: bool Read(SvStream& rStream) override; const OString& GetValue() const; + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append("("); + rBuffer.append(m_aValue); + rBuffer.append(")"); + } }; /// Numbering object: an integer or a real. @@ -339,6 +404,8 @@ public: double GetValue() const; sal_uInt64 GetLocation() const; sal_uInt64 GetLength() const; + + void writeString(OStringBuffer& rBuffer) override { rBuffer.append(m_fValue); } }; /** diff --git a/vcl/source/filter/ipdf/pdfdocument.cxx b/vcl/source/filter/ipdf/pdfdocument.cxx index cf3a0ea430b0..f83d1614364e 100644 --- a/vcl/source/filter/ipdf/pdfdocument.cxx +++ b/vcl/source/filter/ipdf/pdfdocument.cxx @@ -49,6 +49,7 @@ class PDFCommentElement : public PDFElement public: explicit PDFCommentElement(PDFDocument& rDoc); bool Read(SvStream& rStream) override; + void writeString(OStringBuffer& /*rBuffer*/) override {} }; class PDFReferenceElement; @@ -63,6 +64,8 @@ public: PDFEndDictionaryElement(); bool Read(SvStream& rStream) override; sal_uInt64 GetLocation() const; + + void writeString(OStringBuffer& /*rBuffer*/) override {} }; /// End of a stream: 'endstream' keyword. @@ -70,6 +73,8 @@ class PDFEndStreamElement : public PDFElement { public: bool Read(SvStream& rStream) override; + + void writeString(OStringBuffer& /*rBuffer*/) override {} }; /// End of an object: 'endobj' keyword. @@ -77,6 +82,8 @@ class PDFEndObjectElement : public PDFElement { public: bool Read(SvStream& rStream) override; + + void writeString(OStringBuffer& /*rBuffer*/) override {} }; /// End of an array: ']'. @@ -89,14 +96,27 @@ public: PDFEndArrayElement(); bool Read(SvStream& rStream) override; sal_uInt64 GetOffset() const; + + void writeString(OStringBuffer& /*rBuffer*/) override {} }; /// Boolean object: a 'true' or a 'false'. class PDFBooleanElement : public PDFElement { + bool m_aValue; + public: - explicit PDFBooleanElement(bool bValue); + explicit PDFBooleanElement(bool bValue) + : m_aValue(bValue) + { + } + bool Read(SvStream& rStream) override; + + void writeString(OStringBuffer& rBuffer) override + { + rBuffer.append(m_aValue ? "true" : "false"); + } }; /// Null object: the 'null' singleton. @@ -104,6 +124,8 @@ class PDFNullElement : public PDFElement { public: bool Read(SvStream& rStream) override; + + void writeString(OStringBuffer& rBuffer) override { rBuffer.append("null"); } }; /// The trailer singleton is at the end of the doc. @@ -119,6 +141,8 @@ public: bool Read(SvStream& rStream) override; PDFElement* Lookup(const OString& rDictionaryKey); sal_uInt64 GetLocation() const; + + void writeString(OStringBuffer& /*rBuffer*/) override {} }; XRefEntry::XRefEntry() = default; @@ -2212,8 +2236,6 @@ sal_uInt64 PDFNumberElement::GetLocation() const { return m_nOffset; } sal_uInt64 PDFNumberElement::GetLength() const { return m_nLength; } -PDFBooleanElement::PDFBooleanElement(bool /*bValue*/) {} - bool PDFBooleanElement::Read(SvStream& /*rStream*/) { return true; } bool PDFNullElement::Read(SvStream& /*rStream*/) { return true; } _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits