def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' self._turtles.turtle_window.canvas.draw_pixbuf( pixbuf, a, b, x, y, w, h, self._heading) if self._turtles.turtle_window.sharing() and share: if self._turtles.turtle_window.running_sugar: tmp_path = get_path(self._turtles.turtle_window.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join( get_path(self._turtles.turtle_window.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() pos = self._turtles.screen_to_turtle_coordinates((x, y)) event = 'P|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(b), round_int(pos[0]), round_int(pos[1]), round_int(w), round_int(h), round_int(width), round_int(height), data]])) GObject.idle_add(self._turtles.turtle_window.send_event, event) os.remove(tmp_file)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' self._turtles.turtle_window.canvas.draw_pixbuf( pixbuf, a, b, x, y, w, h, self._heading) if self._turtles.turtle_window.sharing() and share: if self._turtles.turtle_window.running_sugar: tmp_path = get_path(self._turtles.turtle_window.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join( get_path(self._turtles.turtle_window.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() pos = self._turtles.screen_to_turtle_coordinates((x, y)) event = 'P|%s' % (data_to_string([self._turtles.turtle_window.nick, [round_int(a), round_int(b), round_int(pos[0]), round_int(pos[1]), round_int(w), round_int(h), round_int(width), round_int(height), data]])) gobject.idle_add(self._turtles.turtle_window.send_event, event) os.remove(tmp_file)
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cr = gtk.gdk.CairoContext(self.canvas) cr.save() # center the rotation on the center of the image cr.translate(x + w / 2., y + h / 2.) cr.rotate(self.heading * DEGTOR) cr.translate(-x - w / 2., -y - h / 2.) cr.set_source_pixbuf(pixbuf, x, y) cr.rectangle(x, y, w, h) cr.fill() cr.restore() self.inval() if self.tw.saving_svg: if self.tw.running_sugar: # In Sugar, we embed the images inside the SVG tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) self.tw.svg_string += self.svg.image( x, y, w, h, path, image_to_base64(tmp_file, get_path(self.tw.activity, 'instance'))) os.remove(tmp_file) else: # In GNOME, we embed a path self.tw.svg_string += self.svg.image(x - self.width / 2, y, w, h, path) if self.tw.sharing() and share: if self.tw.running_sugar: tmp_path = get_path(self.tw.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() x, y = self.screen_to_turtle_coordinates(x, y) event = 'P|%s' % (data_to_string([ self._get_my_nick(), [ round_int(a), round_int(b), round_int(x), round_int(y), round_int(w), round_int(h), round_int(width), round_int(height), data ] ])) self.tw.send_event(event) os.remove(tmp_file)
def get_svg_path(self): '''We use a separate file for the svg used for generating Sugar icons ''' if self.turtle_window.running_sugar: return os.path.join(get_path(self.turtle_window.activity, 'instance'), 'output.svg') else: return TMP_SVG_PATH
def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): ''' Draw a pixbuf ''' def _draw_pixbuf(cr, pixbuf, a, b, x, y, w, h, heading): # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cc = gtk.gdk.CairoContext(cr) cc.save() # center the rotation on the center of the image cc.translate(x + w / 2., y + h / 2.) cc.rotate(heading * DEGTOR) cc.translate(-x - w / 2., -y - h / 2.) cc.set_source_pixbuf(pixbuf, x, y) cc.rectangle(x, y, w, h) cc.fill() cc.restore() _draw_pixbuf(self.canvas, pixbuf, a, b, x, y, w, h, self.heading) self.inval() if self.cr_svg is not None: _draw_pixbuf(self.cr_svg, pixbuf, a, b, x, y, w, h, self.heading) if self.tw.sharing() and share: if self.tw.running_sugar: tmp_path = get_path(self.tw.activity, 'instance') else: tmp_path = '/tmp' tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), 'tmpfile.png') pixbuf.save(tmp_file, 'png', {'quality': '100'}) data = image_to_base64(tmp_file, tmp_path) height = pixbuf.get_height() width = pixbuf.get_width() x, y = self.screen_to_turtle_coordinates(x, y) event = 'P|%s' % (data_to_string([self._get_my_nick(), [round_int(a), round_int(b), round_int(x), round_int(y), round_int(w), round_int(h), round_int(width), round_int(height), data]])) gobject.idle_add(self.tw.send_event, event) os.remove(tmp_file)
def setup_svg_surface(self): ''' Set up a surface for saving to SVG ''' if self.turtle_window.running_sugar: svg_surface = cairo.SVGSurface( os.path.join(get_path(self.turtle_window.activity, 'instance'), 'output.svg'), self.width, self.height) else: svg_surface = cairo.SVGSurface( TMP_SVG_PATH, self.width, self.height) self.cr_svg = cairo.Context(svg_surface) self.cr_svg.set_line_cap(1) # Set the line cap to be round
def setup_svg_surface(self): ''' Set up a surface for saving to SVG ''' if self.turtle_window.running_sugar: svg_surface = cairo.SVGSurface( os.path.join(get_path(self.turtle_window.activity, 'instance'), 'output.svg'), self.width, self.height) else: svg_surface = cairo.SVGSurface(TMP_SVG_PATH, self.width, self.height) self.cr_svg = cairo.Context(svg_surface) self.cr_svg.set_line_cap(1) # Set the line cap to be round
def save_html(self, tw, embed_flag=True): """ Either save the canvas and code or pictures to HTML """ if embed_flag: HTML_GLUE['img'] = ('<img width="400" height="300" alt=' + \ '"Image" src="data:image/png;base64,\n', '"/>\n') HTML_GLUE['img2'] = ('<img alt="Image" src="data:image/png;' + \ 'base64,\n', '"/>\n') """ If there are saved_pictures, put them into a .html; otherwise, save a screendump and the turtle project code. """ htmlcode = '' if len(tw.saved_pictures) > 0: # saved_picture list is a collection of tuples of either the # image_file or the containing dsobject and an SVG flag for i, (image, svg_flag) in enumerate(tw.saved_pictures): htmlcode += HTML_GLUE['slide'][0] + str(i) htmlcode += HTML_GLUE['slide'][1] + \ HTML_GLUE['div'][0] + \ HTML_GLUE['h1'][0] if tw.running_sugar: from sugar.datastore import datastore dobject = datastore.get(image) # dsobject.object_id image_file = dobject.file_path else: image_file = image if embed_flag: f = open(image_file, 'r') imgdata = f.read() f.close() if svg_flag: tmp = imgdata else: imgdata = image_to_base64( image_file, get_path(tw.activity, 'instance')) tmp = HTML_GLUE['img2'][0] tmp += imgdata tmp += HTML_GLUE['img2'][1] else: if svg_flag: f = open(image_file, 'r') imgdata = f.read() f.close() tmp = imgdata else: tmp = HTML_GLUE['img3'][0] tmp += image_file tmp += HTML_GLUE['img3'][1] htmlcode += tmp + \ HTML_GLUE['h1'][1] + \ HTML_GLUE['div'][1] else: if embed_flag: tmp_file = os.path.join(get_path(tw.activity, 'instance'), 'tmpfile.png') save_picture(self.tw.canvas, tmp_file) imgdata = image_to_base64(tmp_file, get_path(tw.activity, 'instance')) else: imgdata = os.path.join(self.tw.load_save_folder, 'image') self.tw.save_as_image(imgdata) htmlcode += (HTML_GLUE['img'][0] + imgdata + \ HTML_GLUE['img'][1]) htmlcode += HTML_GLUE['div'][0] htmlcode += escape( data_to_string(tw.assemble_data_to_save(False, True))) htmlcode += HTML_GLUE['div'][1] if tw.running_sugar: title = _('Turtle Art') + ' ' + tw.activity.metadata['title'] else: title = _('Turtle Art') header = HTML_GLUE['doctype'] + \ HTML_GLUE['html'][0] style = HTML_GLUE['style'][0] + \ HTML_GLUE['style'][1] if len(tw.saved_pictures) > 0: if tw.saved_pictures[0][1]: header = HTML_GLUE['html_svg'][0] style = COMMENT return header + \ HTML_GLUE['head'][0] + \ HTML_GLUE['meta'] + \ HTML_GLUE['title'][0] + \ title + \ HTML_GLUE['title'][1] + \ style + \ HTML_GLUE['head'][1] + \ HTML_GLUE['body'][0] + \ htmlcode + \ HTML_GLUE['body'][1] + \ HTML_GLUE['html'][1]