[Mono-list] Text on Cairo

Ian Norton inb at ncipher.com
Tue Nov 16 15:46:19 EST 2010


Hi Francisco,

As far as I know, text extends are to work out where the boundaries of
your text will be after you write it, this is useful if you want to put
several strings next to each other ( eg, write the next word or next
line ). You would use the extends to find out how many cairo pixels tall
the string would be.

If you just want to print one string of text starting at a particular
location on screen and don't need to know how wide or tall it ends up
then you can safely do this:-

public void StringAt( Context ctx, string str, int x, int y )
{
  ctx.NewPath();
  ctx.Color = new Color( 0, 0, 0, 1 );
  ctx.SetFontSize(12);
  ctx.MoveTo(x,y);
  ctx.ShowText(str);  
}


If you wanted to use extents to find the boundaries of your text you
might do this:

public void StringsInRows( Context ctx, string[] strlist, int x, int y )
{
  var next_y = y;
  // print strings in lines
  foreach ( var str in strlist ){
    ctx.NewPath();
    ctx.Color = new Color( 0, 0, 0, 1 );
    ctx.SetFontSize(12); 
    ctx.MoveTo(x,next_y);

    var extent = ctx.TextExtents(str);
    next_y += (extent.Height + 5); // next line will be 5px under this
line
    ctx.ShowText(str);  
  }

}

Hope that helps

Regards

Ian

On Tue, 2010-11-16 at 17:21 +0000, Francisco M. Marzoa wrote:
> Hello there,
> 
> I'm currently fighting with Cairo's text system, and I should to admit
> that I've a slight headache...
> 
> The main problem that I've is that, for coherence, I would like to
> specify a point (as a pair of x,y coords) and the text should be drawn
> taken that point like the top-left point of the text.
> 
> I think I must use TextExtents and/or FontExtents for this purpouse, but
> I didn't figure out HOW.
> 
> There's an explanation in this tutorial:
> 
> http://www.mono-project.com/Mono.Cairo_Tutorial#Understanding_Text
> 
> But unfortunatly the code is missing.
> 
> Does someone has some code on this issue that he/she dont mind to share???
> 
> Thanks a lot in advance,
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list




More information about the Mono-list mailing list