[gnome-characters] search-provider: Show char as icon



commit e5983528d01d1617aef6aa4e5ce2fe2b73b262f9
Author: Maximiliano Sandoval R <msandova gnome org>
Date:   Sat Jan 29 17:29:14 2022 +0100

    search-provider: Show char as icon

 meson.build           |  2 +-
 src/searchProvider.js |  4 +++-
 src/util.js           | 56 +++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/meson.build b/meson.build
index e3329f7..5621010 100644
--- a/meson.build
+++ b/meson.build
@@ -45,7 +45,7 @@ endif
 
 # Just check that gjs-1.0 is present and recent enough
 dependency('gjs-1.0', version: '>= 1.50')
-dependency('gtk4')
+dependency('gtk4', version: '>=4.6')
 libhandy = dependency ('libadwaita-1', version: '>= 1.0')
 
 libgc_deps = [
diff --git a/src/searchProvider.js b/src/searchProvider.js
index 0966064..f39b4e1 100644
--- a/src/searchProvider.js
+++ b/src/searchProvider.js
@@ -88,13 +88,15 @@ var SearchProvider = GObject.registerClass({
                 name = _('Unknown character name');
             else
                 name = Util.capitalize(name);
-            let summary = _('U+%s, %s: %s').format(codePointHex, character, name);
+            let summary = _('U+%s').format(codePointHex);
 
+            let iconData = Util.characterToIconData(character);
             ret.push({
                 name: new GLib.Variant('s', name),
                 id: new GLib.Variant('s', identifiers[i]),
                 description: new GLib.Variant('s', summary),
                 clipboardText: new GLib.Variant('s', character),
+                'icon-data': iconData,
             });
         }
         return ret;
diff --git a/src/util.js b/src/util.js
index 45492f1..919e65f 100644
--- a/src/util.js
+++ b/src/util.js
@@ -1,4 +1,4 @@
-/* exported capitalize getSettings initActions searchResultToArray toCodePoint */
+/* exported capitalize getSettings initActions searchResultToArray toCodePoint characterToIconData */
 // -*- Mode: js; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-
 //
 // Copyright (c) 2013 Giovanni Campagna <scampa giovanni gmail com>
@@ -25,7 +25,7 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-const { Gc, Gio } = imports.gi;
+const { Gc, Gio, GdkPixbuf, Gdk, GLib, Graphene, Gsk, Gtk, PangoCairo, Pango } = imports.gi;
 
 const System = imports.system;
 
@@ -108,3 +108,55 @@ function searchResultToArray(result) {
 
     return characters;
 }
+
+function characterToIconData(character) {
+    let size = 48.0;
+
+    const fontMap = PangoCairo.FontMap.get_default();
+    const context = fontMap.create_context();
+    const layout = Pango.Layout.new(context);
+    layout.set_text(character, -1);
+    let white = new Gdk.RGBA({ red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0 });
+
+    let [textWidth, textHeight] = layout.get_pixel_size();
+    let textSize = Math.max(textWidth, textHeight);
+
+    const snapshot = Gtk.Snapshot.new();
+
+    let originX = (textSize - textWidth) / 2.0;
+    let originY = (textSize - textHeight) / 2.0;
+    let origin = new Graphene.Point({ x: originX, y: originY });
+
+    let ratio = size / textSize;
+    snapshot.scale(ratio, ratio);
+
+    snapshot.save();
+    snapshot.translate(origin);
+    snapshot.append_layout(layout, white);
+    snapshot.restore();
+
+    const node = snapshot.to_node();
+    const renderer = Gsk.GLRenderer.new();
+    renderer.realize(null);
+    let rect = new Graphene.Rect({
+        origin: new Graphene.Point({ x: 0.0, y: 0.0 }),
+        size: new Graphene.Size({ width: size, height: size }),
+    });
+    const texture = renderer.render_texture(node, rect);
+    const bytes = texture.save_to_png_bytes();
+    renderer.unrealize();
+
+    const stream = Gio.MemoryInputStream.new_from_bytes(bytes);
+    const px = GdkPixbuf.Pixbuf.new_from_stream_at_scale(stream, size / 2.0, size / 2.0, true, null);
+
+    let variantBytes = GLib.Variant.new_from_bytes(GLib.VariantType.new('ay'), px.read_pixel_bytes(), true);
+    return GLib.Variant.new_tuple([
+        new GLib.Variant('i', px.get_width()),
+        new GLib.Variant('i', px.get_height()),
+        new GLib.Variant('i', px.get_rowstride()),
+        new GLib.Variant('b', px.get_has_alpha()),
+        new GLib.Variant('i', px.get_bits_per_sample()),
+        new GLib.Variant('i', px.get_n_channels()),
+        variantBytes,
+    ]);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]