/// <summary> /// Reads all required tables from the font data. /// </summary> internal void Read() { try { // 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); //this.tableDictionary = (this.offsetTable.TableCount); // ReSharper disable InconsistentNaming // Determine font technology const uint OTTO = 0x4f54544f; // Adobe OpenType CFF data, tag: 'OTTO' const uint TTCF = 0x74746366; // TrueType Collection tag: 'ttcf' // ReSharper restore InconsistentNaming if (this.offsetTable.Version == TTCF) { this.fontTechnology = FontTechnology.TrueTypeCollection; throw new InvalidOperationException("TrueType collection fonts are not supported by PDFsharp."); } else if (this.offsetTable.Version == OTTO) { this.fontTechnology = FontTechnology.PostscriptOutlines; } else { this.fontTechnology = FontTechnology.TrueTypeOutlines; } for (int idx = 0; idx < this.offsetTable.TableCount; idx++) { TableDirectoryEntry entry = TableDirectoryEntry.ReadFrom(this); this.tableDictionary.Add(entry.Tag, entry); #if VERBOSE Debug.WriteLine(String.Format("Font table: {0}", entry.Tag)); #endif } // PDFlib checks this, but it is not part of the OpenType spec anymore if (this.tableDictionary.ContainsKey("bhed")) { throw new NotSupportedException("Bitmap fonts are not supported by PDFsharp."); } // Read required tables if (Seek(CMapTable.Tag) != -1) { this.cmap = new CMapTable(this); } if (Seek(ControlValueTable.Tag) != -1) { this.cvt = new ControlValueTable(this); } if (Seek(FontProgram.Tag) != -1) { this.fpgm = new FontProgram(this); } if (Seek(MaximumProfileTable.Tag) != -1) { this.maxp = new MaximumProfileTable(this); } if (Seek(NameTable.Tag) != -1) { this.name = new NameTable(this); } if (Seek(FontHeaderTable.Tag) != -1) { this.head = new FontHeaderTable(this); } if (Seek(HorizontalHeaderTable.Tag) != -1) { this.hhea = new HorizontalHeaderTable(this); } if (Seek(HorizontalMetricsTable.Tag) != -1) { this.hmtx = new HorizontalMetricsTable(this); } if (Seek(OS2Table.Tag) != -1) { this.os2 = new OS2Table(this); } if (Seek(PostScriptTable.Tag) != -1) { this.post = new PostScriptTable(this); } if (Seek(GlyphDataTable.Tag) != -1) { this.glyf = new GlyphDataTable(this); } if (Seek(IndexToLocationTable.Tag) != -1) { this.loca = new IndexToLocationTable(this); } if (Seek(GlyphSubstitutionTable.Tag) != -1) { this.gsub = new GlyphSubstitutionTable(this); } if (Seek(ControlValueProgram.Tag) != -1) { this.prep = new ControlValueProgram(this); } } catch (Exception) { GetType(); throw; } }
/// <summary> /// Adds the specified table to this font image. /// </summary> public void AddTable(OpenTypeFontTable fontTable) { if (!CanWrite) { throw new InvalidOperationException("Font image cannot be modified."); } if (fontTable == null) { throw new ArgumentNullException("fontTable"); } if (fontTable.fontData == null) { fontTable.fontData = this; } else { Debug.Assert(fontTable.fontData.CanRead); // Create a reference to this font table fontTable = new IRefFontTable(this, fontTable); } //Debug.Assert(fontTable.FontData == null); //fontTable.fontData = this; this.tableDictionary[fontTable.DirectoryEntry.Tag] = fontTable.DirectoryEntry; switch (fontTable.DirectoryEntry.Tag) { case TableTagNames.CMap: this.cmap = fontTable as CMapTable; break; case TableTagNames.Cvt: this.cvt = fontTable as ControlValueTable; break; case TableTagNames.Fpgm: this.fpgm = fontTable as FontProgram; break; case TableTagNames.MaxP: this.maxp = fontTable as MaximumProfileTable; break; case TableTagNames.Name: this.name = fontTable as NameTable; break; case TableTagNames.Head: this.head = fontTable as FontHeaderTable; break; case TableTagNames.HHea: this.hhea = fontTable as HorizontalHeaderTable; break; case TableTagNames.HMtx: this.hmtx = fontTable as HorizontalMetricsTable; break; case TableTagNames.OS2: this.os2 = fontTable as OS2Table; break; case TableTagNames.Post: this.post = fontTable as PostScriptTable; break; case TableTagNames.Glyf: this.glyf = fontTable as GlyphDataTable; break; case TableTagNames.Loca: this.loca = fontTable as IndexToLocationTable; break; case TableTagNames.GSUB: this.gsub = fontTable as GlyphSubstitutionTable; break; case TableTagNames.Prep: this.prep = fontTable as ControlValueProgram; break; } }
/// <summary> /// Reads all required tables from the font data. /// </summary> internal void Read() { try { // 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); //this.tableDictionary = (this.offsetTable.TableCount); // ReSharper disable InconsistentNaming // Determine font technology const uint OTTO = 0x4f54544f; // Adobe OpenType CFF data, tag: 'OTTO' const uint TTCF = 0x74746366; // TrueType Collection tag: 'ttcf' // ReSharper restore InconsistentNaming if (this.offsetTable.Version == TTCF) { this.fontTechnology = FontTechnology.TrueTypeCollection; throw new InvalidOperationException("TrueType collection fonts are not supported by PDFsharp."); } else if (this.offsetTable.Version == OTTO) this.fontTechnology = FontTechnology.PostscriptOutlines; else this.fontTechnology = FontTechnology.TrueTypeOutlines; for (int idx = 0; idx < this.offsetTable.TableCount; idx++) { TableDirectoryEntry entry = TableDirectoryEntry.ReadFrom(this); this.tableDictionary.Add(entry.Tag, entry); #if VERBOSE Debug.WriteLine(String.Format("Font table: {0}", entry.Tag)); #endif } // PDFlib checks this, but it is not part of the OpenType spec anymore if (this.tableDictionary.ContainsKey("bhed")) throw new NotSupportedException("Bitmap fonts are not supported by PDFsharp."); // Read required tables if (Seek(CMapTable.Tag) != -1) this.cmap = new CMapTable(this); if (Seek(ControlValueTable.Tag) != -1) this.cvt = new ControlValueTable(this); if (Seek(FontProgram.Tag) != -1) this.fpgm = new FontProgram(this); if (Seek(MaximumProfileTable.Tag) != -1) this.maxp = new MaximumProfileTable(this); if (Seek(NameTable.Tag) != -1) this.name = new NameTable(this); if (Seek(FontHeaderTable.Tag) != -1) this.head = new FontHeaderTable(this); if (Seek(HorizontalHeaderTable.Tag) != -1) this.hhea = new HorizontalHeaderTable(this); if (Seek(HorizontalMetricsTable.Tag) != -1) this.hmtx = new HorizontalMetricsTable(this); if (Seek(OS2Table.Tag) != -1) this.os2 = new OS2Table(this); if (Seek(PostScriptTable.Tag) != -1) this.post = new PostScriptTable(this); if (Seek(GlyphDataTable.Tag) != -1) this.glyf = new GlyphDataTable(this); if (Seek(IndexToLocationTable.Tag) != -1) this.loca = new IndexToLocationTable(this); if (Seek(GlyphSubstitutionTable.Tag) != -1) this.gsub = new GlyphSubstitutionTable(this); if (Seek(ControlValueProgram.Tag) != -1) this.prep = new ControlValueProgram(this); } catch (Exception) { GetType(); throw; } }
/// <summary> /// Source: https://forum.pdfsharp.net/viewtopic.php?p=9039 /// </summary> internal void CorrectOffsetForTTC() { _pos = 0; string sTTCTag = this.ReadString(4); if (sTTCTag != "ttcf") { _pos = 0; // Reset offset return; } uint uiVersion = this.ReadULong(); uint uiFontCount = this.ReadULong(); bool bFoundFont = false; for (uint uiFontIndex = 0; uiFontIndex < uiFontCount; ++uiFontIndex) { _pos = (int)(12 + (uiFontIndex * 4)); _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) { _pos = nameTableEntry.Offset; TableDictionary.Add(nameTableEntry.Tag, nameTableEntry); // Add for create NameTable NameTable nametbl = new NameTable(this); TableDictionary.Remove(nameTableEntry.Tag); // clear after used if (nametbl.Name == FontSource.FontName) { // Found font _pos = (int)(12 + (uiFontIndex * 4)); _pos = (int)this.ReadULong(); bFoundFont = true; break; } } } if (!bFoundFont) { _pos = 0; } }
/// <summary> /// Adds the specified table to this font image. /// </summary> public void AddTable(OpenTypeFontTable fontTable) { if (!CanWrite) throw new InvalidOperationException("Font image cannot be modified."); if (fontTable == null) throw new ArgumentNullException("fontTable"); if (fontTable.fontData == null) { fontTable.fontData = this; } else { Debug.Assert(fontTable.fontData.CanRead); // Create a reference to this font table fontTable = new IRefFontTable(this, fontTable); } //Debug.Assert(fontTable.FontData == null); //fontTable.fontData = this; this.tableDictionary[fontTable.DirectoryEntry.Tag] = fontTable.DirectoryEntry; switch (fontTable.DirectoryEntry.Tag) { case TableTagNames.CMap: this.cmap = fontTable as CMapTable; break; case TableTagNames.Cvt: this.cvt = fontTable as ControlValueTable; break; case TableTagNames.Fpgm: this.fpgm = fontTable as FontProgram; break; case TableTagNames.MaxP: this.maxp = fontTable as MaximumProfileTable; break; case TableTagNames.Name: this.name = fontTable as NameTable; break; case TableTagNames.Head: this.head = fontTable as FontHeaderTable; break; case TableTagNames.HHea: this.hhea = fontTable as HorizontalHeaderTable; break; case TableTagNames.HMtx: this.hmtx = fontTable as HorizontalMetricsTable; break; case TableTagNames.OS2: this.os2 = fontTable as OS2Table; break; case TableTagNames.Post: this.post = fontTable as PostScriptTable; break; case TableTagNames.Glyf: this.glyf = fontTable as GlyphDataTable; break; case TableTagNames.Loca: this.loca = fontTable as IndexToLocationTable; break; case TableTagNames.GSUB: this.gsub = fontTable as GlyphSubstitutionTable; break; case TableTagNames.Prep: this.prep = fontTable as ControlValueProgram; break; } }