# HG changeset patch # User David Douard # Date 1198277496 -3600 # Node ID c691b040e8c9213406d1b9215d1460818118b25d # Parent fb8aa055f6e42827954d43fdeb9198fe5fba5e2a added a (very) beginning HPGL parser (with a simplist matplotlib backend) diff -r fb8aa055f6e4 -r c691b040e8c9 hpgl_mpl.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hpgl_mpl.py Fri Dec 21 23:51:36 2007 +0100 @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +import numpy +import pylab + +from hpgl_parser import HPGLParser + +class HPGLmplPlotter(HPGLParser): + pens = "krbgcmyw" + lines = ['-', ':', '--', '-.', '.', '-', '-', '-'] + def __init__(self, data): + super(HPGLmplPlotter, self).__init__(data) + pylab.show() + + def plot_symbols(self, points): + x, y = points.T + pylab.plot(x, y, 'o') + + def plot_lines(self, points): + x, y = points.T + pylab.plot(x, y, self.pens[self.pen]+self.lines[self.line_type]) + +if __name__ == "__main__": + import sys + data = open(sys.argv[1]).read() + HPGLmplPlotter(data) + diff -r fb8aa055f6e4 -r c691b040e8c9 hpgl_parser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hpgl_parser.py Fri Dec 21 23:51:36 2007 +0100 @@ -0,0 +1,408 @@ +# -*- coding: utf-8 -*- + +import re +import numpy + +vsplitter = re.compile('[ ,]') +vextractor = re.compile('(?P[^;\n\r\aA-DF-Za-df-z]*?)(?P[;\n\r\a]+)', re.S+re.M) + +class HPGLParser(object): + def __init__(self, data): + self.data = data + self.idx = 0 + self.str_terminator = chr(0x03) + + self.IN() + + while self.idx1: + self.pattern_len = values[1] + + def PW(self): + """ Pen Width """ + values = self.extract_value() + if len(values) == 1: + self.pen_thickness = values[0] + + def SM(self): + """ Symbol Mode """ + values = self.extract_value() + if len(values) == 0: + self.symbol_mode = False + elif len(values) == 1: + self.symbol_mode = values[0] + + def SP(self): + """ Select Pen """ + values = self.extract_value() + if len(values) == 1: + self.pen = values[0] + + def TL(self): + """ Tick Len """ + values = self.extract_value() + # TODO + + def XT(self): + """ X-axis Tick """ + # TODO + print "X ticks" + + def YT(self): + """ X-axis Tick """ + # TODO + print "Y ticks" + + def PT(self): + """ Pen Thickness Select """ + values = self.extract_value() + if len(values) == 0: + self.pen_thickness = 0.3 + elif len(values) == 1: + self.pen_thickness = values[0] + + def CS(self): + """ Standard Character Set """ + values = self.extract_value() + if len(values) == 0: + self.std_char_set = 0 + elif len(values) == 1: + self.std_char_set = values[0] + + def CA(self): + """ Alternate Character Set """ + values = self.extract_value() + if len(values) == 0: + self.alt_char_set = 0 + elif len(values) == 1: + self.alt_char_set = values[0] + + def SS(self): + """ Select Standard Character Set """ + self.char_set = "std" + + def SA(self): + """ Select Alternate Character Set """ + self.char_set = "alt" + + def DT(self): + """ Define Label Terminator """ + values = self.extract_value(raw=True) + if len(values) == 0: + self.str_terminator = chr(0x03) + elif len(values) == 1: + self.str_terminator = values[0] + + def LB(self): + """ Character Plot """ + values = self.extract_string() + # TODO + + def DI(self): + """ Absolute Direction """ + values = self.extract_value() + if len(values) == 0: + self.char_direction = 1.0, 0.0 + elif len(values) == 2: + self.char_direction = values + + def DR(self): + """ Relative Direction """ + values = self.extract_value() + if len(values) == 0: + self.char_direction = 1.0, 0.0 + elif len(values) == 2: + # TODO : compute as percentages + self.char_direction = values + + def CP(self): + """ Character Plot """ + values = self.extract_value() + # TODO + if len(values) == 0: + values = 0, -1 + #if len(values) == 2: + # self.pos = + + def SI(self): + """ Set Absolute Character Size """ + values = self.extract_value() + self.char_size = "absolute" + if len(values) == 0: + self.char_width = 0.1879 + self.char_height = 0.2690 + elif len(values) == 2: + self.char_width, self.char_height = values + + def SR(self): + """ Set Relative Character Size """ + values = self.extract_value() + self.char_size = "relative" + if len(values) == 0: + self.char_width = 0.75 + self.char_height = 1.5 + elif len(values) == 2: + self.char_width, self.char_height = values + + def SL(self): + """ Character Slant """ + values = self.extract_value() + # TODO + + def UC(self): + """ User Defined Character """ + values = self.extract_value() + # TODO + +if __name__ == '__main__': + import sys + data = open(sys.argv[1]).read() + + p = HPGLParser(data) +