|
1 | 1 | #include <odr/internal/pdf/pdf_object.hpp> |
2 | 2 |
|
| 3 | +#include <iomanip> |
| 4 | +#include <ostream> |
| 5 | +#include <sstream> |
| 6 | +#include <stdexcept> |
| 7 | + |
3 | 8 | namespace odr::internal::pdf { |
4 | 9 |
|
5 | 10 | Object::Object(Array array) : m_holder{std::move(array)} {} |
6 | 11 |
|
7 | 12 | Object::Object(Dictionary dictionary) : m_holder{std::move(dictionary)} {} |
8 | 13 |
|
| 14 | +void Object::to_stream(std::ostream &out) const { |
| 15 | + if (is_null()) { |
| 16 | + out << "null"; |
| 17 | + } else if (is_bool()) { |
| 18 | + if (as_bool()) { |
| 19 | + out << "true"; |
| 20 | + } else { |
| 21 | + out << "false"; |
| 22 | + } |
| 23 | + } else if (is_integer()) { |
| 24 | + out << as_integer(); |
| 25 | + } else if (is_real()) { |
| 26 | + out << std::setprecision(4) << as_real(); |
| 27 | + } else if (is_string()) { |
| 28 | + // TODO restore original format |
| 29 | + out << as_string(); |
| 30 | + } else if (is_array()) { |
| 31 | + as_array().to_stream(out); |
| 32 | + } else if (is_dictionary()) { |
| 33 | + as_dictionary().to_stream(out); |
| 34 | + } else if (is_reference()) { |
| 35 | + out << as_reference().first << " " << as_reference().second << " R"; |
| 36 | + } else { |
| 37 | + throw std::runtime_error("unhandled type"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +std::string Object::to_string() const { |
| 42 | + std::ostringstream ss; |
| 43 | + to_stream(ss); |
| 44 | + return ss.str(); |
| 45 | +} |
| 46 | + |
| 47 | +void Array::to_stream(std::ostream &out) const { |
| 48 | + out << "["; |
| 49 | + |
| 50 | + for (const auto &item : *this) { |
| 51 | + item.to_stream(out); |
| 52 | + out << " "; |
| 53 | + } |
| 54 | + |
| 55 | + out << " ]"; |
| 56 | +} |
| 57 | + |
| 58 | +std::string Array::to_string() const { |
| 59 | + std::ostringstream ss; |
| 60 | + to_stream(ss); |
| 61 | + return ss.str(); |
| 62 | +} |
| 63 | + |
| 64 | +void Dictionary::to_stream(std::ostream &out) const { |
| 65 | + out << "<<"; |
| 66 | + |
| 67 | + for (const auto &[key, value] : *this) { |
| 68 | + out << "/" << key; |
| 69 | + out << " "; |
| 70 | + value.to_stream(out); |
| 71 | + out << " "; |
| 72 | + } |
| 73 | + |
| 74 | + out << " >>"; |
| 75 | +} |
| 76 | + |
| 77 | +std::string Dictionary::to_string() const { |
| 78 | + std::ostringstream ss; |
| 79 | + to_stream(ss); |
| 80 | + return ss.str(); |
| 81 | +} |
| 82 | + |
9 | 83 | } // namespace odr::internal::pdf |
| 84 | + |
| 85 | +namespace odr::internal { |
| 86 | + |
| 87 | +std::ostream &pdf::operator<<(std::ostream &out, const Object &object) { |
| 88 | + object.to_stream(out); |
| 89 | + return out; |
| 90 | +} |
| 91 | + |
| 92 | +std::ostream &pdf::operator<<(std::ostream &out, const Array &array) { |
| 93 | + array.to_stream(out); |
| 94 | + return out; |
| 95 | +} |
| 96 | + |
| 97 | +std::ostream &pdf::operator<<(std::ostream &out, const Dictionary &dictionary) { |
| 98 | + dictionary.to_stream(out); |
| 99 | + return out; |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace odr::internal |
0 commit comments