LibPDF: Get rid of PlainText/Encoded StreamObject

This was a small optimization to allow a stream object to simply hold
a reference to the bytes in a PDF document rather than duplicating
them. However, as we move into features such as encryption, this
optimization does more harm than good. This can be revisited in the
future if necessary.
This commit is contained in:
Matthew Olsson 2022-03-20 11:07:47 -07:00 committed by Andreas Kling
parent 15b7999313
commit c98bda8ce6
3 changed files with 6 additions and 40 deletions

View file

@ -14,9 +14,6 @@ namespace PDF {
class Document;
class Object;
// Note: This macro doesn't care about PlainTextStreamObject and EncodedStreamObject because
// we never need to work directly with either of them.
#define ENUMERATE_OBJECT_TYPES(V) \
V(StringObject, string) \
V(NameObject, name) \

View file

@ -143,55 +143,24 @@ private:
class StreamObject : public Object {
public:
explicit StreamObject(NonnullRefPtr<DictObject> const& dict)
explicit StreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer const& bytes)
: m_dict(dict)
, m_buffer(bytes)
{
}
virtual ~StreamObject() override = default;
[[nodiscard]] ALWAYS_INLINE NonnullRefPtr<DictObject> dict() const { return m_dict; }
[[nodiscard]] virtual ReadonlyBytes bytes() const = 0;
[[nodiscard]] ReadonlyBytes bytes() const { return m_buffer.bytes(); };
const char* type_name() const override { return "stream"; }
String to_string(int indent) const override;
protected:
private:
bool is_stream() const override { return true; }
private:
NonnullRefPtr<DictObject> m_dict;
};
class PlainTextStreamObject final : public StreamObject {
public:
PlainTextStreamObject(NonnullRefPtr<DictObject> const& dict, ReadonlyBytes bytes)
: StreamObject(dict)
, m_bytes(bytes)
{
}
virtual ~PlainTextStreamObject() override = default;
[[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_bytes; }
private:
ReadonlyBytes m_bytes;
};
class EncodedStreamObject final : public StreamObject {
public:
EncodedStreamObject(NonnullRefPtr<DictObject> const& dict, ByteBuffer&& buffer)
: StreamObject(dict)
, m_buffer(buffer)
{
}
virtual ~EncodedStreamObject() override = default;
[[nodiscard]] ALWAYS_INLINE virtual ReadonlyBytes bytes() const override { return m_buffer.bytes(); }
private:
ByteBuffer m_buffer;
};

View file

@ -997,10 +997,10 @@ PDFErrorOr<NonnullRefPtr<StreamObject>> Parser::parse_stream(NonnullRefPtr<DictO
warnln("Failed to decode filter: {}", maybe_bytes.error().string_literal());
return error(String::formatted("Failed to decode filter {}", maybe_bytes.error().string_literal()));
}
return make_object<EncodedStreamObject>(dict, move(maybe_bytes.value()));
return make_object<StreamObject>(dict, maybe_bytes.value());
}
return make_object<PlainTextStreamObject>(dict, bytes);
return make_object<StreamObject>(dict, MUST(ByteBuffer::copy(bytes)));
}
PDFErrorOr<Vector<Command>> Parser::parse_graphics_commands()