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 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): """ Draw a pixbuf """ w *= self.tw.coord_scale h *= self.tw.coord_scale self.canvas.images[0].draw_pixbuf(self.gc, pixbuf, a, b, x, y) self.invalt(x, y, w, h) if self.tw.saving_svg: if self.tw.running_sugar: # In Sugar, we need to embed the images inside the SVG self.tw.svg_string += self.svg.image(x - self.width / 2, y, w, h, path, image_to_base64(pixbuf, self.tw.activity)) else: self.tw.svg_string += self.svg.image(x - self.width / 2, y, w, h, 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 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]
def save_html(self, tw, embed_flag=True): """ Either: Save canvas and code or pictures to HTML """ self.embed_images = embed_flag # A dictionary to define the HTML wrappers around template elements self.html_glue = { 'doctype': "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 " + \ "Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n", 'html': ("<html>\n", "</html>\n"), 'html_svg': ("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", "</html>\n"), 'head': ("<head>\n<!-- Created by Turtle Art -->\n", "</head>\n"), 'meta': "<meta http-equiv=\"content-type\" content=\"text/html; " + \ "charset=UTF-8\"/>\n", 'title': ("<title>", "</title>\n"), 'style': ("<style type=\"text/css\">\n<!--\n", "-->\n</style>\n"), 'body': ("<body>\n", "\n</body>\n"), 'div': ("<div>\n", "</div>\n"), 'slide': ("\n<a name=\"slide", "\"></a>\n"), 'h1': ("<h1>", "</h1>\n"), 'table': ("<table cellpadding=\"10\">\n", "</table>\n"), 'tr': ("<tr>\n", "</tr>\n"), 'td': ("<td valign=\"top\" width=\"400\" height=\"300\">\n", "\n</td>\n"), 'img': ("<img width=\"400\" height=\"300\" alt=\"Image\" " + \ "src=\"file://", ".png\" />\n"), 'img2': ("<img alt=\"Image\" src=\"image", ".png\" />\n"), 'img3': ("<img alt=\"Image\" src=\"file://", "\" />\n"), 'ul': ("<table>\n", "</table>\n"), 'li': ("<tr><td>", "</td></tr>\n")} comment = "<!--\n\ <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" [\n\ <!ENTITY ns_svg \"http://www.w3.org/2000/svg\">\n\ <!ENTITY ns_xlink \"http://www.w3.org/1999/xlink\">\n\ ]>\n\ -->\n" if self.embed_images == True: self.html_glue['img'] = ("<img width=\"400\" height=\"300\" alt=" + \ "\"Image\" src=\"data:image/png;base64,\n", " \"/>\n") self.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. """ code = "" if len(tw.saved_pictures) > 0: for i, p in enumerate(tw.saved_pictures): code += self.html_glue['slide'][0] + str(i) code += self.html_glue['slide'][1] + \ self.html_glue['div'][0] + \ self.html_glue['h1'][0] if self.embed_images == True: f = open(p, "r") imgdata = f.read() f.close() if p.endswith(('.svg')): tmp = imgdata else: pixbuf = gtk.gdk.pixbuf_new_from_file(p) imgdata = image_to_base64(pixbuf, tw.activity) tmp = self.html_glue['img2'][0] tmp += imgdata tmp += self.html_glue['img2'][1] else: if p.endswith(('.svg')): f = open(p, "r") imgdata = f.read() f.close() tmp = imgdata else: tmp = self.html_glue['img3'][0] tmp += p tmp += self.html_glue['img3'][1] code += tmp + \ self.html_glue['h1'][1] + \ self.html_glue['div'][1] else: if self.embed_images == True: imgdata = image_to_base64(save_picture(self.tw.canvas), tw.activity) else: imgdata = os.path.join(self.tw.load_save_folder, 'image') self.tw.save_as_image(imgdata) code += (self.html_glue['img'][0] + imgdata + \ self.html_glue['img'][1]) code += self.html_glue['div'][0] code += escape(data_to_string(tw.assemble_data_to_save(False, True))) code += self.html_glue['div'][1] if tw.running_sugar: title = _("Turtle Art") + " " + tw.activity.metadata['title'] else: title = _("Turtle Art") header = self.html_glue['doctype'] + \ self.html_glue['html'][0] style = self.html_glue['style'][0] + \ self.html_glue['style'][1] if len(tw.saved_pictures) > 0: if tw.saved_pictures[0].endswith(('.svg')): header = self.html_glue['html_svg'][0] style = comment code = header + \ self.html_glue['head'][0] + \ self.html_glue['meta'] + \ self.html_glue['title'][0] + \ title + \ self.html_glue['title'][1] + \ style + \ self.html_glue['head'][1] + \ self.html_glue['body'][0] + \ code + \ self.html_glue['body'][1] + \ self.html_glue['html'][1] return code