1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
//
// to get the font metrics
//
PangoFontMetrics * MY_GetFontMetrics( Display * display,
PangoFontDescription * font )
{
PangoFontMetrics * metrics = 0;
PangoFontMap * fontmap = pango_cairo_font_map_get_default(); // this doesn't work (returns null)
if( fontmap )
{
PangoContext * context
= pango_cairo_font_map_create_context( (PangoCairoFontMap *)fontmap );
if( context )
{
pango_context_set_font_description( context, font );
PangoLanguage * lang = pango_language_from_string( "en_US" );
metrics = (PangoFontMetrics *)pango_context_get_metrics( context, font, lang );
g_object_unref( context );
}
}
return metrics;
}
//
// to render a string (works)
//
void MY_RenderString( Display * display, Drawable d, GC gc,
PangoFontDescription * font,
int x, int y,
char * string, int length )
{
// cairo surface dimensions (max)
XRectangle ink, logical;
MY_TextExtents( string, length, font, &ink, &logical ); // see note below about this
const int SIZEX = logical.width;
const int SIZEY = logical.height;
// create a cairo surface
cairo_surface_t * cs = cairo_xlib_surface_create( display, d,
DefaultVisual( display, 0 ),
SIZEX, SIZEY );
if( cs )
{
cairo_t * cr = cairo_create( cs );
if( cr )
{
cairo_translate( cr, x, y );
PangoLayout * layout = pango_cairo_create_layout( cr );
if( layout )
{
pango_layout_set_text( layout, string, length );
pango_layout_set_font_description( layout, font );
// the function to get the text extents basically does the same
// thing as this function to get a surface, context, and layout then calls this:
// pango_layout_get_extents( layout, &ink_rect, &logical_rect );
// extract the foreground Pixel from the GC
XGCValues gcv;
XGetGCValues( display, gc, GCForeground, &gcv );
XColor xcolor;
xcolor.pixel = gcv.foreground;
// convert the Pixel to RGB values
Colormap cmap = DefaultColormap( display, 0 );
XQueryColor( display, cmap, &xcolor );
// set the source RGB in cairo
cairo_set_source_rgb( cr, xcolor.red / (float)0xFFFF,
xcolor.green / (float)0xFFFF,
xcolor.blue / (float)0xFFFF );
pango_cairo_update_layout( cr, layout );
pango_cairo_show_layout( cr, layout );
g_object_unref( layout );
}
cairo_destroy( cr );
}
}
cairo_surface_destroy( cs );
}
|