[Sugar-devel] [PATCH sugar] Fix screenshot extension, port to cairo - SL #3906

Manuel Quiñones manuq at laptop.org
Tue Sep 18 00:38:57 EDT 2012


1. API fix, Gdk.Window now has get_width() and get_height(), not
   get_size().

2. Use cairo to make the screenshot: paint the window surface in a new
   surface and then save it as png to the path that will be provided
   to the datastore.

3. Use the same cairo code as in the toolkit-gtk3 to make a preview of
   the screenshot surface, store it as an array of bytes.

Signed-off-by: Manuel Quiñones <manuq at laptop.org>
---
 extensions/globalkey/screenshot.py | 53 ++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/extensions/globalkey/screenshot.py b/extensions/globalkey/screenshot.py
index d5b88ea..5abf15b 100644
--- a/extensions/globalkey/screenshot.py
+++ b/extensions/globalkey/screenshot.py
@@ -18,6 +18,8 @@
 import os
 import tempfile
 from gettext import gettext as _
+import StringIO
+import cairo
 
 from gi.repository import Gtk
 from gi.repository import Gdk
@@ -38,15 +40,15 @@ def handle_key_press(key):
     os.close(fd)
 
     window = Gdk.get_default_root_window()
-    width, height = window.get_size()
-    x_orig, y_orig = window.get_origin()
+    width, height = window.get_width(), window.get_height()
 
-    screenshot = GdkPixbuf.Pixbuf(GdkPixbuf.Colorspace.RGB, has_alpha=False,
-                                    bits_per_sample=8, width=width,
-                                    height=height)
-    screenshot.get_from_drawable(window, window.get_colormap(), x_orig,
-                                    y_orig, 0, 0, width, height)
-    screenshot.save(file_path, 'png')
+    window_cr = Gdk.cairo_create(window)
+    window_surface = window_cr.get_target()
+    screenshot_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
+    cr = cairo.Context(screenshot_surface)
+    cr.set_source_surface(window_surface)
+    cr.paint()
+    screenshot_surface.write_to_png(file_path)
 
     client = GConf.Client.get_default()
     color = client.get_string('/desktop/sugar/user/color')
@@ -79,7 +81,7 @@ def handle_key_press(key):
         jobject.metadata['title'] = title
         jobject.metadata['keep'] = '0'
         jobject.metadata['buddies'] = ''
-        jobject.metadata['preview'] = _get_preview_data(screenshot)
+        jobject.metadata['preview'] = _get_preview_data(screenshot_surface)
         jobject.metadata['icon-color'] = color
         jobject.metadata['mime_type'] = 'image/png'
         jobject.file_path = file_path
@@ -89,14 +91,31 @@ def handle_key_press(key):
         del jobject
 
 
-def _get_preview_data(screenshot):
-    preview = screenshot.scale_simple(style.zoom(300), style.zoom(225),
-                                      GdkPixbuf.InterpType.BILINEAR)
-    preview_data = []
+def _get_preview_data(screenshot_surface):
+    screenshot_width = screenshot_surface.get_width()
+    screenshot_height = screenshot_surface.get_height()
 
-    def save_func(buf, data):
-        data.append(buf)
+    preview_width, preview_height = style.zoom(300), style.zoom(225)
+    preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
+                                         preview_width, preview_height)
+    cr = cairo.Context(preview_surface)
 
-    preview.save_to_callback(save_func, 'png', user_data=preview_data)
+    scale_w = preview_width * 1.0 / screenshot_width
+    scale_h = preview_height * 1.0 / screenshot_height
+    scale = min(scale_w, scale_h)
 
-    return dbus.ByteArray(''.join(preview_data))
+    translate_x = int((preview_width - (screenshot_width * scale)) / 2)
+    translate_y = int((preview_height - (screenshot_height * scale)) / 2)
+
+    cr.translate(translate_x, translate_y)
+    cr.scale(scale, scale)
+
+    cr.set_source_rgba(1, 1, 1, 0)
+    cr.set_operator(cairo.OPERATOR_SOURCE)
+    cr.paint()
+    cr.set_source_surface(screenshot_surface)
+    cr.paint()
+
+    preview_str = StringIO.StringIO()
+    preview_surface.write_to_png(preview_str)
+    return dbus.ByteArray(preview_str.getvalue())
-- 
1.7.11.4



More information about the Sugar-devel mailing list