[cairo] Font extents mysteries
rael at edge.ping.de
Wed May 9 13:45:43 PDT 2007
I'm having some strange effects with font extents using the Toy API
What I'm doing?
I'm creating a cairo_scaled_font_t by first selecting a font face
where name is an std::string containing a font name like "Nimbus Sans L".
After that I do something like:
Where scaleMatrix is initialized using cairo_matrix_init_scale to sclae
the font in horizontal and vertical dirction by the same factor
(pixelHeight*DPI) and transformMatrix is the result of cairo_get_matrix
(I never explicitly change the default matrix).
The resulting cairo_scaled_font_t is stored for later use.
Later on I calculate the bounds of a text string by doing something like
where buffer is the std::string in question.
After that I calculate the horizontal bounds by assuming that
where "left" is the space between origin and the first pixel of the
first character, "right" is the space after the last letter and "width"
is the overall size of the string (including the amounts for left and
right). The resulting horizontal bounding box would then be something like:
<"left" pixel nothing"><real displayed text of width "width"><"right"
Based on this I later on draw the string in a X11 window by doing
(b again is a std::string holding the text in question)
I also use the values of the text extents to draw a grid marking the
bounds of the text. The problem is, that the text exactly fits the grid
in *most* cases - but not for all!
Something like "Homtitompti" or "Illumination" works, but "Günther",
"Geld" or "d" does not. In the case where the bounds do not exactly
match the last letter extents is bounding box to the right filling the
space claimed to be reserved for space after the last letter by one or
even two pixel. The starting pixel (that means the value of "left")
however always seems to match exactly. I'm using an effective pixel
height for the font of 52 pixel.
Am I doing something wrong? Is there an error in my code or am I basing
by code on wrong assumptions?
More information about the cairo