PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

TrueType Collection Font support
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3093
Page 1 of 1

Author:  kong_s [ Wed Apr 22, 2015 2:58 pm ]
Post subject:  TrueType Collection Font support

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

Author:  petergolde [ Sat Nov 24, 2018 7:44 pm ]
Post subject:  Re: TrueType Collection Font support

Works great! Thank you for this fix and for posting it in this forum. You saved me a lot of time.

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