From e33d13a3e18070c19ad7e39ba72c817ceacd87c2 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 30 Oct 2020 19:52:57 -0400 Subject: [PATCH] rect obj --- pdfmajor/document/PDFDocument.py | 15 ++++--- pdfmajor/document/exceptions.py | 21 +++++++--- pdfmajor/document/pages.py | 17 ++++++++ pdfmajor/document/structures.py | 32 ++++++++++++++ tests/test_document.py | 72 +++++++++++++++++++++++++++++++- 5 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 pdfmajor/document/pages.py create mode 100644 pdfmajor/document/structures.py diff --git a/pdfmajor/document/PDFDocument.py b/pdfmajor/document/PDFDocument.py index 9aff575..f853f72 100644 --- a/pdfmajor/document/PDFDocument.py +++ b/pdfmajor/document/PDFDocument.py @@ -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 diff --git a/pdfmajor/document/exceptions.py b/pdfmajor/document/exceptions.py index 42a1320..dbe657c 100644 --- a/pdfmajor/document/exceptions.py +++ b/pdfmajor/document/exceptions.py @@ -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 diff --git a/pdfmajor/document/pages.py b/pdfmajor/document/pages.py new file mode 100644 index 0000000..08da89b --- /dev/null +++ b/pdfmajor/document/pages.py @@ -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 diff --git a/pdfmajor/document/structures.py b/pdfmajor/document/structures.py new file mode 100644 index 0000000..37e338b --- /dev/null +++ b/pdfmajor/document/structures.py @@ -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) diff --git a/tests/test_document.py b/tests/test_document.py index 61f6ada..da04d4c 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -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), + ] + ) + )