[cairo-commit] 4 commits - configure.ac src/cairo-pdf-surface.c src/check-cairoint.sh src/Makefile.am test/.gitignore test/Makefile.am test/user-font-rescale.c test/user-font-rescale-ref.png

Chris Wilson ickle at kemper.freedesktop.org
Mon Sep 15 18:17:12 PDT 2008


 configure.ac                   |    4 
 src/Makefile.am                |    2 
 src/cairo-pdf-surface.c        |   10 -
 src/check-cairoint.sh          |    2 
 test/.gitignore                |    1 
 test/Makefile.am               |    2 
 test/user-font-rescale-ref.png |binary
 test/user-font-rescale.c       |  329 +++++++++++++++++++++++++++++++++++++++++
 8 files changed, 340 insertions(+), 10 deletions(-)

New commits:
commit 60ab0f79f8d109fa662374f642d2961bb20a820b
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Tue Sep 16 02:08:47 2008 +0100

    [pdf] Kill a compiler warning.
    
    Older versions of gcc complain about the use of a guard variable, and warn
    that solid_color may be used uninitialized. As it happens the guard
    variable is redundant and we can just use solid_color directly.

diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index e717fc4..f72f829 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -2609,13 +2609,11 @@ _cairo_pdf_surface_select_pattern (cairo_pdf_surface_t *surface,
 {
     cairo_status_t status;
     int alpha;
-    cairo_bool_t is_solid_color = FALSE;
-    cairo_color_t *solid_color;
+    cairo_color_t *solid_color = NULL;
 
     if (pattern->type == CAIRO_PATTERN_TYPE_SOLID) {
 	cairo_solid_pattern_t *solid = (cairo_solid_pattern_t *) pattern;
 
-	is_solid_color = TRUE;
 	solid_color = &solid->color;
     }
 
@@ -2624,13 +2622,11 @@ _cairo_pdf_surface_select_pattern (cairo_pdf_surface_t *surface,
     {
 	cairo_gradient_pattern_t *gradient = (cairo_gradient_pattern_t *) pattern;
 
-	if (gradient->n_stops == 1) {
-	    is_solid_color = TRUE;
+	if (gradient->n_stops == 1)
 	    solid_color = &gradient->stops[0].color;
-	}
     }
 
-    if (is_solid_color) {
+    if (solid_color != NULL) {
 	if (surface->current_pattern_is_solid_color == FALSE ||
 	    surface->current_color_red != solid_color->red ||
 	    surface->current_color_green != solid_color->green ||
commit e53fe3ec244833437fb35674258b983f9dcefa2d
Author: Jeff Muizelaar <jeff at infidigm.net>
Date:   Tue Sep 16 01:45:10 2008 +0100

    [test] Add a test case for drawing glyphs with different metrics.
    
    The ability to draw glyphs with different metrics is useful when doing
    font substitution with fixed layout like in pdf and I eventually plan on
    adding code to poppler to do something similar.

diff --git a/test/.gitignore b/test/.gitignore
index 374bbf5..a66739e 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -222,6 +222,7 @@ unbounded-operator
 user-data
 user-font
 user-font-proxy
+user-font-rescale
 xlib-expose-event
 xlib-surface
 xlib-surface-source
diff --git a/test/Makefile.am b/test/Makefile.am
index 2a27310..57aa781 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -174,6 +174,7 @@ unbounded-operator$(EXEEXT)				\
 user-data$(EXEEXT)					\
 user-font$(EXEEXT)					\
 user-font-proxy$(EXEEXT)				\
+user-font-rescale$(EXEEXT)				\
 zero-alpha$(EXEEXT)
 
 # XXX: Here are some existing tests that are currently disabled for
@@ -740,6 +741,7 @@ REFERENCE_IMAGES = \
 	user-font-proxy-pdf-ref.png	\
 	user-font-proxy-ps-ref.png	\
 	user-font-proxy-svg-ref.png	\
+	user-font-rescale-ref.png	\
 	unbounded-operator-quartz-ref.png	\
 	unbounded-operator-quartz-rgb24-ref.png	\
 	xlib-expose-event-ref.png \
diff --git a/test/user-font-rescale-ref.png b/test/user-font-rescale-ref.png
new file mode 100644
index 0000000..39bce60
Binary files /dev/null and b/test/user-font-rescale-ref.png differ
diff --git a/test/user-font-rescale.c b/test/user-font-rescale.c
new file mode 100644
index 0000000..a65d7e5
--- /dev/null
+++ b/test/user-font-rescale.c
@@ -0,0 +1,329 @@
+/*
+ * Copyright © 2008 Jeff Muizelaar
+ *
+ * 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
+ * Jeff Muizelaar not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Jeff Muizelaar makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * JEFF MUIZELAAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL JEFF MUIZELAAR 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):
+ *	Jeff Muizelaar <jeff at infidigm.net>
+ *	Kristian Høgsberg <krh at redhat.com>
+ *	Behdad Esfahbod <behdad at behdad.org>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#define __USE_ISOC99
+#include <math.h>
+
+#include "cairo-test.h"
+
+#define BORDER 10
+#define TEXT_SIZE 32
+#define WIDTH  (TEXT_SIZE * 13.75 + 2*BORDER)
+#define HEIGHT ((TEXT_SIZE + 2*BORDER)*3 + BORDER)
+#define TEXT   "test of rescaled glyphs";
+
+static cairo_test_draw_function_t draw;
+
+static const cairo_test_t test = {
+    "user-font-rescale",
+    "Tests drawing text with user defined widths",
+    WIDTH, HEIGHT,
+    draw
+};
+
+static const cairo_user_data_key_t rescale_font_closure_key;
+
+struct rescaled_font {
+    cairo_font_face_t *substitute_font;
+    cairo_scaled_font_t *measuring_font;
+    unsigned long glyph_count;
+    unsigned long start;
+    double *desired_width;
+    double *rescale_factor;
+};
+
+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)
+{
+    cairo_font_face_t *user_font = cairo_scaled_font_get_font_face (scaled_font);
+    struct rescaled_font *r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key);
+    cairo_glyph_t cairo_glyph;
+
+    cairo_glyph.index = glyph;
+    cairo_glyph.x = 0;
+    cairo_glyph.y = 0;
+
+    cairo_set_font_face (cr, r->substitute_font);
+
+    if (glyph - r->start < r->glyph_count) {
+	cairo_matrix_t matrix;
+
+	if (isnan (r->rescale_factor[glyph - r->start])) {
+	    double desired_width;
+	    double actual_width;
+	    cairo_text_extents_t extents;
+
+	    /* measure the glyph and compute the necessary rescaling factor */
+	    cairo_scaled_font_glyph_extents (r->measuring_font, &cairo_glyph, 1, &extents);
+
+	    desired_width = r->desired_width[glyph - r->start];
+	    actual_width = extents.x_advance;
+
+	    r->rescale_factor[glyph - r->start] = desired_width / actual_width;
+	}
+
+	/* scale the font so that the glyph width matches the desired width */
+	cairo_get_font_matrix (cr, &matrix);
+	cairo_matrix_scale (&matrix, r->rescale_factor[glyph - r->start], 1.);
+	cairo_set_font_matrix (cr, &matrix);
+    }
+
+    cairo_show_glyphs (cr, &cairo_glyph, 1);
+    cairo_glyph_extents (cr, &cairo_glyph, 1, metrics);
+
+    return CAIRO_STATUS_SUCCESS;
+}
+
+/* UNICHAR_TO_UTF8 from Behdad
+ * http://mces.blogspot.com/2004/07/static-unicode-to-utf-8-converter.html */
+#define UNICHAR_TO_UTF8(Char)                                                 \
+  (const char [])                                                             \
+    {                                                                         \
+      /* first octet */                                                       \
+      (Char) < 0x00000080 ?   (Char)                       :                  \
+      (Char) < 0x00000800 ?  ((Char) >>  6)         | 0xC0 :                  \
+      (Char) < 0x00010000 ?  ((Char) >> 12)         | 0xE0 :                  \
+      (Char) < 0x00200000 ?  ((Char) >> 18)         | 0xF0 :                  \
+      (Char) < 0x04000000 ?  ((Char) >> 24)         | 0xF8 :                  \
+                             ((Char) >> 30)         | 0xFC,                   \
+      /* second octet */                                                      \
+      (Char) < 0x00000080 ?    0 /* null-terminator */     :                  \
+      (Char) < 0x00000800 ?  ((Char)        & 0x3F) | 0x80 :                  \
+      (Char) < 0x00010000 ? (((Char) >>  6) & 0x3F) | 0x80 :                  \
+      (Char) < 0x00200000 ? (((Char) >> 12) & 0x3F) | 0x80 :                  \
+      (Char) < 0x04000000 ? (((Char) >> 18) & 0x3F) | 0x80 :                  \
+                            (((Char) >> 24) & 0x3F) | 0x80,                   \
+      /* third octet */                                                       \
+      (Char) < 0x00000800 ?    0 /* null-terminator */     :                  \
+      (Char) < 0x00010000 ?  ((Char)        & 0x3F) | 0x80 :                  \
+      (Char) < 0x00200000 ? (((Char) >>  6) & 0x3F) | 0x80 :                  \
+      (Char) < 0x04000000 ? (((Char) >> 12) & 0x3F) | 0x80 :                  \
+                            (((Char) >> 18) & 0x3F) | 0x80,                   \
+      /* fourth octet */                                                      \
+      (Char) < 0x00010000 ?    0 /* null-terminator */     :                  \
+      (Char) < 0x00200000 ?  ((Char)        & 0x3F) | 0x80 :                  \
+      (Char) < 0x04000000 ? (((Char) >>  6) & 0x3F) | 0x80 :                  \
+                            (((Char) >> 12) & 0x3F) | 0x80,                   \
+      /* fifth octet */                                                       \
+      (Char) < 0x00200000 ?    0 /* null-terminator */     :                  \
+      (Char) < 0x04000000 ?  ((Char)        & 0x3F) | 0x80 :                  \
+                            (((Char) >>  6) & 0x3F) | 0x80,                   \
+      /* sixth octet */                                                       \
+      (Char) < 0x04000000 ?    0 /* null-terminator */     :                  \
+                             ((Char)        & 0x3F) | 0x80,                   \
+                               0 /* null-terminator */                        \
+    }
+
+
+static cairo_status_t
+test_scaled_font_unicode_to_glyph (cairo_scaled_font_t *scaled_font,
+									  unsigned long        unicode,
+									  unsigned long       *glyph_index) {
+    cairo_font_face_t *user_font = cairo_scaled_font_get_font_face (scaled_font);
+    struct rescaled_font *r = cairo_font_face_get_user_data (user_font, &rescale_font_closure_key);
+    int num_glyphs;
+    cairo_glyph_t *glyphs = NULL;
+
+    cairo_status_t status = cairo_scaled_font_text_to_glyphs (r->measuring_font, 0, 0,
+					   UNICHAR_TO_UTF8(unicode), -1,
+					   &glyphs, &num_glyphs,
+					   NULL, NULL,
+					   NULL);
+    if (status)
+	return status;
+
+    *glyph_index = glyphs[0].index;
+
+    cairo_glyph_free (glyphs);
+    return CAIRO_STATUS_SUCCESS;
+}
+
+static void rescale_font_closure_destroy (void *data) {
+    struct rescaled_font *r = data;
+    cairo_scaled_font_destroy (r->measuring_font);
+    free (r->desired_width);
+    free (r->rescale_factor);
+    free (r);
+}
+
+static cairo_font_face_t *
+create_rescaled_font (cairo_font_face_t *substitute_font, int glyph_start, int glyph_count, double *desired_width)
+{
+    cairo_font_face_t *user_font_face = NULL;
+    struct rescaled_font *r = xmalloc (sizeof(struct rescaled_font));
+    cairo_font_options_t *options = cairo_font_options_create ();
+    cairo_matrix_t m;
+    unsigned long i;
+
+    user_font_face = cairo_user_font_face_create ();
+    cairo_user_font_face_set_render_glyph_func (user_font_face, test_scaled_font_render_glyph);
+    cairo_user_font_face_set_unicode_to_glyph_func (user_font_face, test_scaled_font_unicode_to_glyph);
+
+    r->substitute_font = substitute_font;
+
+    /* we don't want any hinting when doing the measuring */
+    cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
+    cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
+
+    cairo_matrix_init_identity (&m);
+
+    r->measuring_font = cairo_scaled_font_create (r->substitute_font, &m, &m, options);
+
+    r->start = glyph_start;
+    r->glyph_count = glyph_count;
+    r->desired_width = xcalloc (sizeof(double), r->glyph_count);
+    r->rescale_factor = xcalloc (sizeof(double), r->glyph_count);
+
+    for (i=0; i<r->glyph_count; i++) {
+	r->desired_width[i] = desired_width[i];
+	/* use NAN to specify unset */
+	r->rescale_factor[i] = NAN;
+    }
+
+    cairo_font_options_destroy (options);
+
+    cairo_font_face_set_user_data (user_font_face, &rescale_font_closure_key,
+	    r, rescale_font_closure_destroy);
+
+    return user_font_face;
+}
+
+
+
+static cairo_font_face_t *
+get_user_font_face (cairo_font_face_t *substitute_font, const char *text, cairo_font_face_t *old)
+{
+    cairo_font_options_t *options = cairo_font_options_create ();
+    cairo_matrix_t m;
+    cairo_scaled_font_t *measure;
+    int i;
+    double *widths;
+    int count;
+    int num_glyphs;
+    unsigned long min_index, max_index;
+    cairo_font_face_t *ret;
+
+    cairo_glyph_t *glyphs = NULL;
+
+    /* we don't want any hinting when doing the measuring */
+    cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
+    cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
+
+    cairo_matrix_init_identity (&m);
+    measure = cairo_scaled_font_create (old, &m, &m, options);
+
+    cairo_scaled_font_text_to_glyphs (measure, 0, 0,
+					   text, -1,
+					   &glyphs, &num_glyphs,
+					   NULL, NULL,
+					   NULL);
+
+    /* find the glyph range the text covers */
+    max_index = glyphs[0].index;
+    min_index = glyphs[0].index;
+    for (i=0; i<num_glyphs; i++) {
+	if (glyphs[i].index < min_index)
+	    min_index = glyphs[i].index;
+	if (glyphs[i].index > max_index)
+	    max_index = glyphs[i].index;
+    }
+
+    count = max_index - min_index + 1;
+    widths = xmalloc (sizeof(double) * count);
+    /* measure all of the necessary glyphs individually */
+    for (i=0; i<num_glyphs; i++) {
+	cairo_text_extents_t extents;
+	cairo_scaled_font_glyph_extents (measure, &glyphs[i], 1, &extents);
+	widths[glyphs[i].index - min_index] = extents.x_advance;
+    }
+
+    cairo_glyph_free (glyphs);
+
+    cairo_font_options_destroy (options);
+    cairo_scaled_font_destroy (measure);
+
+    ret = create_rescaled_font (substitute_font, min_index, count, widths);
+    free (widths);
+    return ret;
+}
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    cairo_font_extents_t font_extents;
+    cairo_text_extents_t extents;
+    cairo_font_face_t *rescaled;
+    cairo_font_face_t *old;
+    const char text[] = TEXT;
+
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    cairo_paint (cr);
+
+    cairo_select_font_face (cr, "Bitstream Vera Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
+
+    cairo_set_font_size (cr, TEXT_SIZE);
+
+    cairo_font_extents (cr, &font_extents);
+    cairo_text_extents (cr, text, &extents);
+
+    cairo_set_source_rgba (cr, 0, 0, 0, 0.5);
+    cairo_move_to (cr, BORDER, BORDER + font_extents.ascent);
+    cairo_show_text (cr, text);
+
+    /* same text in 'mono' with widths that match the 'sans' version */
+    old = cairo_get_font_face (cr);
+    cairo_select_font_face (cr, "Bitstream Vera Sans Mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
+    rescaled = get_user_font_face (cairo_get_font_face (cr), text, old);
+    cairo_set_font_face (cr, rescaled);
+
+    cairo_set_source_rgba (cr, 0, 0, 1, 0.5);
+    cairo_move_to (cr, BORDER, BORDER + font_extents.height + 2*BORDER + font_extents.ascent);
+    cairo_show_text (cr, text);
+
+    cairo_font_face_destroy (rescaled);
+
+    /* mono text */
+    cairo_select_font_face (cr, "Bitstream Vera Sans Mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
+
+    cairo_set_source_rgba (cr, 0, 0, 1, 0.5);
+    cairo_move_to (cr, BORDER, BORDER + 2*font_extents.height + 4*BORDER + font_extents.ascent);
+    cairo_show_text (cr, text);
+
+    return CAIRO_TEST_SUCCESS;
+}
+
+int
+main (void)
+{
+    return cairo_test (&test);
+}
commit 9c207b24542edb2e8e388329471ab9e7d7d61ff9
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Tue Sep 16 01:34:14 2008 +0100

    [configure] Append -lz to LIBS
    
    After going to the effort of detecting zlib for cairo-deflate-stream.c, it
    rather defects the purpose of the exercise (and the mingw32 build) if we
    forget to add the library to LIBS.

diff --git a/configure.ac b/configure.ac
index c48a5df..9019ee6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -27,7 +27,9 @@ AC_CACHE_SAVE
 dnl ===========================================================================
 
 AC_CHECK_LIB(z, compress,
-	 [AC_CHECK_HEADER(zlib.h, [have_libz=yes],
+	 [AC_CHECK_HEADER(zlib.h, [
+	  have_libz=yes
+	  LIBS="$LIBS -lz"],
 	 [have_libz="no (requires zlib http://www.gzip.org/zlib/)"])],
 	 [have_libz="no (requires zlib http://www.gzip.org/zlib/)"])
 
commit 87094e8c78d99b0ae9fed287acaf8ba7abb4e43a
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Tue Sep 16 01:09:45 2008 +0100

    [Makefile] Amend checks for horrible msys environment.
    
    automake-1.8 requires $(EXEEXT) for TESTS and feeds lt-check-*.c to
    check-cairoint.h. Workaround both misfeatures.

diff --git a/src/Makefile.am b/src/Makefile.am
index 9bc3a49..4bebd7d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -71,7 +71,7 @@ TESTS_SH = \
 TESTS += $(TESTS_SH)
 if CROSS_COMPILING
 else
-TESTS += check-link
+TESTS += check-link$(EXEEXT)
 endif
 
 EXTRA_DIST += $(TESTS_SH) check-has-hidden-symbols.c
diff --git a/src/check-cairoint.sh b/src/check-cairoint.sh
index 2809af8..83ee1c9 100755
--- a/src/check-cairoint.sh
+++ b/src/check-cairoint.sh
@@ -7,7 +7,7 @@ stat=0
 
 echo 'Checking source files for missing or misplaced #include "cairoint.h"'
 
-find "$srcdir" \( -name '*.c' -or -name '*.cpp' \) -and ! -name 'check-*.c' |
+find "$srcdir" \( -name '*.c' -or -name '*.cpp' \) -and ! \( -name 'check-*.c' -or -name 'lt-check-*.c' \) |
 while read x; do
 	grep '\<include\>' "$x" /dev/null | head -n 1
 done |


More information about the cairo-commit mailing list