summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJames R. Barlow <james@purplerock.ca>2021-08-22 01:49:11 -0700
committerJames R. Barlow <james@purplerock.ca>2021-08-22 01:49:11 -0700
commit8cbb99616b08d2b5722ee4029db6b061d3dc5e14 (patch)
tree0c7ff85ee2ef08271bccf5931a85cb5ebcb0cf04 /src
parente825652e46ebae716afde78ed2c74e18f13e4970 (diff)
More updates to support ContentStreamInstruction/InlineImage
Diffstat (limited to 'src')
-rw-r--r--src/pikepdf/_qpdf.pyi18
-rw-r--r--src/pikepdf/models/__init__.py14
-rw-r--r--src/qpdf/parsers.cpp11
3 files changed, 36 insertions, 7 deletions
diff --git a/src/pikepdf/_qpdf.pyi b/src/pikepdf/_qpdf.pyi
index 5017134..b15f572 100644
--- a/src/pikepdf/_qpdf.pyi
+++ b/src/pikepdf/_qpdf.pyi
@@ -663,6 +663,24 @@ class NameTree(MutableMapping[Union[str, bytes], Object]):
@property
def obj(self) -> Object: ...
+class ContentStreamInstruction:
+ @property
+ def operands(self) -> _ObjectList: ...
+ @property
+ def operator(self) -> Operator: ...
+ def __getitem__(self, index: int) -> Union[_ObjectList, Operator]: ...
+ def __len__(self) -> int: ...
+
+class ContentStreamInlineImage:
+ @property
+ def operands(self) -> _ObjectList: ...
+ @property
+ def operator(self) -> Operator: ...
+ def __getitem__(self, index: int) -> Union[_ObjectList, Operator]: ...
+ def __len__(self) -> int: ...
+ @property
+ def iimage(self) -> PdfInlineImage: ...
+
def _Null() -> Any: ...
def _encode(handle: Any) -> Object: ...
def _new_array(arg0: Iterable) -> Object: ...
diff --git a/src/pikepdf/models/__init__.py b/src/pikepdf/models/__init__.py
index 7aa709d..f0de95c 100644
--- a/src/pikepdf/models/__init__.py
+++ b/src/pikepdf/models/__init__.py
@@ -21,8 +21,16 @@ from .outlines import (
)
# Operands, Operator
-ContentStreamOperands = Collection[Union[Object, PdfInlineImage]]
-ContentStreamInstructions = Tuple[ContentStreamOperands, Operator]
+_OldContentStreamOperands = Collection[Union[Object, PdfInlineImage]]
+_OldContentStreamInstructions = Tuple[_OldContentStreamOperands, Operator]
+
+ContentStreamInstructions = Union[
+ _qpdf.ContentStreamInstruction, _qpdf.ContentStreamInlineImage
+]
+
+UnparseableContentStreamInstructions = Union[
+ ContentStreamInstructions, _OldContentStreamInstructions
+]
class PdfParsingError(Exception):
@@ -111,7 +119,7 @@ def parse_content_stream(
def unparse_content_stream(
- instructions: Collection[ContentStreamInstructions],
+ instructions: Collection[UnparseableContentStreamInstructions],
) -> bytes:
"""
Given a parsed list of instructions/operand-operators, convert to bytes suitable
diff --git a/src/qpdf/parsers.cpp b/src/qpdf/parsers.cpp
index dcc8bae..5efc760 100644
--- a/src/qpdf/parsers.cpp
+++ b/src/qpdf/parsers.cpp
@@ -152,6 +152,9 @@ py::bytes unparse_content_stream(py::iterable contentstream)
ss.imbue(std::locale::classic());
for (const auto &item : contentstream) {
+ // First iteration: print nothing
+ // All others: print "\n" to delimit previous
+ // Result is no leading or trailing delimiter
ss << delim;
delim = "\n";
@@ -169,12 +172,10 @@ py::bytes unparse_content_stream(py::iterable contentstream)
} catch (py::cast_error &) {
}
+ // Fallback: instruction is some combination of Python iterables.
+ // Destructure and convert to C++ types...
auto operands_op = py::reinterpret_borrow<py::sequence>(item);
- // First iteration: print nothing
- // All others: print "\n" to delimit previous
- // Result is no leading or trailing delimiter
-
if (operands_op.size() != 2) {
errmsg << "Wrong number of operands at content stream instruction " << n
<< "; expected 2";
@@ -253,6 +254,7 @@ void init_parsers(py::module_ &m)
throw py::index_error(
std::string("Invalid index ") + std::to_string(index));
})
+ .def("__len__", [](ContentStreamInstruction &csi) { return 2; })
.def("__repr__", [](ContentStreamInstruction &csi) {
return "pikepdf.ContentStreamInstruction()";
});
@@ -273,6 +275,7 @@ void init_parsers(py::module_ &m)
throw py::index_error(
std::string("Invalid index ") + std::to_string(index));
})
+ .def("__len__", [](ContentStreamInlineImage &csii) { return 2; })
.def_property_readonly("iimage",
[](ContentStreamInlineImage &csii) { return csii.get_inline_image(); })
.def("__repr__", [](ContentStreamInlineImage &csii) {