[cairo] Font metrics problem?

Richard Cole rcole at itee.uq.edu.au
Tue Oct 5 17:49:35 PDT 2004


I'm having trouble making sense out of the font metrics I'm getting from 
Cairo. I appologise that the following code is in Ocaml.

I have trouble these days going back to languages like C. I'll try to 
explain what its doing. The major thing to know about Ocaml is that 
functions don't have brackets, i.e. "insert(x,S)" is written "insert x 
S", the Some and None business is basically pointers being Null or not 
Null, the function "some x" derefences x and throws and exception if x 
is Null. Oh and method calls are marked with a '#' rather than a '.', 
i.e. self#select_font rather than self.select_font. The '.' is used to 
signify namespace, i.e. all that Cairo.something stuff used is access 
something in the Cairo package.

class text =
 val mutable text = "Hello"
 val mutable text_extents = None
 val mutable font_extents = None
 val mutable font_family = "Times"
 val mutable font_slant = Cairo.FONT_SLANT_NORMAL
 val mutable font_weight = Cairo.FONT_WEIGHT_NORMAL
 val mutable font_scale = 12.

 val mutable redraw_broker : redraw_event broker option = None


 method select_font cr =
   Cairo.select_font cr font_family font_slant font_weight;
   Cairo.scale_font cr font_scale;

 method update_rect cr =
   match text_extents, font_extents with
     | Some(t), Some(e) -> ()
     | _ ->
         Cairo.save cr;
         self#select_font cr;
         text_extents <- Some(Cairo.text_extents cr text);
         font_extents <- Some(Cairo.current_font_extents cr);
         Cairo.restore cr;
         rect <- {
           rect with
             width  = (some text_extents).text_width;
             height = (some font_extents).font_height;
         fprintf stderr "Font size=%f, height=%f, ascent=%f\n"
           (some font_extents).font_height;
           (some font_extents).ascent
         flush stderr;
         broker#fire_event ResizeRect;


So basically the update_rect method gets the text_extents and the 
font_extents from Cairo. The font is set to 12pt Roman Normal Normal. 
But the font metric returned has a height of 24.857011 and an ascent of 
19.262864. This seems way to large and when I draw a rectangle of these 
dimentions the box looks very much too big.

Are these results to be expected from the current version of Cairo? I'm 
using the current HEAD of the cvs repository "Tue 5th Oct, 23:40 EST 
(Brisbane, Australia Time)".

Are there any tests of the font metrics? I couldn't find any samples 
that draw a font metrics determined bounding box around the text.

Oh and one more thing. I think there was some discussion previously 
about making font metrics indepedent of the display context. I'd like to 
echo calls for this. When I'm trying to position things like text I need 
to know how high the text is and how wide a sequence of glyphs is, but 
being in logical co-ordinate space these values seem independent of the 
display context.

On the other hand, the actual pixels accessed by the drawing functions 
are resolution dependent and I think someone said true-type fonts are 
also resolution sensitive. Even so it would be nice to have a best guess 
independent of the display context and perhaps exact pixel related 
information dependent on the display context, i.e. two interfaces to 
font metrics.

I've seen programs written using the Java 2D API that had large errors 
in the font metrics. I wonder if this is because resolution dependent 
differences might be small on the glyph level but they add up on 
paragraph level and become quite noticable. Does anyone have information 
about this?



More information about the cairo mailing list