A relatively simple example of using Cairo::UserFontFace
#include <cairomm/cairomm.h>
 
const double HEIGHT = 200.0;
const double WIDTH = 400.0;
const double FONT_SIZE = 64.0;
const double TEXT_ORIGIN_Y = (HEIGHT / 2.0) + (FONT_SIZE / 2.0);
const double TEXT_ORIGIN_X = 50.0;  
const double GLYPH_SPACING = 0.1;
 
struct GlyphBounds
{
  unsigned long glyph;
  double width;
  double height;
};
 
static const GlyphBounds glyphs[] =
{
  { 'c', 0.45, 0.5 },
  { 'a', 0.45, 0.5 },
  { 'i', 0.2, 0.75 },
  { 'r', 0.4, 0.5 },
  { 'o', 0.44, 0.5 },
  { 'm', 0.75, 0.5 },
  { '!', 0.2, 0.75 }
};
 
{
public:
  
  
  {
  }
 
  Cairo::ErrorStatus
  {
    for (unsigned int i = 0; i < sizeof (glyphs) / sizeof (GlyphBounds); ++i) {
      if (glyphs[i].width > 
max)
 
    }
    
    extents.max_x_advance = 
max;
    return CAIRO_STATUS_SUCCESS;
  }
 
  Cairo::ErrorStatus
                    unsigned long unicode, unsigned long& glyph) override
  {
    glyph = 0;
    
    
    for (unsigned int i = 0; i < sizeof (glyphs) / sizeof (GlyphBounds); ++i) {
      if (glyphs[i].glyph == unicode) {
        
        glyph = i+1;
        break;
      }
    }
    return CAIRO_STATUS_SUCCESS;
  }
 
  Cairo::ErrorStatus
               unsigned long glyph,
  {
    
    if (glyph >= 1 && glyph <= sizeof(glyphs)/sizeof(GlyphBounds)) {
      
      
      cr->
rectangle(0.0, 0.0, glyphs[glyph-1].width, -glyphs[glyph-1].height);
      metrics.x_advance = glyphs[glyph-1].width + GLYPH_SPACING;
    }
    return CAIRO_STATUS_SUCCESS;
  }
 
protected:
  
  
  BoxFontFace() : UserFontFace() { }
};
 
int main(int, char**)
{
  auto surface =
  
 
  
  cr->
arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2*M_PI);
 
  
  cr->
move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
  auto font = BoxFontFace::create();
 
  
  cr->
move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y);
  auto toy_font =
 
  const char* filename = "user-font.png";
  try {
    surface->write_to_png(filename);
    return 0;
  {
    return 1;
  }
}
 
void arc(double xc, double yc, double radius, double angle1, double angle2)
Adds a circular arc of the given radius to the current path.
static RefPtr< ImageSurface > create(Format format, int width, int height)
Creates an image surface of the specified format and dimensions.
@ FORMAT_ARGB32
Each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green,...
Definition: enums.h:347
void set_source_rgb(double red, double green, double blue)
Sets the source pattern within the Context to an opaque color.
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:45
void set_font_face(const RefPtr< const FontFace > &font_face)
Replaces the current font face in the context with font_face font_face.
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 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.
Font support with font data provided by the user.
Definition: fontface.h:208
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.
cairo_text_extents_t TextExtents
See the cairo_text_extents_t reference in the cairo manual for more information.
Definition: types.h:48
void rectangle(double x, double y, double width, double height)
Adds a closed-subpath rectangle of the given size to the current path at position (x,...
void show_text(const std::string &utf8)
A drawing operator that generates the shape from a string of UTF-8 characters, rendered according to ...
static RefPtr< ToyFontFace > create(const std::string &family, FontSlant slant, FontWeight weight)
Creates a font face from a triplet of family, slant, and weight.
void stroke()
A drawing operator that strokes the current Path according to the current line width,...
static RefPtr< Context > create(const RefPtr< Surface > &target)
cairo_font_extents_t FontExtents
See the cairo_font_extents_t reference in the cairo manual for more information.
Definition: types.h:42
void move_to(double x, double y)
If the current subpath is not empty, begin a new subpath.
void set_source_rgba(double red, double green, double blue, double alpha)
Sets the source pattern within the Context to a translucent color.
void set_font_size(double size)
Sets the current font matrix to a scale by a factor of size, replacing any font matrix previously set...
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
@ FONT_SLANT_NORMAL
Upright font style.
Definition: enums.h:244
void fill()
A drawing operator that fills the current path according to the current fill rule,...
void set_line_width(double width)
Sets the current line width within the cairo Context.
@ FONT_WEIGHT_BOLD
Bold font weight.
Definition: enums.h:270
void paint()
A drawing operator that paints the current source everywhere within the current clip region.