[cairo-commit] 2 commits - src/cairo.h src/cairoint.h src/cairo-scaled-font-private.h src/cairo-surface.c src/cairo-user-font.c test/Makefile.sources test/meson.build test/reference test/user-font-color.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sat Aug 14 06:06:22 UTC 2021


 src/cairo-scaled-font-private.h                    |    2 
 src/cairo-surface.c                                |   10 
 src/cairo-user-font.c                              |  214 ++++++++++++++---
 src/cairo.h                                        |   31 ++
 src/cairoint.h                                     |    1 
 test/Makefile.sources                              |    1 
 test/meson.build                                   |    1 
 test/reference/user-font-color.image16.ref.png     |binary
 test/reference/user-font-color.pdf.ref.png         |binary
 test/reference/user-font-color.ps.ref.png          |binary
 test/reference/user-font-color.quartz.ref.png      |binary
 test/reference/user-font-color.recording.xfail.png |binary
 test/reference/user-font-color.ref.png             |binary
 test/reference/user-font-color.script.xfail.png    |binary
 test/reference/user-font-color.svg.ref.png         |binary
 test/reference/user-font-color.svg.rgb24.xfail.png |binary
 test/user-font-color.c                             |  251 +++++++++++++++++++++
 17 files changed, 459 insertions(+), 52 deletions(-)

New commits:
commit 6ea9ec75eda7461a51429ae8e40e365bdd2dbd6f
Merge: bb10f0698 62cc53f9b
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sat Aug 14 06:06:20 2021 +0000

    Merge branch 'issue-389' into 'master'
    
    Add color user-font support
    
    See merge request cairo/cairo!223

commit 62cc53f9b806eedbc361e807fb5f523bf953c52a
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sat Aug 14 06:06:19 2021 +0000

    Add color user-font support
    
    Adds new API cairo_user_font_face_set_render_color_glyph_func()
    to set a color glyph renderer.

diff --git a/src/cairo-scaled-font-private.h b/src/cairo-scaled-font-private.h
index 317d21197..4bacb1a01 100644
--- a/src/cairo-scaled-font-private.h
+++ b/src/cairo-scaled-font-private.h
@@ -146,6 +146,8 @@ struct _cairo_scaled_glyph {
     const void		   *dev_private_key;
     void		   *dev_private;
     cairo_list_t            dev_privates;
+
+    cairo_bool_t            has_color;
 };
 
 struct _cairo_scaled_glyph_private {
diff --git a/src/cairo-surface.c b/src/cairo-surface.c
index 45296fccd..3029e2cf0 100644
--- a/src/cairo-surface.c
+++ b/src/cairo-surface.c
@@ -2614,8 +2614,16 @@ ensure_scaled_glyph (cairo_scaled_font_t   *scaled_font,
     if (*scaled_glyph == NULL || _cairo_scaled_glyph_index (*scaled_glyph) != glyph->index) {
         status = _cairo_scaled_glyph_lookup (scaled_font,
                                              glyph->index,
-                                             CAIRO_SCALED_GLYPH_INFO_SURFACE,
+                                             CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE,
                                              scaled_glyph);
+	if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
+	    /* If the color surface not available, ensure scaled_glyph is not NULL. */
+	    status = _cairo_scaled_glyph_lookup (scaled_font,
+						 glyph->index,
+						 CAIRO_SCALED_GLYPH_INFO_METRICS,
+						 scaled_glyph);
+	}
+
         if (unlikely (status))
             status = _cairo_scaled_font_set_error (scaled_font, status);
 
diff --git a/src/cairo-user-font.c b/src/cairo-user-font.c
index a26fb57ee..9b8872db5 100644
--- a/src/cairo-user-font.c
+++ b/src/cairo-user-font.c
@@ -1,3 +1,4 @@
+/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
 /* cairo - a vector graphics library with display and print output
  *
  * Copyright © 2006, 2008 Red Hat, Inc
@@ -44,7 +45,7 @@
  * SECTION:cairo-user-fonts
  * @Title:User Fonts
  * @Short_Description: Font support with font data provided by the user
- * 
+ *
  * The user-font feature allows the cairo user to provide drawings for glyphs
  * in a font.  This is most useful in implementing fonts in non-standard
  * formats, like SVG fonts and Flash fonts, but can also be used by games and
@@ -64,6 +65,7 @@
 
 typedef struct _cairo_user_scaled_font_methods {
     cairo_user_scaled_font_init_func_t			init;
+    cairo_user_scaled_font_render_glyph_func_t		render_color_glyph;
     cairo_user_scaled_font_render_glyph_func_t		render_glyph;
     cairo_user_scaled_font_unicode_to_glyph_func_t	unicode_to_glyph;
     cairo_user_scaled_font_text_to_glyphs_func_t	text_to_glyphs;
@@ -75,7 +77,7 @@ typedef struct _cairo_user_font_face {
     /* Set to true after first scaled font is created.  At that point,
      * the scaled_font_methods cannot change anymore. */
     cairo_bool_t		     immutable;
-
+    cairo_bool_t                     has_color;
     cairo_user_scaled_font_methods_t scaled_font_methods;
 } cairo_user_font_face_t;
 
@@ -98,13 +100,18 @@ typedef struct _cairo_user_scaled_font {
 /* #cairo_user_scaled_font_t */
 
 static cairo_surface_t *
-_cairo_user_scaled_font_create_recording_surface (const cairo_user_scaled_font_t *scaled_font)
+_cairo_user_scaled_font_create_recording_surface (const cairo_user_scaled_font_t *scaled_font,
+						  cairo_bool_t                    color)
 {
     cairo_content_t content;
 
-    content = scaled_font->base.options.antialias == CAIRO_ANTIALIAS_SUBPIXEL ?
-						     CAIRO_CONTENT_COLOR_ALPHA :
-						     CAIRO_CONTENT_ALPHA;
+    if (color) {
+	content = CAIRO_CONTENT_COLOR_ALPHA;
+    } else {
+	content = scaled_font->base.options.antialias == CAIRO_ANTIALIAS_SUBPIXEL ?
+						         CAIRO_CONTENT_COLOR_ALPHA :
+						         CAIRO_CONTENT_ALPHA;
+    }
 
     return cairo_recording_surface_create (content, NULL);
 }
@@ -112,7 +119,8 @@ _cairo_user_scaled_font_create_recording_surface (const cairo_user_scaled_font_t
 
 static cairo_t *
 _cairo_user_scaled_font_create_recording_context (const cairo_user_scaled_font_t *scaled_font,
-						  cairo_surface_t                *recording_surface)
+						  cairo_surface_t                *recording_surface,
+						  cairo_bool_t                    color)
 {
     cairo_t *cr;
 
@@ -127,7 +135,8 @@ _cairo_user_scaled_font_create_recording_context (const cairo_user_scaled_font_t
 
     cairo_set_font_size (cr, 1.0);
     cairo_set_font_options (cr, &scaled_font->base.options);
-    cairo_set_source_rgb (cr, 1., 1., 1.);
+    if (!color)
+	cairo_set_source_rgb (cr, 1., 1., 1.);
 
     return cr;
 }
@@ -147,32 +156,56 @@ _cairo_user_scaled_glyph_init (void			 *abstract_font,
 	cairo_text_extents_t extents = scaled_font->default_glyph_extents;
 	cairo_t *cr;
 
-	if (!face->scaled_font_methods.render_glyph)
+	recording_surface = NULL;
+	if (!face->scaled_font_methods.render_color_glyph && !face->scaled_font_methods.render_glyph)
 	    return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
 
-	recording_surface = _cairo_user_scaled_font_create_recording_surface (scaled_font);
-
 	/* special case for 0 rank matrix (as in _cairo_scaled_font_init): empty surface */
-        if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
-	    cr = _cairo_user_scaled_font_create_recording_context (scaled_font, recording_surface);
-	    status = face->scaled_font_methods.render_glyph ((cairo_scaled_font_t *)scaled_font,
-							     _cairo_scaled_glyph_index(scaled_glyph),
-							     cr, &extents);
-	    if (status == CAIRO_INT_STATUS_SUCCESS)
-	        status = cairo_status (cr);
+        if (_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
+	    recording_surface = _cairo_user_scaled_font_create_recording_surface (scaled_font, FALSE);
+	    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
+						       &scaled_font->base,
+						       recording_surface);
+	} else {
+	    status = CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
+
+	    if (face->scaled_font_methods.render_color_glyph) {
+		recording_surface = _cairo_user_scaled_font_create_recording_surface (scaled_font, TRUE);
+
+		cr = _cairo_user_scaled_font_create_recording_context (scaled_font, recording_surface, TRUE);
+		status = face->scaled_font_methods.render_color_glyph ((cairo_scaled_font_t *)scaled_font,
+								       _cairo_scaled_glyph_index(scaled_glyph),
+								       cr, &extents);
+		if (status == CAIRO_STATUS_SUCCESS) {
+		    status = cairo_status (cr);
+		    scaled_glyph->has_color = TRUE;
+		}
+	    }
 
-	    cairo_destroy (cr);
+	    if (status == CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED &&
+		face->scaled_font_methods.render_glyph) {
+		recording_surface = _cairo_user_scaled_font_create_recording_surface (scaled_font, FALSE);
+
+		cr = _cairo_user_scaled_font_create_recording_context (scaled_font, recording_surface, FALSE);
+		status = face->scaled_font_methods.render_glyph ((cairo_scaled_font_t *)scaled_font,
+								 _cairo_scaled_glyph_index(scaled_glyph),
+								 cr, &extents);
+		if (status == CAIRO_INT_STATUS_SUCCESS)
+		    status = cairo_status (cr);
 
-	    if (unlikely (status)) {
-	        cairo_surface_destroy (recording_surface);
-	        return status;
+		cairo_destroy (cr);
 	    }
-	}
 
-	_cairo_scaled_glyph_set_recording_surface (scaled_glyph,
-						   &scaled_font->base,
-						   recording_surface);
+	    if (status != CAIRO_STATUS_SUCCESS) {
+		if (recording_surface)
+		    cairo_surface_destroy (recording_surface);
+		return status;
+	    }
 
+	    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
+						       &scaled_font->base,
+						       recording_surface);
+	}
 
 	/* set metrics */
 
@@ -210,7 +243,7 @@ _cairo_user_scaled_glyph_init (void			 *abstract_font,
 					 &extents);
     }
 
-    if (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) {
+    if (info & (CAIRO_SCALED_GLYPH_INFO_SURFACE | CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) {
 	cairo_surface_t	*surface;
 	cairo_format_t format;
 	int width, height;
@@ -226,16 +259,26 @@ _cairo_user_scaled_glyph_init (void			 *abstract_font,
 	height = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.y) -
 	  _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y);
 
-	switch (scaled_font->base.options.antialias) {
-	default:
-	case CAIRO_ANTIALIAS_DEFAULT:
-	case CAIRO_ANTIALIAS_FAST:
-	case CAIRO_ANTIALIAS_GOOD:
-	case CAIRO_ANTIALIAS_GRAY:	format = CAIRO_FORMAT_A8;	break;
-	case CAIRO_ANTIALIAS_NONE:	format = CAIRO_FORMAT_A1;	break;
-	case CAIRO_ANTIALIAS_BEST:
-	case CAIRO_ANTIALIAS_SUBPIXEL:	format = CAIRO_FORMAT_ARGB32;	break;
-	}
+	if (scaled_glyph->has_color) {
+            format = CAIRO_FORMAT_ARGB32;
+        } else {
+            switch (scaled_font->base.options.antialias) {
+                default:
+                case CAIRO_ANTIALIAS_DEFAULT:
+                case CAIRO_ANTIALIAS_FAST:
+                case CAIRO_ANTIALIAS_GOOD:
+                case CAIRO_ANTIALIAS_GRAY:
+                    format = CAIRO_FORMAT_A8;
+                    break;
+                case CAIRO_ANTIALIAS_NONE:
+                    format = CAIRO_FORMAT_A1;
+                    break;
+                case CAIRO_ANTIALIAS_BEST:
+                case CAIRO_ANTIALIAS_SUBPIXEL:
+                    format = CAIRO_FORMAT_ARGB32;
+                    break;
+            }
+        }
 	surface = cairo_image_surface_create (format, width, height);
 
 	cairo_surface_set_device_offset (surface,
@@ -248,9 +291,17 @@ _cairo_user_scaled_glyph_init (void			 *abstract_font,
 	    return status;
 	}
 
-	_cairo_scaled_glyph_set_surface (scaled_glyph,
-					 &scaled_font->base,
-					 (cairo_image_surface_t *) surface);
+	if (!scaled_glyph->has_color && (info & CAIRO_SCALED_GLYPH_INFO_SURFACE)) {
+            _cairo_scaled_glyph_set_surface (scaled_glyph,
+                                             &scaled_font->base,
+                                             (cairo_image_surface_t *) surface);
+        }
+
+        if (scaled_glyph->has_color && (info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) {
+            _cairo_scaled_glyph_set_color_surface (scaled_glyph,
+                                                   &scaled_font->base,
+                                                   (cairo_image_surface_t *)surface);
+        }
     }
 
     if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
@@ -303,6 +354,16 @@ not_implemented:
     return glyph;
 }
 
+static cairo_bool_t
+_cairo_user_has_color_glyphs (void         *abstract_font)
+{
+    cairo_user_scaled_font_t *scaled_font = abstract_font;
+    cairo_user_font_face_t *face =
+	(cairo_user_font_face_t *) scaled_font->base.font_face;
+
+    return face->has_color;
+}
+
 static cairo_int_status_t
 _cairo_user_text_to_glyphs (void		      *abstract_font,
 			    double		       x,
@@ -381,9 +442,12 @@ static const cairo_scaled_font_backend_t _cairo_user_scaled_font_backend = {
     _cairo_user_scaled_glyph_init,
     _cairo_user_text_to_glyphs,
     _cairo_user_ucs4_to_index,
-    NULL,	/* show_glyphs */
     NULL,	/* load_truetype_table */
-    NULL	/* index_to_ucs4 */
+    NULL,	/* index_to_ucs4 */
+    NULL,       /* is_synthetic */
+    NULL,       /* index_to_glyph_name */
+    NULL,       /* load_type1_data */
+    _cairo_user_has_color_glyphs,
 };
 
 /* #cairo_user_font_face_t */
@@ -462,8 +526,8 @@ _cairo_user_font_face_scaled_font_create (void                        *abstract_
 	    cairo_surface_t *recording_surface;
 	    cairo_t *cr;
 
-	    recording_surface = _cairo_user_scaled_font_create_recording_surface (user_scaled_font);
-	    cr = _cairo_user_scaled_font_create_recording_context (user_scaled_font, recording_surface);
+	    recording_surface = _cairo_user_scaled_font_create_recording_surface (user_scaled_font, FALSE);
+	    cr = _cairo_user_scaled_font_create_recording_context (user_scaled_font, recording_surface, FALSE);
 	    cairo_surface_destroy (recording_surface);
 
 	    status = font_face->scaled_font_methods.init (&user_scaled_font->base,
@@ -553,6 +617,7 @@ cairo_user_font_face_create (void)
     _cairo_font_face_init (&font_face->base, &_cairo_user_font_face_backend);
 
     font_face->immutable = FALSE;
+    font_face->has_color = FALSE;
     memset (&font_face->scaled_font_methods, 0, sizeof (font_face->scaled_font_methods));
 
     return &font_face->base;
@@ -600,6 +665,57 @@ cairo_user_font_face_set_init_func (cairo_font_face_t                  *font_fac
 }
 slim_hidden_def(cairo_user_font_face_set_init_func);
 
+/**
+ * cairo_user_font_face_set_render_color_glyph_func:
+ * @font_face: A user font face
+ * @render_glyph_func: The render_glyph callback, or %NULL
+ *
+ * Sets the color glyph rendering function of a user-font.
+ * See #cairo_user_scaled_font_render_glyph_func_t for details of how the callback
+ * works.
+ *
+ * The font-face should not be immutable or a %CAIRO_STATUS_USER_FONT_IMMUTABLE
+ * error will occur.  A user font-face is immutable as soon as a scaled-font
+ * is created from it.
+ *
+ * The render_glyph callback is the only mandatory callback of a
+ * user-font. At least one of
+ * cairo_user_font_face_set_render_color_glyph_func() or
+ * cairo_user_font_face_set_render_glyph_func() must be called to set
+ * a render callback. If both callbacks are set, the color glyph
+ * render callback is invoked first. If the color glyph render
+ * callback returns %CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED, the
+ * non-color version of the callback is invoked.
+ *
+ * If the callback is %NULL and a glyph is tried to be rendered using
+ * @font_face, a %CAIRO_STATUS_USER_FONT_ERROR will occur.
+ *
+ * Since: 1.18
+ **/
+void
+cairo_user_font_face_set_render_color_glyph_func (cairo_font_face_t                          *font_face,
+                                                  cairo_user_scaled_font_render_glyph_func_t  render_glyph_func)
+{
+    cairo_user_font_face_t *user_font_face;
+
+    if (font_face->status)
+	return;
+
+    if (! _cairo_font_face_is_user (font_face)) {
+	if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
+	    return;
+    }
+
+    user_font_face = (cairo_user_font_face_t *) font_face;
+    if (user_font_face->immutable) {
+	if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_USER_FONT_IMMUTABLE))
+	    return;
+    }
+    user_font_face->scaled_font_methods.render_color_glyph = render_glyph_func;
+    user_font_face->has_color = render_glyph_func ? TRUE : FALSE;
+}
+slim_hidden_def(cairo_user_font_face_set_render_color_glyph_func);
+
 /**
  * cairo_user_font_face_set_render_glyph_func:
  * @font_face: A user font face
@@ -613,7 +729,15 @@ slim_hidden_def(cairo_user_font_face_set_init_func);
  * error will occur.  A user font-face is immutable as soon as a scaled-font
  * is created from it.
  *
- * The render_glyph callback is the only mandatory callback of a user-font.
+ * The render_glyph callback is the only mandatory callback of a
+ * user-font. At least one of
+ * cairo_user_font_face_set_render_color_glyph_func() or
+ * cairo_user_font_face_set_render_glyph_func() must be called to set
+ * a render callback. If both callbacks are set, the color glyph
+ * render callback is invoked first. If the color glyph render
+ * callback returns %CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED, the
+ * non-color version of the callback is invoked.
+ *
  * If the callback is %NULL and a glyph is tried to be rendered using
  * @font_face, a %CAIRO_STATUS_USER_FONT_ERROR will occur.
  *
diff --git a/src/cairo.h b/src/cairo.h
index 96427b425..8533d44ba 100644
--- a/src/cairo.h
+++ b/src/cairo.h
@@ -1749,10 +1749,16 @@ typedef cairo_status_t (*cairo_user_scaled_font_init_func_t) (cairo_scaled_font_
  * font space.  That is, the matrix set on @cr is the scale matrix of @scaled_font,
  * The @extents argument is where the user font sets the font extents for
  * @scaled_font.  However, if user prefers to draw in user space, they can
- * achieve that by changing the matrix on @cr.  All cairo rendering operations
- * to @cr are permitted, however, the result is undefined if any source other
- * than the default source on @cr is used.  That means, glyph bitmaps should
- * be rendered using cairo_mask() instead of cairo_paint().
+ * achieve that by changing the matrix on @cr.
+ *
+ * All cairo rendering operations to @cr are permitted. However, when
+ * this callback set with
+ * cairo_user_font_face_set_render_glyph_func(), the result is
+ * undefined if any source other than the default source on @cr is
+ * used.  That means, glyph bitmaps should be rendered using
+ * cairo_mask() instead of cairo_paint(). When this callback set with
+ * cairo_user_font_face_set_render_color_glyph_func(), setting the
+ * source is a valid operation.
  *
  * Other non-default settings on @cr include a font size of 1.0 (given that
  * it is set up to be in font space), and font options corresponding to
@@ -1772,8 +1778,17 @@ typedef cairo_status_t (*cairo_user_scaled_font_init_func_t) (cairo_scaled_font_
  * extents, it must be ink extents, and include the extents of all drawing
  * done to @cr in the callback.
  *
- * Returns: %CAIRO_STATUS_SUCCESS upon success, or
- * %CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.
+ * Where both color and non-color callbacks has been set using
+ * cairo_user_font_face_set_render_color_glyph_func(), and
+ * cairo_user_font_face_set_render_glyph_func(), the color glyph
+ * callback may return %CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED if the
+ * glyph is not a color glyph. This is the only case in which the
+ * %CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED may be returned from a
+ * render callback.
+ *
+ * Returns: %CAIRO_STATUS_SUCCESS upon success,
+ * %CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED if fallback options should be tried,
+ * or %CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.
  *
  * Since: 1.8
  **/
@@ -1908,6 +1923,10 @@ cairo_public void
 cairo_user_font_face_set_render_glyph_func (cairo_font_face_t                          *font_face,
 					    cairo_user_scaled_font_render_glyph_func_t  render_glyph_func);
 
+cairo_public void
+cairo_user_font_face_set_render_color_glyph_func (cairo_font_face_t                          *font_face,
+                                                  cairo_user_scaled_font_render_glyph_func_t  render_glyph_func);
+
 cairo_public void
 cairo_user_font_face_set_text_to_glyphs_func (cairo_font_face_t                            *font_face,
 					      cairo_user_scaled_font_text_to_glyphs_func_t  text_to_glyphs_func);
diff --git a/src/cairoint.h b/src/cairoint.h
index 64ed2aa4d..23b1d9c25 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -2069,6 +2069,7 @@ slim_hidden_proto (cairo_translate);
 slim_hidden_proto (cairo_transform);
 slim_hidden_proto (cairo_user_font_face_create);
 slim_hidden_proto (cairo_user_font_face_set_init_func);
+slim_hidden_proto (cairo_user_font_face_set_render_color_glyph_func);
 slim_hidden_proto (cairo_user_font_face_set_render_glyph_func);
 slim_hidden_proto (cairo_user_font_face_set_unicode_to_glyph_func);
 slim_hidden_proto (cairo_device_to_user);
diff --git a/test/Makefile.sources b/test/Makefile.sources
index b887b054d..007cb6945 100644
--- a/test/Makefile.sources
+++ b/test/Makefile.sources
@@ -386,6 +386,7 @@ test_sources = \
 	unclosed-strokes.c				\
 	user-data.c					\
 	user-font.c					\
+	user-font-color.c				\
 	user-font-mask.c				\
 	user-font-proxy.c				\
 	user-font-rescale.c				\
diff --git a/test/meson.build b/test/meson.build
index 9d84d048d..c79357c81 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -386,6 +386,7 @@ test_sources = [
   'unclosed-strokes.c',
   'user-data.c',
   'user-font.c',
+  'user-font-color.c',
   'user-font-mask.c',
   'user-font-proxy.c',
   'user-font-rescale.c',
diff --git a/test/reference/user-font-color.image16.ref.png b/test/reference/user-font-color.image16.ref.png
new file mode 100644
index 000000000..8b29f0b38
Binary files /dev/null and b/test/reference/user-font-color.image16.ref.png differ
diff --git a/test/reference/user-font-color.pdf.ref.png b/test/reference/user-font-color.pdf.ref.png
new file mode 100644
index 000000000..849614f0d
Binary files /dev/null and b/test/reference/user-font-color.pdf.ref.png differ
diff --git a/test/reference/user-font-color.ps.ref.png b/test/reference/user-font-color.ps.ref.png
new file mode 100644
index 000000000..36c5c84a7
Binary files /dev/null and b/test/reference/user-font-color.ps.ref.png differ
diff --git a/test/reference/user-font-color.quartz.ref.png b/test/reference/user-font-color.quartz.ref.png
new file mode 100644
index 000000000..86ce77e38
Binary files /dev/null and b/test/reference/user-font-color.quartz.ref.png differ
diff --git a/test/reference/user-font-color.recording.xfail.png b/test/reference/user-font-color.recording.xfail.png
new file mode 100644
index 000000000..37b8aaa1e
Binary files /dev/null and b/test/reference/user-font-color.recording.xfail.png differ
diff --git a/test/reference/user-font-color.ref.png b/test/reference/user-font-color.ref.png
new file mode 100644
index 000000000..bce2f000b
Binary files /dev/null and b/test/reference/user-font-color.ref.png differ
diff --git a/test/reference/user-font-color.script.xfail.png b/test/reference/user-font-color.script.xfail.png
new file mode 100644
index 000000000..6b1f7ddc6
Binary files /dev/null and b/test/reference/user-font-color.script.xfail.png differ
diff --git a/test/reference/user-font-color.svg.ref.png b/test/reference/user-font-color.svg.ref.png
new file mode 100644
index 000000000..9e9bf7d58
Binary files /dev/null and b/test/reference/user-font-color.svg.ref.png differ
diff --git a/test/reference/user-font-color.svg.rgb24.xfail.png b/test/reference/user-font-color.svg.rgb24.xfail.png
new file mode 100644
index 000000000..37b8aaa1e
Binary files /dev/null and b/test/reference/user-font-color.svg.rgb24.xfail.png differ
diff --git a/test/user-font-color.c b/test/user-font-color.c
new file mode 100644
index 000000000..437e57a27
--- /dev/null
+++ b/test/user-font-color.c
@@ -0,0 +1,251 @@
+/*
+ * Copyright © 2006, 2008 Red Hat, Inc.
+ * Copyright © 2021 Adrian Johnson
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Red Hat, Inc. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Red Hat, Inc. makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Contributor(s):
+ *	Kristian Høgsberg <krh at redhat.com>
+ *	Behdad Esfahbod <behdad at behdad.org>
+ */
+
+/* Test that user-fonts can handle color and non-color glyphs in the
+ * same font.
+ */
+
+#include "cairo-test.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+
+#define BORDER 10
+#define TEXT_SIZE 64
+#define WIDTH  (TEXT_SIZE * 6 + 2*BORDER)
+#define HEIGHT (TEXT_SIZE + 2*BORDER)
+
+#define TEXT   "abcdef"
+
+
+static cairo_status_t
+test_scaled_font_init (cairo_scaled_font_t  *scaled_font,
+		       cairo_t              *cr,
+		       cairo_font_extents_t *metrics)
+{
+    metrics->ascent  = .75;
+    metrics->descent = .25;
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static void
+render_glyph_solid (cairo_t *cr, double width, double height, cairo_bool_t color)
+{
+    if (color)
+        cairo_set_source_rgba (cr, 0, 1, 1, 0.5);
+    cairo_rectangle (cr, 0, 0, width/2, height/2);
+    cairo_fill (cr);
+
+    if (color)
+        cairo_set_source_rgba (cr, 1, 0, 1, 0.5);
+    cairo_rectangle (cr, width/4, height/4, width/2, height/2);
+    cairo_fill (cr);
+
+    if (color)
+        cairo_set_source_rgba (cr, 1, 1, 0, 0.5);
+    cairo_rectangle (cr, width/2, height/2, width/2, height/2);
+    cairo_fill (cr);
+}
+
+static void
+render_glyph_linear (cairo_t *cr, double width, double height, cairo_bool_t color)
+{
+    cairo_pattern_t *pat;
+
+    pat = cairo_pattern_create_linear (0.0, 0.0, width, height);
+    cairo_pattern_add_color_stop_rgba (pat, 0,   1, 0, 0, 1);
+    cairo_pattern_add_color_stop_rgba (pat, 0.5, 0, 1, 0, color ? 0.5 : 1);
+    cairo_pattern_add_color_stop_rgba (pat, 1,   0, 0, 1, 1);
+    cairo_set_source (cr, pat);
+
+    cairo_rectangle (cr, 0, 0, width, height);
+    cairo_fill (cr);
+}
+
+static void
+render_glyph_text (cairo_t *cr, double width, double height, cairo_bool_t color)
+{
+    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
+			    CAIRO_FONT_SLANT_NORMAL,
+			    CAIRO_FONT_WEIGHT_NORMAL);
+    cairo_set_font_size(cr, 0.5);
+
+    if (color)
+        cairo_set_source_rgb (cr, 0.5, 0.5, 0);
+    cairo_move_to (cr, width*0.1, height/2);
+    cairo_show_text (cr, "a");
+
+    if (color)
+        cairo_set_source_rgb (cr, 0, 0.5, 0.5);
+    cairo_move_to (cr, width*0.4, height*0.9);
+    cairo_show_text (cr, "z");
+}
+
+static cairo_status_t
+test_scaled_font_render_color_glyph (cairo_scaled_font_t  *scaled_font,
+                                     unsigned long         glyph,
+                                     cairo_t              *cr,
+                                     cairo_text_extents_t *metrics)
+{
+    cairo_status_t status = CAIRO_STATUS_SUCCESS;
+    double width = 0.5;
+    double height = 0.8;
+
+    metrics->x_advance = 0.75;
+    cairo_translate (cr,  0.125, -0.6);
+    switch (glyph) {
+        case 'a':
+            render_glyph_solid (cr, width, height, TRUE);
+            break;
+        case 'b':
+            render_glyph_linear (cr, width, height, TRUE);
+            break;
+        case 'c':
+            render_glyph_text (cr, width, height, TRUE);
+            break;
+        case 'd':
+            render_glyph_solid (cr, width, height, TRUE);
+            status = CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
+            break;
+        case 'e':
+            render_glyph_linear (cr, width, height, TRUE);
+            status = CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
+            break;
+        case 'f':
+            render_glyph_solid (cr, width, height, TRUE);
+            status = CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED;
+            break;
+    }
+
+    return status;
+}
+
+static cairo_status_t
+test_scaled_font_render_glyph (cairo_scaled_font_t  *scaled_font,
+			       unsigned long         glyph,
+			       cairo_t              *cr,
+			       cairo_text_extents_t *metrics)
+{
+    double width = 0.5;
+    double height = 0.8;
+    metrics->x_advance = 0.75;
+    cairo_translate (cr,  0.125, -0.6);
+    switch (glyph) {
+        case 'd':
+            render_glyph_solid (cr, width, height, FALSE);
+            break;
+        case 'e':
+            render_glyph_linear (cr, width, height, FALSE);
+            break;
+        case 'f':
+            render_glyph_text (cr, width, height, FALSE);
+            break;
+    }
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static cairo_status_t
+_user_font_face_create (cairo_font_face_t **out)
+{
+
+    cairo_font_face_t *user_font_face;
+
+    user_font_face = cairo_user_font_face_create ();
+    cairo_user_font_face_set_init_func (user_font_face, test_scaled_font_init);
+    cairo_user_font_face_set_render_color_glyph_func (user_font_face, test_scaled_font_render_color_glyph);
+    cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
+
+    *out = user_font_face;
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    cairo_font_face_t *font_face;
+    const char text[] = TEXT;
+    cairo_font_extents_t font_extents;
+    cairo_text_extents_t extents;
+    cairo_status_t status;
+
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    cairo_paint (cr);
+
+    status = _user_font_face_create (&font_face);
+    if (status) {
+	return cairo_test_status_from_status (cairo_test_get_context (cr),
+					      status);
+    }
+
+    cairo_set_font_face (cr, font_face);
+    cairo_font_face_destroy (font_face);
+
+    cairo_set_font_size (cr, TEXT_SIZE);
+
+    cairo_font_extents (cr, &font_extents);
+    cairo_text_extents (cr, text, &extents);
+
+    /* logical boundaries in red */
+    cairo_move_to (cr, 0, BORDER);
+    cairo_rel_line_to (cr, WIDTH, 0);
+    cairo_move_to (cr, 0, BORDER + font_extents.ascent);
+    cairo_rel_line_to (cr, WIDTH, 0);
+    cairo_move_to (cr, 0, BORDER + font_extents.ascent + font_extents.descent);
+    cairo_rel_line_to (cr, WIDTH, 0);
+    cairo_move_to (cr, BORDER, 0);
+    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
+    cairo_move_to (cr, BORDER + extents.x_advance, 0);
+    cairo_rel_line_to (cr, 0, 2*BORDER + TEXT_SIZE);
+    cairo_set_source_rgb (cr, 1, 0, 0);
+    cairo_set_line_width (cr, 2);
+    cairo_stroke (cr);
+
+    /* ink boundaries in green */
+    cairo_rectangle (cr,
+		     BORDER + extents.x_bearing, BORDER + font_extents.ascent + extents.y_bearing,
+		     extents.width, extents.height);
+    cairo_set_source_rgb (cr, 0, 1, 0);
+    cairo_set_line_width (cr, 2);
+    cairo_stroke (cr);
+
+    /* text in color */
+    cairo_set_source_rgb (cr, 0, 0, 0);
+    cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
+    cairo_show_text (cr, text);
+
+    return CAIRO_TEST_SUCCESS;
+}
+
+CAIRO_TEST (user_font_color,
+	    "Tests user font color feature",
+	    "font, user-font", /* keywords */
+	    "cairo >= 1.17.4", /* requirements */
+	    WIDTH, HEIGHT,
+	    NULL, draw)


More information about the cairo-commit mailing list