From d4d619a31499df824f0a29b569846af923b37042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= <manuq@laptop.org>
Date: Tue, 18 Sep 2012 01:29:08 -0300
Subject: [PATCH sugar] Fix screenshot extension, port to cairo - SL #3906
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Mail-Followup-To: <sugar-devel@lists.sugarlabs.org>
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@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
|
b
|
|
18 | 18 | import os |
19 | 19 | import tempfile |
20 | 20 | from gettext import gettext as _ |
| 21 | import StringIO |
| 22 | import cairo |
21 | 23 | |
22 | 24 | from gi.repository import Gtk |
23 | 25 | from gi.repository import Gdk |
… |
… |
def handle_key_press(key): |
38 | 40 | os.close(fd) |
39 | 41 | |
40 | 42 | window = Gdk.get_default_root_window() |
41 | | width, height = window.get_size() |
42 | | x_orig, y_orig = window.get_origin() |
| 43 | width, height = window.get_width(), window.get_height() |
43 | 44 | |
44 | | screenshot = GdkPixbuf.Pixbuf(GdkPixbuf.Colorspace.RGB, has_alpha=False, |
45 | | bits_per_sample=8, width=width, |
46 | | height=height) |
47 | | screenshot.get_from_drawable(window, window.get_colormap(), x_orig, |
48 | | y_orig, 0, 0, width, height) |
49 | | screenshot.save(file_path, 'png') |
| 45 | window_cr = Gdk.cairo_create(window) |
| 46 | window_surface = window_cr.get_target() |
| 47 | screenshot_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
| 48 | cr = cairo.Context(screenshot_surface) |
| 49 | cr.set_source_surface(window_surface) |
| 50 | cr.paint() |
| 51 | screenshot_surface.write_to_png(file_path) |
50 | 52 | |
51 | 53 | client = GConf.Client.get_default() |
52 | 54 | color = client.get_string('/desktop/sugar/user/color') |
… |
… |
def handle_key_press(key): |
79 | 81 | jobject.metadata['title'] = title |
80 | 82 | jobject.metadata['keep'] = '0' |
81 | 83 | jobject.metadata['buddies'] = '' |
82 | | jobject.metadata['preview'] = _get_preview_data(screenshot) |
| 84 | jobject.metadata['preview'] = _get_preview_data(screenshot_surface) |
83 | 85 | jobject.metadata['icon-color'] = color |
84 | 86 | jobject.metadata['mime_type'] = 'image/png' |
85 | 87 | jobject.file_path = file_path |
… |
… |
def handle_key_press(key): |
89 | 91 | del jobject |
90 | 92 | |
91 | 93 | |
92 | | def _get_preview_data(screenshot): |
93 | | preview = screenshot.scale_simple(style.zoom(300), style.zoom(225), |
94 | | GdkPixbuf.InterpType.BILINEAR) |
95 | | preview_data = [] |
| 94 | def _get_preview_data(screenshot_surface): |
| 95 | screenshot_width = screenshot_surface.get_width() |
| 96 | screenshot_height = screenshot_surface.get_height() |
96 | 97 | |
97 | | def save_func(buf, data): |
98 | | data.append(buf) |
| 98 | preview_width, preview_height = style.zoom(300), style.zoom(225) |
| 99 | preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, |
| 100 | preview_width, preview_height) |
| 101 | cr = cairo.Context(preview_surface) |
99 | 102 | |
100 | | preview.save_to_callback(save_func, 'png', user_data=preview_data) |
| 103 | scale_w = preview_width * 1.0 / screenshot_width |
| 104 | scale_h = preview_height * 1.0 / screenshot_height |
| 105 | scale = min(scale_w, scale_h) |
101 | 106 | |
102 | | return dbus.ByteArray(''.join(preview_data)) |
| 107 | translate_x = int((preview_width - (screenshot_width * scale)) / 2) |
| 108 | translate_y = int((preview_height - (screenshot_height * scale)) / 2) |
| 109 | |
| 110 | cr.translate(translate_x, translate_y) |
| 111 | cr.scale(scale, scale) |
| 112 | |
| 113 | cr.set_source_rgba(1, 1, 1, 0) |
| 114 | cr.set_operator(cairo.OPERATOR_SOURCE) |
| 115 | cr.paint() |
| 116 | cr.set_source_surface(screenshot_surface) |
| 117 | cr.paint() |
| 118 | |
| 119 | preview_str = StringIO.StringIO() |
| 120 | preview_surface.write_to_png(preview_str) |
| 121 | return dbus.ByteArray(preview_str.getvalue()) |