def parse_postscript(fullString): ''' Takes the usable part of the ps file (without newlines) and chops it up. intput: takes full string from get_postscript function output: generates a list of discrete commands for every object found. if additional primitives are foudn, it outputs those too. ''' commandList = [] # because the last possible parsable character is an f, warning bad naming convention! commandListString = fullString.split(" f")[:-1] for item in commandListString: commandList.append(regex_this_string(item)) # plotList takes the last chunk, this seems to be the primitive path drawing (ie not shapes) # should send extra regex here. plotListString = fullString.split(" f")[-1] plotListString = fix_plotListString(plotListString) plotList = plotListString.split("Q")[:-1] finalPlotList = [] for item in plotList: finalPlotList.append(regex_plotListString(item)) return commandList, finalPlotList
def regex_plotListString(plotListString): '''this function takes the second half of the instructions extracted from the ps The function exists only because supporting more commands was a bit of an afterthought, it requires some fixing by fix_plotListString due to zealous string chopping at the start. It feels a little wonky, and it is, but this gives me some idea of what to do next. Ultimately the initial parse pass should take care of both Paths and Shapes. intput: unformated primitive plot instructions like lines/dashes/circles output: after applying regular expressions the instructions are stored in a list and can be used similarly to c;eanStringList ''' plotListString = fix_plotListString(plotListString) primitive_commands = re.compile(r""" [0-9.]+\s[w] # value w = line width, and accepts int and floats | [-0-9.]+\s[JjmM] # value J = set line cap # value j = set line join # value M = set miter limit | [-0-9.]+\s[-0-9.]+\s[ml] # value value m = move to # value value l = line to | [[][-0-9. ]*[]][-0-9. ]+[d]\s\b # [optional value] value d = | [q][-0-9. ]+\s[c][m] # q value value value value cm = | [-0-9. ]+\s[c] # value*6 c = curve to | [S] # S = set stroke | [Q] # Q = seems to be a delimter. | [h] # h = closePath(); """, re.VERBOSE) m = primitive_commands.findall(plotListString) mclean = [] for i in m: mclean.append( i.lstrip()) return mclean