summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2017-04-21 09:44:08 +0100
committerRichard Hughes <richard@hughsie.com>2017-04-21 11:17:28 +0100
commitf044e6fca2354cf4e21cd1a21060b9c30b48b6a5 (patch)
treec594987617df26d852f98ae2bb45bb931c919475
parent207e1fb357b7ef8b48b61ba65ba7c12caa724f7a (diff)
downloadappstream-glib-f044e6fca2354cf4e21cd1a21060b9c30b48b6a5.tar.gz
Add support for icon scaling
-rw-r--r--libappstream-glib/as-icon.c68
-rw-r--r--libappstream-glib/as-icon.h5
-rw-r--r--libappstream-glib/as-self-test.c52
3 files changed, 119 insertions, 6 deletions
diff --git a/libappstream-glib/as-icon.c b/libappstream-glib/as-icon.c
index fd1fcf3..9fc3ba7 100644
--- a/libappstream-glib/as-icon.c
+++ b/libappstream-glib/as-icon.c
@@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
- * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
+ * Copyright (C) 2014-2017 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
@@ -50,6 +50,7 @@ typedef struct
AsRefString *prefix_private;
guint width;
guint height;
+ guint scale;
GdkPixbuf *pixbuf;
GBytes *data;
} AsIconPrivate;
@@ -263,6 +264,23 @@ as_icon_get_height (AsIcon *icon)
}
/**
+ * as_icon_get_scale:
+ * @icon: a #AsIcon instance.
+ *
+ * Gets the icon scale.
+ *
+ * Returns: scale factor
+ *
+ * Since: 0.6.13
+ **/
+guint
+as_icon_get_scale (AsIcon *icon)
+{
+ AsIconPrivate *priv = GET_PRIVATE (icon);
+ return priv->scale;
+}
+
+/**
* as_icon_get_kind:
* @icon: a #AsIcon instance.
*
@@ -410,6 +428,22 @@ as_icon_set_height (AsIcon *icon, guint height)
}
/**
+ * as_icon_set_scale:
+ * @icon: a #AsIcon instance.
+ * @scale: the scale as a factor.
+ *
+ * Sets the icon scale.
+ *
+ * Since: 0.6.13
+ **/
+void
+as_icon_set_scale (AsIcon *icon, guint scale)
+{
+ AsIconPrivate *priv = GET_PRIVATE (icon);
+ priv->scale = scale;
+}
+
+/**
* as_icon_set_kind:
* @icon: a #AsIcon instance.
* @kind: the #AsIconKind, e.g. %AS_ICON_KIND_STOCK.
@@ -483,6 +517,8 @@ as_icon_node_insert_embedded (AsIcon *icon, GNode *parent, AsNodeContext *ctx)
as_node_add_attribute_as_uint (n, "width", priv->width);
as_node_add_attribute_as_uint (n, "height", priv->height);
}
+ if (priv->scale > 1 && as_node_context_get_version (ctx) > 0.9)
+ as_node_add_attribute_as_uint (n, "scale", priv->scale);
as_node_insert (n, "name", priv->name, 0, NULL);
data = g_base64_encode (g_bytes_get_data (priv->data, NULL),
g_bytes_get_size (priv->data));
@@ -545,6 +581,8 @@ as_icon_node_insert (AsIcon *icon, GNode *parent, AsNodeContext *ctx)
as_node_add_attribute_as_uint (n, "width", priv->width);
if (priv->height > 0)
as_node_add_attribute_as_uint (n, "height", priv->height);
+ if (priv->scale > 1)
+ as_node_add_attribute_as_uint (n, "scale", priv->scale);
}
return n;
}
@@ -674,12 +712,27 @@ as_icon_node_parse (AsIcon *icon, GNode *node,
}
priv->height = size;
+ /* scale is optional, assume 1 if missing */
+ size = as_node_get_attribute_as_uint (node, "scale");
+ if (size == G_MAXUINT)
+ size = 1;
+ priv->scale = size;
+
/* only use the size if the metadata has width and height */
if (prepend_size) {
- g_autofree gchar *sz = g_strdup_printf ("%s/%ux%u",
- priv->prefix,
- priv->width,
- priv->height);
+ g_autofree gchar *sz = NULL;
+ if (priv->scale > 1) {
+ sz = g_strdup_printf ("%s/%ux%u@%u",
+ priv->prefix,
+ priv->width,
+ priv->height,
+ priv->scale);
+ } else {
+ sz = g_strdup_printf ("%s/%ux%u",
+ priv->prefix,
+ priv->width,
+ priv->height);
+ }
as_ref_string_assign_safe (&priv->prefix_private, sz);
}
break;
@@ -723,6 +776,11 @@ as_icon_node_parse_dep11 (AsIcon *icon, GNode *node,
if (size == G_MAXUINT)
size = 64;
priv->height = size;
+ } else if (g_strcmp0 (key, "scale") == 0) {
+ size = as_yaml_node_get_value_as_uint (n);
+ if (size == G_MAXUINT)
+ size = 1;
+ priv->scale = size;
} else {
if (priv->kind == AS_ICON_KIND_REMOTE) {
if (g_strcmp0 (key, "url") == 0) {
diff --git a/libappstream-glib/as-icon.h b/libappstream-glib/as-icon.h
index 7a1c9f5..8b0f5f7 100644
--- a/libappstream-glib/as-icon.h
+++ b/libappstream-glib/as-icon.h
@@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
- * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
+ * Copyright (C) 2014-2017 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
@@ -112,6 +112,7 @@ const gchar *as_icon_get_filename (AsIcon *icon);
const gchar *as_icon_get_prefix (AsIcon *icon);
guint as_icon_get_width (AsIcon *icon);
guint as_icon_get_height (AsIcon *icon);
+guint as_icon_get_scale (AsIcon *icon);
AsIconKind as_icon_get_kind (AsIcon *icon);
GdkPixbuf *as_icon_get_pixbuf (AsIcon *icon);
@@ -128,6 +129,8 @@ void as_icon_set_width (AsIcon *icon,
guint width);
void as_icon_set_height (AsIcon *icon,
guint height);
+void as_icon_set_scale (AsIcon *icon,
+ guint scale);
void as_icon_set_kind (AsIcon *icon,
AsIconKind kind);
void as_icon_set_pixbuf (AsIcon *icon,
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index 3e9a872..48a4ba2 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -959,6 +959,7 @@ as_test_icon_func (void)
g_assert_cmpstr (as_icon_get_url (icon), ==, NULL);
g_assert_cmpint (as_icon_get_height (icon), ==, 64);
g_assert_cmpint (as_icon_get_width (icon), ==, 64);
+ g_assert_cmpint (as_icon_get_scale (icon), ==, 1);
g_assert (as_icon_get_pixbuf (icon) == NULL);
g_assert (as_icon_get_data (icon) == NULL);
@@ -988,6 +989,56 @@ as_test_icon_func (void)
}
static void
+as_test_icon_scale_func (void)
+{
+ GError *error = NULL;
+ AsNode *n;
+ AsNode *root;
+ GString *xml;
+ const gchar *src = "<icon type=\"cached\" height=\"128\" scale=\"2\" width=\"128\">app.png</icon>";
+ gboolean ret;
+ g_autoptr(AsNodeContext) ctx = NULL;
+ g_autoptr(AsIcon) icon = NULL;
+ g_autoptr(GdkPixbuf) pixbuf = NULL;
+
+ icon = as_icon_new ();
+
+ /* to object */
+ root = as_node_from_xml (src, 0, &error);
+ g_assert_no_error (error);
+ g_assert (root != NULL);
+ n = as_node_find (root, "icon");
+ g_assert (n != NULL);
+ ctx = as_node_context_new ();
+ ret = as_icon_node_parse (icon, n, ctx, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+ as_node_unref (root);
+
+ /* verify */
+ g_assert_cmpint (as_icon_get_kind (icon), ==, AS_ICON_KIND_CACHED);
+ g_assert_cmpstr (as_icon_get_name (icon), ==, "app.png");
+ g_assert_cmpstr (as_icon_get_filename (icon), ==, NULL);
+ g_assert_cmpstr (as_icon_get_url (icon), ==, NULL);
+ g_assert_cmpint (as_icon_get_height (icon), ==, 128);
+ g_assert_cmpint (as_icon_get_width (icon), ==, 128);
+ g_assert_cmpint (as_icon_get_scale (icon), ==, 2);
+ g_assert (as_icon_get_pixbuf (icon) == NULL);
+ g_assert (as_icon_get_data (icon) == NULL);
+
+ /* back to node */
+ root = as_node_new ();
+ as_node_context_set_version (ctx, 0.9);
+ n = as_icon_node_insert (icon, root, ctx);
+ xml = as_node_to_xml (n, AS_NODE_TO_XML_FLAG_NONE);
+ ret = as_test_compare_lines (xml->str, src, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+ g_string_free (xml, TRUE);
+ as_node_unref (root);
+}
+
+static void
as_test_checksum_func (void)
{
GError *error = NULL;
@@ -5403,6 +5454,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/release{appdata}", as_test_release_appdata_func);
g_test_add_func ("/AppStream/release{appstream}", as_test_release_appstream_func);
g_test_add_func ("/AppStream/icon", as_test_icon_func);
+ g_test_add_func ("/AppStream/icon{scale}", as_test_icon_scale_func);
g_test_add_func ("/AppStream/icon{embedded}", as_test_icon_embedded_func);
g_test_add_func ("/AppStream/bundle", as_test_bundle_func);
g_test_add_func ("/AppStream/review", as_test_review_func);