Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Aug 18, 2024
1 parent f3f1e83 commit 3366c8a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/odr/internal/pdf/pdf_cmap_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ std::variant<Object, std::string> CMapParser::read_token() const {
m_parser.read_string());
}
if (m_parser.peek_name()) {
return m_parser.read_name();
return Object(m_parser.read_name());
}
if (m_parser.peek_dictionary()) {
return m_parser.read_dictionary();
return Object(m_parser.read_dictionary());
}

std::string token;
Expand Down
20 changes: 10 additions & 10 deletions src/odr/internal/pdf/pdf_document_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pdf::Font *parse_font(DocumentParser &parser, const ObjectReference &reference,

font->type = Type::font;
font->object_reference = reference;
font->object = dictionary;
font->object = Object(dictionary);

if (dictionary.has_key("ToUnicode")) {
IndirectObject to_unicode_obj =
Expand All @@ -43,12 +43,12 @@ pdf::Font *parse_font(DocumentParser &parser, const ObjectReference &reference,

pdf::Resources *parse_resources(DocumentParser &parser, const Object &object,
Document &document) {
Resources *resources = document.create_element<Resources>();
auto *resources = document.create_element<Resources>();

Dictionary dictionary = parser.resolve_object_copy(object).as_dictionary();

resources->type = Type::resources;
resources->object = dictionary;
resources->object = Object(dictionary);

if (!dictionary["Font"].is_null()) {
Dictionary font_table =
Expand All @@ -64,14 +64,14 @@ pdf::Resources *parse_resources(DocumentParser &parser, const Object &object,
pdf::Annotation *parse_annotation(DocumentParser &parser,
const ObjectReference &reference,
Document &document) {
Annotation *annotation = document.create_element<Annotation>();
auto *annotation = document.create_element<Annotation>();

IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();

annotation->type = Type::annotation;
annotation->object_reference = reference;
annotation->object = dictionary;
annotation->object = Object(dictionary);

return annotation;
}
Expand All @@ -85,7 +85,7 @@ pdf::Page *parse_page(DocumentParser &parser, const ObjectReference &reference,

page->type = Type::page;
page->object_reference = reference;
page->object = dictionary;
page->object = Object(dictionary);
page->parent = dynamic_cast<Pages *>(parent);
page->resources = parse_resources(parser, dictionary["Resources"], document);

Expand All @@ -112,14 +112,14 @@ pdf::Page *parse_page(DocumentParser &parser, const ObjectReference &reference,

pdf::Pages *parse_pages(DocumentParser &parser,
const ObjectReference &reference, Document &document) {
Pages *pages = document.create_element<Pages>();
auto *pages = document.create_element<Pages>();

IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();

pages->type = Type::pages;
pages->object_reference = reference;
pages->object = dictionary;
pages->object = Object(dictionary);
pages->count = dictionary["Count"].as_integer();

for (const Object &kid : dictionary["Kids"].as_array()) {
Expand Down Expand Up @@ -151,15 +151,15 @@ pdf::Element *parse_page_or_pages(DocumentParser &parser,
pdf::Catalog *parse_catalog(DocumentParser &parser,
const ObjectReference &reference,
Document &document) {
Catalog *catalog = document.create_element<Catalog>();
auto *catalog = document.create_element<Catalog>();

IndirectObject object = parser.read_object(reference);
const Dictionary &dictionary = object.object.as_dictionary();
const ObjectReference &pages_reference = dictionary["Pages"].as_reference();

catalog->type = Type::catalog;
catalog->object_reference = reference;
catalog->object = dictionary;
catalog->object = Object(dictionary);
catalog->pages = parse_pages(parser, pages_reference, document);

return catalog;
Expand Down
10 changes: 5 additions & 5 deletions src/odr/internal/pdf/pdf_graphics_operator_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ GraphicsOperator GraphicsOperatorParser::read_operator() const {

while (true) {
if (m_parser.peek_number()) {
std::visit([&](auto v) { result.arguments.push_back(v); },
std::visit([&](auto v) { result.arguments.emplace_back(v); },
m_parser.read_integer_or_real());
} else if (m_parser.peek_name()) {
result.arguments.push_back(m_parser.read_name());
result.arguments.emplace_back(m_parser.read_name());
} else if (m_parser.peek_string()) {
std::visit([&](auto s) { result.arguments.push_back(std::move(s)); },
std::visit([&](auto s) { result.arguments.emplace_back(std::move(s)); },
m_parser.read_string());
} else if (m_parser.peek_array()) {
result.arguments.push_back(m_parser.read_array());
result.arguments.emplace_back(m_parser.read_array());
} else if (m_parser.peek_dictionary()) {
result.arguments.push_back(m_parser.read_dictionary());
result.arguments.emplace_back(m_parser.read_dictionary());
} else {
m_parser.skip_whitespace();
break;
Expand Down
143 changes: 77 additions & 66 deletions src/odr/internal/pdf/pdf_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,28 @@ using Boolean = bool;
struct StandardString {
std::string string;

StandardString(std::string _string) : string{std::move(_string)} {}
explicit StandardString(std::string _string) : string{std::move(_string)} {}

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const StandardString &);
[[nodiscard]] std::string to_string() const;
};

struct HexString {
std::string string;

HexString(std::string _string) : string{std::move(_string)} {}
explicit HexString(std::string _string) : string{std::move(_string)} {}

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const HexString &);
[[nodiscard]] std::string to_string() const;
};

struct Name {
std::string string;

Name(std::string _string) : string{std::move(_string)} {}
explicit Name(std::string _string) : string{std::move(_string)} {}

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const Name &);
[[nodiscard]] std::string to_string() const;
};

struct ObjectReference {
Expand All @@ -58,8 +55,7 @@ struct ObjectReference {
[[nodiscard]] std::size_t hash() const noexcept;

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const ObjectReference &);
[[nodiscard]] std::string to_string() const;
};

class Array;
Expand All @@ -70,47 +66,53 @@ class Object {
using Holder = std::any;

Object() = default;
Object(Boolean boolean) : m_holder{boolean} {}
Object(Integer integer) : m_holder{integer} {}
Object(Real real) : m_holder{real} {}
Object(StandardString string) : m_holder{std::move(string)} {}
Object(HexString string) : m_holder{std::move(string)} {}
Object(Name name) : m_holder{std::move(name)} {}
Object(Array);
Object(Dictionary);
Object(ObjectReference reference) : m_holder{std::move(reference)} {}

Holder &holder() { return m_holder; }
const Holder &holder() const { return m_holder; }

bool is_null() const { return !m_holder.has_value(); }
bool is_bool() const { return is<Boolean>(); }
bool is_integer() const { return is<Integer>(); }
bool is_real() const { return is<Real>() || is_integer(); }
bool is_standard_string() const { return is<StandardString>(); }
bool is_hex_string() const { return is<HexString>(); }
bool is_name() const { return is<Name>(); }
bool is_string() const {
explicit Object(Boolean boolean) : m_holder{boolean} {}
explicit Object(Integer integer) : m_holder{integer} {}
explicit Object(Real real) : m_holder{real} {}
explicit Object(StandardString string) : m_holder{std::move(string)} {}
explicit Object(HexString string) : m_holder{std::move(string)} {}
explicit Object(Name name) : m_holder{std::move(name)} {}
explicit Object(Array);
explicit Object(Dictionary);
explicit Object(ObjectReference reference) : m_holder{reference} {}

[[nodiscard]] Holder &holder() { return m_holder; }
[[nodiscard]] const Holder &holder() const { return m_holder; }

[[nodiscard]] bool is_null() const { return !m_holder.has_value(); }
[[nodiscard]] bool is_bool() const { return is<Boolean>(); }
[[nodiscard]] bool is_integer() const { return is<Integer>(); }
[[nodiscard]] bool is_real() const { return is<Real>() || is_integer(); }
[[nodiscard]] bool is_standard_string() const { return is<StandardString>(); }
[[nodiscard]] bool is_hex_string() const { return is<HexString>(); }
[[nodiscard]] bool is_name() const { return is<Name>(); }
[[nodiscard]] bool is_string() const {
return is_standard_string() || is_hex_string() || is_name();
}
bool is_array() const { return is<Array>(); }
bool is_dictionary() const { return is<Dictionary>(); }
bool is_reference() const { return is<ObjectReference>(); }

Boolean as_bool() const { return as<Boolean>(); }
Integer as_integer() const { return as<Integer>(); }
Real as_real() const { return is<Real>() ? as<Real>() : as_integer(); }
const std::string &as_standard_string() const {
[[nodiscard]] bool is_array() const { return is<Array>(); }
[[nodiscard]] bool is_dictionary() const { return is<Dictionary>(); }
[[nodiscard]] bool is_reference() const { return is<ObjectReference>(); }

[[nodiscard]] Boolean as_bool() const { return as<Boolean>(); }
[[nodiscard]] Integer as_integer() const { return as<Integer>(); }
[[nodiscard]] Real as_real() const {
return is<Real>() ? as<Real>() : as_integer();
}
[[nodiscard]] const std::string &as_standard_string() const {
return as<const StandardString &>().string;
}
const std::string &as_hex_string() const {
[[nodiscard]] const std::string &as_hex_string() const {
return as<const HexString &>().string;
}
const std::string &as_name() const { return as<const Name &>().string; }
const std::string &as_string() const;
const Array &as_array() const & { return as<const Array &>(); }
const Dictionary &as_dictionary() const & { return as<const Dictionary &>(); }
const ObjectReference &as_reference() const {
[[nodiscard]] const std::string &as_name() const {
return as<const Name &>().string;
}
[[nodiscard]] const std::string &as_string() const;
[[nodiscard]] const Array &as_array() const & { return as<const Array &>(); }
[[nodiscard]] const Dictionary &as_dictionary() const & {
return as<const Dictionary &>();
}
[[nodiscard]] const ObjectReference &as_reference() const {
return as<const ObjectReference &>();
}

Expand All @@ -123,8 +125,7 @@ class Object {
}

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const Object &);
[[nodiscard]] std::string to_string() const;

private:
Holder m_holder;
Expand All @@ -149,21 +150,22 @@ class Array {
Array &operator=(const Array &) = default;
Array &operator=(Array &&) = default;

Holder &holder() { return m_holder; }
const Holder &holder() const { return m_holder; }
[[nodiscard]] Holder &holder() { return m_holder; }
[[nodiscard]] const Holder &holder() const { return m_holder; }

std::size_t size() const { return m_holder.size(); }
Holder::iterator begin() { return m_holder.begin(); }
Holder::iterator end() { return m_holder.end(); }
Holder::const_iterator begin() const { return m_holder.cbegin(); }
Holder::const_iterator end() const { return m_holder.cend(); }
[[nodiscard]] std::size_t size() const { return m_holder.size(); }
[[nodiscard]] Holder::iterator begin() { return m_holder.begin(); }
[[nodiscard]] Holder::iterator end() { return m_holder.end(); }
[[nodiscard]] Holder::const_iterator begin() const {
return m_holder.cbegin();
}
[[nodiscard]] Holder::const_iterator end() const { return m_holder.cend(); }

Object &operator[](std::size_t i) { return m_holder.at(i); }
const Object &operator[](std::size_t i) const { return m_holder.at(i); }

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const Array &);
[[nodiscard]] std::string to_string() const;

private:
Holder m_holder;
Expand All @@ -177,31 +179,40 @@ class Dictionary {
explicit Dictionary(Holder holder) : m_holder{std::move(holder)} {}

Holder &holder() { return m_holder; }
const Holder &holder() const { return m_holder; }
[[nodiscard]] const Holder &holder() const { return m_holder; }

std::size_t size() const { return m_holder.size(); }
Holder::iterator begin() { return m_holder.begin(); }
Holder::iterator end() { return m_holder.end(); }
Holder::const_iterator begin() const { return m_holder.cbegin(); }
Holder::const_iterator end() const { return m_holder.cend(); }
[[nodiscard]] std::size_t size() const { return m_holder.size(); }
[[nodiscard]] Holder::iterator begin() { return m_holder.begin(); }
[[nodiscard]] Holder::iterator end() { return m_holder.end(); }
[[nodiscard]] Holder::const_iterator begin() const {
return m_holder.cbegin();
}
[[nodiscard]] Holder::const_iterator end() const { return m_holder.cend(); }

Object &operator[](const std::string &name) { return m_holder[name]; }
const Object &operator[](const std::string &name) const {
return m_holder.at(name);
}

bool has_key(const std::string &name) const {
[[nodiscard]] bool has_key(const std::string &name) const {
return m_holder.find(name) != std::end(m_holder);
}

void to_stream(std::ostream &) const;
std::string to_string() const;
friend std::ostream &operator<<(std::ostream &, const Dictionary &);
[[nodiscard]] std::string to_string() const;

private:
Holder m_holder;
};

std::ostream &operator<<(std::ostream &, const StandardString &);
std::ostream &operator<<(std::ostream &, const HexString &);
std::ostream &operator<<(std::ostream &, const Name &);
std::ostream &operator<<(std::ostream &, const ObjectReference &);
std::ostream &operator<<(std::ostream &, const Object &);
std::ostream &operator<<(std::ostream &, const Array &);
std::ostream &operator<<(std::ostream &, const Dictionary &);

} // namespace odr::internal::pdf

template <> struct std::hash<odr::internal::pdf::ObjectReference> {
Expand Down
17 changes: 9 additions & 8 deletions src/odr/internal/pdf/pdf_object_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void ObjectParser::read_name(std::ostream &out) const {
Name ObjectParser::read_name() const {
std::stringstream ss;
read_name(ss);
return ss.str();
return Name(ss.str());
}

bool ObjectParser::peek_null() const {
Expand Down Expand Up @@ -409,7 +409,7 @@ Dictionary ObjectParser::read_dictionary() const {
skip_whitespace();

UnsignedInteger id = value.as_integer();
value = ObjectReference{id, gen};
value = Object(ObjectReference{id, gen});
}

result.emplace(std::move(name.string), std::move(value));
Expand All @@ -424,23 +424,24 @@ Object ObjectParser::read_object() const {
return {};
}
if (peek_boolean()) {
return read_boolean();
return Object(read_boolean());
}
if (peek_number()) {
return std::visit([](auto v) -> Object { return v; },
return std::visit([](auto v) -> Object { return Object(v); },
read_integer_or_real());
}
if (peek_name()) {
return read_name();
return Object(read_name());
}
if (peek_string()) {
return std::visit([](auto v) -> Object { return v; }, read_string());
return std::visit([](auto v) -> Object { return Object(v); },
read_string());
}
if (peek_array()) {
return read_array();
return Object(read_array());
}
if (peek_dictionary()) {
return read_dictionary();
return Object(read_dictionary());
}

throw std::runtime_error("unknown object");
Expand Down

0 comments on commit 3366c8a

Please sign in to comment.