Exemplo n.º 1
0
 def draw(self, root, surface=None):
     surface = surface if surface is not None else self.screen
     points = []
     for func, args in flatten(root):
         #if func not in ["begin_region", "move_to", "line_to"]: continue
         if func == "stroke_and_fill":
             # Hack for arcs
             if len(points) <= 1: continue
             #flat_pts = [coord for point in points for coord in point]
             # Problem: with alpha, want a bbox and then translate...
             if default_get(args, "fill_color") and len(points) > 2:
                 pygame.draw.polygon(
                     surface, intcol(default_get(args, "fill_color")),
                     map(tuple, points), 0)
             if default_get(args, "stroke_color"):
                 if default_get(args, "dash"):
                     #[start*t + end*(1-t) for t in midpoints]
                     pass
                 pygame.draw.lines(
                     surface, intcol(default_get(args, "stroke_color")),
                     False, points, int(default_get(args, "line_width")))
         elif func == "text":
             font_size = args['font_size'] if numpy.array_equal(args["transform"], identity) or args['transform'][0][1] or args['transform'][1][0]\
                         else args['font_size'] * args["transform"][0][0]
             #font_size = args['font_size']
             font_param = (args.get("font_face"),
                           int(round(1.5 * font_size)))
             color = intcol(default_get(args, "stroke_color"))
             text = get_font(font_param).render(str(args["text"]), True,
                                                color)
             surface.blit(text, args["botleft"] - P(0, text.get_height()))
         elif func == "group":
             pass
         elif func == "begin_region":
             points = []
         elif func == "end_region":
             pass
         elif func in ["move_to", "line_to"]:
             points.append(args)
         elif func == "arc":
             # Doesn't work inside polygons yet.
             topleft = args["center"] - P(args["radius"], args["radius"])
             wh = P(2 * args["radius"], 2 * args["radius"])
             angles = args["angle"]
             if angles == (0, 2 * math.pi):
                 pygame.draw.circle(
                     surface,
                     intcol(default_get(args["style"], "stroke_color")),
                     map(int, args["center"]), args["radius"], 0)
             else:
                 pygame.draw.arc(
                     surface,
                     intcol(default_get(args["style"], "stroke_color")),
                     (topleft, wh), angles[0], angles[1],
                     default_get(args, "line_width"))
         else:
             raise Exception('Unknown function %s, %s' % (func, args))
Exemplo n.º 2
0
 def draw(self, root):
     points = []
     for func, args in flatten(root):
         #if func not in ["begin_region", "move_to", "line_to"]: continue
         if func == "stroke_and_fill":
             # Hack for arcs
             if len(points) <= 1: continue
             flat_pts = [coord for point in points for coord in point]
             self.canvas.create_polygon(
                 *flat_pts,
                 width=default_get(args, "line_width"),
                 dash=default_get(args, "dash")[0],
                 outline=hexcol(default_get(args, "stroke_color")),
                 fill=hexcol(default_get(args, "fill_color")))
         elif func == "text":
             font = (args["font_face"] if args.get("font_face") else
                     "TkFixedFont", int(round(args["font_size"])))
             self.canvas.create_text(*args["botleft"],
                                     font=font,
                                     fill=hexcol(
                                         default_get(args, "stroke_color")),
                                     text=args["text"],
                                     anchor="sw")
         elif func == "group":
             pass
         elif func == "begin_region":
             points = []
         elif func == "end_region":
             pass
         elif func in ["move_to", "line_to"]:
             points.append(args)
         elif func == "arc":
             # Doesn't work inside polygons yet.
             x, y = args["center"]
             r = args["radius"]
             start = args["angle"][0] * 180 / math.pi,
             angle_diff = (args["angle"][0] -
                           args["angle"][1]) * 180 / math.pi
             slice_type = Tkinter.PIESLICE if default_get(
                 args["style"], "fill_color") else Tkinter.ARC
             if angle_diff % 360 == 0:
                 #self.canvas.create_arc(x-r, y-r, x+r, y+r, start=0, extent=360, fill="red")
                 start = 0.0
                 # Must be a tkinter bug
                 angle_diff = 359
             self.canvas.create_arc(
                 x - r,
                 y - r,
                 x + r,
                 y + r,
                 start=start,
                 extent=angle_diff,
                 style=slice_type,
                 outline=hexcol(default_get(args["style"], "stroke_color")),
                 fill=hexcol(default_get(args["style"], "fill_color")))
         else:
             raise Exception('Unknown function %s, %s' % (func, args))
Exemplo n.º 3
0
 def draw(self, root):
     points = []
     for func, args in flatten(root):
         #if func not in ["begin_region", "move_to", "line_to"]: continue
         if func == "stroke_and_fill":
             # Hack for arcs
             if len(points) <= 1: continue
             if default_get(args, "fill_color"):
                 glColor3f(*default_get(args, "fill_color"))
                 glBegin(GL_POLYGON)
                 for point in points:
                     glVertex2d(*(point))
                 glEnd()
             if default_get(args, "stroke_color"):
                 glColor3f(*default_get(args, "stroke_color"))
                 glBegin(GL_LINE_LOOP)
                 for point in points:
                     glVertex2d(*(point))
                 glEnd()
         elif func == "text":
             glWindowPos2f(args["botleft"][0],
                           self.height - args["botleft"][1])
             for ch in args["text"]:
                 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,
                                     ctypes.c_int(ord(ch)))
         elif func == "group":
             pass
         elif func == "begin_region":
             points = []
         elif func == "end_region":
             pass
         elif func in ["move_to", "line_to"]:
             points.append(args)
         elif func == "arc":
             # Doesn't work inside polygons yet.
             x, y = args["center"]
             r = args["radius"]
             start = args["angle"][0] * 180 / math.pi,
             angle_diff = (args["angle"][0] -
                           args["angle"][1]) * 180 / math.pi
             if "fill_color" in args["style"]:
                 glColor3f(*default_get(args["style"], "fill_color"))
                 glBegin(GL_POLYGON)
             else:
                 glColor3f(*default_get(args["style"], "stroke_color"))
                 glBegin(GL_LINES)
             N = 20
             for i in xrange(N):
                 theta = i * 2 * math.pi / N
                 glVertex2d(x + r * math.sin(theta),
                            y + r * math.cos(theta))
             glEnd()
         else:
             raise Exception('Unknown function %s, %s' % (func, args))
Exemplo n.º 4
0
 def draw(self, layer = "drawing", root = None):
     if root is None:
         doc, root_id = self.layers[layer]
         root = doc[root_id]
     context = self.contexts[layer]
     for func, args in flatten(root):
         if func == "stroke_and_fill":
             if default_get(args, "fill_color"):
                 context.set_source_rgb(*default_get(args, "fill_color"))
                 if args.get("stroke_color") is not None:
                     context.fill_preserve()
                 else:
                     context.fill()
             if default_get(args, "stroke_color"):
                 context.set_line_width(default_get(args, "line_width"))
                 context.set_source_rgb(*default_get(args, "stroke_color"))
                 context.set_dash(*default_get(args, "dash"))
                 context.stroke()
         elif func == "text":
             if args["text"] is None:
                 continue
             context.set_source_rgb(*default_get(args, "stroke_color"))
             context.move_to(*args["botleft"])
             context.set_font_size(1.5 * args["font_size"])
             if numpy.array_equal(args["transform"], identity):
                 context.show_text(unicode(args["text"]))
             else:
                 context.save()
                 matrix = cairo.Matrix(*args["transform"].T[:,:2].flatten())
                 context.transform(matrix)
                 context.show_text(unicode(args["text"]))
                 context.restore()
         elif func == "group":
             pass
         elif func in ["begin_region", "end_region"]:
             pass
         elif func == "arc":
             flat = list(args["center"]) + [args["radius"]] + list(args["angle"])
             context.arc(*flat)
         else:
             getattr(context, func)(*args)
Exemplo n.º 5
0
 def draw(self, layer="drawing", root=None):
     if root is None:
         doc, root_id = self.layers[layer]
         root = doc[root_id]
     context = self.contexts[layer]
     for func, args in flatten(root):
         if func == "stroke_and_fill":
             opacity = default_get(args, "opacity")
             if default_get(args, "fill_color"):
                 context.set_source_rgba(*default_get(args, "fill_color"),
                                         alpha=opacity)
                 if args.get("stroke_color") is not None:
                     context.fill_preserve()
                 else:
                     context.fill()
             if default_get(args, "stroke_color"):
                 context.set_line_width(default_get(args, "line_width"))
                 context.set_source_rgba(*default_get(args, "stroke_color"),
                                         alpha=opacity)
                 context.set_dash(*default_get(args, "dash"))
                 context.stroke()
         elif func == "text":
             if args["text"] is None:
                 continue
             opacity = default_get(args, "opacity")
             context.set_source_rgba(*default_get(args, "stroke_color"),
                                     alpha=opacity)
             context.move_to(*args["botleft"])
             context.set_font_size(1.5 * args["font_size"])
             if args.get("font_face"):
                 context.select_font_face(args["font_face"],
                                          cairo.FONT_SLANT_NORMAL,
                                          cairo.FONT_WEIGHT_NORMAL)
             else:
                 context.set_font_face(None)
             if numpy.array_equal(args["transform"], identity):
                 context.show_text(unicode(args["text"]))
             else:
                 context.save()
                 matrix = cairo.Matrix(
                     *args["transform"].T[:, :2].flatten())
                 context.transform(matrix)
                 context.show_text(unicode(args["text"]))
                 context.restore()
         elif func == "image":
             # PNG only for the moment
             img = cairo.ImageSurface.create_from_png(args["filename"])
             if numpy.array_equal(args["transform"], identity):
                 context.set_source_surface(img, *args["topleft"])
                 context.paint()
             else:
                 context.save()
                 matrix = cairo.Matrix(
                     *args["transform"].T[:, :2].flatten())
                 context.transform(matrix)
                 context.set_source_surface(img, *args["topleft"])
                 context.paint()
                 context.restore()
         elif func == "group":
             pass
         elif func in ["begin_region", "end_region"]:
             pass
         elif func == "arc":
             flat = list(args["center"]) + [args["radius"]] + list(
                 args["angle"])
             context.arc(*flat)
         else:
             getattr(context, func)(*args)