-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpdf_file.cpp
More file actions
221 lines (175 loc) · 7.22 KB
/
Copy pathpdf_file.cpp
File metadata and controls
221 lines (175 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <odr/internal/html/pdf_file.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/internal/crypto/crypto_util.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/pdf/pdf_document.hpp>
#include <odr/internal/pdf/pdf_document_element.hpp>
#include <odr/internal/pdf/pdf_document_parser.hpp>
#include <odr/internal/pdf/pdf_graphics_operator.hpp>
#include <odr/internal/pdf/pdf_graphics_operator_parser.hpp>
#include <odr/internal/pdf/pdf_graphics_state.hpp>
#include <fstream>
#include <iostream>
namespace odr::internal::html {
namespace {
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(PdfFile pdf_file, HtmlConfig config,
std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_pdf_file{std::move(pdf_file)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "document", 0, "document.html"));
}
void warmup() const override {}
[[nodiscard]] const HtmlViews &list_views() const override { return m_views; }
[[nodiscard]] bool exists(const std::string &path) const override {
if (path == "document.html") {
return true;
}
return false;
}
[[nodiscard]] std::string mimetype(const std::string &path) const override {
if (path == "document.html") {
return "text/html";
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
if (path == "document.html") {
HtmlWriter writer(out, config());
write_document(writer);
return;
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
if (path == "document.html") {
return write_document(out);
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_document(HtmlWriter &out) const {
HtmlResources resources;
auto in = m_pdf_file.file().stream();
pdf::DocumentParser parser(*in);
std::unique_ptr<pdf::Document> document = parser.parse_document();
std::vector<pdf::Page *> ordered_pages;
std::function<void(pdf::Pages * pages)> recurse_pages =
[&](const pdf::Pages *pages) {
for (pdf::Element *kid : pages->kids) {
if (auto *p = dynamic_cast<pdf::Pages *>(kid); p != nullptr) {
recurse_pages(p);
} else if (auto page = dynamic_cast<pdf::Page *>(kid);
page != nullptr) {
ordered_pages.push_back(page);
} else {
throw std::runtime_error("unhandled element");
}
}
};
recurse_pages(document->catalog->pages);
out.write_begin();
out.write_header_begin();
out.write_header_charset("UTF-8");
out.write_header_target("_blank");
out.write_header_title("odr");
out.write_header_viewport(
"width=device-width,initial-scale=1.0,user-scalable=yes");
out.write_header_end();
out.write_body_begin();
for (pdf::Page *page : ordered_pages) {
pdf::Array page_box = page->object.as_dictionary()["MediaBox"].as_array();
out.write_element_begin(
"div", HtmlElementOptions().set_style([&](std::ostream &o) {
o << "position:relative;";
o << "width:" << page_box[2].as_real() / 72.0 << "in;";
o << "height:" << page_box[3].as_real() / 72.0 << "in;";
}));
std::string stream;
for (const auto &content_reference : page->contents_reference) {
pdf::IndirectObject page_contents_object =
parser.read_object(content_reference);
std::string page_content =
parser.read_object_stream(page_contents_object);
page_content = crypto::util::zlib_inflate(page_content);
stream += page_content;
}
std::istringstream ss(stream);
pdf::GraphicsOperatorParser parser2(ss);
pdf::GraphicsState state;
while (!ss.eof()) {
pdf::GraphicsOperator op = parser2.read_operator();
state.execute(op);
if (op.type == pdf::GraphicsOperatorType::text_next_line) {
double leading = state.current().text.leading;
double size = state.current().text.size;
state.current().text.offset[1] -= size + leading;
} else if (op.type == pdf::GraphicsOperatorType::show_text) {
const std::string &font_ref = state.current().text.font;
double size = state.current().text.size;
std::array<double, 2> offset = state.current().text.offset;
pdf::Font *font = page->resources->font.at(font_ref);
const std::string &glyphs = op.arguments[0].as_string();
std::string unicode = font->cmap.translate_string(glyphs);
if (unicode.find("Colored Line") != std::string::npos) {
std::cout << "hi" << std::endl;
}
out.write_element_begin(
"span", HtmlElementOptions().set_style([&](std::ostream &o) {
o << "position:absolute;";
o << "left:" << offset[0] / 72.0 << "in;";
o << "bottom:" << offset[1] / 72.0 << "in;";
o << "font-size:" << size << "pt;";
}));
out.write_raw(unicode);
out.write_element_end("span");
} else if (op.type ==
pdf::GraphicsOperatorType::show_text_manual_spacing) {
const std::string &font_ref = state.current().text.font;
pdf::Font *font = page->resources->font.at(font_ref);
double size = state.current().text.size;
std::cout << font->object << std::endl;
for (const auto &element : op.arguments[0].as_array()) {
if (element.is_real()) {
std::cout << "spacing: " << element.as_real() << std::endl;
} else if (element.is_string()) {
const std::string &glyphs = element.as_string();
std::string unicode = font->cmap.translate_string(glyphs);
std::cout << "show text manual spacing: font=" << font
<< ", size=" << size << ", text=" << unicode
<< std::endl;
}
}
} else if (op.type == pdf::GraphicsOperatorType::show_text_next_line) {
std::cout << "TODO show_text_next_line" << std::endl;
} else if (op.type ==
pdf::GraphicsOperatorType::show_text_next_line_set_spacing) {
std::cout << "TODO show_text_next_line_set_spacing" << std::endl;
}
}
out.write_element_end("div");
}
out.write_body_end();
out.write_end();
return resources;
}
protected:
PdfFile m_pdf_file;
HtmlViews m_views;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
HtmlService html::create_pdf_service(const PdfFile &pdf_file,
const std::string & /*cache_path*/,
HtmlConfig config,
std::shared_ptr<Logger> logger) {
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
pdf_file, std::move(config), std::move(logger)));
}
} // namespace odr::internal