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 to set color space from color convenience ops #794

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `TypeError` when getting default width of font ([#720](https://github.com/pdfminer/pdfminer.six/issues/720))
- Installing typing-extensions on Python 3.6 and 3.7 ([#775](https://github.com/pdfminer/pdfminer.six/pull/775))
- `TypeError` in cmapdb.py when parsing null characters ([#768](https://github.com/pdfminer/pdfminer.six/pull/768))
- Color "convenience operators" now (per spec) also set color space ([#779](https://github.com/pdfminer/pdfminer.six/issues/779))

### Deprecated

Expand Down
6 changes: 6 additions & 0 deletions pdfminer/pdfinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,21 +652,25 @@ def do_cs(self, name: PDFStackT) -> None:
def do_G(self, gray: PDFStackT) -> None:
"""Set gray level for stroking operations"""
self.graphicstate.scolor = cast(float, gray)
self.scs = self.csmap["DeviceGray"]
return

def do_g(self, gray: PDFStackT) -> None:
"""Set gray level for nonstroking operations"""
self.graphicstate.ncolor = cast(float, gray)
self.ncs = self.csmap["DeviceGray"]
return

def do_RG(self, r: PDFStackT, g: PDFStackT, b: PDFStackT) -> None:
"""Set RGB color for stroking operations"""
self.graphicstate.scolor = (cast(float, r), cast(float, g), cast(float, b))
self.scs = self.csmap["DeviceRGB"]
return

def do_rg(self, r: PDFStackT, g: PDFStackT, b: PDFStackT) -> None:
"""Set RGB color for nonstroking operations"""
self.graphicstate.ncolor = (cast(float, r), cast(float, g), cast(float, b))
self.ncs = self.csmap["DeviceRGB"]
return

def do_K(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
Expand All @@ -677,6 +681,7 @@ def do_K(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
cast(float, y),
cast(float, k),
)
self.scs = self.csmap["DeviceCMYK"]
return

def do_k(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
Expand All @@ -687,6 +692,7 @@ def do_k(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
cast(float, y),
cast(float, k),
)
self.ncs = self.csmap["DeviceCMYK"]
return

def do_SCN(self) -> None:
Expand Down
28 changes: 27 additions & 1 deletion tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import io
from tempfile import TemporaryFile

from helpers import absolute_sample_path
from pdfminer.converter import PDFLayoutAnalyzer, PDFConverter
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTContainer, LTRect, LTLine, LTCurve
from pdfminer.layout import LTChar, LTContainer, LTRect, LTLine, LTCurve
from pdfminer.pdfinterp import PDFGraphicState


Expand Down Expand Up @@ -225,6 +226,31 @@ def test_paint_path_without_starting_m(self):
assert len(analyzer.cur_item._objs) == 0


def get_chars(el):
if isinstance(el, LTContainer):
for item in el:
yield from get_chars(item)
elif isinstance(el, LTChar):
yield el
else:
pass


class TestColorSpace:
def test_do_rg(self):
path = absolute_sample_path("contrib/issue-00352-hash-twos-complement.pdf")
for page in extract_pages(path):
for char in get_chars(page):
cs = char.ncs.name
color = char.graphicstate.ncolor
if cs == "DeviceGray":
assert isinstance(color, (float, int))
elif cs == "DeviceRGB":
assert len(color) == 3
elif cs == "DeviceCMYK":
assert len(color) == 4


class TestBinaryDetector:
def test_stringio(self):
assert not PDFConverter._is_binary_stream(io.StringIO())
Expand Down