def draw(self, scale=0.01, cm=False): self.dat.scale(scale, center=False) self.page = self.page.scale(scale) b = self.dat.bounds() limits = Rect(0, 0, 11, 8.5) if cm: limits = limits.scale(2.54) if b.x >= 0 and b.y >= 0 and b.w <= limits.w and b.h <= limits.h: print("Drawing!") else: print("Too big!", b) return False ad = axidraw.AxiDraw() ad.interactive() ad.options.units = 1 if cm else 0 ad.options.speed_pendown = 50 ad.options.speed_penup = 50 ad.connect() ad.penup() self.ad = ad tp = TransformPen(self, (1, 0, 0, -1, 0, self.page.h)) self.dat.replay(tp) time.sleep(MOVE_DELAY) ad.penup() ad.moveto(0, 0) ad.disconnect()
def draw(self, scale=0.01, dry=True, cm=False): if dry: with viewer() as v: dp = DATPen().record(self.dat).attr(fill=None, stroke=0) v.send(SVGPen.Composite([dp], self.page), self.page) else: self.dat.scale(scale, center=False) self.page = self.page.scale(scale) b = self.dat.bounds() limits = Rect(0, 0, 11, 8.5) if cm: limits = limits.scale(2.54) if b.x >= 0 and b.y >= 0 and b.w <= limits.w and b.h <= limits.h: print("Drawing!") else: print("Too big!", b) return False ad = axidraw.AxiDraw() ad.interactive() ad.options.units = 1 if cm else 0 ad.options.speed_pendown = 50 ad.options.speed_penup = 50 ad.connect() ad.penup() self.ad = ad tp = TransformPen(self, (1, 0, 0, -1, 0, self.page.h)) self.dat.replay(tp) time.sleep(MOVE_DELAY) ad.penup() ad.moveto(0, 0) ad.disconnect()
class renderable(): """ Base class for any content renderable by Coldtype """ def __init__(self, rect=(1080, 1080), bg="whitesmoke", fmt="png", name=None, rasterizer=None, prefix=None, dst=None, custom_folder=None, postfn=None, watch=[], watch_restarts=[], watch_soft=[], solo=False, rstate=False, preview_only=False, direct_draw=False, clip=False, composites=False, bg_render=False, style="default", viewBox=True, layer=False): """Base configuration for a renderable function""" self.rect = Rect(rect) self.bg = normalize_color(bg) self.fmt = fmt self.prefix = prefix self.dst = Path(dst).expanduser().resolve() if dst else None self.custom_folder = custom_folder self.postfn = postfn self.last_passes = [] self.last_result = None self.style = style self.composites = composites self.watch = [] for w in watch: self.add_watchee(w) self.watch_restarts = [] for w in watch_restarts: self.watch_restarts.append(self.add_watchee(w, "restart")) self.watch_soft = [] for w in watch_soft: self.watch_soft.append(self.add_watchee(w, "soft")) self.name = name self.codepath = None self.rasterizer = rasterizer self.self_rasterizing = False self.hidden = solo == -1 self.solo = solo self.preview_only = preview_only self.rstate = rstate self.clip = clip self.viewBox = viewBox self.direct_draw = direct_draw self.bg_render = bg_render self.layer = layer if self.layer: self.bg = normalize_color(None) if not rasterizer: if self.fmt == "svg": self.rasterizer = "svg" elif self.fmt == "pickle": self.rasterizer = "pickle" else: self.rasterizer = "skia" def add_watchee(self, w, flag=None): try: pw = Path(w).expanduser().resolve() if not pw.exists(): print(w, "<<< does not exist (cannot be watched)") else: self.watch.append([pw, flag]) return pw except TypeError: if isinstance(w, Font): self.watch.append([w, flag]) else: raise Exception( "Can only watch path strings, Paths, and Fonts") def __call__(self, func): self.func = func if not self.name: self.name = self.func.__name__ return self def folder(self, filepath): return "" def pass_suffix(self): return self.name def passes(self, action, renderer_state, indices=[]): return [RenderPass(self, self.pass_suffix(), [self.rect])] def package(self, filepath, output_folder): pass def run(self, render_pass, renderer_state): if self.rstate: return render_pass.fn(*render_pass.args, renderer_state) else: return render_pass.fn(*render_pass.args) def runpost(self, result, render_pass, renderer_state): if self.postfn: return self.postfn(self, result) else: return result def draw_preview(self, scale, canvas: skia.Canvas, rect, result, render_pass): sr = self.rect.scale(scale, "mnx", "mxx") SkiaPen.CompositeToCanvas(result, sr, canvas, scale, style=self.style) def hide(self): self.hidden = True return self def show(self): self.hidden = False return self def normalize_result(self, pens): if not pens: return DATPens() elif hasattr(pens, "pens"): return pens elif isinstance(pens, DATPen): return DATPens([pens]) elif isinstance(pens, DATText): return DATPens([pens]) elif not isinstance(pens, DATPens): return DATPens(pens) else: return pens