From 5378a4f34cb46e3eb26bf7f696bfbfc5809555e6 Mon Sep 17 00:00:00 2001 From: Ariel Sosnovsky Date: Mon, 29 Oct 2018 16:25:50 -0400 Subject: [PATCH] - better color grabbing for scn - show custom type in converters --- .version | 2 +- bin/deploy_pip.sh | 2 +- pdfmajor/__init__.py | 2 +- pdfmajor/converters/HTMLConverter.py | 3 ++ pdfmajor/converters/XMLConverter.py | 4 +++ .../PDFGraphicState/PDFGraphicStateColor.py | 14 ++++++++ pdfmajor/interpreter/PDFPageInterpreter.py | 36 +++++++++++++------ pdfmajor/interpreter/PDFStream/PDFObjRef.py | 4 ++- .../interpreter/PSStackParser/PSBaseParser.py | 1 - samples/convert_samples.py | 4 +-- setup.py | 2 +- 11 files changed, 55 insertions(+), 19 deletions(-) diff --git a/.version b/.version index 89fb16f..261f347 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -v1.1.4 \ No newline at end of file +v1.1.5 \ No newline at end of file diff --git a/bin/deploy_pip.sh b/bin/deploy_pip.sh index 21ebd81..c5eea2d 100755 --- a/bin/deploy_pip.sh +++ b/bin/deploy_pip.sh @@ -9,7 +9,7 @@ cd .. python setup.py sdist bdist_wheel git add . -git commit -m "Deployong new version - $(cat .version)" +git commit git push git tag $(cat .version) diff --git a/pdfmajor/__init__.py b/pdfmajor/__init__.py index 593f73c..39d150c 100644 --- a/pdfmajor/__init__.py +++ b/pdfmajor/__init__.py @@ -75,7 +75,7 @@ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -__version__ = (1, 1, 4) +__version__ = (1, 1, 5) if __name__ == '__main__': print( diff --git a/pdfmajor/converters/HTMLConverter.py b/pdfmajor/converters/HTMLConverter.py index 4c3faba..5f73671 100644 --- a/pdfmajor/converters/HTMLConverter.py +++ b/pdfmajor/converters/HTMLConverter.py @@ -26,6 +26,9 @@ def get_color(col: PDFGraphicStateColor): return f"cmyk({','.join(map(str, color_val))})" elif color_type == 'gray': return f'rgba(0,0,0,{1-color_val})' + elif color_type == "custom": + # Todo: convert these to more friendly color types + return f"custom({col.custom['type']})" return "none" class HTMLConverter(PDFConverter): diff --git a/pdfmajor/converters/XMLConverter.py b/pdfmajor/converters/XMLConverter.py index 2d68635..e823744 100644 --- a/pdfmajor/converters/XMLConverter.py +++ b/pdfmajor/converters/XMLConverter.py @@ -1,5 +1,6 @@ import logging import re +import json from typing import Dict @@ -29,6 +30,9 @@ def get_color(col: PDFGraphicStateColor): return f"cmyk({','.join(map(str, color_val))})" elif color_type == 'gray': return f'gray({color_val})' + elif color_type == "custom": + # Todo: convert these to more friendly color types + return f"custom({col.custom['type']})" return "" FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)') diff --git a/pdfmajor/interpreter/PDFGraphicState/PDFGraphicStateColor.py b/pdfmajor/interpreter/PDFGraphicState/PDFGraphicStateColor.py index c3c28d9..092e1c5 100644 --- a/pdfmajor/interpreter/PDFGraphicState/PDFGraphicStateColor.py +++ b/pdfmajor/interpreter/PDFGraphicState/PDFGraphicStateColor.py @@ -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 + '/>' diff --git a/pdfmajor/interpreter/PDFPageInterpreter.py b/pdfmajor/interpreter/PDFPageInterpreter.py index e5eda40..6977023 100644 --- a/pdfmajor/interpreter/PDFPageInterpreter.py +++ b/pdfmajor/interpreter/PDFPageInterpreter.py @@ -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): diff --git a/pdfmajor/interpreter/PDFStream/PDFObjRef.py b/pdfmajor/interpreter/PDFStream/PDFObjRef.py index 3b4b82c..3e8c010 100644 --- a/pdfmajor/interpreter/PDFStream/PDFObjRef.py +++ b/pdfmajor/interpreter/PDFStream/PDFObjRef.py @@ -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.') diff --git a/pdfmajor/interpreter/PSStackParser/PSBaseParser.py b/pdfmajor/interpreter/PSStackParser/PSBaseParser.py index 2ce6d92..314aa55 100644 --- a/pdfmajor/interpreter/PSStackParser/PSBaseParser.py +++ b/pdfmajor/interpreter/PSStackParser/PSBaseParser.py @@ -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): diff --git a/samples/convert_samples.py b/samples/convert_samples.py index de96361..e888b8f 100644 --- a/samples/convert_samples.py +++ b/samples/convert_samples.py @@ -16,9 +16,9 @@ if not os.path.exists(OUTPUT_FOLDER): # files = os.listdir(INPUT_FOLDER) # files = ['fonts.pdf'] -# files = ['bar-charts.pdf'] +files = ['bar-charts.pdf'] # files = ['font-color.pdf'] -files = ['tables.pdf'] +# files = ['tables.pdf'] TOTAL = len(files) for idx, file_name in enumerate(files): diff --git a/setup.py b/setup.py index 9667b98..b9ebf37 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ import sys with open('README.md') as readme_file: SETUP = { "name": "pdfmajor", - "version": "1.1.4", + "version": "1.1.5", "packages": find_packages(include=['pdfmajor*']), "install_requires": [ 'pycryptodome',