PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Feb 22, 2018 4:53 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Apr 04, 2012 9:06 pm 
Offline

Joined: Wed Apr 04, 2012 8:47 pm
Posts: 1
I used two fonts in my PDF page: HelveticaNeueLTStd-Roman and HelveticaNeueLTStd-BdIt. The actual font that is embedded in the PDF by PDFSharp.NET is dependent on which font is used first during creation of the PDF; and then only the first font that is used is embedded. The text is rendered using that single font.

I looked through the source code and I found a bug which can be easily fixed. The FontSelector constructor clears styles Bold and Italic when these styles are not available to a font family. However, it does not check if a combination of both styles is supported. Here is the invalid code:

Code:
public class FontSelector
    {
      /// <summary>
      /// Initializes a new instance of PdfFontSelector from an XFont.
      /// </summary>
      public FontSelector(XFont font)
      {
        this.name = font.Name;
        // Ignore Strikeout and Underline
        this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
        // Clear styles that are not available as a separate type face to prevent embedding of identical font files
#if GDI && !WPF
        if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
            this.style &= ~XFontStyle.Bold;
        if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
            this.style &= ~XFontStyle.Italic;


And here is the bug fix I implemented:
Code:
public FontSelector(XFont font)
      {
        this.name = font.Name;
        // Ignore Strikeout and Underline
        this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
        // Clear styles that are not available as a separate type face to prevent embedding of identical font files
#if GDI && !WPF
        if ((this.style & XFontStyle.Bold) == XFontStyle.Bold &&
            (this.style & XFontStyle.Italic) == XFontStyle.Italic)
        {
            if (!font.FontFamily.IsStyleAvailable(XFontStyle.Bold | XFontStyle.Italic))
            {
                this.style &= ~XFontStyle.Bold;
                this.style &= ~XFontStyle.Italic;
            }
        }
        else
        {
            if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
                this.style &= ~XFontStyle.Bold;
            if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
                this.style &= ~XFontStyle.Italic;
        }


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 05, 2012 7:19 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2777
Location: Cologne, Germany
Thank you for the feedback, the fix looks plausible.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group