summaryrefslogtreecommitdiff
path: root/test/mask-glyphs.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2009-06-08 23:58:56 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2009-06-09 00:06:08 +0100
commitac563715506b911f142dd0e4460a6effee45119d (patch)
treef8e02a9d76b6c6044aa3a7f26a0017a9da093779 /test/mask-glyphs.c
parent015df191ba947e714285145c3a4ead198ba0d07e (diff)
downloadcairo-ac563715506b911f142dd0e4460a6effee45119d.tar.gz
[test] Exercise overlapping glyphs
Quite an expensive test that converts an image into a distorted array of glyphs, using a perspective transformation taking the intensity of the pixel as depth. This generates a pretty picture and many overlapping glyphs.
Diffstat (limited to 'test/mask-glyphs.c')
-rw-r--r--test/mask-glyphs.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/test/mask-glyphs.c b/test/mask-glyphs.c
new file mode 100644
index 000000000..045dcaf8b
--- /dev/null
+++ b/test/mask-glyphs.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2009 Chris Wilson
+ *
+ * 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
+ * Chris Wilson not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Chris Wilson makes no representations about the
+ * suitability of this software for any purpose. It is provided "as
+ * is" without express or implied warranty.
+ *
+ * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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.
+ *
+ * Author: Chris Wilson <chris@chris-wilson.co.uk>
+ */
+
+#include "cairo-test.h"
+
+#include <assert.h>
+
+static const char *png_filename = "romedalen.png";
+
+#define WIDTH 800
+#define HEIGHT 600
+
+static int
+_image_to_glyphs (cairo_surface_t *image,
+ int channel,
+ int level,
+ cairo_scaled_font_t *scaled_font,
+ cairo_glyph_t *glyphs,
+ double tx, double ty)
+{
+ int width, height, stride;
+ const unsigned char *data;
+ cairo_format_t format;
+ int x, y, z, n;
+
+ width = cairo_image_surface_get_width (image);
+ height = cairo_image_surface_get_height (image);
+ stride = cairo_image_surface_get_stride (image);
+ format = cairo_image_surface_get_format (image);
+ data = cairo_image_surface_get_data (image);
+
+ n = 0;
+ for (y = 0; y < height; y++) {
+ const uint32_t *row = (uint32_t *) (data + y * stride);
+
+ for (x = 0; x < width; x++) {
+ z = (row[x] >> channel) & 0xff;
+ if (z == level) {
+ double xx, yy, zz;
+ char c = n % 26 + 'a';
+ int count = 1;
+ cairo_glyph_t *glyphs_p = &glyphs[n];
+
+ xx = 4 * (x - width/2.) + width/2.;
+ yy = 4 * (y - height/2.) + height/2.;
+
+ zz = z / 1000.;
+ xx = xx + zz*(width/2. - xx);
+ yy = yy + zz*(height/2. - yy);
+
+ cairo_scaled_font_text_to_glyphs (scaled_font,
+ tx + xx, ty + yy,
+ &c, 1,
+ &glyphs_p, &count,
+ NULL, NULL,
+ NULL);
+ assert (glyphs_p == &glyphs[n]);
+ assert (count == 1);
+ n++;
+ }
+ }
+ }
+
+ return n;
+}
+
+static cairo_status_t
+_render_image (cairo_t *cr,
+ int width, int height,
+ cairo_surface_t *image)
+{
+ int ww, hh;
+ cairo_glyph_t *glyphs;
+ cairo_pattern_t *mask;
+ cairo_scaled_font_t *scaled_font;
+ double tx, ty;
+ const struct {
+ int shift;
+ double red;
+ double green;
+ double blue;
+ } channel[3] = {
+ { 0, 0.9, 0.3, 0.4 },
+ { 8, 0.4, 0.9, 0.3 },
+ { 16, 0.3, 0.4, 0.9 },
+ };
+ unsigned int n, i;
+
+ ww = cairo_image_surface_get_width (image);
+ hh = cairo_image_surface_get_height (image);
+
+ glyphs = cairo_glyph_allocate (ww * hh);
+ if (glyphs == NULL) {
+ cairo_surface_destroy (image);
+ return CAIRO_STATUS_NO_MEMORY;
+ }
+
+ tx = (width - ww) / 2.;
+ ty = (height - hh) / 2.;
+
+ cairo_set_font_size (cr, 5);
+ scaled_font = cairo_get_scaled_font (cr);
+
+ for (i = 0; i < sizeof (channel) / sizeof (channel[0]); i++) {
+ cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
+ for (n = 0; n < 256; n++) {
+ int num_glyphs;
+
+ num_glyphs = _image_to_glyphs (image, channel[i].shift, n,
+ scaled_font,
+ glyphs, tx, ty);
+
+ cairo_set_source_rgba (cr,
+ 0, 0, 0,
+ .15 + .85 * n / 255.);
+ cairo_show_glyphs (cr, glyphs, num_glyphs);
+ }
+ mask = cairo_pop_group (cr);
+ cairo_set_source_rgb (cr,
+ channel[i].red,
+ channel[i].green,
+ channel[i].blue);
+ cairo_mask (cr, mask);
+ cairo_pattern_destroy (mask);
+ }
+
+ cairo_glyph_free (glyphs);
+ return CAIRO_STATUS_SUCCESS;
+}
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+ const cairo_test_context_t *ctx = cairo_test_get_context (cr);
+ cairo_surface_t *image;
+ cairo_status_t status;
+
+ cairo_set_source_rgb (cr, 0, 0, 0);
+ cairo_paint (cr);
+
+ image = cairo_test_create_surface_from_png (ctx, png_filename);
+ status = _render_image (cr, width, height, image);
+ cairo_surface_destroy (image);
+
+ return cairo_test_status_from_status (ctx, status);
+}
+
+CAIRO_TEST (mask_glyphs,
+ "Creates a mask using a distorted array of overlapping glyphs",
+ "XFAIL=svg,pdf mask, glyphs", /* keywords */
+ NULL, /* requirements */
+ WIDTH, HEIGHT,
+ NULL, draw)