def test_inspect_works_with_bytes(self):
        path = root_path / "rectangles_yes.pdf"
        with open(path, "rb") as f:
            data = f.read()

        redactions = xray.inspect(data)
        self.assertTrue(redactions)
 def test_multiline_redaction(self):
     path = root_path / "multi_line_redaction_ok.pdf"
     redactions = xray.inspect(path)
     self.assertEqual(
         redactions,
         {},
         msg="Got redactions on a multiline redaction, but shouldn't have.",
     )
 def test_ok_words_not_redacted(self):
     path = root_path / "ok_words.pdf"
     redactions = xray.inspect(path)
     self.assertEqual(
         redactions,
         {},
         msg="Got redaction even though none in document",
     )
 def test_unfilled_rect(self):
     """Do unfilled boxes (with only borders and no fill) get ignored?"""
     path = root_path / "unfilled_rect.pdf"
     redactions = xray.inspect(path)
     self.assertEqual(
         redactions,
         {},
         msg="Got redactions 'under' an unfilled rectangle.",
     )
 def test_inspect_works_with_path_or_str(self):
     path_str = "rectangles_yes.pdf"
     paths = (
         root_path / path_str,
         os.path.join(str(root_path), path_str),
     )
     for path in paths:
         redactions = xray.inspect(path)
         self.assertTrue(redactions)
 def test_whitespace_only_redaction_no_results(self):
     """Do we ignore redactions containing only whitespace chars?"""
     paths = (
         "whitespace_redactions.pdf",
         "whitespace_redactions_2.pdf",
         "whitespace_redaction_with_comma.pdf",
     )
     for path in paths:
         redactions = xray.inspect(root_path / path)
         self.assertEqual(
             redactions,
             {},
             msg="Didn't get empty dict when encountering exclusively "
             "whitespace-filled redactions.",
         )
    def test_overlapping_text(self):
        """Do we find bad redactions with visible text below them?

        This test case is a nasty one. If you look closely at LLC with your
        cursor or, even better, at TROPPER in the heading, you'll see that
        there is hidden text on top of the visible text. TROPPER is fun because
        the hidden text is the correctly spelled word, "TROOPER."

        Anyway, for now we don't support this at all, because our pixmap
        approach sees the visible text below the hidden text, and thinks that
        there's no bad redaction there. Someday, we should fix this, but it
        seems very difficult.
        """
        path = root_path / "hidden_text_on_visible_text.pdf"
        redactions = xray.inspect(path)
        expected_redaction_count = 2
        self.assertEqual(
            len(list(redactions.values())),
            expected_redaction_count,
        )
import json
import sys

import xray

if __name__ == "__main__":
    bad_redactions = xray.inspect(sys.argv[1])
    print(json.dumps(bad_redactions, indent=2))
    def test_tricky_rectangles(self):
        """Check that tricky PDFs don't create false positives.

        These are a variety of tough cases that don't have redactions, but
        which can appear to due to their complexity. When our approach uses
        only rectangles and text from parsing the PDF, each of these examples
        comes back as a false positive. To fix this, we render the relevant
        part of the document as a pixmap and then analyze that for more than
        one color in the box. If we see multiple colors, we know that it's not
        a bad redaction.

        Note that any of these weird PDFs can be inspected with:

            mutools trace some-doc.pdf
        """
        self.maxDiff = None

        # The first digit in these file names is just a counter for the
        # example. The second is the page in the original it was pulled from
        # or the page in the current one (if multi-page) where it caused an
        # issue.
        paths = (
            # The red rectangles in this document are complicated due to
            # non-zero winding rules:
            #
            #   https://en.wikipedia.org/wiki/Nonzero-rule
            #
            # In short, when paths in a drawing overlap, you need a method of
            # figuring out which enclosed parts of the drawing are filled
            # (inside the drawing), and which are not (outside the drawing).
            #
            # PyMuPDF doesn't have a way of determining that at present, so
            # when we look at the two squares in this PDF, it looks like the
            # text that the squares surround is inside of them. That's
            # intuitively true, but due to the winding rules, the surrounded
            # part is actually not inside the drawing, and that's why that part
            # of the rectangles is transparent, not red. In fact, the center of
            # the rectangle is outside of the drawing, and despite the drawing
            # and the text occupying the same x-y space, one does not occlude
            # the other (note that you can plainly see the text).
            #
            # More discussion: https://github.com/pymupdf/PyMuPDF/issues/1355
            "no_bad_redactions.2.1.pdf",
            "no_bad_redactions.3.1.pdf",
            # A white rectangle in this drawing occupies the same location as
            # the text across the top due clipping paths.
            #
            # See: https://github.com/pymupdf/PyMuPDF/issues/1387
            "no_bad_redactions.3.2.pdf",
            "no_bad_redactions.4.1.pdf",
            # Lots of messy stuff starting on page five. The word "Article"
            # says it's white, but it appears black when rendered. Don't know
            # why. Yanking off page 5 using pdftk changes the structure of this
            # one, so it gets to have multiple pages in the test case.
            "no_bad_redactions.5.5.pdf",
            "no_bad_redactions.6.2.pdf",
            # The JS-6 rectangle causes issues
            "no_bad_redactions.7.1.pdf",
            # This one has a big image covering literally everything else, and
            # the image appears to have black rectangles. This doesn't have bad
            # redactions b/c the text under the image is just dates, which are
            # fine. Each text box appears to be wrapped in four lines forming
            # a visible rectangle (but not a Rect object). This test case is
            # important for if we ever start dealing with images in the PDFs,
            # because it should continue *not* having bad redactions. (That'll
            # need to be fixed by handling dates though, probably.)
            "no_bad_redactions.8.1.pdf",
        )
        for path in paths:
            path = root_path / path
            with self.subTest(f"{path=}"):
                redactions = xray.inspect(path)
                self.assertEqual(
                    redactions,
                    {},
                    msg="Didn't get empty dict when there were no redactions.",
                )
 def test_inspect_method_on_a_filepath(self):
     redactions = xray.inspect(self.path)
     self.assertEqual(len(redactions[1]), 3)