From 874701288d92975d537926983829f1e79a4fd4ba Mon Sep 17 00:00:00 2001 From: Adam Mayer Date: Thu, 21 Dec 2017 17:26:11 -0500 Subject: [PATCH] moving to Cairo instead of svgwrite --- apple410/__init__.py | 2 + apple410/plot_to_cairo.py | 158 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 apple410/plot_to_cairo.py diff --git a/apple410/__init__.py b/apple410/__init__.py index 557d9c2..145e4a3 100755 --- a/apple410/__init__.py +++ b/apple410/__init__.py @@ -44,6 +44,8 @@ class Apple410(Apple410Common): self.serial.flush() else: self.serial.flush() + while not self.serial.cts: + time.sleep(0.2) while not self.serial.dsr: time.sleep(0.2) self.serial.write(c.encode('ascii')) diff --git a/apple410/plot_to_cairo.py b/apple410/plot_to_cairo.py new file mode 100755 index 0000000..5048730 --- /dev/null +++ b/apple410/plot_to_cairo.py @@ -0,0 +1,158 @@ +#!/usr/bin/python3 +import sys +import math +import cairo + +def coordlist(line,expected=-1): + l = list(map(float,line.split(','))) + if expected > -1: + assert len(l) == expected + return l + +#pens = [ +# svgwrite.rgb(0, 0, 0, '%'), +# svgwrite.rgb(100, 0, 0, '%'), +# svgwrite.rgb(0, 100, 0, '%'), +# svgwrite.rgb(0, 0, 100, '%') ] + +# Default W/H +W = 2394 +H = 1759 +LW = 5 + +class CairoPlotter: + + def __init__(self, path): + self.surface = cairo.SVGSurface(path, W, H) + self.context = cairo.Context(self.surface) + #self.context.scale(W,H) + self.context.select_font_face("monospace") + self.pennum = 0 + self.viewport = (0, 0, W, H) + self.window = (0, 0, W, H) + self.text_theta = 0 + self.text_size = 1 + #self.pos = (0,0) + #self.clip = None + #self.clipno = 0 + self.in_path = False + self.update_ctm() + self.context.set_line_width(LW) + + # A quick review of coordinate systems: + # Cairo native: origin at upper left, x+ to right, y+ down. + # Plotter native: origin at lower left, x+ to right, y+ up. + # Plotter window: same as plotter native. + # We want to display in plotter native. + def update_ctm(self): + pn_to_cn = cairo.Matrix(yy=-1.0, y0=H) + self.context.set_matrix(pn_to_cn) + + def update_font(self): + m = cairo.Matrix(xx=self.text_size, yy=-self.text_size) + m2 = cairo.Matrix.init_rotate(math.radians(self.text_theta)) + m.multiply(m2) + self.context.set_font_matrix(m) + + def finish_path(self): + if self.in_path: + self.context.stroke_preserve() + self.in_path = False + + def invalidate_window(self): + pass + + def vp(self, params): + self.invalidate_window() + l=coordlist(params,4) + self.viewport=(l[0],l[1],l[2],l[3]) + + def wd(self, params): + self.invalidate_window() + l=coordlist(params,4) + self.window=(l[0],l[1],l[2],l[3]) + + def ma(self, params): + l=coordlist(params,2) + self.context.move_to(l[0],l[1]) + + def mr(self, params): + l=coordlist(params,2) + p=self.context.get_current_point() + self.context.move_to(p[0]+l[0],p[1]+l[1]) + + def da(self, params): + l=coordlist(params) + self.in_path = True + while len(l) > 0: + x, y, l = l[0],l[1],l[2:] + self.context.line_to(x,y) + + def dr(self, params): + l=coordlist(params) + self.in_path = True + while len(l) > 0: + x, y, l = l[0],l[1],l[2:] + p=self.context.get_current_point() + self.context.line_to(p[0]+x,p[1]+y) + + def ca(self, params): + self.context.new_sub_path() + l=coordlist(params) + r=l[0] + if len(l) > 2: + p=(l[1],l[2]) + else: + p=self.context.get_current_point() + self.context.arc(p[0],p[1],r,0.0,math.pi*2) + + def ac(self,params): + l=coordlist(params) + r=l[0] + if len(l) > 3: + p=(l[3],l[4]) + else: + p=self.pos + t2, t1 = math.radians(l[1]),math.radians(l[2]) + self.context.arc(p[0],p[1],r,t1,t2) + + def lr(self,params): + self.text_theta = float(params) + self.update_font() + + def ls(self,params): + self.text_size = float(params) + self.update_font() + + def ps(self,params): + self.finish_path() + self.pennum = int(params) - 1 + + def pl(self,params): + self.finish_path() + (x,y) = self.context.get_current_point() + sys.stderr.write("text at {} {}\n".format(x,y)) + self.context.show_text(params) + + def write(self,out): + self.finish_path() + self.surface.finish() + + def process_cmd(self,command): + cmd_code = command[0:2].lower() + params = command[2:] + try: + getattr(self,cmd_code)(params) + except AttributeError: + sys.stderr.write("Unrecognized command code {}\n".format(cmd_code)) + + def read(self,inf): + for line in inf.readlines(): + line = line.strip() + self.process_cmd(line) + +if __name__=='__main__': + p = CairoPlotter('foo.svg') + p.read(sys.stdin) + p.write(sys.stdout) +