PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 8:33 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



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
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
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 44 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group