diff --git a/apple410/cmdline.py b/apple410/cmdline.py index 09efa46..69718ae 100644 --- a/apple410/cmdline.py +++ b/apple410/cmdline.py @@ -62,3 +62,17 @@ def plot2svg(): p.read(f) p.write() +def plot2png(): + parser = argparse.ArgumentParser("plot2png", + description="Convert a set of Apple 410 Color Plotter commands to a PNG") + parser.add_argument('PLOT', help='The plotter file to process. "-" will read plotter commands from standard input.') + args = parser.parse_args() + if args.PLOT == '-': + f = sys.stdin + else: + f = open(args.PLOT) + p = CairoPlotter(sys.stdout.buffer,'PNG') + p.read(f) + p.write() + + diff --git a/apple410/plot_to_cairo.py b/apple410/plot_to_cairo.py index f98929b..b6f1927 100755 --- a/apple410/plot_to_cairo.py +++ b/apple410/plot_to_cairo.py @@ -22,9 +22,18 @@ LW = 5 class CairoPlotter: - def __init__(self, path): - self.surface = cairo.SVGSurface(path, W, H) + def __init__(self, path, surftype = 'SVG'): + self.surftype = surftype + if surftype == 'SVG': + self.surface = cairo.SVGSurface(path, W, H) + elif surftype == 'PNG': + self.path = path + self.surface = cairo.ImageSurface(cairo.Format.RGB24,W,H) self.context = cairo.Context(self.surface) + if surftype == 'PNG': + self.context.rectangle(0,0,W,H) + self.context.set_source_rgb(1.0,1.0,1.0) + self.context.fill() #self.context.scale(W,H) self.context.select_font_face("monospace") self.pennum = 0 @@ -141,6 +150,11 @@ class CairoPlotter: def write(self): self.finish_path() + if self.surftype == 'PNG': + if self.path == '-': + self.surface.write_to_png(sys.stdout) + else: + self.surface.write_to_png(self.path) self.surface.finish() def process_cmd(self,command): diff --git a/setup.py b/setup.py index 649a6b6..8387f81 100644 --- a/setup.py +++ b/setup.py @@ -55,6 +55,7 @@ setup( 'apple410=apple410.cmdline:main', 'a410svg2plot=apple410.cmdline:svg2plot', 'a410plot2svg=apple410.cmdline:plot2svg', + 'a410plot2png=apple410.cmdline:plot2png', ], }, )