diff --git a/DocTest/DocumentRepresentation.py b/DocTest/DocumentRepresentation.py index d349624..d53507d 100644 --- a/DocTest/DocumentRepresentation.py +++ b/DocTest/DocumentRepresentation.py @@ -312,8 +312,23 @@ def _process_pattern_ignore_area_from_pdf(self, ignore_area: Dict): if self.pdf_text_words: for word in self.pdf_text_words: if search_pattern.match(word[4]): - (x, y, w, h) = (word[0]*self.dpi/72, word[1]*self.dpi/72, word[2]*self.dpi/72, word[3]*self.dpi/72) - text_mask = {"x": int(x) - xoffset, "y": int(y) - yoffset, "width": int(w) + 2 * xoffset, "height": int(h) + 2 * yoffset} + x0, y0 = word[0], word[1] + x = x0 * self.dpi / 72 + y = y0 * self.dpi / 72 + if word[2] > x0: + # PyMuPDF >= 1.25 returns (x0, y0, x1, y1, text, ...) + w = (word[2] - x0) * self.dpi / 72 + h = (word[3] - y0) * self.dpi / 72 + else: + # Older versions return (x0, y0, width, height, text, ...) + w = word[2] * self.dpi / 72 + h = word[3] * self.dpi / 72 + text_mask = { + "x": int(x) - xoffset, + "y": int(y) - yoffset, + "width": int(w) + 2 * xoffset, + "height": int(h) + 2 * yoffset, + } self.pixel_ignore_areas.append(text_mask) else: if self.pdf_text_dict: diff --git a/pyproject.toml b/pyproject.toml index a0d51d6..2de0b1c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ homepage = "https://github.com/manykarim/robotframework-doctestlibrary" [tool.poetry.dependencies] python = "^3.8.1" PyMuPDF = [ - { version = ">=1.23.1, <1.25.0", python = ">=3.8, <3.9" }, + { version = ">=1.23.1, !=1.25.0, <1.26.0", python = ">=3.8, <3.9" }, { version = ">=1.26.0", python = ">=3.9" }, ] imutils = "*" diff --git a/utest/test_masks.py b/utest/test_masks.py index a65211b..7281533 100644 --- a/utest/test_masks.py +++ b/utest/test_masks.py @@ -27,4 +27,15 @@ def test_pdf_area_mask(testdata_dir): def test_pdf_text_mask(testdata_dir): img = DocumentRepresentation(testdata_dir / 'sample_1_page.pdf', ignore_area_file=testdata_dir / 'pdf_pattern_mask.json') assert len(img.abstract_ignore_areas)==2 - assert np.not_equal(img.pages[0].get_image_with_ignore_areas(), img.pages[0].image).any() \ No newline at end of file + assert np.not_equal(img.pages[0].get_image_with_ignore_areas(), img.pages[0].image).any() + + +def test_pdf_word_pattern_mask_dimensions(testdata_dir): + img = DocumentRepresentation( + testdata_dir / 'sample_1_page.pdf', + dpi=300, + ignore_area_file=testdata_dir / 'pdf_pattern_mask.json', + ) + word_mask = img.pages[0].pixel_ignore_areas[0] + assert word_mask['width'] == 350 + assert word_mask['height'] == 47