Esempio n. 1
0
def createSVG(filename, tool, points):
  # Generate the paths
  allPoints = list()
  paths = list()
  t = Template(PATH_TEMPLATE)
  for point in points:
    allPoints.extend(point)
    path = Path()
    lx, ly = point[0]
    pc = 0
    for p in point[1:]:
      l = mkLine(lx, ly, p[0], p[1])
      if l is not None:
        path.append(l)
        pc = pc + 1
      lx = p[0]
      ly = p[1]
    path.closed = True
    paths.append(t.safe_substitute({ 'strokeWidth': tool, 'path': path.d() }))
    print "Generated path with %d points." % pc
  # Build the final output
  v = { 
    'xmin': min([ x for x, y in allPoints ]) - (tool / 2),
    'ymin': min([ y for x, y in allPoints ]) - (tool / 2),
    'xmax': max([ x for x, y in allPoints ]) + (tool / 2),
    'ymax': max([ y for x, y in allPoints ]) + (tool / 2)
    }
  v['width'] = abs(v['xmax'] - v['xmin'])
  v['height'] = abs(v['ymax'] - v['ymin'])
  v['path'] = "\n".join(paths)
  out = Template(SVG_TEMPLATE).substitute(v).strip()
  # And write the file
  with open(filename, "w") as svg:
    svg.write(out)
Esempio n. 2
0
def path_to_polygon(path):
    polygon = []
    global max_x, max_y, min_x, min_y
    new_path = Path()
    for segment in parse_path(path):
        new_path.append(Line(segment.start, segment.end))
    new_path.closed = True
    raw_path = new_path.d()

    # search for compound path bits and remove them
    path_bits = re.findall('M.+?[ZM]', raw_path)
    if len(path_bits) > 0:
        raw_path = path_bits[0]
        raw_path_size = len(re.findall(',', raw_path))
        for bit in path_bits:
            bit_size = len(re.findall(',', bit))
            if bit_size > raw_path_size:
                raw_path = bit
                raw_path_size = bit_size

    # convert to simple list of nodes
    nodes = re.findall('[ML]\s*(\d+\.*\d*,\d+\.*\d*)\s*', raw_path)
    for n in nodes:
        coords = n.split(',')
        if max_x < int(coords[0]):
            max_x = int(coords[0])
        if max_y < int(coords[1]):
            max_y = int(coords[1])
        if min_x > int(coords[0]):
            min_x = int(coords[0])
        if min_y > int(coords[1]):
            min_y = int(coords[1])
        polygon.append([int(coords[0]), int(coords[1])])
    polygon.pop()
    return polygon
Esempio n. 3
0
def createSVG(filename, tool, points):
    # Generate the paths
    allPoints = list()
    paths = list()
    t = Template(PATH_TEMPLATE)
    for point in points:
        allPoints.extend(point)
        path = Path()
        lx, ly = point[0]
        pc = 0
        for p in point[1:]:
            l = mkLine(lx, ly, p[0], p[1])
            if l is not None:
                path.append(l)
                pc = pc + 1
            lx = p[0]
            ly = p[1]
        path.closed = True
        paths.append(t.safe_substitute({
            'strokeWidth': tool,
            'path': path.d()
        }))
        print "Generated path with %d points." % pc
    # Build the final output
    v = {
        'xmin': min([x for x, y in allPoints]) - (tool / 2),
        'ymin': min([y for x, y in allPoints]) - (tool / 2),
        'xmax': max([x for x, y in allPoints]) + (tool / 2),
        'ymax': max([y for x, y in allPoints]) + (tool / 2)
    }
    v['width'] = abs(v['xmax'] - v['xmin'])
    v['height'] = abs(v['ymax'] - v['ymin'])
    v['path'] = "\n".join(paths)
    out = Template(SVG_TEMPLATE).substitute(v).strip()
    # And write the file
    with open(filename, "w") as svg:
        svg.write(out)