rect obj
This commit is contained in:
1 parent
3f5bac98b3
commit
e33d13a3e1
5 files changed
+145
-12
No files matched your search
@@ -1,9 +1,8 @@
|
||||
from pathlib import Path
|
||||
from pdfmajor.document.PDFParsingContext import PDFParsingContext
|
||||
from typing import BinaryIO
|
||||
|
||||
from pdfmajor.healthlog import PDFHealthReport
|
||||
from pdfmajor.streambuffer import BufferStream
|
||||
from pdfmajor.xref.xrefdb import XRefDB
|
||||
|
||||
|
||||
class PDFDocument:
|
||||
@@ -22,7 +21,11 @@ class PDFDocument:
|
||||
return cls(BufferStream(path.open("rb"), buffer_size))
|
||||
|
||||
def __init__(self, buffer: BufferStream) -> None:
|
||||
self.buffer = buffer
|
||||
self.health_report = PDFHealthReport()
|
||||
with self.buffer.get_window():
|
||||
self.xref_db = XRefDB(buffer)
|
||||
self.__parser = PDFParsingContext(buffer)
|
||||
self.catalog = self.__parser.get_catalog()
|
||||
self.info = self.__parser.get_info()
|
||||
|
||||
@property
|
||||
def health_report(self):
|
||||
"""A log of errors that occured in the document during the processing of the file"""
|
||||
return self.__parser.health_report
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import Set
|
||||
from decimal import Decimal
|
||||
from typing import List, Set
|
||||
|
||||
from pdfmajor.exceptions import BrokenFile, PDFMajorException
|
||||
from pdfmajor.parser.objects.indirect import ObjectRef
|
||||
@@ -8,15 +9,25 @@ class PDFDocumentError(PDFMajorException):
|
||||
pass
|
||||
|
||||
|
||||
class MissingCatalogObj(PDFDocumentError, BrokenFile):
|
||||
class BrokenFilePDF(BrokenFile):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidCatalogObj(PDFDocumentError, BrokenFile):
|
||||
class MissingCatalogObj(BrokenFilePDF):
|
||||
pass
|
||||
|
||||
|
||||
class TooManyInfoObj(PDFDocumentError, BrokenFile):
|
||||
class InvalidCatalogObj(BrokenFilePDF):
|
||||
pass
|
||||
|
||||
|
||||
class TooManyInfoObj(BrokenFilePDF):
|
||||
def __init__(self, elms: Set[ObjectRef]) -> None:
|
||||
super().__init__(f"Too many info elements found | total={elms}")
|
||||
super().__init__(f"Too many info elements found | elms={elms}")
|
||||
self.elms = elms
|
||||
|
||||
|
||||
class TooManyRectField(BrokenFilePDF):
|
||||
def __init__(self, values: List[Decimal]) -> None:
|
||||
super().__init__(f"Too many elements found for rectangle | values={values}")
|
||||
self.values = values
|
||||
@@ -0,0 +1,17 @@
|
||||
from typing import List, NamedTuple, Optional
|
||||
from pdfmajor.parser.objects.indirect import ObjectRef
|
||||
from pdfmajor.parser.objects.collections import PDFDictionary
|
||||
|
||||
|
||||
class PDFPageTreeNode(NamedTuple):
|
||||
"""A PDF Pages Node representation as it confirms with PDF spec 1.7 section 7.7.3.2"""
|
||||
|
||||
kids: List[ObjectRef]
|
||||
parent: Optional[ObjectRef] = None
|
||||
|
||||
|
||||
class PDFPage(NamedTuple):
|
||||
"""A PDF Page representation as it confirms with PDF Spec 1.7 section 7.7.3.3"""
|
||||
|
||||
parent: ObjectRef
|
||||
resources: PDFDictionary
|
||||
@@ -0,0 +1,32 @@
|
||||
from decimal import Decimal
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from pdfmajor.parser.objects.primitives import PDFInteger, PDFReal
|
||||
from typing import List, NamedTuple
|
||||
from pdfmajor.parser.objects.collections import PDFArray
|
||||
|
||||
|
||||
class PDFRectangle(NamedTuple):
|
||||
"""A PDF Rectangle as specified by PDF spec 1.7 section 7.9.5"""
|
||||
|
||||
llx: Decimal
|
||||
lly: Decimal
|
||||
urx: Decimal
|
||||
ury: Decimal
|
||||
|
||||
@classmethod
|
||||
def from_pdfarray(cls, arr: PDFArray):
|
||||
vals: List[Decimal] = []
|
||||
for obj in arr:
|
||||
if isinstance(obj, PDFInteger):
|
||||
vals.append(Decimal(obj.to_python()))
|
||||
elif isinstance(obj, PDFReal):
|
||||
vals.append(obj.to_python())
|
||||
else:
|
||||
raise BrokenFilePDF(f"Invalid type for initilizing rectangle {obj}")
|
||||
if len(vals) > 4:
|
||||
raise TooManyRectField(vals)
|
||||
elif len(vals) < 4:
|
||||
raise BrokenFilePDF(
|
||||
f"Insufficient number of fields for rectangle {arr} {vals}"
|
||||
)
|
||||
return cls(*vals)
|
||||
+71
-1
@@ -1,3 +1,9 @@
|
||||
from decimal import Decimal
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from typing import List
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.collections import PDFArray
|
||||
from pdfmajor.document.structures import PDFRectangle
|
||||
from unittest import TestCase
|
||||
|
||||
from pdfmajor.document.PDFDocumentCatalog import PDFDocumentCatalog
|
||||
@@ -7,12 +13,13 @@ from pdfmajor.parser.objects.primitives import (
|
||||
PDFBoolean,
|
||||
PDFInteger,
|
||||
PDFName,
|
||||
PDFReal,
|
||||
PDFString,
|
||||
)
|
||||
from tests.util import all_pdf_files
|
||||
|
||||
|
||||
class Catalog(TestCase):
|
||||
class ParsingState(TestCase):
|
||||
def run_test(self, file_name: str, expcat: PDFDocumentCatalog):
|
||||
with all_pdf_files[file_name].get_window() as buffer:
|
||||
pdf = PDFParsingContext(buffer)
|
||||
@@ -77,3 +84,66 @@ class Catalog(TestCase):
|
||||
parser.get_catalog()
|
||||
self.assertEqual(len(parser.health_report), 0)
|
||||
parser.get_info()
|
||||
|
||||
|
||||
class Structures(TestCase):
|
||||
def run_rect_test(self, arr_vals: List[PDFObject], exprect: PDFRectangle):
|
||||
self.assertEqual(
|
||||
PDFRectangle.from_pdfarray(PDFArray.from_list(arr_vals)),
|
||||
exprect,
|
||||
)
|
||||
|
||||
def test_rect(self):
|
||||
self.run_rect_test(
|
||||
[
|
||||
PDFInteger(20, 0, 0),
|
||||
PDFInteger(10, 0, 0),
|
||||
PDFInteger(0, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
],
|
||||
PDFRectangle(Decimal(20), Decimal(10), Decimal(0), Decimal(3)),
|
||||
)
|
||||
self.run_rect_test(
|
||||
[
|
||||
PDFReal(Decimal("0.23"), 0, 0),
|
||||
PDFInteger(10, 0, 0),
|
||||
PDFInteger(Decimal("3.3"), 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
],
|
||||
PDFRectangle(Decimal("0.23"), Decimal(10), Decimal("3.3"), Decimal(3)),
|
||||
)
|
||||
|
||||
def test_rect_fail(self):
|
||||
with self.assertRaises(BrokenFilePDF):
|
||||
PDFRectangle.from_pdfarray(
|
||||
PDFArray.from_list(
|
||||
[
|
||||
PDFString("h0", 0, 0),
|
||||
PDFString("h0", 0, 0),
|
||||
PDFString("h0", 0, 0),
|
||||
PDFString("h0", 0, 0),
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
with self.assertRaises(TooManyRectField):
|
||||
PDFRectangle.from_pdfarray(
|
||||
PDFArray.from_list(
|
||||
[
|
||||
PDFInteger(3, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
]
|
||||
)
|
||||
)
|
||||
with self.assertRaises(BrokenFilePDF):
|
||||
PDFRectangle.from_pdfarray(
|
||||
PDFArray.from_list(
|
||||
[
|
||||
PDFInteger(3, 0, 0),
|
||||
PDFInteger(3, 0, 0),
|
||||
]
|
||||
)
|
||||
)
|
||||
Reference in new issue
Block a user