Skip to content

Commit 21dc909

Browse files
authored
to string (#346)
1 parent 0614e3d commit 21dc909

4 files changed

Lines changed: 204 additions & 1 deletion

File tree

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,87 @@
11
#include <odr/internal/pdf/pdf_graphics_operator.hpp>
22

3-
namespace odr::internal::pdf {}
3+
#include <iomanip>
4+
#include <ostream>
5+
#include <sstream>
6+
7+
namespace odr::internal::pdf {
8+
9+
void SimpleArrayElement::to_stream(std::ostream &out) const {
10+
if (is_integer()) {
11+
out << as_integer();
12+
} else if (is_real()) {
13+
out << std::setprecision(4) << as_real();
14+
} else if (is_string()) {
15+
// TODO restore original format
16+
out << as_string();
17+
} else {
18+
throw std::runtime_error("unhandled type");
19+
}
20+
}
21+
22+
std::string SimpleArrayElement::to_string() const {
23+
std::ostringstream ss;
24+
to_stream(ss);
25+
return ss.str();
26+
}
27+
28+
void SimpleArray::to_stream(std::ostream &out) const {
29+
out << "[";
30+
31+
for (const auto &item : *this) {
32+
item.to_stream(out);
33+
out << " ";
34+
}
35+
36+
out << " ]";
37+
}
38+
39+
std::string SimpleArray::to_string() const {
40+
std::ostringstream ss;
41+
to_stream(ss);
42+
return ss.str();
43+
}
44+
45+
void GraphicsArgument::to_stream(std::ostream &out) const {
46+
if (is_integer()) {
47+
out << as_integer();
48+
} else if (is_real()) {
49+
out << std::setprecision(4) << as_real();
50+
} else if (is_string()) {
51+
// TODO restore original format
52+
out << as_string();
53+
} else if (is_array()) {
54+
as_array().to_stream(out);
55+
} else {
56+
throw std::runtime_error("unhandled type");
57+
}
58+
}
59+
60+
std::string GraphicsArgument::to_string() const {
61+
std::ostringstream ss;
62+
to_stream(ss);
63+
return ss.str();
64+
}
65+
66+
} // namespace odr::internal::pdf
67+
68+
namespace odr::internal {
69+
70+
std::ostream &pdf::operator<<(std::ostream &out,
71+
const SimpleArrayElement &element) {
72+
element.to_stream(out);
73+
return out;
74+
}
75+
76+
std::ostream &pdf::operator<<(std::ostream &out, const SimpleArray &array) {
77+
array.to_stream(out);
78+
return out;
79+
}
80+
81+
std::ostream &pdf::operator<<(std::ostream &out,
82+
const GraphicsArgument &argument) {
83+
argument.to_stream(out);
84+
return out;
85+
}
86+
87+
} // namespace odr::internal

src/odr/internal/pdf/pdf_graphics_operator.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <odr/internal/pdf/pdf_object.hpp>
55

6+
#include <iosfwd>
67
#include <string>
78
#include <variant>
89
#include <vector>
@@ -28,6 +29,10 @@ class SimpleArrayElement {
2829
Real as_real() const { return is<Real>() ? as<Real>() : as_integer(); }
2930
const std::string &as_string() const { return as<std::string>(); }
3031

32+
void to_stream(std::ostream &) const;
33+
std::string to_string() const;
34+
friend std::ostream &operator<<(std::ostream &, const SimpleArrayElement &);
35+
3136
private:
3237
Holder m_holder;
3338

@@ -55,6 +60,10 @@ class SimpleArray {
5560
return m_holder.at(i);
5661
}
5762

63+
void to_stream(std::ostream &) const;
64+
std::string to_string() const;
65+
friend std::ostream &operator<<(std::ostream &, const SimpleArray &);
66+
5867
private:
5968
Holder m_holder;
6069
};
@@ -81,6 +90,10 @@ class GraphicsArgument {
8190
const std::string &as_string() const { return as<std::string>(); }
8291
const SimpleArray &as_array() const { return as<SimpleArray>(); }
8392

93+
void to_stream(std::ostream &) const;
94+
std::string to_string() const;
95+
friend std::ostream &operator<<(std::ostream &, const GraphicsArgument &);
96+
8497
private:
8598
Holder m_holder;
8699

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,102 @@
11
#include <odr/internal/pdf/pdf_object.hpp>
22

3+
#include <iomanip>
4+
#include <ostream>
5+
#include <sstream>
6+
#include <stdexcept>
7+
38
namespace odr::internal::pdf {
49

510
Object::Object(Array array) : m_holder{std::move(array)} {}
611

712
Object::Object(Dictionary dictionary) : m_holder{std::move(dictionary)} {}
813

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+
983
} // 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

src/odr/internal/pdf/pdf_object.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <any>
55
#include <cstdint>
6+
#include <iosfwd>
67
#include <map>
78
#include <string>
89
#include <variant>
@@ -57,6 +58,10 @@ class Object {
5758
return as<const ObjectReference &>();
5859
}
5960

61+
void to_stream(std::ostream &) const;
62+
std::string to_string() const;
63+
friend std::ostream &operator<<(std::ostream &, const Object &);
64+
6065
private:
6166
Holder m_holder;
6267

@@ -85,6 +90,10 @@ class Array {
8590

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

93+
void to_stream(std::ostream &) const;
94+
std::string to_string() const;
95+
friend std::ostream &operator<<(std::ostream &, const Array &);
96+
8897
private:
8998
Holder m_holder;
9099
};
@@ -109,6 +118,10 @@ class Dictionary {
109118
return m_holder.find(name) != std::end(m_holder);
110119
}
111120

121+
void to_stream(std::ostream &) const;
122+
std::string to_string() const;
123+
friend std::ostream &operator<<(std::ostream &, const Dictionary &);
124+
112125
private:
113126
Holder m_holder;
114127
};

0 commit comments

Comments
 (0)