1 px difference on render

Nov 11, 2009 at 3:51 PM

hi,

when using:

System.Drawing.Html.HtmlRenderer.Render(
                        g,
                        "<html><head></head><body>$<font style=\"font:24px Verdana; color:red;\">99,457</font>.55</b></body></html>",
                        new RectangleF(new PointF(0.0F, 0.0F),new SizeF(500.0F,500.0F)),
                        false);

the image generated has the following problem: the larger text part's baseline (99,457) is placed 1 or 2 pixels above the smaller text baseline.

i myself tried to figure out a way to equalize the baselines for all sizes, with no success.

i've rounded the resulting size up and down, tried to extract some consistent logic for adding or subtracting pixels - no success.  do u have any idea of how to solve that?

the 1 px mismatch drives me crazy.

i thought of working with the web-browser control, grabbing the non-transparent region. the baseline problem wont happen there, but then i remembered that there will be no text antialiasing there.

this is where i am out of ideas.

this is the fn, which causes the iritating baseline diff:

private static SizeF Get_concatSize(String s, Font f, StringFormat sformat, Graphics g)
        {
            SizeF size = new SizeF();

            if (String.IsNullOrEmpty(s)) return size;
            
            CharacterRange[] characterRanges = {
                new CharacterRange(0, s.Length) };

            StringFormat sf = new StringFormat(sformat);

            sf.SetMeasurableCharacterRanges(characterRanges);

            // Measure whole string.
            Region[] stringRegions = new Region[1];
            
            stringRegions = g.MeasureCharacterRanges(
                s, f, new RectangleF(), sf);

            RectangleF measureRect = stringRegions[0].GetBounds(g);

            Single fontHeight = f.GetHeight(g);
            Single fontAscentHeight =
                (fontHeight / f.FontFamily.GetLineSpacing(f.Style)) *
                f.FontFamily.GetCellAscent(f.Style);

            size.Width = measureRect.Width;
            size.Height = fontAscentHeight;

            return size;
        }