# Cairo::UserFontFace Class Reference

Font support with font data provided by the user. More...

Inheritance diagram for Cairo::UserFontFace:
[legend]

List of all members.

## Public Member Functions

virtual | ~UserFontFace ()

## Protected Member Functions

virtual ErrorStatus | init (const RefPtr< ScaledFont >& scaled_font, const RefPtr< Context >& cr, FontExtents& extents)
| This function is called when a scaled-font needs to be created for a user font-face.
virtual ErrorStatus | unicode_to_glyph (const RefPtr< ScaledFont >& scaled_font, unsigned long unicode, unsigned long& glyph)
| This function is called to convert an input Unicode character to a single glyph.
virtual ErrorStatus | render_glyph (const RefPtr< ScaledFont >& scaled_font, unsigned long glyph, const RefPtr< Context >& cr, TextExtents& metrics)=0
| This function is called when a user scaled-font needs to render a glyph.
virtual ErrorStatus | text_to_glyphs (const RefPtr< ScaledFont >& scaled_font, const std::string& utf8, std::vector< Glyph >& glyphs, std::vector< TextCluster >& clusters, TextClusterFlags& cluster_flags)
| This function is called to convert input text to an array of glyphs.
| UserFontFace ()

## Detailed Description

Font support with font data provided by the user.

The user-font feature allows the cairo user to provide drawings for glyphs in a font. This is most useful in implementing fonts in non-standard formats, like SVG fonts and Flash fonts, but can also be used by games and other application to draw "funky" fonts.

To use user fonts, you must derive from this class and implement the virtual functions below. The only virtual function that absolutely must be implemented is render_glyph(). You should make the constructor protected and provide a factory function that returns a new object in a RefPtr since it is a refcounted object

     class MyUserFont : public UserFontFace {

     public:
       static Cairo::RefPtr<MyUserFont> create() {
         return Cairo::RefPtr<MyUserFont>(new MyUserFont);

     protected:
       // implement render_glyph() and any other virtual functions you want to override
       ErrorStatus render_glyph(const RefPtr<ScaledFont>& scaled_font,
                                unsigned long glyph,
                                const RefPtr<Context>& cr,
                                TextExtents& metrics) {
         // render the glyph into cr here
       }

       MyUserFont() : UserFontFace() {
         // constructor implementation
       }
     };

Warning:
  Because of a design flaw in cairomm, it is currently necessary to keep the the UserFontFace object around until as long as you are rendering text with the user font. The following code illustrates the issue:

     {
       Cairo::RefPtr<MyUserFont> face = MyUserFont::create();
       cr->set_font_face(face);
     } // scope for demonstration purposes

     // the following call will cause a crash because your user font is no longer
     // in scope but it needs to call the virtual functions in face
     cr->show_text("hello, world");

The preceding is obviously a very contrived example, but the important thing to know is that you *must* cache all userfont objects yourself as long as you intend to render text with that font. A future release of cairomm will fix this requirement, but that will require ABI-incompatible changes.

Since:
  1.8

## Constructor & Destructor Documentation

virtual Cairo::UserFontFace::~UserFontFace | ( |  | ) | `[virtual]`

Cairo::UserFontFace::UserFontFace | ( |  | ) | `[protected]`

## Member Function Documentation

virtual ErrorStatus Cairo::UserFontFace::init | ( | const RefPtr< ScaledFont >& | scaled_font,
|  | const RefPtr< Context >& | cr,
|  | FontExtents& | extents
| ) |  | `[protected, virtual]`

This function is called when a scaled-font needs to be created for a user font-face.

The Context cr is not used by the caller, but is prepared in font space, similar to what the cairo contexts passed to the render_glyph method will look like. The callback can use this context for extents computation for example. After the callback is called, cr is checked for any error status.

The extents argument is where the user font sets the font extents for scaled_font. It is in font space, which means that for most cases its ascent and descent members should add to 1.0. extents is preset to hold a value of 1.0 for ascent, height, and max_x_advance, and 0.0 for descent and max_y_advance members.

The default implementation sets the font extents as described in the previous paragraph. If you need different extents, you can override this function in your derived class.

Note that scaled_font is not fully initialized at this point and trying to use it for text operations in the callback will result in deadlock.

Parameters:

  scaled_font | the scaled-font being created
  cr | cairo context, in font space
  extents | extents to fill in, in font space

Returns:
  CAIRO_STATUS_SUCCESS upon success, or CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.

Since:
  1.8

virtual ErrorStatus Cairo::UserFontFace::render_glyph | ( | const RefPtr< ScaledFont >& | scaled_font,
|  | unsigned long | glyph,
|  | const RefPtr< Context >& | cr,
|  | TextExtents& | metrics
| ) |  | `[protected, pure virtual]`

This function is called when a user scaled-font needs to render a glyph.

You must implement this in your derived class, and it is expected to draw the glyph with code glyph to the Context cr. cr is prepared such that the glyph drawing is done in font space. That is, the matrix set on cr is the scale matrix of scaled_font, The extents argument is where the user font sets the font extents for scaled_font. However, if user prefers to draw in user space, they can achieve that by changing the matrix on cr. All cairo rendering operations to cr are permitted, however, the result is undefined if any source other than the default source on cr is used. That means, glyph bitmaps should be rendered using Context::mask() instead of Context::paint().

Other non-default settings on cr include a font size of 1.0 (given that it is set up to be in font space), and font options corresponding to scaled_font.

The extents argument is preset to have x_bearing, width, and y_advance of zero, y_bearing set to -font_extents.ascent, height to font_extents.ascent+font_extents.descent, and x_advance to font_extents.max_x_advance. The only field user needs to set in majority of cases is x_advance. If the width field is zero upon this function returning (which is its preset value), the glyph extents are automatically computed based on the drawings done to cr. This is in most cases exactly what the desired behavior is. However, if for any reason this function sets the extents, it must be ink extents, and include the extents of all drawing done to cr.

Parameters:

  scaled_font | user scaled-font
  glyph | glyph code to render
  cr | Context to draw to, in font space
  extents | glyph extents to fill in, in font space

Returns:
  CAIRO_STATUS_SUCCESS upon success, or CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.

Since:
  1.8

virtual ErrorStatus Cairo::UserFontFace::text_to_glyphs | ( | const RefPtr< ScaledFont >& | scaled_font,
|  | const std::string & | utf8,
|  | std::vector< Glyph >& | glyphs,
|  | std::vector< TextCluster >& | clusters,
|  | TextClusterFlags& | cluster_flags
| ) |  | `[protected, virtual]`

This function is called to convert input text to an array of glyphs.

This is used by the Context::show_text() operation.

Using this function, the user-font has full control on glyphs and their positions. That means, it allows for features like ligatures and kerning, as well as complex shaping required for scripts like Arabic and Indic.

This function should populate the glyph indices and positions (in font space) assuming that the text is to be shown at the origin.

If clusters is not empty, cluster mapping should be computed.

If you do not override this virtual function in your derived class, the unicode_to_glyph function is used instead.

Note: While cairo does not impose any limitation on glyph indices, some applications may assume that a glyph index fits in a 16-bit unsigned integer. As such, it is advised that user-fonts keep their glyphs in the 0 to 65535 range. Furthermore, some applications may assume that glyph 0 is a special glyph-not-found glyph. User-fonts are advised to use glyph 0 for such purposes and do not use that glyph value for other purposes.

Parameters:

  scaled_font | the scaled-font being created
  utf8 | a string of text encoded in UTF-8
  glyphs | array of glyphs to fill, in font space
  clusters | array of cluster mapping information to fill
  cluster_flags | a variable to set the cluster flags corresponding to the output clusters

Returns:
  CAIRO_STATUS_SUCCESS upon success, or CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.

Since:
  1.8

virtual ErrorStatus Cairo::UserFontFace::unicode_to_glyph | ( | const RefPtr< ScaledFont >& | scaled_font,
|  | unsigned long | unicode,
|  | unsigned long & | glyph
| ) |  | `[protected, virtual]`

This function is called to convert an input Unicode character to a single glyph.

This is used by the Context::show_text() operation.

This function is used to provide the same functionality as the text_to_glyphs callback does but has much less control on the output, in exchange for increased ease of use. The inherent assumption to using this callback is that each character maps to one glyph, and that the mapping is context independent. It also assumes that glyphs are positioned according to their advance width. These mean no ligatures, kerning, or complex scripts can be implemented using this callback.

The default implementation of this function is an identity mapping from Unicode code-points to glyph indices. If you need different behavior, you may override this virtual function in your derived class.

Note: While cairo does not impose any limitation on glyph indices, some applications may assume that a glyph index fits in a 16-bit unsigned integer. As such, it is advised that user-fonts keep their glyphs in the 0 to 65535 range. Furthermore, some applications may assume that glyph 0 is a special glyph-not-found glyph. User-fonts are advised to use glyph 0 for such purposes and do not use that glyph value for other purposes.

Parameters:

  scaled_font | the scaled-font being created
  unicode | input unicode character code-point
  glyph_index | output glyph index

Returns:
  CAIRO_STATUS_SUCCESS upon success, or CAIRO_STATUS_USER_FONT_ERROR or any other error status on error.

Since:
  1.8

The documentation for this class was generated from the following file:

- cairomm/fontface.h