internal static bool TryReadHeaderAfterVersion(BigEndianReader reader, TrueTypeVersionReader version, bool validate, out TrueTypeHeader header)
        {
            header = null;

            ushort numtables = reader.ReadUInt16();
            ushort search    = reader.ReadUInt16();
            ushort entry     = reader.ReadUInt16();
            ushort range     = reader.ReadUInt16();

            //Validate values returned.
            if (validate)
            {
                //searchRange is the (Maximum power of 2 <= numTables) * 16
                ushort max2 = 2;
                while (max2 * 2 <= numtables)
                {
                    max2 *= 2;
                }


                if (search != max2 * 16)
                {
                    return(false);
                }

                //entrySelector is Log2(max2)
                if (Math.Log(max2, 2) != entry)
                {
                    return(false);
                }

                //rangeShift = numTables * 16-searchRange
                if (range != ((numtables * 16) - search))
                {
                    return(false);
                }
            }

            header               = new TrueTypeHeader(version, numtables);
            header.SearchRange   = search;
            header.EntrySelector = entry;
            header.RangeShift    = range;

            return(true);
        }
        internal static bool TryReadHeader(BigEndianReader reader, out TrueTypeHeader header)
        {
            header = null;

            TypefaceVersionReader vers;

            if (TypefaceVersionReader.TryGetVersion(reader, out vers) == false)
            {
                return(false);
            }
            else if (vers is TrueTypeVersionReader ttv)
            {
                return(TryReadHeaderAfterVersion(reader, ttv, false, out header));
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Performs the actual reading and loading of the file with the BigEndian reader
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        private static TTFRef DoLoadRef(BigEndianReader reader, string fullpath)
        {
            TrueTypeHeader head;

            if (TrueTypeHeader.TryReadHeader(reader, out head) == false)
            {
                return(null);
            }

            TrueTypeTableEntryList list = new TrueTypeTableEntryList();
            bool hasOs2   = false;
            bool hasFHead = false;
            bool hasName  = false;

            for (int i = 0; i < head.NumberOfTables; i++)
            {
                TrueTypeTableEntry dir = new TrueTypeTableEntry();
                dir.Read(reader);
                list.Add(dir);
                if (dir.Tag == TrueTypeTableNames.WindowsMetrics)
                {
                    hasOs2 = true;
                }
                else if (dir.Tag == TrueTypeTableNames.FontHeader)
                {
                    hasFHead = true;
                }
                else if (dir.Tag == TrueTypeTableNames.NamingTable)
                {
                    hasName = true;
                }
            }


            TrueTypeTableFactory fact = (head.Version as TrueTypeVersionReader).GetTableFactory();


            SubTables.NamingTable ntable = null;

            if (hasName)
            {
                ntable = fact.ReadTable(TrueTypeTableNames.NamingTable, list, reader) as SubTables.NamingTable;
            }
            else
            {
                throw new ArgumentNullException("The required '" + TrueTypeTableNames.NamingTable + "' is not present in this font file. The OpenType file is corrupt");
            }


            //if (fhead == null)
            //    throw new ArgumentNullException("The required '" + FontHeaderTable + "' is not present in this font file. The OpenType file is corrupt");



            TTFRef    ttfref = new TTFRef(fullpath);
            NameEntry entry;
            int       FamilyNameID = 1;

            if (ntable.Names.TryGetEntry(FamilyNameID, out entry))
            {
                ttfref.FamilyName = entry.ToString();
            }

            if (hasOs2)
            {
                SubTables.OS2Table os2table = fact.ReadTable(TrueTypeTableNames.WindowsMetrics, list, reader) as SubTables.OS2Table;
                ttfref.FontRestrictions = os2table.FSType;
                ttfref.FontWidth        = os2table.WidthClass;
                ttfref.FontWeight       = os2table.WeightClass;
                ttfref.FontSelection    = os2table.Selection;
            }
            else if (hasFHead)
            {
                SubTables.FontHeader fhead = fact.ReadTable(TrueTypeTableNames.FontHeader, list, reader) as SubTables.FontHeader;
                var mac = fhead.MacStyle;
                ttfref.FontRestrictions = FontRestrictions.InstallableEmbedding;
                ttfref.FontWeight       = WeightClass.Normal;

                if ((mac & FontStyleFlags.Condensed) > 0)
                {
                    ttfref.FontWidth = WidthClass.Condensed;
                }

                else if ((mac & FontStyleFlags.Extended) > 0)
                {
                    ttfref.FontWidth = WidthClass.Expanded;
                }

                ttfref.FontSelection = 0;
                if ((mac & FontStyleFlags.Italic) > 0)
                {
                    ttfref.FontSelection |= FontSelection.Italic;
                }

                if ((mac & FontStyleFlags.Bold) > 0)
                {
                    ttfref.FontSelection |= FontSelection.Bold;
                    ttfref.FontWeight     = WeightClass.Bold;
                }
                if ((mac & FontStyleFlags.Outline) > 0)
                {
                    ttfref.FontSelection |= FontSelection.Outlined;
                }

                if ((mac & FontStyleFlags.Underline) > 0)
                {
                    ttfref.FontSelection |= FontSelection.Underscore;
                }
            }
            else
            {
                throw new ArgumentNullException("The required '" + TrueTypeTableNames.WindowsMetrics + "' or '" + TrueTypeTableNames.FontHeader + " are not present in this font file. The OpenType file is corrupt");
            }


            return(ttfref);
        }