[cairo-commit] libsvg-cairo/src Makefile.am, 1.7, 1.8 svg-cairo-internal.h, 1.20, 1.21 svg_cairo.c, 1.38, 1.39 svg_cairo_sprintf_alloc.c, 1.4, 1.5 svg_cairo_state.c, 1.15, 1.16

Keith Packard commit at pdx.freedesktop.org
Tue Jul 12 12:37:46 PDT 2005


Committed by: keithp

Update of /cvs/cairo/libsvg-cairo/src
In directory gabe:/tmp/cvs-serv24540/src

Modified Files:
	Makefile.am svg-cairo-internal.h svg_cairo.c 
	svg_cairo_sprintf_alloc.c svg_cairo_state.c 
Log Message:
2005-07-12  Keith Packard  <keithp at keithp.com>

* configure.in:
* src/Makefile.am:
* src/svg-cairo-internal.h:
* src/svg_cairo.c: (_svg_cairo_set_font_family),
(_svg_cairo_set_font_size), (_svg_cairo_set_font_style),
(_svg_cairo_set_font_weight), (_svg_cairo_render_text),
(_svg_cairo_text_size), (_svg_cairo_length_to_pixel):
* src/svg_cairo_sprintf_alloc.c:
* src/svg_cairo_state.c: (_svg_cairo_state_init),
(_svg_cairo_state_init_copy), (_svg_cairo_state_deinit):
Use pangocairo where available to draw text instead of
the toy API. Disable with ./configure -disable-pango

Also, add a few #defines needed to define some new libc
functions from system includes

Index: Makefile.am
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/Makefile.am,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- Makefile.am	20 Jan 2005 16:42:40 -0000	1.7
+++ Makefile.am	12 Jul 2005 19:37:43 -0000	1.8
@@ -10,6 +10,6 @@
 
 libsvg_cairo_la_LDFLAGS = -version-info @VERSION_INFO@
 
-INCLUDES = $(WARN_CFLAGS) $(LIBSVG_CAIRO_CFLAGS) $(X_CFLAGS)
+INCLUDES = $(WARN_CFLAGS) $(PANGOCAIRO_CFLAGS) $(LIBSVG_CAIRO_CFLAGS) $(X_CFLAGS)
 
-libsvg_cairo_la_LIBADD = $(LIBSVG_CAIRO_LIBS) $(X_LIBS)
+libsvg_cairo_la_LIBADD = $(PANGOCAIRO_LIBS) $(LIBSVG_CAIRO_LIBS) $(X_LIBS)

Index: svg-cairo-internal.h
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/svg-cairo-internal.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- svg-cairo-internal.h	12 Jul 2005 18:46:22 -0000	1.20
+++ svg-cairo-internal.h	12 Jul 2005 19:37:43 -0000	1.21
@@ -21,14 +21,22 @@
 
 #ifndef SVG_CAIRO_INTERNAL_H
 #define SVG_CAIRO_INTERNAL_H
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include "svg-cairo.h"
 #include "svg-cairo-version.h"
 
 #include <stdarg.h>
 
+#if HAVE_PANGOCAIRO
+#include <pango/pango.h>
+#include <pango/pangocairo.h>
+#endif
+
 /* XXX: What should this actually be? */
-#define SVG_CAIRO_FONT_FAMILY_DEFAULT "verdana"
+#define SVG_CAIRO_FONT_FAMILY_DEFAULT "sans-serif"
 
 typedef struct svg_cairo_pt {
     double x;
@@ -51,11 +59,15 @@
     double fill_opacity;
     double stroke_opacity;
 
+#if HAVE_PANGOCAIRO
+    PangoFontDescription *font_description;
+#else
     char *font_family;
     double font_size;
     svg_font_style_t font_style;
     unsigned int font_weight;
     int font_dirty;
+#endif
 
     double *dash;
     int num_dashes;

Index: svg_cairo.c
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/svg_cairo.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -d -r1.38 -r1.39
--- svg_cairo.c	12 Jul 2005 18:40:43 -0000	1.38
+++ svg_cairo.c	12 Jul 2005 19:37:43 -0000	1.39
@@ -20,6 +20,7 @@
  */
 
 #include <stdlib.h>
+#define __USE_SVID
 #include <string.h>
 
 #include "svg-cairo-internal.h"
@@ -87,9 +88,6 @@
 _svg_cairo_set_fill_rule (void *closure, svg_fill_rule_t fill_rule);
 
 static svg_status_t
-_svg_cairo_select_font (svg_cairo_t *svg_cairo);
-
-static svg_status_t
 _svg_cairo_set_font_family (void *closure, const char *family);
 
 static svg_status_t
@@ -760,6 +758,7 @@
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));    
 }
 
+#if !HAVE_PANGOCAIRO
 static svg_status_t
 _svg_cairo_select_font (svg_cairo_t *svg_cairo)
 {
@@ -795,17 +794,23 @@
 
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
+#endif
 
 static svg_status_t
 _svg_cairo_set_font_family (void *closure, const char *family)
 {
     svg_cairo_t *svg_cairo = closure;
 
+#if HAVE_PANGOCAIRO
+    pango_font_description_set_family (svg_cairo->state->font_description,
+				       family);
+#else
     if (svg_cairo->state->font_family)
 	free (svg_cairo->state->font_family);
 
     svg_cairo->state->font_family = strdup (family);
     svg_cairo->state->font_dirty = 1;
+#endif
 
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
@@ -815,8 +820,13 @@
 {
     svg_cairo_t *svg_cairo = closure;
 
+#if HAVE_PANGOCAIRO
+    pango_font_description_set_absolute_size (svg_cairo->state->font_description,
+					      (int) (size * PANGO_SCALE));
+#else
     svg_cairo->state->font_size = size;
     svg_cairo->state->font_dirty = 1;
+#endif
 
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
@@ -825,9 +835,28 @@
 _svg_cairo_set_font_style (void *closure, svg_font_style_t font_style)
 {
     svg_cairo_t *svg_cairo = closure;
+    
+#if HAVE_PANGOCAIRO
+    PangoStyle	style;
 
+    switch (font_style) {
+    case SVG_FONT_STYLE_ITALIC:
+	style = PANGO_STYLE_ITALIC;
+	break;
+    case SVG_FONT_STYLE_OBLIQUE:
+	style = PANGO_STYLE_OBLIQUE;
+	break;
+    default:
+	style = PANGO_STYLE_NORMAL;
+	break;
+    }
+	
+    pango_font_description_set_style (svg_cairo->state->font_description,
+				      style);
+#else
     svg_cairo->state->font_style = font_style;
     svg_cairo->state->font_dirty = 1;
+#endif
 
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
@@ -837,8 +866,14 @@
 {
     svg_cairo_t *svg_cairo = closure;
 
+#if HAVE_PANGOCAIRO
+    /* Pango weights are the same as SVG weights */
+    pango_font_description_set_weight (svg_cairo->state->font_description,
+				       font_weight);
+#else
     svg_cairo->state->font_weight = font_weight;
     svg_cairo->state->font_dirty = 1;
+#endif
 
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
@@ -1157,11 +1192,17 @@
     double x, y;
     svg_status_t status;
     svg_paint_t *fill_paint, *stroke_paint;
-
+    double rel_x = 0, rel_y = 0;
+#if HAVE_PANGOCAIRO
+    PangoLayout *layout;
+    PangoLayoutLine *line;
+#endif
+    
     fill_paint = &svg_cairo->state->fill_paint;
     stroke_paint = &svg_cairo->state->stroke_paint;
 
-    _svg_cairo_select_font (svg_cairo);
+    if (utf8 == NULL || *utf8 == '\0')
+	return SVG_STATUS_SUCCESS;
 
     _svg_cairo_length_to_pixel (svg_cairo, x_len, &x);
     _svg_cairo_length_to_pixel (svg_cairo, y_len, &y);
@@ -1170,15 +1211,41 @@
     if (status)
 	return status;
 
+#if HAVE_PANGOCAIRO
+    layout = pango_cairo_create_layout (svg_cairo->cr);
+    pango_layout_set_font_description (layout, svg_cairo->state->font_description);
+    pango_layout_set_text (layout, utf8, -1);
+    line = pango_layout_get_lines (layout)->data;
+#else
+    _svg_cairo_select_font (svg_cairo);
+#endif
+
     if (svg_cairo->state->text_anchor != SVG_TEXT_ANCHOR_START) {
+#if HAVE_PANGOCAIRO
+	PangoRectangle log;
+	pango_layout_get_extents (layout, NULL, &log);
+#else
 	cairo_text_extents_t extents;
 	cairo_text_extents (svg_cairo->cr, utf8, &extents);
+#endif
 
-	if (svg_cairo->state->text_anchor == SVG_TEXT_ANCHOR_END)
-	    cairo_rel_move_to (svg_cairo->cr, -extents.x_advance, -extents.y_advance);
-	else if (svg_cairo->state->text_anchor == SVG_TEXT_ANCHOR_MIDDLE)
-	    cairo_rel_move_to (svg_cairo->cr, -extents.x_advance / 2.0, -extents.y_advance / 2.0);
+	if (svg_cairo->state->text_anchor == SVG_TEXT_ANCHOR_END) {
+#if HAVE_PANGOCAIRO
+	    rel_x = -(double)log.width / PANGO_SCALE;    
+#else
+	    rel_x = -extents.x_advance;
+	    rel_y = -extents.y_advance;
+#endif
+	} else if (svg_cairo->state->text_anchor == SVG_TEXT_ANCHOR_MIDDLE) {
+#if HAVE_PANGOCAIRO
+	    rel_x = -((double)log.width / PANGO_SCALE) / 2.0;
+#else
+	    rel_x = -extents.x_advance / 2.0;
+	    rel_y = -extents.y_advance / 2.0;
+#endif
+	}
     }
+    cairo_rel_move_to (svg_cairo->cr, rel_x, rel_y);
 
     if (fill_paint->type) {
 	if (stroke_paint->type)
@@ -1186,7 +1253,12 @@
 	_svg_cairo_set_paint_and_opacity (svg_cairo, fill_paint,
 					  svg_cairo->state->fill_opacity,
 					  SVG_CAIRO_RENDER_TYPE_FILL);
+	
+#if HAVE_PANGOCAIRO
+	pango_cairo_show_layout_line (svg_cairo->cr, line);
+#else
 	cairo_show_text (svg_cairo->cr, utf8);
+#endif
 	if (stroke_paint->type)
 	    cairo_restore (svg_cairo->cr);
     }
@@ -1195,10 +1267,18 @@
 	_svg_cairo_set_paint_and_opacity (svg_cairo, stroke_paint,
 					  svg_cairo->state->stroke_opacity,
 					  SVG_CAIRO_RENDER_TYPE_STROKE);
+#if HAVE_PANGOCAIRO
+	pango_cairo_layout_line_path (svg_cairo->cr, line);
+#else
 	cairo_text_path (svg_cairo->cr, utf8);
+#endif
 	cairo_stroke (svg_cairo->cr);
     }
 
+#if HAVE_PANGOCAIRO
+    g_object_unref (layout);
+#endif
+
     return _cairo_status_to_svg_status (cairo_status (svg_cairo->cr));
 }
 
@@ -1326,6 +1406,17 @@
 
 #define	DPI	100.0 // This is NOT what we want. How to reach global DPI? (Rob)
 
+static double
+_svg_cairo_text_size (svg_cairo_t *svg_cairo)
+{
+#if HAVE_PANGOCAIRO
+    return ((double) pango_font_description_get_size (svg_cairo->state->font_description)
+	    / PANGO_SCALE);
+#else
+    return svg_cairo->state->font_size;
+#endif
+}
+
 static svg_status_t
 _svg_cairo_length_to_pixel (svg_cairo_t * svg_cairo, svg_length_t *length, double *pixel)
 {
@@ -1351,10 +1442,10 @@
 	*pixel = (length->value / 6.0) * DPI;
 	break;
     case SVG_LENGTH_UNIT_EM:
-	*pixel = length->value * svg_cairo->state->font_size;
+	*pixel = length->value * _svg_cairo_text_size (svg_cairo);
 	break;
     case SVG_LENGTH_UNIT_EX:
-	*pixel = length->value * svg_cairo->state->font_size / 2.0;
+	*pixel = length->value * _svg_cairo_text_size (svg_cairo) / 2.0;
 	break;
     case SVG_LENGTH_UNIT_PCT:
 	if (svg_cairo->state->bbox) {

Index: svg_cairo_sprintf_alloc.c
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/svg_cairo_sprintf_alloc.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- svg_cairo_sprintf_alloc.c	12 Jul 2005 18:46:22 -0000	1.4
+++ svg_cairo_sprintf_alloc.c	12 Jul 2005 19:37:43 -0000	1.5
@@ -21,6 +21,8 @@
 
 #include <stdlib.h>
 #include <stdarg.h>
+#define __USE_ISOC99
+#include <stdio.h>
 
 #include "svg-cairo-internal.h"
 

Index: svg_cairo_state.c
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/svg_cairo_state.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- svg_cairo_state.c	12 Jul 2005 18:46:22 -0000	1.15
+++ svg_cairo_state.c	12 Jul 2005 19:37:43 -0000	1.16
@@ -48,6 +48,9 @@
     state->child_surface = NULL;
     state->saved_cr = NULL;
 
+#if HAVE_PANGOCAIRO
+    state->font_description = pango_font_description_new ();
+#else
     state->font_family = strdup (SVG_CAIRO_FONT_FAMILY_DEFAULT);
     if (state->font_family == NULL)
 	return SVG_CAIRO_STATUS_NO_MEMORY;
@@ -56,6 +59,7 @@
     state->font_style = SVG_FONT_STYLE_NORMAL;
     state->font_weight = 400;
     state->font_dirty = 1;
+#endif
 
     state->dash = NULL;
     state->num_dashes = 0;
@@ -86,8 +90,12 @@
     state->child_surface = NULL;
     state->saved_cr = NULL;
 
+#if HAVE_PANGOCAIRO
+    state->font_description = pango_font_description_copy (other->font_description);
+#else
     if (other->font_family)
 	state->font_family = strdup ((char *) other->font_family);
+#endif
 
     state->viewport_width = other->viewport_width;
     state->viewport_height = other->viewport_height;
@@ -115,10 +123,17 @@
 	state->saved_cr = NULL;
     }
 
+#if HAVE_PANGOCAIRO
+    if (state->font_description) {
+	pango_font_description_free (state->font_description);
+	state->font_description = NULL;
+    }
+#else
     if (state->font_family) {
 	free (state->font_family);
 	state->font_family = NULL;
     }
+#endif
 
     if (state->dash) {
 	free (state->dash);




More information about the cairo-commit mailing list