diff --git a/pdfmajor/interpreter/commands/__init__.py b/pdfmajor/interpreter/commands/__init__.py index bcbc71c..cc838b7 100644 --- a/pdfmajor/interpreter/commands/__init__.py +++ b/pdfmajor/interpreter/commands/__init__.py @@ -23,10 +23,16 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st if font_cache is None: font_cache = {} parser = PDFContentParser(streams) + # from .cmd_record import CmdRecord + # history = CmdRecord() for obj in parser: if isinstance(obj, PSKeyword): name = keyword_name(obj) method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q') + func = None + args = [] + if method == "Do": + pass if method in PDFCommands.commands.keys(): func = PDFCommands.commands.get(method) nargs = func.__code__.co_argcount-1 @@ -40,8 +46,12 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st func(state) else: log.debug('Unknown operator: %r (i.e. %r)' % (name, method)) + # history.write(name, + # None if func is None else func.__name__ + # , args) else: state.argstack.append(obj) + # history.write('arg', None, obj) for complete_item in state.complete_layout_items: if isinstance(complete_item, LTXObject): @@ -51,6 +61,7 @@ def process_command_stream(streams: List[PDFStream], font_cache: dict = None, st resources=complete_item.resources, font_cache=font_cache ) + xobj_state.graphics = state.graphics.copy() for item in process_command_stream( [complete_item.stream], font_cache=font_cache, diff --git a/pdfmajor/interpreter/commands/cmd_record.py b/pdfmajor/interpreter/commands/cmd_record.py new file mode 100644 index 0000000..82ae325 --- /dev/null +++ b/pdfmajor/interpreter/commands/cmd_record.py @@ -0,0 +1,9 @@ + +class CmdRecord: + def __init__(self, file_name: str = "./cmd_record"): + self.file = open(file_name, 'w') + self.file.write("c_name,func_name,args\n") + def write(self, c_name, func_name, args): + self.file.write(f"{c_name},{func_name},{args}\n") + def close(self): + self.file.close() diff --git a/tests/samples/pdf/lorem-v4.pdf b/tests/samples/pdf/lorem-v4.pdf index f1718c5..ad1959c 100644 Binary files a/tests/samples/pdf/lorem-v4.pdf and b/tests/samples/pdf/lorem-v4.pdf differ diff --git a/tests/test_cols.py b/tests/test_cols.py new file mode 100644 index 0000000..67c4a16 --- /dev/null +++ b/tests/test_cols.py @@ -0,0 +1,110 @@ +import os +from typing import List +from tqdm import tqdm + +# from pdfmajor.extractor import extract_items_from_pdf +from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging +from pdfmajor.interpreter import PageInterpreter +from pdfmajor.interpreter.commands import LTCurve, PDFColor, LTXObject +from pdfmajor.interpreter.commands import PREDEFINED_COLORSPACE + +CUR_PATH = os.path.dirname(os.path.dirname(__file__)) +INPUT_FOLDER = os.path.join( + CUR_PATH, "tests/samples/pdf" +) +FILES = os.listdir(INPUT_FOLDER) +NUM_LOOPS = 10 + +file_path = os.path.join(INPUT_FOLDER, "lorem-v4.pdf") + +def run_test(file_path: str, curve_defs: List[List[PDFColor]]): + def run_once(item): + if isinstance(item, LTCurve): + t_stroke, t_fill = curve_defs.pop(0) + # print([item.stroke, item.fill]) + # print('...>', [t_stroke, t_fill]) + assert item.stroke.values == t_stroke.values + assert item.stroke.color_space == t_stroke.color_space + assert item.fill.values == t_fill.values + assert item.fill.color_space == t_fill.color_space + elif isinstance(item, LTXObject): + for c in item: + run_once(c) + + for page in PDFInterpreter(file_path): + for item in page: + run_once(item) + +run_test( + os.path.join(INPUT_FOLDER, "lorem-v3.pdf"), + [ + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ], + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207), + ], + [ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 0), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207), + ], + [ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 0), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 114, 159, 207), + ], + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ], + [ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 0, 0), + ] + ] + + ([[ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ]]*24)+ + ([[ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 128), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 0), + ]]*11)+ + ([[ + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 128, 128, 128), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 0, 0, 0), + ]]*5)+ + [[ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ]] +) + +run_test( + os.path.join(INPUT_FOLDER, "lorem-v4.pdf"), + [ + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ], + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 64, 64, 64), + ] + ] +) + +run_test( + os.path.join(INPUT_FOLDER, "lorem-v5.pdf"), + [ + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 255, 255, 255), + ], + [ + PDFColor(None), + PDFColor(PREDEFINED_COLORSPACE['DeviceRGB'], 128, 128, 128), + ] + ] +)