def __init__(self): VG.create_context((2048,2048)) self.x = 0 self.y = -.99 self.vr = 10 self.vt = math.pi/2 self.at = 0
def __init__(self): VG.create_context((2048,2048)) self.offsets = [] self.displacements = [] self.values = [] self.fill()
def draw(self): if self.transform: mat = VG.get_matrix() VG.mult_matrix(self.transform) self.path.draw(self.paint_mode, style=self.style) if self.transform: VG.load_matrix(mat)
def center_and_concat(p1, p2): (dx, dy), (dw, dh) = p1.bounds() (sx, sy), (sw, sh) = p2.bounds() dst_cx, dst_cy = (dx+dw/2.0), (dy+dh/2.0) src_cx, src_cy = (sx+sw/2.0), (sy+sh/2.0) VG.translate(dst_cx-src_cx, dst_cy-src_cy) p2.transform(p1) VG.load_identity()
def setup(name, width, height, flags=pygame.DOUBLEBUF): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) screen = pygame.display.set_mode((width, height), pygame.OPENGL | flags) pygame.display.set_caption(name) VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) pm.init_pymunk() return screen
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Rainbow Gradient Test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) canvas = VG.Path(capabilities=VG_PATH_CAPABILITY_APPEND_TO) VGU.rect(canvas, (0, 0), (width, height)) rgb_gradient = VG.GradientPaint([(0,0), (width,0)], linear=True) rgb_gradient.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT rgb_gradient.stops = generate_rgb_stops(7) alpha_stops = [(0.0, (0.0, 0.0, 0.0, 0.0)), (1.0, (1.0, 1.0, 1.0, 1.0))] alpha_gradient = VG.GradientPaint([(0,0), (0,height)], linear=True) alpha_gradient.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT alpha_gradient.stops = alpha_stops n = 7 running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 4: n += 1 rgb_gradient.stops = generate_rgb_stops(n) elif e.button == 5: if n > 2: n -= 1 rgb_gradient.stops = generate_rgb_stops(n) VG.clear((0, 0), (640, 480)) VG.set_paint(rgb_gradient, VG_FILL_PATH) canvas.draw(VG_FILL_PATH) VG.set_paint(alpha_gradient, VG_FILL_PATH) canvas.draw(VG_FILL_PATH) pygame.display.flip()
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Polyline test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) polyline = VG.Path() stroke_paint = VG.ColorPaint((0.5, 0.2, 0.8, 0.6)) VG.set_paint(stroke_paint, VG_STROKE_PATH) fill_paint = VG.ColorPaint((0.3, 1.0, 0.0, 0.6)) VG.set_paint(fill_paint, VG_FILL_PATH) polyline.style = VG.Style(VG_STROKE_LINE_WIDTH = 4.0, VG_STROKE_JOIN_STYLE = VG_JOIN_MITER, VG_STROKE_CAP_STYLE = VG_CAP_ROUND) print "Usage" print "Left click: LINE_TO" print "Right click: MOVE_TO" running = True first_click = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: #left button if first_click: polyline.move_to((e.pos[0], height-e.pos[1]), rel=False) first_click = False else: polyline.line_to((e.pos[0], height-e.pos[1]), rel=False) elif e.button == 3:#right button polyline.move_to((e.pos[0], height-e.pos[1]), rel=False) VG.clear((0, 0), (width, height)) polyline.draw(VG_STROKE_PATH) pygame.display.flip()
def draw(self): with self.style: VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) old_matrix = VG.get_matrix() VG.translate(*self.body.position) VG.rotate(math.degrees(self.body.angle)) self.path.draw(VG_STROKE_PATH) VG.load_matrix(old_matrix)
def main(width, height, message, rpm=20): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Freetype Font test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) font = Font("data/fonts/Vera.ttf", 64) red_paint = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) text = font.build_path(message) (x, y), (w, h) = text.bounds() text.style = VG.Style(VG_STROKE_LINE_WIDTH = 1.0, fill_paint = red_paint) clock = pygame.time.Clock() dt = 0 cx = width/2.0 - w/2.0 cy = height/2.0 - h/2.0 running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: pos = e.pos cx = pos[0] - w/2.0 cy = height-pos[1] - h/2.0 VG.clear((0, 0), (width, height)) VG.load_identity() VG.translate(cx, cy) rotate_about((x+w/2.0, y+h/2.0), dt*360.0*rpm / (60*1000.0)) text.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip() dt += clock.tick(60)
def draw(self): if self.transform: mat = VG.get_matrix() VG.mult_matrix(self.transform) if self.style: self.style.enable() for child in self.drawables: child.draw() if self.style: self.style.disable() if self.transform: VG.load_matrix(mat)
def draw(self): if self.transform: mat = VG.get_matrix() VG.mult_matrix(self.transform) if self.style: self.style.enable() for child in self.drawables: if isinstance(child, VG.Path): child.draw(self.paint_mode) else: child.draw() if self.style: self.style.disable() if self.transform: VG.load_matrix(mat)
def main(width, height, path, flags=0): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) flags |= pygame.OPENGL | pygame.DOUBLEBUF screen = pygame.display.set_mode((width, height), flags) pygame.display.set_caption("Pygame Image Direct Write Test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) im = load_image(path) dest_x = (width-im.width)/2.0 dest_y = (height-im.height)/2.0 dragging = False running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: dragging = True elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: dest_x += e.rel[0] dest_y -= e.rel[1] VG.clear((0, 0), (width, height)) VG.write_image(im, (dest_x, dest_y)) pygame.display.flip()
def wheel(self, num_segments, duty_cycle, outer_radius, inner_radius, line_width, color_bias, saturation, value): VG.set(VG_STROKE_LINE_WIDTH, 0.05) for i in range(0, num_segments): inner = 2 * pi / num_segments * (i + 0.5) inner_x = inner_radius * cos(inner) inner_y = inner_radius * sin(inner) p = VG.Path() r_start = 360.0 / num_segments * i r_dist = 360.0 / num_segments * duty_cycle p.move_to((inner_x, inner_y)) VGU.arc(p, (0, 0), (outer_radius * 2, outer_radius * 2), r_start, r_dist, 0xF100) p.line_to((inner_x, inner_y)) # XXX doesn't join to arc start quite correctly p.line_to((outer_radius * cos(r_start / 360.0 * 2 * pi), outer_radius * sin(r_start / 360.0 * 2 * pi))) (r, g, b) = hsv_to_rgb(float(i) / num_segments + color_bias * 2 * pi, saturation, value) paint = VG.ColorPaint((0, 0, 0, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) paint = VG.ColorPaint((r, g, b, 1.0)) VG.set_paint(paint, VG_FILL_PATH) p.draw(VG_FILL_PATH) p.draw(VG_STROKE_PATH)
def main(): WIDTH = 640 HEIGHT = 480 screen = setup("Pymunk + PyOpenVG test", WIDTH, HEIGHT, pygame.DOUBLEBUF | pygame.RESIZABLE) black = VG.ColorPaint((0.0, 0.0, 0.0, 1.0)) white = VG.ColorPaint((1.0, 1.0, 1.0, 1.0)) red = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) yellow = VG.ColorPaint((1.0, 1.0, 0.0, 1.0)) space = pm.Space() space.gravity = (0, -900) Ls = [StaticL(space, (300, 300), 200, 25), StaticL(space, (250, 150), 200, 25)] balls = [] running = True fps = 60 multiplier = 1 clock = pygame.time.Clock() while running: #Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif pygame.K_1 <= event.key <= pygame.K_3: multiplier = event.key - pygame.K_0 elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: pos = (event.pos[0], HEIGHT-event.pos[1]) balls.append(Ball(space, pos, 12*multiplier, smiley=True)) elif event.button == 3: pos = (event.pos[0], HEIGHT-event.pos[1]) balls.append(Ball(space, pos, 12*multiplier, smiley=False)) #Update world space.step(1.0/fps) #Redraw VG.clear((0,0), (WIDTH, HEIGHT)) for L in Ls: L.draw() offscreen = [] for ball in balls: if ball.body.position.y < 0: offscreen.append(ball) elif ball.body.position.x < 0 or ball.body.position.x > WIDTH: offscreen.append(ball) else: ball.draw() for ball in offscreen: balls.remove(ball) pygame.display.flip() clock.tick(fps)
def main(): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((640,480), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Paint test") VG.create_context((640, 480)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) VG.set(VG_STROKE_LINE_WIDTH, 3.0) p = VG.Path(capabilities=VG_PATH_CAPABILITY_APPEND_TO) VGU.ellipse(p, (0, 0), (64*2,64*2)) solid_paint = VG.ColorPaint((1.0, 0.0, 1.0)) stops = [(0.0,(1.0,1.0,1.0,1.0)), (0.333,(1.0,0.0,0.0,1.0)), (0.666,(0.0,1.0,0.0,1.0)), (1.0,(0.0,0.0,1.0,1.0))] linear_gradient = [(-60,0), (60,0)] linear_paint = VG.GradientPaint(linear_gradient, linear=True) linear_paint.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT linear_paint.stops = stops radial_gradient = [(0,0), (0,0), 64] radial_paint = VG.GradientPaint(radial_gradient, linear=False) radial_paint.stops = stops running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False VG.clear((0, 0), (640, 480)) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.translate(68, 240-64) VG.set_paint(solid_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.translate(192, 0) VG.set_paint(linear_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.translate(192, 0) VG.set_paint(radial_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def rotate_about(p, angle): VG.translate(p[0], p[1]) VG.rotate(angle) VG.translate(-p[0], -p[1])
def main(width, height, font_path, size): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Freetype Font test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) font = Font(font_path, size) xmin, ymin, xmax, ymax = [x*font.scale for x in font.face.bbox] the_path = VG.Path() message = font.build_path("left click and drag to move") (x, y), (w, h) = message.bounds() VG.translate(width/2.0-w/2.0, height-h) message.transform(the_path) VG.load_identity() VG.translate(0-xmin, height-ymax-h) boxes_per_line = int(width/float(xmax-xmin)) i = 0 for char_code, glyph in sorted(font.glyph_table.items()): font.get_path_for_glyph(glyph).transform(the_path) VG.translate(xmax-xmin, 0) i += 1 if i >= boxes_per_line: VG.translate((xmax-xmin)*-boxes_per_line, ymin-ymax) i = 0 VG.load_identity() red_paint = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) style = VG.Style(VG_STROKE_LINE_WIDTH = 1.0, fill_paint = red_paint) style.enable() dragging = False running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: dragging = True elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: VG.translate(e.rel[0], -e.rel[1]) VG.clear((0, 0), (width, height)) the_path.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4) screen = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Flower test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (0.0, 0.0, 0.0, 1.0)) vera = Font("data/fonts/Vera.ttf", 32) message = vera.build_path("Hold down LMB to create flowers") message.style = VG.Style(fill_paint=VG.ColorPaint((1.0, 1.0, 1.0, 0.7))) doc = parse_svg("data/svg/flower.svg") flower = doc.getroot() (x,y), (w,h) = flower.bounds() cx,cy = x+w/2.0, y+h/2.0 particles = [] to_remove = [] clock = pygame.time.Clock() dt = 0 running = True while running: for e in pygame.event.get(): if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False if pygame.mouse.get_pressed()[0]: x,y = pygame.mouse.get_pos() m = random.randint(-60, 60) angle = random.randint(0,359) pos = (x-cx, height-y-cy) vel = (m*math.cos(math.radians(angle)), m*math.sin(math.radians(angle))) rot = random.randint(-30, 30) scale = random.randint(8, 12)/10.0 particles.append([pos, vel, random.randint(0,359), rot, scale]) VG.clear((0,0), (width, height)) for particle in particles: VG.load_identity() pos, vel, angle, rot, scale = particle VG.translate(pos[0]+cx, pos[1]+cy) VG.scale(scale, scale) VG.rotate(angle) VG.translate(-cx, -cy) flower.draw() particle[0] = (pos[0]+vel[0]*dt/1000.0,pos[1]+vel[1]*dt/1000.0) particle[2] += rot * dt/1000.0 if particle[0][0] + w < 0 or particle[0][1] + h < 0: to_remove.append(particle) elif particle[0][0] - w > width or particle[0][1] - h > height: to_remove.append(particle) particles = [particle for particle in particles if particle not in to_remove] del to_remove[:] VG.load_identity() message.draw(VG_FILL_PATH) dt = clock.tick(30) pygame.display.flip()
def render(self): return smack = 1/2048.0 glScale(smack,smack,1.0) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0/smack, 1.0/smack) # Create the path (a simple circle for now...) p = VG.Path() r = 0.1 VGU.ellipse(p, (pe.kaleidoscope_x,pe.kaleidoscope_y), (r,r)) # Set up the drawing and painting parameters pe.vg_stroke_r = 0.5 + 0.35 * math.sin(7/10.0*pe.time) pe.vg_stroke_g = 0.5 + 0.35 * math.sin(11/10.0*pe.time) pe.vg_stroke_g = 0.5 + 0.35 * math.sin(13/10.0*pe.time) pe.vg_stroke_a = 1.0; pe.vg_fill_r = 1.0 - pe.vg_stroke_r pe.vg_fill_g = 1.0 - pe.vg_stroke_g pe.vg_fill_g = 1.0 - pe.vg_stroke_b VG.set(VG_STROKE_LINE_WIDTH, pe.vg_stroke_thickness) paint = VG.ColorPaint((pe.vg_stroke_r, pe.vg_stroke_g, pe.vg_stroke_b, pe.vg_stroke_a)) VG.set_paint(paint, VG_STROKE_PATH) paint = VG.ColorPaint((pe.vg_fill_r, pe.vg_fill_g, pe.vg_fill_b, pe.vg_fill_a)) VG.set_paint(paint, VG_FILL_PATH) p.draw(VG_FILL_PATH) p.draw(VG_STROKE_PATH)
def render_bg_old(self): smack = 1/2048.0 glScale(smack,smack,1.0) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0/smack, 1.0/smack) here = -1 idx = 0 while here < 1: width = random()*.2*3 p = VG.Path() p.move_to((-5,here+width/2)) p.line_to((5,here+width/2 + random()*.1)) v = random() VG.set(VG_STROKE_LINE_WIDTH, width) paint = VG.ColorPaint((0.0, 0.0, 0.0, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) p.draw(VG_STROKE_PATH) VG.set(VG_STROKE_LINE_WIDTH, width*.85) paint = VG.ColorPaint((v, v, v, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) p.draw(VG_STROKE_PATH) here += width
def build_path(self, text, horizontal=True, do_kerning=True): old_matrix = VG.get_matrix() VG.load_identity() path = VG.Path() #Kerning and vertical layouts are mutually exclusive in Freetype if do_kerning and horizontal and self.face.has_kerning: last_glyph = None for char in text: glyph = self.get_glyph(char) subpath = self.get_path_for_glyph(glyph) if last_glyph is not None: kerning = self.face.get_kerning(last_glyph.index, glyph.index, FT_KERNING_UNSCALED) VG.translate(self.scale*kerning[0], self.scale*kerning[1]) last_glyph = glyph subpath.transform(path) VG.translate(self.scale*glyph.advance[0], 0.0) else: for char in text: glyph = self.get_glyph(char) subpath = self.get_path_for_glyph(glyph) subpath.transform(path) VG.translate(self.scale*glyph.advance[0]*horizontal, self.scale*glyph.advance[1]*(not horizontal)) VG.load_matrix(old_matrix) return path
def render_bg(self): return self.update() smack = 1/2048.0 glScale(smack,smack,1.0) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0/smack, 1.0/smack) idx = 0 while 1: here = self.offsets[idx] width = self.offsets[idx+1]-here disp = self.displacements[idx] v = self.values[idx] idx += 1 if here > 1.0: break p = VG.Path() p.move_to((-5,here+width/2)) p.line_to((5,here+width/2 + disp)) VG.set(VG_STROKE_LINE_WIDTH, width) paint = VG.ColorPaint((0.0, 0.0, 0.0, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) p.draw(VG_STROKE_PATH) VG.set(VG_STROKE_LINE_WIDTH, width*.85) paint = VG.ColorPaint((v, v, v, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) p.draw(VG_STROKE_PATH)
def main(width, height, radius, count, flags=0): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) flags |= pygame.OPENGL | pygame.DOUBLEBUF srf = pygame.display.set_mode((width, height), flags) pygame.display.set_caption("Blending test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) orange_paint = VG.ColorPaint((1.0, 0.5, 0.0, 0.5)) blue_paint = VG.ColorPaint((0.0, 0.0, 1.0, 0.5)) black_paint = VG.ColorPaint((0.0, 0.0, 0.0)) blend_modes = [VG_BLEND_SRC, VG_BLEND_SRC_OVER, VG_BLEND_DST_OVER, VG_BLEND_SRC_IN, VG_BLEND_DST_IN, VG_BLEND_MULTIPLY, VG_BLEND_SCREEN, VG_BLEND_DARKEN, VG_BLEND_LIGHTEN, VG_BLEND_ADDITIVE] blend_index = 0 VG.set(VG_BLEND_MODE, blend_modes[0]) font = Font("data/fonts/Vera.ttf", 30) src_path = font.build_path("SRC") dst_path = font.build_path("DST") message = font.build_path("Scroll to change the blend mode") circle = VG.Path() VGU.ellipse(circle, (0, 0), (radius*2, radius*2)) center_and_concat(circle, dst_path) square = VG.Path() VGU.rect(square, (-radius, -radius), (radius*2, radius*2)) center_and_concat(square, src_path) circle2 = VG.Path() VGU.ellipse(circle2, (0, 0), (radius*2, radius*2)) running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 4: blend_index += 1 VG.set(VG_BLEND_MODE, blend_modes[blend_index % len(blend_modes)]) elif e.button == 5: blend_index -= 1 VG.set(VG_BLEND_MODE, blend_modes[blend_index % len(blend_modes)]) VG.clear((0, 0), (width, height)) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.translate(width - radius*1.5, radius * 2.5) circle.draw(VG_FILL_PATH | VG_STROKE_PATH) VG.translate(-radius, -radius) VG.set_paint(orange_paint, VG_FILL_PATH) square.draw(VG_FILL_PATH | VG_STROKE_PATH) VG.set_paint(blue_paint, VG_FILL_PATH) for i in xrange(count): angle = i*2*math.pi/count VG.load_identity() VG.translate(width//2, height//2) VG.translate(radius*math.cos(angle), radius*math.sin(angle)) circle2.draw(VG_FILL_PATH) VG.load_identity() (x,y), (w,h) = message.bounds() VG.translate(-x, height-y-h) message.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def render(self): smack = 1/2048.0 glScale(smack,smack,1.0) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0/smack, 1.0/smack) p = VG.Path() p.move_to((-0.5,-0.5)) VG.set(VG_STROKE_LINE_WIDTH, .01) pe.wave_a=0 pe.square_a=0 r = pe.mid theta = pe.time #VGU.round_rect(p, (-0.5,-0.5), (1,1), 1-pe.bass, 1-pe.bass) p.quad_to((-r*cos(theta), r*sin(theta)), (0.5, -0.5)) p.quad_to((-r*cos(theta), r*sin(theta)), (0, 0.5)) p.quad_to((-r*cos(theta), r*sin(theta)), (-0.5, -0.5)) paint = VG.ColorPaint((pe.wave_r, pe.wave_g, pe.wave_b, 1.0)) VG.set_paint(paint, VG_STROKE_PATH) paint = VG.ColorPaint((1-pe.wave_r, 1-pe.wave_g, 1-pe.wave_b, 1.0)) VG.set_paint(paint, VG_FILL_PATH) p.draw(VG_FILL_PATH) p.draw(VG_STROKE_PATH)
def build_path(self, string, index, ctp): ctp_x, ctp_y = ctp path = VG.Path() vertical = False with VG.push([1,0,0,0,1,0,0,0,1]): #TODO: vertical and bidi text. last_glyph = None for char in string: ctp_x, ctp_y = self._transform(ctp_x, ctp_y, index) glyph = self.font.get_glyph(char) subpath = self.font.get_path_for_glyph(glyph) #Flip it. with VG.push([1,0,0,0,-1,0,0,0,1]): subpath = subpath.transform() VG.translate(ctp_x, ctp_y) if self.font.face.has_kerning and last_glyph is not None: kerning = self.font.face.get_kerning(last_glyph.index, glyph.index, FT_KERNING_UNSCALED) VG.translate(self.font.scale*kerning[0], self.font.scale*kerning[1]) ctp_x += self.font.scale*kerning[0] ctp_y += self.font.scale*kerning[1] if index < len(self.trot)-1: VG.translate(ctp_x, ctp_y) VG.rotate(self.rotate[index]) VG.translate(-ctp_x, -ctp_y) last_glyph = glyph subpath.transform(path) ctp_x += self.font.scale*glyph.advance[0] ctp_y += self.font.scale*glyph.advance[1]*vertical VG.load_matrix([1,0,0,0,1,0,0,0,1]) return path, ctp
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Interpolation test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) paths = [] path_tag = ".//{http://www.w3.org/2000/svg}path" VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.scale(5.0, 5.0) doc = parse_svg("data/svg/shapes.svg") for element in doc.findall(path_tag): path = element.path.transform() path.style = element.style if not path.style: path.style = VG.Style(VG_STROKE_LINE_WIDTH=5.0) else: path.style[VG_STROKE_LINE_WIDTH] *= 2 paths.append(path) VG.load_identity() morph = VG.Path() start = paths[0] end = paths[1] morph.style = start.style VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.translate(220, 140) clock = pygame.time.Clock() running = True dt = 0 i = 0 while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False VG.clear((0, 0), (width, height)) morph.clear() VG.interpolate(start, end, morph, dt/float(MORPH_TIME)) morph.draw(VG_STROKE_PATH) pygame.display.flip() dt += clock.tick(60) if dt >= MORPH_TIME: dt -= MORPH_TIME i += 1 start = paths[i % len(paths)] end = paths[(i+1) % len(paths)] morph.style = start.style
def __init__(self): VG.create_context((2048,2048))
def main(width, height, directory): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4) screen = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("SVG Viewer") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) register_font_finder(pygame.font.match_font) @register_font_finder def fallback_font(name): fname = pygame.font.get_default_font() return os.path.join(os.path.dirname(pygame.font.__file__), fname) vera = Font("data/fonts/Vera.ttf", 16) text = vera.build_path("Scroll to change svg files. Drag to see more.") drawings = [] for root, dirs, files in os.walk(directory): for fname in files: if not fname.endswith(".svg"): continue path = os.path.join(root, fname) try: tree = parse_svg(path) name = vera.build_path(fname, 16) tree.getroot().fit(width, height) drawings.append((tree.getroot(), name)) except: print "Error in loading %s" % path traceback.print_exc(0) dragging = False dx = dy = 0 i = 0 scale = 1 drawing, name = drawings[0] (x,y), (w,h) = drawing.bounds() running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.key == pygame.K_1: scale = 1 elif e.key == pygame.K_2: scale = 2 elif e.key == pygame.K_3: scale = 3 elif e.key == pygame.K_4: scale = 4 elif e.key == pygame.K_5: scale = 5 elif e.key == pygame.K_6: scale = 6 elif e.key == pygame.K_7: scale = 7 elif e.key == pygame.K_8: scale = 8 elif e.key == pygame.K_9: scale = 9 elif e.key == pygame.K_EQUALS: i += 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.key == pygame.K_MINUS: i -= 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1 or e.button == 2 or e.button == 3: dragging = True else: if e.button == 4: i += 1 else: i -= 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1 or e.button == 2 or e.button == 3: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: dx += e.rel[0]/2.0**(scale-1) dy -= e.rel[1]/2.0**(scale-1) VG.clear((0, 0), (width, height)) VG.load_identity() VG.scale(2**(scale-1),2**(scale-1)) VG.translate(width/2.0-w/2.0-x+dx, height/2.0-h/2.0-y+dy) drawing.draw() VG.load_identity() VG.translate(10, 10) text.draw(VG_FILL_PATH) VG.translate(text.bounds()[1][0]+10, 0) name.draw(VG_FILL_PATH) pygame.display.flip()
def render(self): if (pe.square_a != 0.0 and pe.vg_mode == 3): VG.set(VG_STROKE_LINE_WIDTH, 15.0) fill_paint = VG.ColorPaint((pe.square_r, pe.square_g, pe.square_b, pe.square_a)) stroke_paint = VG.ColorPaint(( 0.65 + 0.350*( 0.60*math.sin(0.742*pe.time) + 0.40*math.sin(1.021*pe.time) ), 0.65 + 0.350*( 0.60*math.sin(0.703*pe.time) + 0.40*math.sin(0.969*pe.time) ), 0.65 + 0.350*( 0.60*math.sin(1.090*pe.time) + 0.40*math.sin(0.963*pe.time) ), pe.square_a)) VG.set_paint(fill_paint, VG_FILL_PATH) VG.set_paint(stroke_paint, VG_STROKE_PATH) p = VG.Path(capabilities=VG_PATH_CAPABILITY_APPEND_TO) vx=self.vr*math.cos(self.vt) vy=self.vr*math.sin(self.vt) p.move_to((self.x,self.y)) self.x+=vx self.y+=vy p.line_to((self.x,self.y)) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(0.001, 0.001) p.draw(VG_STROKE_PATH); if random()<.01: self.at = 0 if self.x<-1000 or self.x>1000 or self.y<-1000 or self.y>1000: self.x=0 self.y=0 self.vt += (random()-.5)*.1 self.vt += self.at self.at += (random()-.5)*.1
def render(self): if pe.vg_mode == 1: smack = 1 / 2048.0 glScale(smack, smack, 1.0) # VG.set(VG_STROKE_JOIN_STYLE, VG_JOIN_ROUND) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.scale(1.0 / smack, 1.0 / smack) p = VG.Path() p.move_to((0, 0)) VG.set(VG_STROKE_LINE_WIDTH, 0.01) # VGU.rect(p, (0,0), (pe.q1,pe.q2)) # VGU.rect(p, (0,0), (x,y)) # VGU.arc(p, (0,0), (pe.q1,pe.q2), # 0, 2*pi, # 0xF100) # VGU.arc(p, (0,0), (.5,.5), 0, 90, 0xF100) # r = pe.q1 # p.move_to((0,r)) # p.arc_to((r,0), r, r, 0, False, False) # paint = VG.ColorPaint((0.0, 1.0, 1.0, 1.0)) # VG.set_paint(paint, VG_STROKE_PATH) # p.draw(VG_STROKE_PATH); # self.wheel(NUM_SEGMENTS, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, # 0, 1, 1) RADIUS = 0.2 * (sin(pe.time) + 1.0) / 2.0 * 3.0 COUNT = 10 SIZE = 0.2 BIAS = pe.time / COUNT * 2 * pi for i in range(0, COUNT): saved = VG.get_matrix() angle = 2 * pi * (float(i) / COUNT) + BIAS VG.translate(RADIUS * cos(angle), RADIUS * sin(angle)) VG.scale(SIZE, SIZE) VG.rotate(pe.time * 2 * pi * 10.0 + float(i) / COUNT * 2 * pi * 2.0) self.wheel(5, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, float(COUNT), 1, 1) VG.load_matrix(saved) return GRID_X = 5 GRID_Y = 5 idx = 0 seed(int(pe.time)) step = pe.time - int(pe.time) phase = sin(step * pi) for j in range(0, GRID_Y): for i in range(0, GRID_X): saved = VG.get_matrix() VG.translate((i + 0.5) / GRID_X * 2 - 1, (j + 0.5) / GRID_Y * 2 - 1) VG.scale(2.0 / GRID_X, 2.0 / GRID_Y) # if random() < .1: # VG.scale(1.0+phase,1.0+phase) VG.scale(pe.bass + 1.0, pe.treb + 1.0) VG.rotate(float(i) / GRID_X * 360.0 * pe.time) # awesome # VG.translate(sin(pe.time+i/GRID_Y),0) segments = min(j + 1, GRID_Y - j) flag = True if ((j + 2) % (i + 2)) and ((i + 2) % (j + 2)) else False self.wheel( segments, DUTY_CYCLE, OUTER_RADIUS, INNER_RADIUS, LINE_WIDTH, float(i) / GRID_X, sin(float(i) / GRID_X * pi) + sin(pe.time) * 2 * pi, 1, ) VG.load_matrix(saved)