PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 9:50 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Aug 07, 2015 7:44 pm 
Offline

Joined: Fri Aug 07, 2015 7:12 pm
Posts: 3
I have a WinForms application, and I'm trying to get private fonts to work using the new FontResolver interface in 1.50.3638-beta.

I have modeled my code on TH-Soft's sample, which uses the WPF version of PDFsharp.

Here's my code:
Code:
// In the static constructor:
// GlobalFontSettings.FontResolver = new FontResolver();

void GetPrivateFont() {
    var dataFont = new XFont( "Avenir Medium", 10, XFontStyle.Regular, fontOptions );
    Debug.WriteLine( "Got font " + dataFont.FontFamily.Name );
}

class FontResolver : FontResolverBase {
    public override FontResolverInfo ResolveTypeface( string familyName, bool isBold, bool isItalic ) {
        Debug.WriteLine( "ResolveTypeface: " + familyName );

        switch ( familyName.ToLowerInvariant() ) {
            case "avenir medium":
                return new FontResolverInfo( "Avenir Medium/" );
        }

        return base.ResolveTypeface( familyName, isBold, isItalic );
    }

    public override byte[] GetFont( string faceName ) {
        Debug.WriteLine( "GetFont: " + faceName );

        var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;

        switch ( faceName.ToLowerInvariant() ) {
            case "avenir medium/":
                Debug.WriteLine( "Getting Avenir Medium" );
                return LoadFontData( assemblyName + ".Fonts.Avenir-Medium.ttf" );
        }

        Debug.WriteLine( "faceName not found" );
        return base.GetFont( faceName );
    }

    static byte[] LoadFontData( string name ) {
        using ( var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) ) {
            if ( resourceStream == null )
                throw new ArgumentException( "No resource with name " + name );

            var fontBytes = new byte[resourceStream.Length];
            resourceStream.Read( fontBytes, 0, fontBytes.Length );
            Debug.WriteLine( name + " " + fontBytes.Length );
            return fontBytes;
        }
    }
}

This outputs:
Code:
ResolveTypeface: Avenir Medium
GetFont: Avenir Medium/
Getting Avenir Medium
MyAssembly.Fonts.Avenir-Medium.ttf 138892
Got font Microsoft Sans Serif

Note that everything in the FontResolver class appears to be working fine, but I finally end up with Microsoft Sans Serif instead of the font I wanted.

Also, if I actually install the font to my system (and disable the FontResolver by commenting out the GlobalFontSettings.FontResolver = new FontResolver() line), then I get the correct font. So that tells me that my var dataFont = new XFont(...) line is probably fine as well (including requesting the correct font name).

Meanwhile, TH-Soft's WPF version works fine.

Am I missing something, or is this a bug in the GDI version of PDFsharp? If the latter, is there a workaround?

Update

From spelunking around with .NET Reflector and run-time reflection, I have found the following:

The constructor of XFont calls XFont.Initialize(), which in turn calls XPrivateFontCollection.TryCreateFont(). If XPrivateFontCollection.TryCreateFont() returns null, XFont.Initialize() initializes itself with System.Drawing.Font(familyName, ...). Since this is a private font, the System.Drawing.Font constructor doesn't find it, and I get the default Microsoft Sans Serif.

Meanwhile, XPrivateFontCollection.TryCreateFont() is returning null because XPrivateFontCollection.Global._privateFontCollection.Families.Count == 0. So the private collection is empty, despite having (tried to) add a font to it via the FontResolver class.


Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 08, 2015 7:02 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Hi,
Thanks for the feedback.
SWB wrote:
Meanwhile, XPrivateFontCollection.TryCreateFont() is returning null because XPrivateFontCollection.Global._privateFontCollection.Families.Count == 0. So the private collection is empty, despite having (tried to) add a font to it via the FontResolver class.
I admit I tested the FontResolver with the WPF build only.
Looks like there is a bug with the GDI+ build of PDFsharp.
Is switching to the WPF build an option for you?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 10, 2015 5:37 pm 
Offline

Joined: Fri Aug 07, 2015 7:12 pm
Posts: 3
TH-Soft wrote:
Looks like there is a bug with the GDI+ build of PDFsharp.
Is switching to the WPF build an option for you?

I don't think so … I'm working on a WinForms application that also outputs to a thermal printer, and I need to render to a WinForms control, a 1-bit-per-pixel Bitmap, and to PDF.


I was able to find a workaround.

First, I removed the FontResolver completely. (I am no longer using it.)

Next, I added the following to my class's static constructor:

Code:
static MyClass() {
    var assembly = Assembly.GetExecutingAssembly();

    foreach ( var resourceName in assembly.GetManifestResourceNames() ) {
        if ( resourceName.EndsWith( ".ttf" ) ) {
            using ( var resourceStream = assembly.GetManifestResourceStream( resourceName ) ) {
                XPrivateFontCollection.AddFont( resourceStream );
            }
        }
    }
}


This searches the resources of my application for TrueType fonts and manually adds each one it finds to the global XPrivateFontCollection. After that, PDFsharp can find it.

(Frankly, that's a lot simpler than the FontResolver approach, anyway. Not sure what advantages using FontResolver are meant to provide.)



However, there is a bug in XPrivateFontCollection.AddGlyphTypeface() (which is called by and does the main work of XPrivateFontCollection.AddFont() ):

The .NET Reflector disassembly of XPrivateFontCollection.AddGlyphTypeface() looks like this:

Code:
private void AddGlyphTypeface(XGlyphTypeface glyphTypeface) {
    string key = MakeName(glyphTypeface);

    if (this._typefaces.ContainsKey(key)) {
        throw new InvalidOperationException(PSSR.FontAlreadyAdded(glyphTypeface.DisplayName));
    }

    this._typefaces.Add(key, glyphTypeface);
    byte[] data = glyphTypeface.Fontface.Data;
    int length = data.Length;
    IntPtr destination = Marshal.AllocCoTaskMem(length);
    Marshal.Copy(data, 0, destination, length);
    this._privateFontCollection.AddMemoryFont(destination, length);
    Marshal.FreeCoTaskMem(destination);
}


The problem is the last line: Marshal.FreeCoTaskMem(destination).

_privateFontCollection is a System.Drawing.Text.PrivateFontCollection.

AddMemoryFont() does not make a copy of the font. Therefore, the memory must not be released until PDFsharp is done with the font. Marshal.FreeCoTaskMem() should go ... elsewhere. Probably, XPrivateFontCollection should implement IDisposable, and Marshal.FreeCoTaskMem() should go in the Dispose() method.

More details at StackOverflow.

I'm currently pondering how (or whether) I can workaround this ... Maybe reimplement AddGlyphTypeface() in my own code using reflection to get to the necessary private members of XPrivateFontCollection?

Would be nice if the source code for PDFsharp 1.50.3638-beta was available ...


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 365 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