Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix converting path to multiple rectangles #371

Merged
merged 10 commits into from
Jul 11, 2020
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added
- Support for painting multiple rectangles at once ([#371](https://github.com/pdfminer/pdfminer.six/pull/371))

### Fixed
- Validate image object in do_EI is a PDFStream ([#451](https://github.com/pdfminer/pdfminer.six/pull/451))

Expand Down
9 changes: 9 additions & 0 deletions pdfminer/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@


class PDFLayoutAnalyzer(PDFTextDevice):

RECTS = re.compile('^(mlllh)+$')

def __init__(self, rsrcmgr, pageno=1, laparams=None):
PDFTextDevice.__init__(self, rsrcmgr)
self.pageno = pageno
Expand Down Expand Up @@ -100,6 +103,12 @@ def paint_path(self, gstate, stroke, fill, evenodd, path):
stroke, fill, evenodd, gstate.scolor,
gstate.ncolor))
return
if self.RECTS.match(shape):
for path_group in zip(*(iter(path),) * 5):
self.paint_path(gstate, stroke, fill, evenodd,
list(path_group))
return

# other shapes
pts = []
for p in path:
Expand Down
Binary file added samples/contrib/issue-00369-excel.pdf
Binary file not shown.
38 changes: 38 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from nose.tools import assert_equal

from pdfminer.converter import PDFLayoutAnalyzer
from pdfminer.layout import LTContainer
from pdfminer.pdfinterp import PDFGraphicState


class TestPaintPath():
def test_paint_path(self):
path = [('m', 136, 73.28), ('l', 137.28, 73.28)]
analyzer = self._get_analyzer()
analyzer.cur_item = LTContainer([0, 100, 0, 100])
analyzer.paint_path(PDFGraphicState(), False, False, False, path)
assert_equal(len(analyzer.cur_item._objs), 1)

def test_paint_path_mlllh(self):
path = [('m', 136, 73.28), ('l', 137.28, 73.28), ('l', 137.28, 91.04), ('l', 136, 91.04), ('h',)]
analyzer = self._get_analyzer()
analyzer.cur_item = LTContainer([0, 100, 0, 100])
analyzer.paint_path(PDFGraphicState(), False, False, False, path)
assert_equal(len(analyzer.cur_item), 1)

def test_paint_path_multiple_mlllh(self):
"""Path from samples/contrib/issue-00369-excel.pdf"""
path = [
('m', 136, 73.28), ('l', 137.28, 73.28), ('l', 137.28, 91.04), ('l', 136, 91.04), ('h',),
('m', 204.8, 73.28), ('l', 206.08, 73.28), ('l', 206.08, 91.04), ('l', 204.8, 91.04), ('h',),
('m', 67.2, 72), ('l', 68.48, 72), ('l', 68.48, 133.28), ('l', 67.2, 133.28), ('h',)
]
analyzer = self._get_analyzer()
analyzer.cur_item = LTContainer([0, 100, 0, 100])
analyzer.paint_path(PDFGraphicState(), False, False, False, path)
assert_equal(len(analyzer.cur_item._objs), 3)

def _get_analyzer(self):
analyzer = PDFLayoutAnalyzer(None)
analyzer.set_ctm([1, 0, 0, 1, 0, 0])
return analyzer
8 changes: 7 additions & 1 deletion tests/test_tools_pdf2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_contrib_issue_350(self):
run('contrib/issue-00352-asw-oct96-p41.pdf')

def test_scancode_patchelf(self):
"""Regression test for # https://github.com/euske/pdfminer/issues/96"""
"""Regression test for https://github.com/euske/pdfminer/issues/96"""
run('scancode/patchelf.pdf')

def test_contrib_hash_two_complement(self):
Expand All @@ -76,6 +76,12 @@ def test_contrib_hash_two_complement(self):
"""
run('contrib/issue-00352-hash-twos-complement.pdf')

def test_contrib_excel(self):
"""Regression test for
https://github.com/pdfminer/pdfminer.six/issues/369
"""
run('contrib/issue-00369-excel.pdf', '-t html')


class TestDumpImages:

Expand Down