def new_drawing(filename='plot.svg', display_scale=1): from svgwrite.drawing import Drawing from svgwrite.container import Group size = 2 * PADDING + SCALE dwg = Drawing(filename, size=(size * display_scale, size * display_scale)) dwg.viewbox(minx=0, miny=0, width=size, height=size) add_stylesheet(dwg) canvas = Group(id='canvas') canvas.translate(PADDING, PADDING) canvas.scale(1, -1) canvas.translate(0, -SCALE) dwg.add(canvas) return dwg, canvas
def convert_font(): cwd = pathlib.Path.cwd() for root, dirs, files in os.walk('{}/exports'.format(cwd)): for name in files: source = os.path.join(root, name) target = 'fonts/{}.min.svg'.format(name.split('.')[0]) print(source) try: paths, attr = svg2paths(source) except Exception as e: print(e) continue try: xmin, xmax, ymin, ymax = bounding_box(paths) except Exception as e: print(e) continue dx = xmax - xmin dy = ymax - ymin viewbox = '{} {} {} {}'.format(xmin, ymin, dx, dy) attr = {'viewBox': viewbox, 'preserveAspectRatio': 'xMidYMid meet'} wsvg(paths=paths, svg_attributes=attr, filename=source) doc = SaxDocument(source) d = doc.get_pathd_and_matrix()[0] g = Group() dwg = svgwrite.Drawing(target) dwg.viewbox(minx=xmin, miny=ymin, width=dx, height=dy) dwg.add(g) g.scale(sx=1, sy=-1) g.translate(tx=0, ty=-dy - ymin * 2) g.add(dwg.path(d)) dwg.save() generate_pdf(target)
def encode(self, bits_data: List[int]): tattoo_bit_height = int(len(bits_data) / 8) x, y = 0, 0 xmin, xmax = 0, 0 ymin, ymax = 0, 0 tattoo = Group(id="tattoo") for j in range(tattoo_bit_height): for i in range(8): if x > xmax: xmax = x if y > ymax: ymax = y bit = bits_data.pop() if bit == 0: tattoo.add(Use("#zero", (x, y))) elif bit == 1: tattoo.add(Use("#one", (x, y))) else: raise RuntimeError() x, y = self.shift_bit(x, y) x, y = self.shift_byte(j, y) xmax += self.poly.inner_circle_radius * self.dimens.bit_radius * 2 ymax += self.poly.outer_circle_radius * self.dimens.bit_radius * 2 tattoo_width = xmax - xmin tattoo_height = ymax - ymin scaled_width_mm = 65 scale_factor = scaled_width_mm * 3.78 / tattoo_width scaled_height_mm = tattoo_height * scale_factor / 3.78 tattoo.scale(scale_factor) tattoo.translate( 3.78 * (210 - scaled_width_mm) / 2, 3.78 * (297 - scaled_height_mm) / 2 ) self.dwg.add(tattoo) self.dwg.save(pretty=True)