[cairo] Proposing cairo_set_scaled_font

Behdad Esfahbod behdad at cs.toronto.edu
Tue Jan 31 18:46:17 PST 2006


Hi,

I would like to propose adding the function
cairo_set_scaled_font.  This will set_font_face, set_font_matrix,
and set_font_options on the cairo_t from the cairo_scaled_font_t.

With this patch, PangoCairoFont's can simply keep a
cairo_scaled_t and to install the font, simply call
cairo_set_scaled_font.  Previously pango had to keep the cairo
scaled_font object around, as well as the font_face,
font_options, font_matrix, and ctm.  With the
cairo_scaled_font_get_* functions that went into CVS, it don't
have to keep them anymore, but to install the font, Pango has to
get all those stuff out of the scaled_font, and set them on the
cairo_t.  While that does the job, it's much cleaner and more
intuitive to be able to set an scaled_font directly on the
cairo_t.

Patch attached.  The patch also includes the fix for bitmap fonts
crash.  This patch satisfies all Pango's need for the upcoming
GNOME 2.14 release.


Thanks,

--behdad
http://behdad.org/

"Commandment Three says Do Not Kill, Amendment Two says Blood Will Spill"
	-- Dan Bern, "New American Language"
-------------- next part --------------
Index: src/cairo-ft-font.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo-ft-font.c,v
retrieving revision 1.115
diff -u -p -r1.115 cairo-ft-font.c
--- src/cairo-ft-font.c	21 Dec 2005 16:19:47 -0000	1.115
+++ src/cairo-ft-font.c	29 Jan 2006 15:58:36 -0000
@@ -715,12 +715,7 @@ _get_bitmap_surface (FT_Bitmap		     *bi
     width = bitmap->width;
     height = bitmap->rows;
     
-    if (width * height == 0) {
-	if (own_buffer && bitmap->buffer)
-	    free (bitmap->buffer);
-	
-	*surface = NULL;
-    } else {
+    {
 	switch (bitmap->pixel_mode) {
 	case FT_PIXEL_MODE_MONO:
 	    stride = (((width + 31) & ~31) >> 3);
@@ -1074,7 +1069,9 @@ _render_glyph_bitmap (FT_Face		      fac
     if (error)
 	return CAIRO_STATUS_NO_MEMORY;
 
-    _get_bitmap_surface (&glyphslot->bitmap, FALSE, font_options, surface);
+    status = _get_bitmap_surface (&glyphslot->bitmap, FALSE, font_options, surface);
+    if (status)
+	return status;
     
     /*
      * Note: the font's coordinate system is upside down from ours, so the
Index: src/cairo.c
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.c,v
retrieving revision 1.138
diff -u -p -r1.138 cairo.c
--- src/cairo.c	18 Jan 2006 17:46:42 -0000	1.138
+++ src/cairo.c	29 Jan 2006 15:58:37 -0000
@@ -1979,6 +1979,45 @@ cairo_get_font_options (cairo_t         
 }
 
 /**
+ * cairo_set_scaled_font:
+ * @cr: a #cairo_t
+ * @scaled_font: a #cairo_scaled_font_t
+ *
+ * Replaces the current font face, font matrix, and font options in
+ * the #cairo_t with those of the #cairo_scaled_font_t.  The current
+ * CTM of the #cairo_t should be the same as that of the
+ * #cairo_scaled_font_t except for some translation.
+ **/
+void
+cairo_set_scaled_font (cairo_t                   *cr,
+		       const cairo_scaled_font_t *scaled_font)
+{
+    if (cr->status)
+	return;
+
+    cr->status = scaled_font->status;
+    if (cr->status)
+        goto BAIL;
+
+    cr->status = _cairo_gstate_set_font_face (cr->gstate, scaled_font->font_face);  
+    if (cr->status)
+        goto BAIL;
+
+    cr->status = _cairo_gstate_set_font_matrix (cr->gstate, &scaled_font->font_matrix);
+    if (cr->status)
+        goto BAIL;
+
+    cr->status = _cairo_gstate_set_font_options (cr->gstate, &scaled_font->options);
+    if (cr->status)
+        goto BAIL;
+
+    return;
+
+BAIL:
+    _cairo_set_error (cr, cr->status);
+}
+
+/**
  * cairo_text_extents:
  * @cr: a #cairo_t
  * @utf8: a string of text, encoded in UTF-8
Index: src/cairo.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.h,v
retrieving revision 1.157
diff -u -p -r1.157 cairo.h
--- src/cairo.h	22 Jan 2006 10:33:26 -0000	1.157
+++ src/cairo.h	29 Jan 2006 15:58:37 -0000
@@ -852,6 +852,10 @@ cairo_public void
 cairo_get_font_options (cairo_t              *cr,
 			cairo_font_options_t *options);
 
+cairo_public void
+cairo_set_scaled_font (cairo_t                   *cr,
+		       const cairo_scaled_font_t *scaled_font);
+
 cairo_public void
 cairo_show_text (cairo_t *cr, const char *utf8);
 


More information about the cairo mailing list