/*****************
         * public methods
         */


        public bool Validate()
        {
            bool bRet = true;
            int  canrast;

            Validator v = GetFile().GetValidator();

            if (v.PerformTest(T._OFFSET_sfntVersion))
            {
                uint sfnt = m_OffsetTable.sfntVersion.GetUint();
                if (!OTFile.IsValidSfntVersion(sfnt))
                {
                    v.Error(T._OFFSET_sfntVersion, E._OFFSET_E_InvalidSFNT, null, "0x" + sfnt.ToString("x8"));
                    bRet = false;
                }
            }

            if (v.PerformTest(T._OFFSET_numTables))
            {
                if (m_OffsetTable.numTables == 0)
                {
                    v.Error(T._OFFSET_numTables, E._OFFSET_E_numTables, null);
                    bRet = false;
                }
                else
                {
                    v.Pass(T._OFFSET_numTables, P._OFFSET_P_numTables, null, m_OffsetTable.numTables.ToString());
                }
            }

            if (v.PerformTest(T._OFFSET_BinarySearchFields))
            {
                ushort numTables               = m_OffsetTable.numTables;
                ushort CalculatedSearchRange   = (ushort)(util.MaxPower2LE(numTables) * 16);
                ushort CalculatedEntrySelector = util.Log2(util.MaxPower2LE(numTables));
                ushort CalculatedRangeShift    = (ushort)(numTables * 16 - CalculatedSearchRange);

                bool bBinaryFieldsOk = true;

                if (m_OffsetTable.searchRange != CalculatedSearchRange)
                {
                    v.Error(T._OFFSET_BinarySearchFields, E._OFFSET_E_searchRange, null, m_OffsetTable.searchRange.ToString());
                    bBinaryFieldsOk = false;
                    bRet            = false;
                }
                if (m_OffsetTable.entrySelector != CalculatedEntrySelector)
                {
                    v.Error(T._OFFSET_BinarySearchFields, E._OFFSET_E_entrySelector, null, m_OffsetTable.entrySelector.ToString());
                    bBinaryFieldsOk = false;
                    bRet            = false;
                }
                if (m_OffsetTable.rangeShift != CalculatedRangeShift)
                {
                    v.Error(T._OFFSET_BinarySearchFields, E._OFFSET_E_rangeShift, null, m_OffsetTable.rangeShift.ToString());
                    bBinaryFieldsOk = false;
                    bRet            = false;
                }

                if (bBinaryFieldsOk)
                {
                    v.Pass(T._OFFSET_BinarySearchFields, P._OFFSET_P_BinarySearchTables, null);
                }
            }

            bRet &= CheckDirectoryEntriesNonZero(v);

            bRet &= CheckTablesInFileAndNotOverlapping(v);

            bRet &= CheckNoDuplicateTags(v);

            if (v.PerformTest(T._DE_TagsAscendingOrder))
            {
                bRet &= CheckTagsAscending(v);
            }

            if (v.PerformTest(T._DE_TagNames))
            {
                bRet &= CheckTagNames(v);
            }

            if (v.PerformTest(T._DE_TableAlignment))
            {
                bRet &= CheckTableAlignment(v);
            }

            if (v.PerformTest(T._FONT_RequiredTables))
            {
                bRet &= CheckForRequiredTables(v);
            }

            if (v.PerformTest(T._FONT_RecommendedTables))
            {
                bRet &= CheckForRecommendedTables(v);
            }

            if (v.PerformTest(T._FONT_UnnecessaryTables))
            {
                bRet &= CheckForNoUnnecessaryTables(v);
            }

            if (v.PerformTest(T._FONT_OptimalTableOrder))
            {
                bRet &= CheckForOptimalTableOrder(v);
            }


            // Validate each table

            if (m_OffsetTable != null)
            {
                for (int i = 0; i < m_OffsetTable.DirectoryEntries.Count; i++)
                {
                    // check to see if user canceled validation
                    if (v.CancelFlag)
                    {
                        break;
                    }

                    // get the table
                    DirectoryEntry de    = (DirectoryEntry)m_OffsetTable.DirectoryEntries[i];
                    OTTable        table = GetFile().GetTableManager().GetTable(this, de);

                    // Will it really happen?
                    if (GetFile().GetTableManager().GetUnaliasedTableName(de.tag) == "DSIG" && GetFile().IsCollection())
                    {
                        continue;
                    }

                    if (GetFile().GetTableManager().GetUnaliasedTableName(de.tag) == "glyf" &&
                        Is_TTC_LaterIdenticalTable("glyf"))
                    {
                        v.Info(T.T_NULL, I.glyf_I_IDENTICAL_GLYF_TABLES_IN_TTC, de.tag, "glyf");
                        continue;
                    }

                    if (GetFile().GetTableManager().GetUnaliasedTableName(de.tag) == "EBDT" &&
                        Is_TTC_LaterIdenticalTable("EBDT"))
                    {
                        v.Info(T.T_NULL, I.glyf_I_IDENTICAL_GLYF_TABLES_IN_TTC, de.tag, "EBDT");
                        continue;
                    }

                    // Call the function that validates a single table
                    bRet &= this.GetFile().ValidateTable(table, v, de, this);
                }
            }

            canrast = TestFontRasterization();
            ushort numGlyphs = GetMaxpNumGlyphs();

            // rasterization test - BW

            v.OnRastTestValidationEvent_BW(true);
            if (v.PeformRastTest_BW() && Is_TTC_LaterIdenticalTable("glyf"))
            {
                v.Info(T.T_NULL, I.glyf_I_IDENTICAL_GLYF_TABLES_IN_TTC, null, "B/W Rasterization");
            }
            else
            if (v.PeformRastTest_BW())
            {
                if (canrast > 0)
                {
                    try
                    {
                        // fetch the rasterizer object and initialize it with this font
                        RasterInterf ri = GetFile().GetRasterizer();
                        ri.RasterNewSfnt(GetFile().GetFileStream(), GetFontIndexInFile());


                        // call the rasterizer
                        RasterInterf.UpdateProgressDelegate upg  = new RasterInterf.UpdateProgressDelegate(v.OnTableProgress);
                        RasterInterf.RastTestErrorDelegate  rted = new RasterInterf.RastTestErrorDelegate(v.OnRastTestError);
                        int               x          = v.GetRastTestXRes();
                        int               y          = v.GetRastTestYRes();
                        int []            pointsizes = v.GetRastTestPointSizes();
                        RastTestTransform rtt        = v.GetRastTestTransform();
                        bRet &= ri.RastTest(
                            x, y, pointsizes,
                            rtt.stretchX, rtt.stretchY, rtt.rotation, rtt.skew, rtt.matrix,
                            true, false, false, 0,
                            rted, upg, numGlyphs);
                        if (ri.GetRastErrorCount() == 0)
                        {
                            v.Pass(T.T_NULL, P._rast_P_rasterization, null);
                        }
                    }
                    catch (Exception e)
                    {
                        v.ApplicationError(T.T_NULL, E._rast_A_ExceptionUnhandled, null, e.Message + e.StackTrace);
                    }
                }
                else if (canrast == 0)
                {
                    v.Info(T.T_NULL, I._rast_I_rasterization, null, GetDevMetricsDataError());
                }
                else
                {
                    v.Error(T.T_NULL, E._rast_E_rasterization, null, GetDevMetricsDataError());
                    bRet = false;
                }
            }
            else
            {
                v.Info(I._TEST_I_RastTestNotSelected, null);
            }
            v.OnRastTestValidationEvent_BW(false);


            // rasterization test - Grayscale

            v.OnRastTestValidationEvent_Grayscale(true);
            if (v.PeformRastTest_Grayscale() && Is_TTC_LaterIdenticalTable("glyf"))
            {
                v.Info(T.T_NULL, I.glyf_I_IDENTICAL_GLYF_TABLES_IN_TTC, null, "Grayscale Rasterization");
            }
            else
            if (v.PeformRastTest_Grayscale())
            {
                if (canrast > 0)
                {
                    try
                    {
                        // fetch the rasterizer object and initialize it with this font
                        RasterInterf ri = GetFile().GetRasterizer();
                        ri.RasterNewSfnt(GetFile().GetFileStream(), GetFontIndexInFile());


                        // call the rasterizer
                        RasterInterf.UpdateProgressDelegate upg  = new RasterInterf.UpdateProgressDelegate(v.OnTableProgress);
                        RasterInterf.RastTestErrorDelegate  rted = new RasterInterf.RastTestErrorDelegate(v.OnRastTestError);
                        int               x          = v.GetRastTestXRes();
                        int               y          = v.GetRastTestYRes();
                        int []            pointsizes = v.GetRastTestPointSizes();
                        RastTestTransform rtt        = v.GetRastTestTransform();
                        bRet &= ri.RastTest(
                            x, y, pointsizes,
                            rtt.stretchX, rtt.stretchY, rtt.rotation, rtt.skew, rtt.matrix,
                            false, true, false, 0,
                            rted, upg, numGlyphs);
                        if (ri.GetRastErrorCount() == 0)
                        {
                            v.Pass(T.T_NULL, P._rast_P_rasterization, null);
                        }
                    }
                    catch (Exception e)
                    {
                        v.ApplicationError(T.T_NULL, E._rast_A_ExceptionUnhandled, null, e.Message + e.StackTrace);
                    }
                }
                else if (canrast == 0)
                {
                    v.Info(T.T_NULL, I._rast_I_rasterization, null, GetDevMetricsDataError());
                }
                else
                {
                    v.Error(T.T_NULL, E._rast_E_rasterization, null, GetDevMetricsDataError());
                    bRet = false;
                }
            }
            else
            {
                v.Info(I._TEST_I_RastTestNotSelected, null);
            }
            v.OnRastTestValidationEvent_Grayscale(false);



            // rasterization test - Cleartype

            v.OnRastTestValidationEvent_Cleartype(true);
            if (v.PeformRastTest_Cleartype() && Is_TTC_LaterIdenticalTable("glyf"))
            {
                v.Info(T.T_NULL, I.glyf_I_IDENTICAL_GLYF_TABLES_IN_TTC, null, "Cleartype Rasterization");
            }
            else
            if (v.PeformRastTest_Cleartype())
            {
                if (canrast > 0)
                {
                    try
                    {
                        uint CTFlags = v.GetCleartypeFlags();

                        // fetch the rasterizer object and initialize it with this font
                        RasterInterf ri = GetFile().GetRasterizer();
                        ri.RasterNewSfnt(GetFile().GetFileStream(), GetFontIndexInFile());


                        // call the rasterizer
                        RasterInterf.UpdateProgressDelegate upg  = new RasterInterf.UpdateProgressDelegate(v.OnTableProgress);
                        RasterInterf.RastTestErrorDelegate  rted = new RasterInterf.RastTestErrorDelegate(v.OnRastTestError);
                        int               x          = v.GetRastTestXRes();
                        int               y          = v.GetRastTestYRes();
                        int []            pointsizes = v.GetRastTestPointSizes();
                        RastTestTransform rtt        = v.GetRastTestTransform();
                        bRet &= ri.RastTest(
                            x, y, pointsizes,
                            rtt.stretchX, rtt.stretchY, rtt.rotation, rtt.skew, rtt.matrix,
                            false, false, true, CTFlags,
                            rted, upg, numGlyphs);
                        if (ri.GetRastErrorCount() == 0)
                        {
                            v.Pass(T.T_NULL, P._rast_P_rasterization, null);
                        }
                    }
                    catch (Exception e)
                    {
                        v.ApplicationError(T.T_NULL, E._rast_A_ExceptionUnhandled, null, e.Message + e.StackTrace);
                    }
                }
                else if (canrast == 0)
                {
                    v.Info(T.T_NULL, I._rast_I_rasterization, null, GetDevMetricsDataError());
                }
                else
                {
                    v.Error(T.T_NULL, E._rast_E_rasterization, null, GetDevMetricsDataError());
                    bRet = false;
                }
            }
            else
            {
                v.Info(I._TEST_I_RastTestNotSelected, null);
            }
            v.OnRastTestValidationEvent_Cleartype(false);

            return(bRet);
        }
        /******************
         * protected methods
         */


        protected static OffsetTable ReadOffsetTable(OTFileVal file, uint filepos)
        {
            // read the Offset Table from the file

            Validator v = file.GetValidator();

            const int SIZEOF_OFFSETTABLE = 12;

            OffsetTable ot = null;


            // read the offset table

            MBOBuffer buf = file.ReadPaddedBuffer(filepos, SIZEOF_OFFSETTABLE);

            if (buf != null)
            {
                if (OTFile.IsValidSfntVersion(buf.GetUint(0)))
                {
                    ot = new OffsetTable(buf);
                }
                else
                {
                    v.Error(T.T_NULL, E._OFFSET_E_InvalidSFNT, null, "0x" + buf.GetUint(0).ToString("x8"));
                }
            }


            // now read the directory entries

            if (ot != null)
            {
                const int SIZEOF_DIRECTORYENTRY = 16;


                for (int i = 0; i < ot.numTables; i++)
                {
                    uint      dirFilePos = (uint)(filepos + SIZEOF_OFFSETTABLE + i * SIZEOF_DIRECTORYENTRY);
                    MBOBuffer DirEntBuf  = file.ReadPaddedBuffer(dirFilePos, SIZEOF_DIRECTORYENTRY);

                    if (DirEntBuf != null)
                    {
                        DirectoryEntry de = new DirectoryEntry();

                        de.tag      = new OTTag(DirEntBuf.GetBuffer());
                        de.checkSum = DirEntBuf.GetUint(4);
                        de.offset   = DirEntBuf.GetUint(8);
                        de.length   = DirEntBuf.GetUint(12);

                        ot.DirectoryEntries.Add(de);

                        if (de.offset > file.GetFileLength())
                        {
                            v.Error(T.T_NULL, E._DE_E_OffsetPastEOF, de.tag, "0x" + de.offset.ToString("x8"));
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(ot);
        }