[cairo] user-font proxies

Behdad Esfahbod behdad at behdad.org
Sat May 24 22:30:16 PDT 2008


Hi,

I added the test user-font-proxy that simply uses the default cairo toy
font to show text.  The render_glyph part is as beautiful as I wanted it
to, except for one catch:

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 *extents)
{
    char text[2] = "\0";

    /* XXX only works for ASCII.  need ucs4_to_utf8 :( */
    text[0] = glyph;

    cairo_set_scaled_font (cr,
                           cairo_scaled_font_get_user_data (scaled_font,
                                                            &fallback_scaled_font_key));
    cairo_show_text (cr, text);
    cairo_text_extents (cr, text, extents);

    return CAIRO_STATUS_SUCCESS;
}

Ughh. We get ucs4, but need UTF-8.  That's because we don't have
cairo_scaled_font_text_to_glyph() exposed publicly.  Should we finally
add that?  With user fonts that makes some sense now as it's not
necessarily toy anymore.


The create part was uglier:

static cairo_status_t
test_scaled_font_init (cairo_scaled_font_t  *scaled_font,
                       cairo_font_extents_t *extents)
{
  cairo_t *cr;
  cairo_surface_t *surface;
  cairo_matrix_t ctm;
  cairo_font_options_t *font_options;
  cairo_scaled_font_t *fallback_scaled_font;

  /* painful way to get default font face used by toy api */
  surface = cairo_image_surface_create (CAIRO_FORMAT_A8, 0, 0);
  cr = cairo_create (surface);
  cairo_surface_destroy (surface);

  cairo_set_font_size (cr, 1.);
  cairo_scaled_font_get_scale_matrix (scaled_font, &ctm);
  cairo_set_matrix (cr, &ctm);

  font_options = cairo_font_options_create ();
  cairo_scaled_font_get_font_options (scaled_font, font_options);
  cairo_set_font_options (cr, font_options);
  cairo_font_options_destroy (font_options);

  fallback_scaled_font = cairo_scaled_font_reference (cairo_get_scaled_font (cr)),
  cairo_scaled_font_set_user_data (scaled_font,
                                   &fallback_scaled_font_key,
                                   fallback_scaled_font,
                                   cairo_scaled_font_destroy);

  cairo_destroy (cr);

  cairo_scaled_font_extents (fallback_scaled_font, extents);

  return CAIRO_STATUS_SUCCESS;
}

Still not too bad if we correctly document the API and provide samples.
Most of the pain is in getting the default toy font face without having
a cairo_t around.

I fixed fontmap locking so this works now.

Next, add twin's stroke font to user.
Next next, write a Pango user font.

-- 
behdad
http://behdad.org/

"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759



More information about the cairo mailing list