mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-13 02:36:17 +03:00
Merge pull request #1 from radarhere/parse-eps-trailer
Last trailer comment has priority
This commit is contained in:
commit
b21ea55beb
BIN
Tests/images/zero_bb_eof_before_boundingbox.eps
Normal file
BIN
Tests/images/zero_bb_eof_before_boundingbox.eps
Normal file
Binary file not shown.
Binary file not shown.
|
@ -406,10 +406,16 @@ def test_timeout(test_file):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_boundary_box_in_trailer():
|
def test_bounding_box_in_trailer():
|
||||||
# Check whether boundary boxes are parsed in the same way
|
# Check bounding boxes are parsed in the same way
|
||||||
# when specified in the header or the trailer
|
# when specified in the header and the trailer
|
||||||
with Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image, Image.open(
|
with Image.open("Tests/images/zero_bb_trailer.eps") as trailer_image, Image.open(
|
||||||
FILE1
|
FILE1
|
||||||
) as header_image:
|
) as header_image:
|
||||||
assert trailer_image.size == header_image.size
|
assert trailer_image.size == header_image.size
|
||||||
|
|
||||||
|
|
||||||
|
def test_eof_before_bounding_box():
|
||||||
|
with pytest.raises(OSError):
|
||||||
|
with Image.open("Tests/images/zero_bb_eof_before_boundingbox.eps"):
|
||||||
|
pass
|
||||||
|
|
|
@ -245,6 +245,34 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
msg = 'EPS header missing "%%BoundingBox" comment'
|
msg = 'EPS header missing "%%BoundingBox" comment'
|
||||||
raise SyntaxError(msg)
|
raise SyntaxError(msg)
|
||||||
|
|
||||||
|
def _read_comment(s):
|
||||||
|
nonlocal reading_trailer_comments
|
||||||
|
try:
|
||||||
|
m = split.match(s)
|
||||||
|
except re.error as e:
|
||||||
|
msg = "not an EPS file"
|
||||||
|
raise SyntaxError(msg) from e
|
||||||
|
|
||||||
|
if m:
|
||||||
|
k, v = m.group(1, 2)
|
||||||
|
self.info[k] = v
|
||||||
|
if k == "BoundingBox":
|
||||||
|
if v == "(atend)":
|
||||||
|
reading_trailer_comments = True
|
||||||
|
elif not self._size or reading_trailer_comments:
|
||||||
|
try:
|
||||||
|
# Note: The DSC spec says that BoundingBox
|
||||||
|
# fields should be integers, but some drivers
|
||||||
|
# put floating point values there anyway.
|
||||||
|
box = [int(float(i)) for i in v.split()]
|
||||||
|
self._size = box[2] - box[0], box[3] - box[1]
|
||||||
|
self.tile = [
|
||||||
|
("eps", (0, 0) + self.size, offset, (length, box))
|
||||||
|
]
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return True
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
byte = self.fp.read(1)
|
byte = self.fp.read(1)
|
||||||
if byte == b"":
|
if byte == b"":
|
||||||
|
@ -289,22 +317,7 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
s = str(bytes_mv[:bytes_read], "latin-1")
|
s = str(bytes_mv[:bytes_read], "latin-1")
|
||||||
|
if not _read_comment(s):
|
||||||
try:
|
|
||||||
m = split.match(s)
|
|
||||||
except re.error as e:
|
|
||||||
msg = "not an EPS file"
|
|
||||||
raise SyntaxError(msg) from e
|
|
||||||
|
|
||||||
if m:
|
|
||||||
k, v = m.group(1, 2)
|
|
||||||
self.info[k] = v
|
|
||||||
if k == "BoundingBox":
|
|
||||||
if v == "(atend)":
|
|
||||||
reading_trailer_comments = True
|
|
||||||
else:
|
|
||||||
self._read_boundary_box(v, offset, length)
|
|
||||||
else:
|
|
||||||
m = field.match(s)
|
m = field.match(s)
|
||||||
if m:
|
if m:
|
||||||
k = m.group(1)
|
k = m.group(1)
|
||||||
|
@ -354,27 +367,13 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
# Load EPS trailer
|
# Load EPS trailer
|
||||||
|
|
||||||
# if this line starts with "%%EOF",
|
# if this line starts with "%%EOF",
|
||||||
# then we've reached the end of the trailer
|
# then we've reached the end of the file
|
||||||
if bytes_mv[:5] == b"%%EOF":
|
if bytes_mv[:5] == b"%%EOF":
|
||||||
reading_trailer_comments = False
|
break
|
||||||
continue
|
|
||||||
|
|
||||||
s = str(bytes_mv[:bytes_read], "latin-1")
|
s = str(bytes_mv[:bytes_read], "latin-1")
|
||||||
|
_read_comment(s)
|
||||||
try:
|
elif bytes_mv[:9] == b"%%Trailer":
|
||||||
m = split.match(s)
|
|
||||||
except re.error as e:
|
|
||||||
msg = "not an EPS file"
|
|
||||||
raise SyntaxError(msg) from e
|
|
||||||
|
|
||||||
if m:
|
|
||||||
k, v = m.group(1, 2)
|
|
||||||
self.info[k] = v
|
|
||||||
if k == "BoundingBox":
|
|
||||||
if not self._size:
|
|
||||||
self._read_boundary_box(v, offset, length)
|
|
||||||
|
|
||||||
if bytes_mv[:9] == b"%%Trailer":
|
|
||||||
trailer_reached = True
|
trailer_reached = True
|
||||||
bytes_read = 0
|
bytes_read = 0
|
||||||
|
|
||||||
|
@ -407,17 +406,6 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
return length, offset
|
return length, offset
|
||||||
|
|
||||||
def _read_boundary_box(self, v, offset, length):
|
|
||||||
try:
|
|
||||||
# Note: The DSC spec says that BoundingBox
|
|
||||||
# fields should be integers, but some drivers
|
|
||||||
# put floating point values there anyway.
|
|
||||||
box = [int(float(i)) for i in v.split()]
|
|
||||||
self._size = box[2] - box[0], box[3] - box[1]
|
|
||||||
self.tile = [("eps", (0, 0) + self.size, offset, (length, box))]
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def load(self, scale=1, transparency=False):
|
def load(self, scale=1, transparency=False):
|
||||||
# Load EPS via Ghostscript
|
# Load EPS via Ghostscript
|
||||||
if self.tile:
|
if self.tile:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user