Luca Cappa schrieb:
> I wrote a simple function which draws a string with the its bounding box  
> centered to the given pCenter parameter:
> 
> void drawCenteredText (wxPdfDocument& pPDF, wxString const& pString,  
> Vector2f const& pCenter)
> {
>    float lHeightMM = float (pPDF.GetFontSize ()) / 72.f * 2.54f / 10.f;
>    //Height in mm.

No. Your formula is wrong. To get the height in millimeters it should read:

float lHeightMM = float (pPDF.GetFontSize ()) / 72.f * 25.4f;

>    double lWidthMM = pPDF.GetStringWidth (pString);//Width in mm.
>    pPDF.SetXY (pCenter.x - lWidthMM / 2.f, pCenter.y - lHeightMM / 2.f);
>    pPDF.Cell (0, 0, pString);
> }
> 
> To roughly center the string I need to know how much the actual font  
> height is in millimeters, and for that purpose i use a formula that  
> converts points to mm, assuming that a point is 1/72 inch, and then I  
> convert inch to mm. Is there any better way than this approach?

Well, it depends on what you really want to accomplish.

The y position corresponds to the baseline of the font. The glyphs of a 
font usually ascend above and/or descend below the baseline. The font 
parameters 'ascent' and 'descent' tell you the maximum values. You may 
access these attributes using the method GetFontDescription. From these 
values you could deduce a more precise measure for the mean vertical 
'center' of the glyphs of the font in use.

To convert the values of ascent and descent to millimeters you have to 
divide them by 1000 and then multiply by the font size in millimeters. 
As the offset you would then use

offset = (ascent-descent) / 2000.f * pPDF.GetFontSize() / 72.f * 25.4f;

That is you would set your vertical position to pCenter.y + offset.

Regards,

Ulrich


------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
wxCode-users mailing list
wxCode-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxcode-users

Reply via email to