cssimage: Add gtk_css_image_is_invalid()

Use that instead of hacks to guess when an image is considered invalid
according to https://drafts.csswg.org/css-images-4/#invalid-image

Also add a GtkCssImageInvalid that implements the behavior of invalid
images according to the CSS spec so thjat image implementations can
refer to that image.
This commit is contained in:
Benjamin Otte 2018-02-17 20:31:14 +01:00
parent 5970dac167
commit 4beeb6173b
7 changed files with 173 additions and 7 deletions

View File

@ -96,6 +96,12 @@ gtk_css_image_real_transition (GtkCssImage *start,
return _gtk_css_image_cross_fade_new (start, end, progress); return _gtk_css_image_cross_fade_new (start, end, progress);
} }
static gboolean
gtk_css_image_real_is_invalid (GtkCssImage *image)
{
return FALSE;
}
static gboolean static gboolean
gtk_css_image_real_is_dynamic (GtkCssImage *image) gtk_css_image_real_is_dynamic (GtkCssImage *image)
{ {
@ -118,6 +124,7 @@ _gtk_css_image_class_init (GtkCssImageClass *klass)
klass->compute = gtk_css_image_real_compute; klass->compute = gtk_css_image_real_compute;
klass->equal = gtk_css_image_real_equal; klass->equal = gtk_css_image_real_equal;
klass->transition = gtk_css_image_real_transition; klass->transition = gtk_css_image_real_transition;
klass->is_invalid = gtk_css_image_real_is_invalid;
klass->is_dynamic = gtk_css_image_real_is_dynamic; klass->is_dynamic = gtk_css_image_real_is_dynamic;
klass->get_dynamic_image = gtk_css_image_real_get_dynamic_image; klass->get_dynamic_image = gtk_css_image_real_get_dynamic_image;
} }
@ -279,6 +286,18 @@ gtk_css_image_snapshot (GtkCssImage *image,
klass->snapshot (image, snapshot, width, height); klass->snapshot (image, snapshot, width, height);
} }
gboolean
gtk_css_image_is_invalid (GtkCssImage *image)
{
GtkCssImageClass *klass;
g_return_val_if_fail (GTK_IS_CSS_IMAGE (image), FALSE);
klass = GTK_CSS_IMAGE_GET_CLASS (image);
return klass->is_invalid (image);
}
gboolean gboolean
gtk_css_image_is_dynamic (GtkCssImage *image) gtk_css_image_is_dynamic (GtkCssImage *image)
{ {

View File

@ -154,10 +154,7 @@ gtk_css_image_fallback_compute (GtkCssImage *image,
style, style,
parent_style); parent_style);
/* Assume that failing to load an image leaves a 0x0 surface image */ if (gtk_css_image_is_invalid (copy->images[i]))
if (GTK_IS_CSS_IMAGE_SURFACE (copy->images[i]) &&
_gtk_css_image_get_width (copy->images[i]) == 0 &&
_gtk_css_image_get_height (copy->images[i]) == 0)
continue; continue;
if (copy->used < 0) if (copy->used < 0)

74
gtk/gtkcssimageinvalid.c Normal file
View File

@ -0,0 +1,74 @@
/*
* Copyright © 2011 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtkcssimageinvalidprivate.h"
G_DEFINE_TYPE (GtkCssImageInvalid, gtk_css_image_invalid, GTK_TYPE_CSS_IMAGE)
static void
gtk_css_image_invalid_snapshot (GtkCssImage *image,
GtkSnapshot *snapshot,
double width,
double height)
{
}
static gboolean
gtk_css_image_invalid_equal (GtkCssImage *image1,
GtkCssImage *image2)
{
return TRUE;
}
static void
gtk_css_image_invalid_print (GtkCssImage *image,
GString *string)
{
g_string_append (string, "none /* invalid image */");
}
static gboolean
gtk_css_image_invalid_is_invalid (GtkCssImage *image)
{
return TRUE;
}
static void
gtk_css_image_invalid_class_init (GtkCssImageInvalidClass *klass)
{
GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
image_class->snapshot = gtk_css_image_invalid_snapshot;
image_class->print = gtk_css_image_invalid_print;
image_class->equal = gtk_css_image_invalid_equal;
image_class->is_invalid = gtk_css_image_invalid_is_invalid;
}
static void
gtk_css_image_invalid_init (GtkCssImageInvalid *image_invalid)
{
}
GtkCssImage *
gtk_css_image_invalid_new (void)
{
return g_object_new (GTK_TYPE_CSS_IMAGE_INVALID, NULL);
}

View File

@ -0,0 +1,57 @@
/*
* Copyright © 2011 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#ifndef __GTK_CSS_IMAGE_INVALID_PRIVATE_H__
#define __GTK_CSS_IMAGE_INVALID_PRIVATE_H__
#include "gtk/gtkcssimageprivate.h"
G_BEGIN_DECLS
#define GTK_TYPE_CSS_IMAGE_INVALID (gtk_css_image_invalid_get_type ())
#define GTK_CSS_IMAGE_INVALID(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_CSS_IMAGE_INVALID, GtkCssImageInvalid))
#define GTK_CSS_IMAGE_INVALID_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_CSS_IMAGE_INVALID, GtkCssImageInvalidClass))
#define GTK_IS_CSS_IMAGE_INVALID(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_CSS_IMAGE_INVALID))
#define GTK_IS_CSS_IMAGE_INVALID_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_CSS_IMAGE_INVALID))
#define GTK_CSS_IMAGE_INVALID_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CSS_IMAGE_INVALID, GtkCssImageInvalidClass))
typedef struct _GtkCssImageInvalid GtkCssImageInvalid;
typedef struct _GtkCssImageInvalidClass GtkCssImageInvalidClass;
struct _GtkCssImageInvalid
{
GtkCssImage parent;
GFile *file; /* the file we're loading from */
GtkCssImage *loaded_image; /* the actual image we render */
};
struct _GtkCssImageInvalidClass
{
GtkCssImageClass parent_class;
};
GType gtk_css_image_invalid_get_type (void) G_GNUC_CONST;
GtkCssImage * gtk_css_image_invalid_new (void);
G_END_DECLS
#endif /* __GTK_CSS_IMAGE_INVALID_PRIVATE_H__ */

View File

@ -75,6 +75,8 @@ struct _GtkCssImageClass
GtkSnapshot *snapshot, GtkSnapshot *snapshot,
double width, double width,
double height); double height);
/* is this image to be considered invalid (see https://drafts.csswg.org/css-images-4/#invalid-image for details) */
gboolean (* is_invalid) (GtkCssImage *image);
/* does this image change based on timestamp? (optional) */ /* does this image change based on timestamp? (optional) */
gboolean (* is_dynamic) (GtkCssImage *image); gboolean (* is_dynamic) (GtkCssImage *image);
/* get image for given timestamp or @image when not dynamic (optional) */ /* get image for given timestamp or @image when not dynamic (optional) */
@ -117,6 +119,7 @@ void gtk_css_image_snapshot (GtkCssImage *
GtkSnapshot *snapshot, GtkSnapshot *snapshot,
double width, double width,
double height); double height);
gboolean gtk_css_image_is_invalid (GtkCssImage *image);
gboolean gtk_css_image_is_dynamic (GtkCssImage *image); gboolean gtk_css_image_is_dynamic (GtkCssImage *image);
GtkCssImage * gtk_css_image_get_dynamic_image (GtkCssImage *image, GtkCssImage * gtk_css_image_get_dynamic_image (GtkCssImage *image,
gint64 monotonic_time); gint64 monotonic_time);

View File

@ -22,6 +22,8 @@
#include <string.h> #include <string.h>
#include "gtkcssimageurlprivate.h" #include "gtkcssimageurlprivate.h"
#include "gtkcssimageinvalidprivate.h"
#include "gtkcssimagesurfaceprivate.h" #include "gtkcssimagesurfaceprivate.h"
#include "gtkstyleproviderprivate.h" #include "gtkstyleproviderprivate.h"
@ -68,11 +70,15 @@ gtk_css_image_url_load_image (GtkCssImageUrl *url,
"Error loading image '%s': %s", uri, local_error->message); "Error loading image '%s': %s", uri, local_error->message);
g_free (uri); g_free (uri);
} }
url->loaded_image = gtk_css_image_invalid_new ();
}
else
{
url->loaded_image = gtk_css_image_surface_new (texture);
g_object_unref (texture);
} }
url->loaded_image = gtk_css_image_surface_new (texture);
g_clear_object (&texture);
g_clear_error (&local_error); g_clear_error (&local_error);
return url->loaded_image; return url->loaded_image;
@ -145,6 +151,14 @@ gtk_css_image_url_equal (GtkCssImage *image1,
return g_file_equal (url1->file, url2->file); return g_file_equal (url1->file, url2->file);
} }
static gboolean
gtk_css_image_url_is_invalid (GtkCssImage *image)
{
GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
return gtk_css_image_is_invalid (gtk_css_image_url_load_image (url, NULL));
}
static gboolean static gboolean
gtk_css_image_url_parse (GtkCssImage *image, gtk_css_image_url_parse (GtkCssImage *image,
GtkCssParser *parser) GtkCssParser *parser)
@ -192,6 +206,7 @@ _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
image_class->parse = gtk_css_image_url_parse; image_class->parse = gtk_css_image_url_parse;
image_class->print = gtk_css_image_url_print; image_class->print = gtk_css_image_url_print;
image_class->equal = gtk_css_image_url_equal; image_class->equal = gtk_css_image_url_equal;
image_class->is_invalid = gtk_css_image_url_is_invalid;
object_class->dispose = gtk_css_image_url_dispose; object_class->dispose = gtk_css_image_url_dispose;
} }

View File

@ -46,6 +46,7 @@ gtk_private_sources = files([
'gtkcssimagecrossfade.c', 'gtkcssimagecrossfade.c',
'gtkcssimagefallback.c', 'gtkcssimagefallback.c',
'gtkcssimageicontheme.c', 'gtkcssimageicontheme.c',
'gtkcssimageinvalid.c',
'gtkcssimagelinear.c', 'gtkcssimagelinear.c',
'gtkcssimageradial.c', 'gtkcssimageradial.c',
'gtkcssimagerecolor.c', 'gtkcssimagerecolor.c',