diff options
Diffstat (limited to 'TurtleArt')
-rw-r--r-- | TurtleArt/tacanvas.py | 22 | ||||
-rw-r--r-- | TurtleArt/tawindow.py | 86 |
2 files changed, 101 insertions, 7 deletions
diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index f37a3a4..93d2567 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -121,17 +121,23 @@ class TurtleGraphics: 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) + svg_surface = cairo.SVGSurface(self.get_svg_path(), + self.width, self.height) + self.svg_surface = svg_surface self.cr_svg = cairo.Context(svg_surface) self.cr_svg.set_line_cap(1) # Set the line cap to be round + 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 fill_polygon(self, poly_points): + ''' Draw the polygon... ''' def _fill_polygon(cr, poly_points): cr.new_path() @@ -416,6 +422,8 @@ class TurtleGraphics: def svg_close(self): ''' Close current SVG graphic ''' self.cr_svg.show_page() + self.svg_surface.flush() + self.svg_surface.finish() def svg_reset(self): ''' Reset svg flags ''' diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index ae5fb97..7880ce0 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -4649,6 +4649,92 @@ before making changes to your program')) save_picture(self.canvas, image_file) return ta_file, image_file + def save_as_icon(self, name=''): + from util.sugariconify import SugarIconify + + path = self.canvas.get_svg_path() + self._ensure_square_svg(path) # icons are square + + output_dir, basename = os.path.split(path) + + icon = SugarIconify() + icon.set_use_default_colors(True) + icon.set_output_path(output_dir) + icon.set_stroke_color('rgb(0%,0%,0%)') + icon.set_fill_color('rgb(99.215686%,99.215686%,99.215686%)') + icon.iconify(path) + + # replace .svg with .sugar.svg + sugarized_path = os.path.join(output_dir, basename[:-4] + '.sugar.svg') + + if self.running_sugar: + from sugar.datastore import datastore + from sugar import profile + + dsobject = datastore.create() + if len(name) == 0: + dsobject.metadata['title'] = '%s %s' % \ + (self.activity.metadata['title'], _('icon')) + else: + dsobject.metadata['title'] = name + dsobject.metadata['icon-color'] = profile.get_color().to_string() + dsobject.metadata['mime_type'] = 'image/svg+xml' + dsobject.set_file_path(sugarized_path) + datastore.write(dsobject) + dsobject.destroy() + self.saved_pictures.append((dsobject.object_id, True)) + os.remove(sugarized_path) + else: + if svg: + if len(name) == 0: + name = 'turtleblocks-icon.svg' + if self.save_folder is not None: + self.load_save_folder = self.save_folder + name, self.load_save_folder = get_save_name( + '.svg', self.load_save_folder, name) + datapath = self.load_save_folder + else: + datapath = os.getcwd() + if '.svg' not in name: + name = name + '.svg' + subprocess.check_output( + ['cp', TMP_SVG_PATH, os.path.join(datapath, name)]) + + def write_svg_operation(self): + self.canvas.svg_close() + self.canvas.svg_reset() + + def _ensure_square_svg(self, path): + from xml.dom import minidom + + fil = open(path, 'r') + svg_text = fil.read() + fil.close() + + svg_xml = minidom.parseString(svg_text) + svg_element = svg_xml.getElementsByTagName('svg')[0] + + width = int(svg_element.getAttribute('width')[:-2]) + height = int(svg_element.getAttribute('height')[:-2]) + size = min(width, height) + + svg_element.setAttribute('width', str(size) + 'pt') + svg_element.setAttribute('height', str(size) + 'pt') + + if width > height: + dx = int((width - size) / 2) + view_box = '%d 0 %d %d' % (dx, size, size) + else: + dy = int((height - size) / 2) + view_box = '0 %d %d %d' % (dy, size, size) + + svg_element.setAttribute('viewBox', view_box) + svg_text = svg_xml.toxml() + + fil = open(path, 'w+') + fil.write(svg_text) + fil.close() + def save_as_image(self, name='', svg=False): ''' Grab the current canvas and save it. ''' if svg: |