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

Migrating to a newer version - XStringFormatFlags missing
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3898
Page 1 of 1

Author:  thomasb [ Fri Dec 14, 2018 12:22 pm ]
Post subject:  Migrating to a newer version - XStringFormatFlags missing

Hi!

I have a small problem and I'd be more than happy if anyone helped me.
A project I'm currently working on uses MigraDoc PdfSharp. So far it used the latest stable version: 1.32.2608, but we are trying to migrate to a newer, prerelease version (because of a connected project using 1.50.4619-beta4c). And almost everything looks fine, but there is one thing I'm not sure how to get around. There is a function measuring a string's size, looking (to put it simply) something about that:
Code:
protected SSize GetSize(string text, MigraDoc.DocumentObjectModel.Font font)
{
  var doc = new PdfDocument();
  var page = doc.AddPage;
  var sizer = XGraphics.FromPdfPage(page);
  var style = XFontStyle.Regular;
  //some style checks

  var xf = new XFont(font.Name, font.Size, style);
  var st = new XStringFormat();
  st.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;
  var xs = sizer.MeasureString(text, xf, st);
  return new SSize {
    Height = XUnit.FromPoint(xs.Height * _measureCorrection).Centimeter,
    Width = XUnit.FromPoint(xs.Width * _measureCorrection).Centimeter
  };
}

and the problematic line is:
Code:
st.FormatFlags = XStringFormatFlags.MeasureTrailingSpaces;

as the XStringFormat lost the FormatFlags property, and there is no longer the XStringFormatFlags enum.
Could anyone give me a clue, how to transform it properly to a new version?

Author:  TH-Soft [ Fri Dec 14, 2018 2:25 pm ]
Post subject:  Re: Migrating to a newer version - XStringFormatFlags missin

Hi!

I think I'd try this:
If the string ends with a space, I would add "X" to the string, would also call MeasureString for "X" and use the difference.
If the string does not have trailing spaces, this hack is not needed.

MeasureString has become much faster with version 1.50 and you do not have to measure "X" every time.
So despite this workaround, things should work faster with the new version.

Author:  TH-Soft [ Sat Dec 15, 2018 8:31 am ]
Post subject:  Re: Migrating to a newer version - XStringFormatFlags missin

Your code creates a new PDF document and a new PDF page just to measure a string - for every string.

A better implementation can be found here:
viewtopic.php?f=8&t=3196
This implementation does not have the flags you want, but it shows a more efficient way to get an XGraphics object.

Author:  thomasb [ Mon Dec 17, 2018 7:21 am ]
Post subject:  Re: Migrating to a newer version - XStringFormatFlags missin

Thank you, I'll take a look.

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