def snapshot(self, snapshot: Gtk.Snapshot, bounds: Graphene.Rect, fgcolor: Gdk.RGBA, scale: float): # Drawing cached textures is in large scale faster than rendering # the svg over and over if self._cached_texture_scale < scale: scaled_w = int(WIDTH * scale) scaled_h = int(HEIGHT * scale) cr = cairo.ImageSurface( cairo.Format.ARGB32, scaled_w, scaled_h) ctx = cairo.Context(cr) rect = Rsvg.Rectangle() rect.x = 0 rect.y = 0 rect.width = scaled_w rect.height = scaled_h self._handle.render_document(ctx, rect) pixbuf = Gdk.pixbuf_get_from_surface(cr, 0, 0, scaled_w, scaled_h) self._cached_texture = Gdk.Texture.new_for_pixbuf(pixbuf) self._cached_texture_scale = scale snapshot.append_texture(self._cached_texture, bounds)
def get_base_image(self, width, height): #~ return cairo.ImageSurface.create_from_png(os.path.join("artwork", "grand_staff.png")) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) ctx = cairo.Context(surface) vp = Rsvg.Rectangle() vp.x, vp.y, vp.width, vp.height = 0, 0, width, height #~ self.symbols_svg.render_document(ctx, vp) self.symbols_svg.render_element(ctx, "#treble", vp) self.symbols_svg.render_element(ctx, "#bass", vp) ctx.set_source_rgba(1, 0, 0, 1); ctx.set_line_width(5); ctx.move_to(0, 0); ctx.line_to(200, 200); ctx.stroke(); return surface
def render(self, rect, ctx): xpos, ypos, width, height = rect.get_data() cx = xpos + width / 2. cy = ypos + height / 2. ctx.save() #~ ctx.set_operator(cairo.OPERATOR_OVER); #~ ctx.set_source_surface(self.ims, xpos + self.border_gap, ypos + self.border_gap) #~ ctx.paint() score_xpos = xpos + self.border_gap score_ypos = ypos + self.border_gap score_width = (width - 2 * self.border_gap) score_height = (height - 2 * self.border_gap) yslot_height = score_height / (4 * 7) ctx.set_source_rgb(0.0, 0.1, 0.3) ctx.set_line_width(1) for note in self.SCORE_LINES: yslot = self.yslot_for_note(note) y = score_ypos + yslot * yslot_height ctx.move_to(score_xpos, y) ctx.line_to(score_xpos + score_width, y) ctx.stroke() vp = Rsvg.Rectangle() vp.width, vp.height = score_width, yslot_height * 13 vp.x, vp.y = score_xpos, score_ypos + self.yslot_for_note(81) * yslot_height self.symbols_svg.render_element(ctx, "#treble", vp) vp.x, vp.y = score_xpos, score_ypos + self.yslot_for_note(62) * yslot_height self.symbols_svg.render_element(ctx, "#bass", vp) max_note = self.SCORE_MAX_NOTE for note in range(self.MAX_NOTE, max_note, -1): if self.music_info.keys_pressed[note]: max_note = note break min_note = self.SCORE_MIN_NOTE for note in range(self.MIN_NOTE, min_note - 1, 1): if self.music_info.keys_pressed[note]: min_note = note break vp = Rsvg.Rectangle() vp.width, vp.height = yslot_height * 2.5, yslot_height * 5 for note in range(self.MIN_NOTE, self.MAX_NOTE + 1): x = cx yslot = self.yslot_for_note(note) y = score_ypos + yslot * yslot_height if (yslot % 2) == 0: ledger_line = note > self.SCORE_MAX_NOTE and note <= max_note if (note > self.SCORE_MAX_NOTE and note <= max_note) or \ (note < self.SCORE_MIN_NOTE and note >= min_note) or \ (note == self.SCORE_MID_NOTE and self.music_info.keys_pressed[note]): ctx.set_source_rgb(0.0, 0.1, 0.3) ctx.set_line_width(1) ctx.move_to(x - 6, y) ctx.line_to(x + 6, y) ctx.stroke() if self.music_info.keys_pressed[note]: ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.arc(x, y, 4, 0, 2. * math.pi) ctx.fill() vp.x, vp.y = x - vp.width, y - vp.height / 2 #~ self.symbols_svg.render_element(ctx, "#flat", vp) #~ self.symbols_svg.render_element(ctx, "#sharp", vp) ctx.restore()