Hello,

On Wed, 11 Mar 2009 21:00:40 +0100, Ulrich Telle <ulrich.te...@gmx.de>
wrote:
> 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;

Thanks, I already spotted that error :)

> The y position corresponds to the baseline of the font. T

I expect you to correct me again :) : I tryed to see if the baseline is
effectively
the Y value passed to wxPdfDocument::SetXY/SetY funtions this way:

void BalancerPanel::drawText (wxPdfDocument& pPDF, wxString const&
pString, Vector2f const& pCenter)
{
     //
     // Draw the text with a Cell that has the upperleft border at
(pCenter.x, pCenter.y)
     float lAscent = pPDF.GetFontDescription ().GetAscent ();
     float lDescent = pPDF.GetFontDescription ().GetDescent ();
     float lMaximumHeight = (lAscent - lDescent) / 1000.f *
pPDF.GetFontSize() / 72.f * 25.4f;
     pPDF.SetXY (pCenter.x, pCenter.y);
     pPDF.Cell (0, lMaximumHeight, pString, wxPDF_BORDER_FRAME);//Write
around the text a frame with an height
     //equal to the maximum height of a character of the font.

     //
     //Draw the potential baseline in red.
     wxPdfColour lColor = pPDF.GetDrawColor ();
     pPDF.SetDrawColor (255, 0, 0);
     pPDF.Line (pCenter.x, pCenter.y, pCenter.x + 10, pCenter.y);
     pPDF.SetDrawColor (lColor);
}

Then I called the above function with:

drawText (lPDFDoc, "BagjYIq", Vector2f (10, 10));

which should draw the text with a baseline at 10mm from the start of the
page, but as you cound see in the image
http://img22.imageshack.us/img22/3844/baseline.png
the red line is along the top border of the cell, so a
call to SetY
sets the baseline-ascent Y value, i.e. the upper limit of the text's font,
and not the baseline.

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

Knowing that the curent position (the one set with SetXY) is the upperleft
corner of the text bounding box,
then if "offset" is the the maximum height of the font divided by 2, then
i would draw some text centered
to pCenter with the following code:

void drawCenteredText (wxPdfDocument& pPDF, wxString const& pString,
Vector2f const& pCenter)
{
     //
     // Draw a Cell with text centered to pCenter.
     float lAscent = pPDF.GetFontDescription ().GetAscent ();
     float lDescent = pPDF.GetFontDescription ().GetDescent ();
     float lMaximumHeight = (lAscent - lDescent) / 1000.f *
pPDF.GetFontSize() / 72.f * 25.4f;
     double lWidthMM = pPDF.GetStringWidth (pString);//Width in mm.
     pPDF.SetXY (pCenter.x - lWidthMM / 2.f, pCenter.y - lMaximumHeight /
2.f);//"offset" is basically lMaximumHeight/2
     pPDF.Cell (0, lMaximumHeight, pString, wxPDF_BORDER_FRAME);

     //
     //Draw the potential baseline in red.
     wxPdfColour lColor = pPDF.GetDrawColor ();
     pPDF.SetDrawColor (255, 0, 0);
     pPDF.Line (pCenter.x, pCenter.y, pCenter.x + 10, pCenter.y);
     pPDF.SetDrawColor (lColor);
}

drawCenteredText (lPDFDoc, "BagjYIq", Vector2f (10, 10));

Using the above function gives the passed text string centered to (10mm,
10mm) as you could see in the
http://img22.imageshack.us/img22/8194/textcentered.png
image.

Again correct me if I am wrong, thanks!
Luca

------------------------------------------------------------------------------
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