PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Dec 10, 2018 10:36 pm

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 22, 2015 2:58 pm 
Offline

Joined: Wed Apr 22, 2015 2:00 pm
Posts: 1
I have tried to make the PdfSharp support TTC font.
(Such as "MS Gothic", "MS PGothic", "MS Mincho",... etc).

Now, it works for my application.
I would like to share my tip. 8)

I made changed at PdfSharp.Fonts.OpenType.FontData class. (Base on "PDFsharp & MigraDoc Foundation 1.32")
And then re-build PdfSharp.dll.


These following are the detail.
1. Modified CreateGdiFontImage() function.
2. Added a function, named CreateGdiFontImage(), in the class
3. Modified a constructor, FontData(XFont font, XPdfFontOptions options)
4. Removed an assert in Read() function.


1. Modified CreateGdiFontImage() function.

The current method retrieve font data from the beginning of the data for the currently selected font for TrueType Collection files.
It is cause of an exception that you will found when you try to use TTC font.
So, I modified it as this following code.

(more detail about GetFontData(), see https://msdn.microsoft.com/en-us/librar ... 85%29.aspx)

Code:
        ...
        ...
        int size = NativeMethods.GetFontData(hdc, 0x66637474, 0, null, 0);                // <--- Modified this line
        error = Marshal.GetLastWin32Error();
        if (size > 0)
        {
          this.data = new byte[size];
          int effectiveSize = NativeMethods.GetFontData(hdc, 0x66637474, 0, this.data, this.data.Length); // <--- Modified this line
          Debug.Assert(size == effectiveSize);
          NativeMethods.SelectObject(hdc, oldFont);
          NativeMethods.ReleaseDC(IntPtr.Zero, hdc);
          error.GetType();
        }
        ...
        ...


2. Added a function, named CreateGdiFontImage(), in the class.
Code:
    internal void CorrectOffsetForTTC(XFont font)
    {
        this.pos = 0;   // Reset offset

        // Try to check if it is TTC file.
        string sTTCTag = this.ReadString(4);
        if (sTTCTag != "ttcf")
        {
            this.pos = 0;   // Reset offset
            return;
        }

        uint uiVersion = this.ReadULong();
        uint uiFontCount = this.ReadULong();

        bool bFoundFont = false;

        for (uint uiFontIndex = 0; uiFontIndex < uiFontCount; ++uiFontIndex)
        {
            this.pos = (int)(12 + (uiFontIndex * 4));
            this.pos = (int)this.ReadULong();

            // Read offset table
            uint nTempVersion = ReadULong();
            uint nTempTableCount = ReadUShort();
            uint nTempSearchRange = ReadUShort();
            uint nTempEntrySelector = ReadUShort();
            uint nTempRangeShift = ReadUShort();

            TableDirectoryEntry nameTableEntry = null;

            for (int idx = 0; idx < nTempTableCount; idx++)
            {
                TableDirectoryEntry entry = TableDirectoryEntry.ReadFrom(this);
                if (entry.Tag == NameTable.Tag)
                {
                    nameTableEntry = entry;
                    break;
                }
            }

            if (nameTableEntry != null)
            {
                this.pos = nameTableEntry.Offset;
                this.tableDictionary.Add(nameTableEntry.Tag, nameTableEntry);   // Add for create NameTable
               
                NameTable nametbl = new NameTable(this);

                this.tableDictionary.Remove(nameTableEntry.Tag);                // clear after used

                if (nametbl.Name == font.familyName)
                {
                    // Found font
                    this.pos = (int)(12 + (uiFontIndex * 4));
                    this.pos = (int)this.ReadULong();

                    bFoundFont = true;

                    break;
                }
            }
        }

        if (!bFoundFont)
        {
            this.pos = 0;
        }

    }



3. Modified a constructor, FontData(XFont font, XPdfFontOptions options)
Code:
    public FontData(XFont font, XPdfFontOptions options)
    {
#if GDI && !WPF
      CreateGdiFontImage(font, options);
#endif
#if WPF && !GDI
      CreateWpfFontData(font, options);
#endif
#if WPF && GDI
      System.Drawing.Font gdiFont = font.RealizeGdiFont();
      if (font.font != null)
        CreateGdiFontImage(font, options);
      else if (font.typeface != null)
        CreateWpfFontData(font, options);
#endif
      if (this.data == null)
        throw new InvalidOperationException("Cannot allocate font data.");

      CorrectOffsetForTTC(font);                        // <-- Added this line.
     
      Read();
    }



4. Removed an assert in Read() function.
Code:
        ...
        ...
        // Read offset table
        this.offsetTable.Version = ReadULong();
        this.offsetTable.TableCount = ReadUShort();
        this.offsetTable.SearchRange = ReadUShort();
        this.offsetTable.EntrySelector = ReadUShort();
        this.offsetTable.RangeShift = ReadUShort();

        // Move to table dictionary at position 12
        //Debug.Assert(this.pos == 12);                                // <-- Removed this line
        ...
        ...


When you do all of them, just rebuild PdfSharp.dll and enjoy with TTC font.

But, please noted that my modified works only GDI+ mode.
And I don't yet test it with other TTC font then msgothic.ttc.

PS, I'm Thai. English is not my native language.
If you have question, I may cannot explain you as well. :?
For someone, who want to understand what I have done.
Please research TTF at https://www.microsoft.com/typography/otspec/otff.htm


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 24, 2018 7:44 pm 
Offline

Joined: Tue Dec 10, 2013 12:15 am
Posts: 4
Works great! Thank you for this fix and for posting it in this forum. You saved me a lot of time.


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: Google [Bot] and 5 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