Ejemplo n.º 1
0
class Sketch:
    def __init__(self, width=600, height=600):
        self.width = width
        self.height = height
        self.svg = SVG(viewBox=f"0 0 {width} {height}",
                       style="stroke: black; fill: none;")
        self.background("white")
        self.tcount = 0

    def background(self, color):
        self.svg.rect(x=0,
                      y=0,
                      width=self.width,
                      height=self.height,
                      fill=color,
                      stroke="none")

    def circle(self, x, y, d):
        self.svg.circle(cx=x, cy=y, r=d / 2)

    def line(self, x1, y1, x2, y2, **kwargs):
        self.svg.line(x1=x1, y1=y1, x2=x2, y2=y2, **kwargs)

    def rect(self, x, y, w, h):
        self.svg.rect(x=x, y=y, width=w, height=h)

    def path(self, d):
        self.svg.path(d=d, stroke="black")

    def show(self, node, margin=0, scale=False, size=260):
        if margin or scale:
            s = (self.width - 2 * margin) / size if scale else 1
            g = Node(
                "g",
                transform=f"translate({margin}, {margin}) scale({s}, {s})")
            g.add_node(node)
            self.svg.add_node(g)
        else:
            self.svg.add_node(node)

    def transform(self, node, dx, dy, sx, sy):
        g = Node("g", transform=f"translate({dx}, {dy}) scale({sx}, {sy})")
        g.add_node(node)
        return g

    def thumbnail(self, shape):
        size = 260 / 4
        x = self.tcount * (size + 20) + 20
        y = 10
        self.show(self.transform(shape, x, y, 0.25, 0.25))
        self.tcount += 1

    def clear():
        self.tcount = 0

    def tostring(self):
        return self.svg.tostring()

    def render(self, code):
        g = self.get_globals()
        exec(code, g, g)
        return self.tostring()

    def show_grid(self):
        size = 100
        for y in range(0, self.height + 1, size):
            self.line(0, y, self.width, y, stroke="#ccc")

        for x in range(0, self.width + 1, size):
            self.line(x, 0, x, self.height, stroke="#ccc")

    def get_globals(self):
        return {
            "circle": self.circle,
            "rect": self.rect,
            "line": self.line,
            "path": self.path,
            "width": self.width,
            "height": self.height,
            "show_grid": self.show_grid,
            "Node": Node,
            "show": self.show,
            "thumbnail": self.thumbnail,
            **g.exports
        }
Ejemplo n.º 2
0
    def draw_pattern(self, id, pattern_raw, element, svg_group):
        pattern = [x.split() for x in pattern_raw]
        pheight = len(pattern)
        if pheight == 0:
            raise Exception("Missing pattern for {0}".format(id))
        if pheight > 3:
            raise Exception("Tall pattern for {0}".format(id))
        pwidth = len(pattern[0])

        # Max pattern size that fits on the card.
        max_width = 7
        max_height = 3

        # Center of pattern area.
        pcenter_x = self.width / 2
        pcenter_y = 22.4

        # Size and spacing for each box in pattern.
        box_size = 5.5
        box_spacing = 7

        # Upper left corner of pattern area
        px0 = pcenter_x - (((max_width - 1) * box_spacing) + box_size) / 2
        py0 = pcenter_y - (((max_height - 1) * box_spacing) + box_size) / 2

        # Calc offsets to center the patterns that are less than max size.
        if pwidth % 2 == 0:
            px0 += box_spacing / 2
            max_width = 6
        else:
            max_width = 7
        if pheight % 2 == 0:
            py0 += box_spacing / 2
            max_height = 2
        else:
            max_height = 3

        dot_x0 = px0 + (box_size / 2)
        dot_y0 = py0 + (box_size / 2)

        # The x,y ranges for this pattern (to center on the card)
        x_begin = int((max_width - pwidth) / 2)
        x_end = x_begin + pwidth
        y_begin = int((max_height - pheight) / 2)
        y_end = y_begin + pheight

        for iy in range(0, max_height):
            for ix in range(0, max_width):
                if ix >= x_begin and ix < x_end and iy >= y_begin and iy < y_end:
                    x = ix * box_spacing
                    y = iy * box_spacing

                    col = ix - x_begin
                    row = iy - y_begin
                    cell = pattern[row][col]
                    if cell == '@':
                        elemaster = '#element-{0}'.format(element)
                        eleclone = SVG.clone(0, elemaster, px0 + x, py0 + y)
                        SVG.add_node(svg_group, eleclone)
                    elif cell == 'X':
                        box = SVG.rect(0, px0 + x, py0 + y, box_size, box_size)

                        style_box = Style()
                        style_box.set_fill("none")
                        style_box.set_stroke("#000000", 0.5)
                        style_box.set('stroke-linecap', "round")
                        style_box.set('stroke-miterlimit', 2)
                        box.set_style(style_box)

                        SVG.add_node(svg_group, box)
                    elif cell == '.':
                        dot = SVG.circle(0, dot_x0 + x, dot_y0 + y, 0.8)

                        style_dot = Style()
                        style_dot.set_fill("#c0c0c0")
                        style_dot.set_stroke("none")
                        dot.set_style(style_dot)

                        SVG.add_node(svg_group, dot)
                    else:
                        raise Exception(
                            "Unrecognized pattern symbol: {0}".format(cell))