- better color grabbing for scn

- show custom type in converters
This commit is contained in:
ari committed 2018-10-29 16:25:50 -04:00
1 parent 42cbc8e39e
commit 5378a4f34c
11 files changed
+55 -19

No files matched your search

@@ -5,6 +5,7 @@ class PDFGraphicStateColor:
self.__rgb = None
self.__gray = None
self.__cmyk = None
self.__custom = None
def get_color(self):
if self.__rgb is not None:
@@ -13,6 +14,8 @@ class PDFGraphicStateColor:
return 'cmyk', self.cmyk
elif self.__gray is not None:
return 'gray', self.gray
elif self.__custom is not None:
return 'custom', self.custom
else: return None, None
@property
@@ -34,6 +37,11 @@ class PDFGraphicStateColor:
if self.__gray is not None: return float(self.__gray)
else: raise self.NotSet
@property
def custom(self):
if self.__custom is not None: return dict(self.__custom)
else: raise self.NotSet
@rgb.setter
def rgb(self, rgb):
self.__rgb = rgb
@@ -46,11 +54,16 @@ class PDFGraphicStateColor:
def gray(self, color):
self.__gray = color
@custom.setter
def custom(self, color):
self.__custom = color
def copy(self):
cp = self.__class__()
cp.gray = self.__gray
cp.rgb = self.__rgb
cp.cmyk = self.__cmyk
cp.custom = self.__custom
return cp
def __repr__(self):
@@ -58,4 +71,5 @@ class PDFGraphicStateColor:
if self.__gray is not None: rep += f' gray={self.__gray}'
if self.__rgb is not None: rep += f' rgb=[{",".join(map(str, self.__rgb))}]'
if self.__cmyk is not None: rep += f' cmyk=[{",".join(map(str, self.__cmyk))}]'
if self.__custom is not None: rep += f' custom={self.__custom}'
return rep + '/>'
+25 -11
View File
@@ -1,6 +1,7 @@
import logging
import operator
from collections import OrderedDict
from typing import List
@@ -54,7 +55,7 @@ class PDFPageInterpreter(object):
self.resources = resources
self.fontmap = {}
self.xobjmap = {}
self.csmap = PREDEFINED_COLORSPACE.copy()
self.csmap:OrderedDict = PREDEFINED_COLORSPACE.copy()
if not resources:
return
@@ -351,20 +352,33 @@ class PDFPageInterpreter(object):
return
# setcolor
def do_SCN(self):
if self.scs:
n = self.scs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
return
def do_scn(self):
def __process_scn(self):
if self.ncs:
n = self.ncs.ncomponents
else:
raise PDFInterpreterError('No colorspace specified!')
self.pop(n)
components = self.pop(n)
obj_id = components[0]
if self.ncs.name not in self.resources.keys():
raise PDFInterpreterError("Color space not in resources")
if obj_id.name not in self.resources[self.ncs.name].keys():
raise PDFInterpreterError("Invalid Color reference")
color_data = self.resources[self.ncs.name][obj_id.name].resolve()
if isinstance(color_data, PDFStream):
color_data = color_data.attrs
return {
"type": self.ncs.name,
"defintion": color_data
}
def do_SCN(self):
self.graphicstate.scolor.custom = self.__process_scn()
return
def do_scn(self):
self.graphicstate.ncolor.custom = self.__process_scn()
return
def do_SC(self):
+3 -1
View File
@@ -12,7 +12,9 @@ from .exceptions import * # pylint: disable=W0614
##
class PDFObjRef(PDFObject):
def __init__(self, doc, objid, _):
def __init__(self, doc, objid, a):
if objid == 15:
print(doc, a)
if objid == 0:
if settings.STRICT:
raise PDFValueError('PDF object id cannot be 0.')
@@ -100,7 +100,6 @@ class PSBaseParser(object):
linebuf += self.buf[self.charpos:]
self.charpos = len(self.buf)
log.debug('nextline: %r, %r', linepos, linebuf)
return (linepos, linebuf)
def revreadlines(self):