PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Performance improvement in XGraphics.MeasureString()
http://forum.pdfsharp.com/viewtopic.php?f=4&t=779
Page 1 of 1

Author:  robert_baumann [ Tue Jul 07, 2009 12:32 am ]
Post subject:  Performance improvement in XGraphics.MeasureString()

XGraphics.MeasureString() uses a FormattedText object to measure string width. This is expensive because the FormattedText creates a drawing context underneath the scenes, for any unique text string that gets drawn. A better method is to use the GlyphTypeface method, such a what this blog entry does:

http://incrediblejourneysintotheknown.b ... forth.html

When rendering many pages, this performance is improved significantly by using the following code fix in XGraphics.cs (3213):

#if WPF && !GDI
/* OLD CODE:

FormattedText formattedText = new FormattedText(text, new CultureInfo("en-us"),
FlowDirection.LeftToRight, font.typeface, font.Height, System.Windows.Media.Brushes.Black);
return new XSize(formattedText.WidthIncludingTrailingWhitespace, formattedText.Height);
* */

GlyphTypeface gTypeface;
if (!font.typeface.TryGetGlyphTypeface(out gTypeface))
throw new InvalidOperationException("No glyphtypeface found");
ushort[] glyphIndexes = new ushort[text.Length];
double[] advanceWidths = new double[text.Length];
double totalWidth = 0;
for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = gTypeface.CharacterToGlyphMap[text[n]];
glyphIndexes[n] = glyphIndex;

double width = gTypeface.AdvanceWidths[glyphIndex] * font.Size;
advanceWidths[n] = width;
totalWidth += width;
}
return new XSize(totalWidth, gTypeface.Height * font.Size);
#endif

Author:  robert_baumann [ Tue Jul 07, 2009 12:33 am ]
Post subject:  Re: Performance improvement in XGraphics.MeasureString()

Also - the glyph typeface code is the key to implementing custom kerning... :wink:

Author:  Thomas Hoevel [ Mon Jul 13, 2009 9:14 am ]
Post subject:  Re: Performance improvement in XGraphics.MeasureString()

Thank you for the feedback.

This is not my area of expertise, but I put it on the internal TODO list for the next release.

Author:  robert_baumann [ Mon Apr 22, 2013 9:34 pm ]
Post subject:  Re: Performance improvement in XGraphics.MeasureString()

I looked at the latest version of PdfSharp, and this fix is still not in the code. What can we do to put this fix into the source?

Author:  Thomas Hoevel [ Mon Nov 23, 2015 4:09 pm ]
Post subject:  Re: Performance improvement in XGraphics.MeasureString()

As of PDFsharp 1.50 beta 2, XGraphics.MeasureString() no longer uses a FormattedText object to measure strings.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/