include/vcl/filter/pdfdocument.hxx     |   67 +++++++++++++++++++++++++++++++++
 vcl/source/filter/ipdf/pdfdocument.cxx |   28 ++++++++++++-
 2 files changed, 92 insertions(+), 3 deletions(-)

New commits:
commit fbf14b4e48c7677d5acf9a0ab91a3dc8d4ffc6fd
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Sat Nov 7 16:18:00 2020 +0100
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Nov 12 22:26:13 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>

diff --git a/include/vcl/filter/pdfdocument.hxx 
b/include/vcl/filter/pdfdocument.hxx
index 7e6d210255dd..4177e615516e 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>
 
@@ -59,6 +60,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.
@@ -129,6 +132,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.
@@ -143,6 +148,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.
@@ -168,6 +184,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.
@@ -183,6 +207,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.
@@ -198,6 +229,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.
@@ -229,6 +266,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
@@ -292,6 +343,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.
@@ -302,6 +360,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.
@@ -319,6 +384,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 7a0a7b4dce65..911cb2381ed5 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 {}
 };
 }
 
@@ -66,6 +67,8 @@ public:
     PDFEndDictionaryElement();
     bool Read(SvStream& rStream) override;
     sal_uInt64 GetLocation() const;
+
+    void writeString(OStringBuffer& /*rBuffer*/) override {}
 };
 
 /// End of a stream: 'endstream' keyword.
@@ -73,6 +76,8 @@ class PDFEndStreamElement : public PDFElement
 {
 public:
     bool Read(SvStream& rStream) override;
+
+    void writeString(OStringBuffer& /*rBuffer*/) override {}
 };
 
 /// End of an object: 'endobj' keyword.
@@ -80,6 +85,8 @@ class PDFEndObjectElement : public PDFElement
 {
 public:
     bool Read(SvStream& rStream) override;
+
+    void writeString(OStringBuffer& /*rBuffer*/) override {}
 };
 
 /// End of an array: ']'.
@@ -92,14 +99,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.
@@ -107,6 +127,8 @@ class PDFNullElement : public PDFElement
 {
 public:
     bool Read(SvStream& rStream) override;
+
+    void writeString(OStringBuffer& rBuffer) override { 
rBuffer.append("null"); }
 };
 }
 
@@ -123,6 +145,8 @@ public:
     bool Read(SvStream& rStream) override;
     PDFElement* Lookup(const OString& rDictionaryKey);
     sal_uInt64 GetLocation() const;
+
+    void writeString(OStringBuffer& /*rBuffer*/) override {}
 };
 
 XRefEntry::XRefEntry() = default;
@@ -2287,8 +2311,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

Reply via email to