Esempio n. 1
0
        /// <summary>
        /// Create a new <see cref="TableRegister"/>.
        /// </summary>
        /// <param name="builder">The builder with necessary tables set.</param>
        internal TableRegister(Builder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            HeaderTable            = builder.HeaderTable ?? throw new ArgumentException("The builder did not contain the header table");
            GlyphTable             = builder.GlyphDataTable;
            HorizontalHeaderTable  = builder.HorizontalHeaderTable ?? throw new ArgumentException("The builder did not contain the horizontal header table.");
            HorizontalMetricsTable = builder.HorizontalMetricsTable;
            IndexToLocationTable   = builder.IndexToLocationTable;
            MaximumProfileTable    = builder.MaximumProfileTable ?? throw new ArgumentException("The builder did not contain the maximum profile table.");
            NameTable       = builder.NameTable;
            PostScriptTable = builder.PostScriptTable;
            CMapTable       = builder.CMapTable;
            KerningTable    = builder.KerningTable;
            Os2Table        = builder.Os2Table;
        }
Esempio n. 2
0
        public static Font ReadFromStream(Stream stream)
        {
            var font   = new Font();
            var reader = new BinaryReader(stream);

            // Offset Table
            font.OffsetTable = OffsetTable.Read(reader);

            // Head
            var headTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == HeadTable.Tag);

            stream.Seek(headTableRecord.Offset, SeekOrigin.Begin);
            font.HeadTable = HeadTable.Read(reader);

            // Horizontal Header
            var horizontalHeaderTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == HorizontalHeaderTable.Tag);

            stream.Seek(horizontalHeaderTableRecord.Offset, SeekOrigin.Begin);
            font.HorizontalHeaderTable = HorizontalHeaderTable.Read(reader);

            // Maximum Profile
            var maximumProfileTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == MaximumProfileTableBase.Tag);

            stream.Seek(maximumProfileTableRecord.Offset, SeekOrigin.Begin);
            var maximumProfileTableVersion = Math.Round(reader.ReadFixedPointNumber(), 2);

            if (maximumProfileTableVersion == 0.5m)
            {
                font.MaximumProfileTable = MaximumProfileTableV0_5.Read(reader, maximumProfileTableVersion);
            }
            else if (maximumProfileTableVersion == 1.0m)
            {
                font.MaximumProfileTable = MaximumProfileTableV1.Read(reader, maximumProfileTableVersion);
            }

            // Horizontal Metrics
            var horizontalMetricsTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == HorizontalMetricsTable.Tag);

            stream.Seek(horizontalMetricsTableRecord.Offset, SeekOrigin.Begin);
            font.HorizontalMetricsTable = HorizontalMetricsTable.Read(reader,
                                                                      font.HorizontalHeaderTable.NumberOfHMetrics,
                                                                      font.MaximumProfileTable.NumGlyphs);

            // Glyph
            var glyphTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == GlyphTable.Tag);

            font.GlyphTable = new GlyphTable(reader, glyphTableRecord.Offset);

            // IndexLocation
            var indexLocationTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == IndexLocationTableBase.Tag);

            stream.Seek(indexLocationTableRecord.Offset, SeekOrigin.Begin);
            if (font.HeadTable.IndexToLocFormat == 0)
            {
                font.IndexLocationTable = IndexLocationShortFormatTable.Read(reader, font.MaximumProfileTable.NumGlyphs);
            }
            else
            {
                font.IndexLocationTable = IndexLocationLongFormatTable.Read(reader, font.MaximumProfileTable.NumGlyphs);
            }

            // PostScript
            var postScriptTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == PostScriptTable.Tag);

            stream.Seek(postScriptTableRecord.Offset, SeekOrigin.Begin);
            font.PostScriptTable = PostScriptTable.Read(reader);

            // OS/2 and Windows
            var os2TableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == Os2Table.Tag);

            stream.Seek(os2TableRecord.Offset, SeekOrigin.Begin);
            font.Os2Table = Os2Table.Read(reader);

            // CharacterMap
            var characterMapTableRecord = font.OffsetTable.TableRecords.Single(x => x.TableTag == CharacterMapTable.Tag);

            stream.Seek(characterMapTableRecord.Offset, SeekOrigin.Begin);
            font.CharacterMapTable = CharacterMapTable.Read(reader);

            return(font);
        }