qpdf_ruby 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,294 +0,0 @@
1
- #include "pdf_image_mapper.hpp"
2
- #include <qpdf/BufferInputSource.hh>
3
- #include <qpdf/QPDFTokenizer.hh>
4
-
5
- #include <algorithm>
6
- #include <optional>
7
-
8
- using Matrix = std::array<double, 6>;
9
-
10
- static std::pair<double, double> apply_matrix(const Matrix& m, double x, double y) {
11
- double x_new = m[0] * x + m[2] * y + m[4];
12
- double y_new = m[1] * x + m[3] * y + m[5];
13
- return {x_new, y_new};
14
- }
15
-
16
- static std::array<double, 4> compute_bbox(double width, double height, const Matrix& matrix) {
17
- std::array<std::pair<double, double>, 4> corners = {apply_matrix(matrix, 0, 0), apply_matrix(matrix, width, 0),
18
- apply_matrix(matrix, width, height),
19
- apply_matrix(matrix, 0, height)};
20
-
21
- double min_x = corners[0].first;
22
- double max_x = corners[0].first;
23
- double min_y = corners[0].second;
24
- double max_y = corners[0].second;
25
-
26
- for (int i = 1; i < 4; ++i) {
27
- min_x = std::min(min_x, corners[i].first);
28
- max_x = std::max(max_x, corners[i].first);
29
- min_y = std::min(min_y, corners[i].second);
30
- max_y = std::max(max_y, corners[i].second);
31
- }
32
-
33
- return {min_x, min_y, max_x, max_y};
34
- }
35
-
36
- static char const* tokenTypeName(QPDFTokenizer::token_type_e ttype) {
37
- // Do this is a case statement instead of a lookup so the compiler
38
- // will warn if we miss any.
39
- switch (ttype) {
40
- case QPDFTokenizer::tt_bad:
41
- return "bad";
42
- case QPDFTokenizer::tt_array_close:
43
- return "array_close";
44
- case QPDFTokenizer::tt_array_open:
45
- return "array_open";
46
- case QPDFTokenizer::tt_brace_close:
47
- return "brace_close";
48
- case QPDFTokenizer::tt_brace_open:
49
- return "brace_open";
50
- case QPDFTokenizer::tt_dict_close:
51
- return "dict_close";
52
- case QPDFTokenizer::tt_dict_open:
53
- return "dict_open";
54
- case QPDFTokenizer::tt_integer:
55
- return "integer";
56
- case QPDFTokenizer::tt_name:
57
- return "name";
58
- case QPDFTokenizer::tt_real:
59
- return "real";
60
- case QPDFTokenizer::tt_string:
61
- return "string";
62
- case QPDFTokenizer::tt_null:
63
- return "null";
64
- case QPDFTokenizer::tt_bool:
65
- return "bool";
66
- case QPDFTokenizer::tt_word:
67
- return "word";
68
- case QPDFTokenizer::tt_eof:
69
- return "eof";
70
- case QPDFTokenizer::tt_space:
71
- return "space";
72
- case QPDFTokenizer::tt_comment:
73
- return "comment";
74
- case QPDFTokenizer::tt_inline_image:
75
- return "inline-image";
76
- }
77
- return nullptr;
78
- }
79
-
80
- static std::optional<ImageInfo> get_image_info(QPDFObjectHandle resources, const std::string& name) {
81
- if (resources.isNull() || !resources.isDictionary()) {
82
- return std::nullopt;
83
- }
84
-
85
- QPDFObjectHandle xobjects = resources.getKey("/XObject");
86
- if (xobjects.isNull() || !xobjects.isDictionary()) {
87
- return std::nullopt;
88
- }
89
-
90
- if (!xobjects.hasKey(name)) {
91
- return std::nullopt;
92
- }
93
-
94
- QPDFObjectHandle xobject = xobjects.getKey(name);
95
- if (!xobject.isStream()) {
96
- return std::nullopt;
97
- }
98
-
99
- QPDFObjectHandle dict = xobject.getDict();
100
- bool is_image =
101
- (dict.hasKey("/Subtype") && dict.getKey("/Subtype").isName() && dict.getKey("/Subtype").getName() == "/Image");
102
-
103
- if (is_image) {
104
- ImageInfo image_info;
105
-
106
- if (dict.hasKey("/Height") && dict.getKey("/Height").isInteger()) {
107
- image_info.height = dict.getKey("/Height").getIntValue();
108
- }
109
-
110
- if (dict.hasKey("/Width") && dict.getKey("/Width").isInteger()) {
111
- image_info.width = dict.getKey("/Width").getIntValue();
112
- }
113
-
114
- return image_info;
115
- }
116
-
117
- return std::nullopt;
118
- ;
119
- }
120
-
121
- PDFImageMapper::PDFImageMapper(int target_mcid) : target_mcid(target_mcid), in_target_mcid(false) {}
122
-
123
- void PDFImageMapper::push_cm(double value) {
124
- if (cm_fixed_size_queue.size() == 6) {
125
- cm_fixed_size_queue.pop_front(); // Remove oldest element
126
- }
127
- cm_fixed_size_queue.push_back(value);
128
- }
129
-
130
- void PDFImageMapper::find(QPDF& pdf) {
131
- QPDFPageDocumentHelper doc_helper(pdf);
132
- std::vector<QPDFPageObjectHelper> pages = doc_helper.getAllPages();
133
- for (auto& page : pages) {
134
- QPDFPageObjectHelper poh(page);
135
-
136
- auto crop_box = poh.getCropBox();
137
-
138
- this->find(poh);
139
- }
140
- }
141
-
142
- struct OperatorInfo {
143
- const char* name;
144
- const char* description;
145
- int operand_count;
146
- };
147
-
148
- static constexpr OperatorInfo OPERATOR_CM = {"cm", "Concatenate matrix (set CTM)", 6};
149
- static constexpr OperatorInfo OPERATOR_Q = {"q", "Save graphics state", 0};
150
- static constexpr OperatorInfo OPERATOR_Q_UPPER = {"Q", "Restore graphics state", 0};
151
- static constexpr OperatorInfo OPERATOR_DO = {"Do", "Invoke named XObject (draw image/form)", 1};
152
- static constexpr OperatorInfo OPERATOR_BDC = {"BDC", "Begin Marked Content sequence with property list", 2};
153
- static constexpr OperatorInfo OPERATOR_EMC = {"EMC", "End Marked Content sequence", 0};
154
-
155
- class CMDoExtractor : public QPDFObjectHandle::ParserCallbacks {
156
- public:
157
- explicit CMDoExtractor(QPDFPageObjectHelper& page_ref) : page(page_ref) {
158
- // You can add any other initialization logic here if needed
159
- }
160
-
161
- std::vector<double> current_matrix = {1, 0, 0, 1, 0, 0};
162
- std::stack<std::vector<double>> matrix_stack;
163
- std::vector<QPDFObjectHandle> operand_stack;
164
-
165
- std::stack<int> mcid_stack;
166
- int current_mcid;
167
-
168
- void handleEOF() override {
169
- // No action needed for this example
170
- }
171
-
172
- std::optional<ImageInfo> image_info(std::string imgName) {
173
- QPDFObjectHandle resources = page.getObjectHandle().getKey("/Resources");
174
-
175
- return get_image_info(resources, imgName);
176
- }
177
-
178
- void handleObject(QPDFObjectHandle obj, size_t offset, size_t length) override {
179
- if (obj.isOperator()) {
180
- std::string op = obj.getOperatorValue();
181
-
182
- if (op == OPERATOR_CM.name && operand_stack.size() >= OPERATOR_CM.operand_count) {
183
- auto numeric_at = [&](std::size_t distance_from_top) -> double {
184
- auto& obj = operand_stack[operand_stack.size() - distance_from_top];
185
- auto t = obj.getTypeName(); // "integer", "real", …
186
- if (std::strcmp(t, "integer") != 0 && std::strcmp(t, "real") != 0) {
187
- std::stringstream ss;
188
- ss << "numeric operand expected (got " << t << ")";
189
- throw std::runtime_error(ss.str());
190
- }
191
- return obj.getNumericValue();
192
- };
193
-
194
- // pull operands (top of stack is distance 1)
195
- double f = numeric_at(1);
196
- double e = numeric_at(2);
197
- double d = numeric_at(3);
198
- double c = numeric_at(4);
199
- double b = numeric_at(5);
200
- double a = numeric_at(6);
201
- current_matrix = {a, b, c, d, e, f};
202
-
203
- operand_stack.resize(operand_stack.size() - 6);
204
- } else if (op == OPERATOR_Q.name) {
205
- matrix_stack.push(current_matrix);
206
- } else if (op == OPERATOR_Q_UPPER.name) {
207
- if (!matrix_stack.empty()) {
208
- current_matrix = matrix_stack.top();
209
- matrix_stack.pop();
210
- }
211
- } else if (op == OPERATOR_BDC.name && operand_stack.size() >= OPERATOR_BDC.operand_count) {
212
- mcid_stack.push(current_mcid); // Save current MCID
213
-
214
- QPDFObjectHandle properties_obj = operand_stack.back(); // Properties operand
215
- // QPDFObjectHandle tag_obj = operand_stack[operand_stack.size() - 2]; // Tag operand
216
-
217
- int mcid_val = -1; // Default if MCID not found or not an integer
218
- if (properties_obj.isDictionary()) {
219
- if (properties_obj.hasKey("/MCID") && properties_obj.getKey("/MCID").isInteger()) {
220
- mcid_val = properties_obj.getKey("/MCID").getIntValue();
221
- }
222
- } else if (properties_obj.isName()) {
223
- // Properties is a name, look it up in Resources /Properties dictionary
224
- QPDFObjectHandle resources = page.getObjectHandle().getKey("/Resources");
225
- if (resources.isDictionary() && resources.hasKey("/Properties")) {
226
- QPDFObjectHandle properties_dict = resources.getKey("/Properties");
227
- if (properties_dict.isDictionary() && properties_dict.hasKey(properties_obj.getName())) {
228
- QPDFObjectHandle actual_props = properties_dict.getKey(properties_obj.getName());
229
- if (actual_props.isDictionary() && actual_props.hasKey("/MCID") &&
230
- actual_props.getKey("/MCID").isInteger()) {
231
- mcid_val = actual_props.getKey("/MCID").getIntValue();
232
- }
233
- }
234
- }
235
- }
236
- current_mcid = mcid_val;
237
- operand_stack.resize(operand_stack.size() - OPERATOR_BDC.operand_count);
238
- } else if (op == OPERATOR_EMC.name) { // EMC takes 0 operands
239
- if (!mcid_stack.empty()) {
240
- current_mcid = mcid_stack.top();
241
- mcid_stack.pop();
242
- } else {
243
- current_mcid = -1; // Reset to default if stack underflow (should be balanced)
244
- }
245
- // EMC takes 0 operands, so no change to operand_stack based on operand_count
246
- } else if (op == OPERATOR_DO.name && !operand_stack.empty()) {
247
- std::string imgName = operand_stack.back().getName();
248
- auto image_info = this->image_info(imgName);
249
-
250
- std::copy(current_matrix.begin(), current_matrix.end(), image_info->cm_matrix.begin());
251
-
252
- image_info->mcid = current_mcid;
253
-
254
- image_info->bbox = compute_bbox(image_info->width, image_info->height, image_info->cm_matrix);
255
-
256
- QPDFObjectHandle mb = page.getObjectHandle().getKey("/MediaBox");
257
-
258
- double px0 = mb.getArrayItem(0).getNumericValue();
259
- double py0 = mb.getArrayItem(1).getNumericValue();
260
- double px1 = mb.getArrayItem(2).getNumericValue();
261
- double py1 = mb.getArrayItem(3).getNumericValue();
262
-
263
- auto& bb = image_info->bbox; // shorthand reference
264
- bb[0] = std::max(bb[0], px0); // left edge
265
- bb[1] = std::max(bb[1], py0); // bottom edge
266
- bb[2] = std::min(bb[2], px1); // right edge
267
- bb[3] = std::min(bb[3], py1); // top edge
268
-
269
- image_to_mcid[imgName] = *image_info;
270
-
271
- operand_stack.pop_back();
272
- } else {
273
- // For other operators, just clear the operand stack
274
- operand_stack.clear();
275
- }
276
- } else {
277
- operand_stack.push_back(obj);
278
- }
279
- }
280
-
281
- const std::map<std::string, ImageInfo>& getImageMap() const { return image_to_mcid; }
282
-
283
- private:
284
- QPDFPageObjectHelper& page;
285
- std::map<std::string, ImageInfo> image_to_mcid;
286
- };
287
-
288
- void PDFImageMapper::find(QPDFPageObjectHelper& page) {
289
- CMDoExtractor cb(page);
290
- page.parseContents(&cb);
291
-
292
- const auto& extracted_map = cb.getImageMap();
293
- image_to_mcid.insert(extracted_map.begin(), extracted_map.end());
294
- }
@@ -1,68 +0,0 @@
1
- // x_object_finder.hpp
2
- #pragma once
3
-
4
- #define POINTERHOLDER_TRANSITION 1
5
-
6
- #include <qpdf/QPDF.hh>
7
- #include <qpdf/QPDFPageDocumentHelper.hh>
8
- #include <qpdf/QPDFPageObjectHelper.hh>
9
- #include <qpdf/QPDFObjectHandle.hh>
10
- #include <qpdf/QIntC.hh>
11
- #include <qpdf/QPDFExc.hh>
12
- #include <qpdf/QTC.hh>
13
- #include <qpdf/QUtil.hh>
14
- #include <qpdf/BufferInputSource.hh>
15
- #include <qpdf/QPDFTokenizer.hh>
16
- #include <qpdf/Buffer.hh>
17
-
18
- #include <string>
19
- #include <vector>
20
- #include <iostream>
21
- #include <map>
22
- #include <stack>
23
- #include <regex>
24
- #include <sstream>
25
- #include <deque>
26
-
27
- struct ImageInfo {
28
- int mcid;
29
- double width;
30
- double height;
31
- std::array<double, 6> cm_matrix; // stores the transformation matrix values
32
- std::array<double, 4> bbox = {0, 0, 0, 0}; // user-space bounding box [llx, lly, urx, ury]
33
-
34
- ImageInfo() : mcid(-1), width(0), height(0), cm_matrix{0, 0, 0, 0, 0, 0} {}
35
-
36
- ImageInfo(int m, double w, double h, const std::array<double, 6>& cm) : mcid(m), width(w), height(h), cm_matrix(cm) {}
37
-
38
- std::string to_string() const {
39
- std::ostringstream oss;
40
- oss << "ImageInfo(mcid=" << mcid << ", width=" << width << ", height=" << height << ", cm_matrix=[";
41
- for (size_t i = 0; i < cm_matrix.size(); ++i) {
42
- oss << cm_matrix[i];
43
- if (i != cm_matrix.size() - 1) oss << ", ";
44
- }
45
- oss << "])";
46
- return oss.str();
47
- }
48
- };
49
-
50
- class PDFImageMapper {
51
- public:
52
- explicit PDFImageMapper(int target_mcid);
53
-
54
- void find(QPDF& pdf);
55
- // Parses the page's content stream to find the XObject name for the MCID
56
- void find(QPDFPageObjectHelper& page);
57
-
58
- void push_cm(double value);
59
-
60
- // Expose internal map for external use
61
- const std::map<std::string, ImageInfo>& getImageMap() const { return image_to_mcid; }
62
-
63
- private:
64
- int target_mcid;
65
- bool in_target_mcid;
66
- std::map<std::string, ImageInfo> image_to_mcid;
67
- std::deque<double> cm_fixed_size_queue;
68
- };