fixed stream parsing
This commit is contained in:
1 parent
3d39bd575f
commit
4653b53a6c
6 files changed
+47
-46
No files matched your search
@@ -1,3 +1,4 @@
|
||||
from pdfmajor.parser.stream.PDFStream import PDFStream
|
||||
from pdfmajor.parser.xref import XRefRow
|
||||
from pdfmajor.parser.xrefdb import XRefDB
|
||||
from typing import Iterator, Optional
|
||||
@@ -11,9 +12,8 @@ from pdfmajor.lexer.token import (
|
||||
TokenKeyword,
|
||||
)
|
||||
from pdfmajor.lexer import iter_tokens
|
||||
from .objects.stream import PDFStream
|
||||
from .objects.indirect import IndirectObject
|
||||
from .objects.base import PDFContextualObject, PDFObject
|
||||
from .objects.base import PDFObject
|
||||
from .objects.collections import PDFArray, PDFDictionary
|
||||
from .objects.comment import PDFComment
|
||||
from .state import ParsingState
|
||||
@@ -101,7 +101,7 @@ def _on_endobj(state: ParsingState, token: TokenKeyword) -> IndirectObject:
|
||||
raise BrokenFile(f"Invalid number of integers in obj defition {token}")
|
||||
for num in state.flush_int_collections():
|
||||
last_ctx.pass_item(num)
|
||||
return last_ctx.into_readonly_copy()
|
||||
return last_ctx
|
||||
else:
|
||||
raise ParserError(f"Recived an endobj while not procesing an indirect object")
|
||||
|
||||
@@ -120,20 +120,17 @@ def _on_stream(
|
||||
last_ctx = state.current_context
|
||||
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
||||
xrefdb.save_indobject(last_ctx)
|
||||
stream = PDFStream(token.end_loc + 1, 0)
|
||||
# stream = PDFStream(token.end_loc + 1, 0)
|
||||
obj = _save_or_get_object(buffer, xrefdb, last_ctx.obj_num, last_ctx.gen_num)
|
||||
with buffer.get_window():
|
||||
next_token = next(iter_tokens(buffer))
|
||||
last_ctx.stream = stream = PDFStream.from_pdfdict(token.end_loc, obj)
|
||||
buffer.seek(stream.offset + stream.length)
|
||||
next_token = next(iter_tokens(buffer))
|
||||
if not (
|
||||
isinstance(next_token, TokenKeyword) and next_token.value == b"endstream"
|
||||
):
|
||||
raise BrokenFile(
|
||||
f"stream did not contain 'endstream', instead the token {next_token} was found"
|
||||
)
|
||||
stream.pass_item(obj)
|
||||
buffer.seek(stream.offset + stream.length)
|
||||
last_ctx.save_object(stream)
|
||||
state.context_stack.append(stream)
|
||||
else:
|
||||
raise ParserError(f"PDFStream is missing initilization dictionary")
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Optional
|
||||
from ..exceptions import InvalidIndirectObjAccess
|
||||
|
||||
from pdfmajor.parser.stream.PDFStream import PDFStream
|
||||
from .base import PDFContextualObject, PDFObject
|
||||
from .primitives import PDFNull
|
||||
|
||||
|
||||
class IndirectObject(PDFContextualObject):
|
||||
@@ -13,11 +13,13 @@ class IndirectObject(PDFContextualObject):
|
||||
gen_num: int,
|
||||
offset: int,
|
||||
data: Optional[PDFObject] = None,
|
||||
stream: Optional[PDFStream] = None,
|
||||
) -> None:
|
||||
self.offset = offset
|
||||
self.obj_num = obj_num
|
||||
self.gen_num = gen_num
|
||||
self.__data = data
|
||||
self.stream = stream
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"IndirectObject(obj_num={self.obj_num}, gen_num={self.gen_num}, offset={self.offset}, data={self.get_object()})"
|
||||
@@ -34,11 +36,6 @@ class IndirectObject(PDFContextualObject):
|
||||
"""
|
||||
self.__data = obj
|
||||
|
||||
def into_readonly_copy(self):
|
||||
return IndirectObject(
|
||||
self.obj_num, self.gen_num, self.offset, self.get_object()
|
||||
)
|
||||
|
||||
def to_python(self):
|
||||
obj = self.get_object()
|
||||
if obj is None:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from pdfmajor.parser.exceptions import ParserError
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
from .base import PDFContextualObject, PDFObject
|
||||
from .primitives import PDFName
|
||||
from .collections import PDFDictionary
|
||||
|
||||
from ..objects.base import PDFObject
|
||||
from ..objects.collections import PDFDictionary
|
||||
|
||||
|
||||
class PDFStream(PDFContextualObject):
|
||||
class PDFStream:
|
||||
"""A class representing a PDF Stream as specified in PDF spec 1.7 section 7.3.8"""
|
||||
|
||||
def __init__(
|
||||
@@ -18,7 +18,6 @@ class PDFStream(PDFContextualObject):
|
||||
ffilter: Optional[List[str]] = None,
|
||||
fdecode_parms: Optional[List[Dict[str, Any]]] = None,
|
||||
dl: Optional[int] = None,
|
||||
other_metadata: Optional[Dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Args:
|
||||
@@ -39,25 +38,23 @@ class PDFStream(PDFContextualObject):
|
||||
self.ffilter: Optional[List[str]] = ffilter
|
||||
self.fdecode_parms: Optional[List[Dict[str, Any]]] = fdecode_parms
|
||||
self.dl: Optional[int] = dl
|
||||
self.other_metadata: Dict[str, Any] = (
|
||||
other_metadata if other_metadata is not None else {}
|
||||
)
|
||||
|
||||
def pass_item(self, item: PDFObject) -> None:
|
||||
@classmethod
|
||||
def from_pdfdict(cls, offset: int, item: PDFObject) -> None:
|
||||
if not isinstance(item, PDFDictionary):
|
||||
raise ParserError(f"Cannot initilize a pdfstream with {type(item)}")
|
||||
data = item.to_python()
|
||||
self.length = data["Length"]
|
||||
self.filter = get_single_or_list(data.get("Filter", self.filter))
|
||||
self.decode_parms = get_single_or_list(
|
||||
data.get("DecodeParms", self.decode_parms)
|
||||
stream = cls(offset, data["Length"])
|
||||
stream.filter = get_single_or_list(data.get("Filter", stream.filter))
|
||||
stream.decode_parms = get_single_or_list(
|
||||
data.get("DecodeParms", stream.decode_parms)
|
||||
)
|
||||
self.f = data.get("F", self.f)
|
||||
self.ffilter = get_single_or_list(data.get("FFilter", self.ffilter))
|
||||
self.fdecode_parms = get_single_or_list(
|
||||
data.get("FDecodeParms", self.fdecode_parms)
|
||||
stream.f = data.get("F", stream.f)
|
||||
stream.ffilter = get_single_or_list(data.get("FFilter", stream.ffilter))
|
||||
stream.fdecode_parms = get_single_or_list(
|
||||
data.get("FDecodeParms", stream.fdecode_parms)
|
||||
)
|
||||
self.other_metadata = data
|
||||
return stream
|
||||
|
||||
def to_python(self):
|
||||
return {
|
||||
File renamed without changes.
Whitespace-only changes.
+21
-11
@@ -12,7 +12,7 @@ from pdfmajor.streambuffer import BufferStream
|
||||
from pdfmajor.parser.objects.indirect import IndirectObject
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.comment import PDFComment
|
||||
from pdfmajor.parser.objects.stream import PDFStream
|
||||
from pdfmajor.parser.stream.PDFStream import PDFStream
|
||||
|
||||
CURRENT_FOLDER = Path(__file__).parent
|
||||
|
||||
@@ -184,7 +184,7 @@ class IndirectObjects(TestCase):
|
||||
buffer = BufferStream.from_bytes(
|
||||
br"""
|
||||
5 0 obj
|
||||
<< /Type /XRef /Length 124 /Filter /FlateDecode >>
|
||||
<< /Type /XRef /Length 123 /Filter /FlateDecode /otherfeature [10 20]>>
|
||||
stream
|
||||
"<99><A3><C1>l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567
|
||||
<80>endstream
|
||||
@@ -193,18 +193,28 @@ endobj
|
||||
)
|
||||
for obj in iter_objects(buffer, XRefDB()):
|
||||
self.assertIsInstance(obj, IndirectObject)
|
||||
self.assertIsInstance(obj.get_object(), PDFStream)
|
||||
self.assertIsInstance(obj.get_object(), PDFDictionary)
|
||||
self.assertIsInstance(obj.stream, PDFStream) # type: ignore
|
||||
self.assertDictEqual(
|
||||
obj.stream.to_python(), # type: ignore
|
||||
{
|
||||
"offset": 99,
|
||||
"length": 123,
|
||||
"filter": ["/FlateDecode"],
|
||||
"decode_parms": [],
|
||||
"f": None,
|
||||
"ffilter": [],
|
||||
"fdecode_parms": [],
|
||||
"dl": None,
|
||||
},
|
||||
)
|
||||
self.assertDictEqual(
|
||||
obj.get_object().to_python(),
|
||||
{
|
||||
"offset": 78,
|
||||
"length": 0,
|
||||
"filter": None,
|
||||
"decode_parms": None,
|
||||
"f": None,
|
||||
"ffilter": None,
|
||||
"fdecode_parms": None,
|
||||
"dl": None,
|
||||
"Filter": "/FlateDecode",
|
||||
"Length": 123,
|
||||
"Type": "/XRef",
|
||||
"otherfeature": [10, 20],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user