Exemplo n.º 1
0
 public bool Equals(OTTag tag)
 {
     if (tag is OTTag) // non-null
     {
         // get byte array for tag and compare with mTag
         byte[] tb = tag.GetBytes();
         return(this.Equals(tb));
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 2
0
        public bool TryGetTable(OTTag tag, out OTTable table)
        {
            uint?idx = _offsetTable.GetTableRecordIndex(tag);

            if (idx.HasValue)
            {
                table = _tables[idx.Value];
                return(true);
            }
            else
            {
                table = null;
                return(false);
            }
        }
Exemplo n.º 3
0
        } // Validate

        public uint?GetTableRecordIndex(OTTag tag)
        {
            if (_tableMap == null)
            {
                throw new OTInvalidOperationException("GetTableRecordIndex method error: cannot retrieve table records when Offset table has not yet been read");
            }

            uint index;

            if (_tableMap.TryGetValue(tag.ToString(), out index))
            {
                return(index);
            }
            else
            {
                return(null);
            }
        } // GetTableRecordIndex
Exemplo n.º 4
0
        public static bool IsSupportedTableType(OTTag tag)
        {
            switch (tag.ToString())
            {
            case "COLR":
            case "fmtx":
            //case "fvar":
            //case "GPOS":
            //case "GSUB":
            case "head":
            case "hhea":
            case "maxp":
                //case "name":
                //case "OS/2":
                return(true);

            default:
                return(false);
            }
        }
Exemplo n.º 5
0
 public static bool IsSupportedSfntVersion(OTTag tag)
 {
     if (tag is OTTag)
     {
         if ((tag == new byte[4] {
             0, 1, 0, 0
         }) || (tag == (OTTag)"OTTO") || (tag == (OTTag)"true"))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 6
0
        public void ReadTableRecord(MemoryStream ms)
        {
            _validationStatus = OTFile.OTFileValidation_PartialValidation;

            if (ms.Length - ms.Position < TableRecordSize)
            {
                _validationStatus |= OTFile.OTFileValidation_StructureLengthOutOfRange;
            }

            try
            {
                _tableTag    = OTTag.ReadTag(ms);
                _checksum    = OTFile.ReadUInt32(ms);
                _tableOffset = OTFile.ReadUInt32(ms);
                _tableLength = OTFile.ReadUInt32(ms);
            }
            catch (OTDataIncompleteReadException e)
            {
                _validationStatus |= OTFile.OTFileValidation_ReadTrunctated;
                throw new OTRecordParseException("OT parse error: unable to read table record", e);
            }
        } // ReadTableRecord
Exemplo n.º 7
0
        public void ReadOffsetTable(MemoryStream ms, uint fileOffset)
        {
            // offset was validated as in bounds by upstream caller; set read position
            _offsetInFile = fileOffset;
            ms.Seek((long)fileOffset, SeekOrigin.Begin);

            _validationStatus = OTFile.OTFileValidation_PartialValidation;

            // check length to read sfnt version tag and read
            if (_offsetInFile + 4 > ms.Length)
            {
                _validationStatus |= OTFile.OTFileValidation_OffsetTableLengthOutOfRange | OTFile.OTFileValidation_StructureLengthOutOfRange;
            }
            try
            {
                _sfntTag = OTTag.ReadTag(ms);
            }
            catch (OTDataIncompleteReadException e)
            {
                _validationStatus |= OTFile.OTFileValidation_ReadTrunctated;
                throw new OTTableParseException("OT parse error: unable to read Offset Table", e);
            }


            // check that the sfnt version is supported before continuing
            if (!OTFont.IsSupportedSfntVersion(_sfntTag))
            {
                _validationStatus |= OTFile.OTFileValidation_SfntVersionNotSupported;
                throw new OTTableParseException("OT parse error: font resource has an unsupported sfnt version");
            }


            // check length to read remaining header fields, then read
            if (_offsetInFile + 12 > ms.Length)
            {
                _validationStatus |= OTFile.OTFileValidation_OffsetTableLengthOutOfRange | OTFile.OTFileValidation_StructureLengthOutOfRange;
            }
            try
            {
                _numTables     = OTFile.ReadUInt16(ms);
                _searchRange   = OTFile.ReadUInt16(ms);
                _entrySelector = OTFile.ReadUInt16(ms);
                _rangeShift    = OTFile.ReadUInt16(ms);
            }
            catch (OTDataIncompleteReadException e)
            {
                _validationStatus |= OTFile.OTFileValidation_ReadTrunctated;
                throw new OTTableParseException("OT parse error: unable to read Offset Table", e);
            }


            // check length to read encoding records array, then read the records
            if (_offsetInFile + 12 + _numTables * TableRecord.TableRecordSize > ms.Length)
            {
                _validationStatus |= OTFile.OTFileValidation_OffsetTableLengthOutOfRange | OTFile.OTFileValidation_StructureLengthOutOfRange;
            }
            _tableRecords = new TableRecord[_numTables]; // constructs struct records with default values
            _tableMap     = new Dictionary <string, uint>();
            try
            {
                for (uint i = 0; i < _numTables; i++)
                {
                    _tableRecords[i].ReadTableRecord(ms);
                    try
                    {
                        _tableMap.Add(_tableRecords[i].Tag.ToString(), i);
                    }
                    catch (ArgumentException)
                    {
                        // duplicate tag; first one wins
                        _validationStatus |= OTFile.OTFileValidation_StructureHasDuplicateEntries;
                    }
                }
            }
            catch (OTDataIncompleteReadException e)
            {
                _validationStatus |= OTFile.OTFileValidation_ReadTrunctated;
                throw new OTTableParseException("OT parse error: unable to read Offset Table", e);
            }
        } // ReadOffsetTable
Exemplo n.º 8
0
        public bool ContainsTable(OTTag tag)
        {
            uint?idx = _offsetTable.GetTableRecordIndex(tag);

            return(idx.HasValue);
        }
Exemplo n.º 9
0
        public static bool IsKnownTableType(OTTag tag)
        {
            switch (tag.ToString())
            {
            // OT tables
            case "avar":
            case "BASE":
            case "CBDT":
            case "CBLC":
            case "CFF ":
            case "CFF2":
            case "cmap":
            case "COLR":
            case "CPAL":
            case "cvar":
            case "cvt ":
            case "DSIG":
            case "EBDT":
            case "EBLC":
            case "EBSC":
            case "fpgm":
            case "fvar":
            case "gasp":
            case "GDEF":
            case "glyf":
            case "GPOS":
            case "GSUB":
            case "gvar":
            case "hdmx":
            case "head":
            case "hhea":
            case "hmtx":
            case "HVAR":
            case "JSTF":
            case "kern":
            case "loca":
            case "LTSH":
            case "MATH":
            case "maxp":
            case "MERG":
            case "meta":
            case "MVAR":
            case "name":
            case "OS/2":
            case "PCLT":
            case "post":
            case "prep":
            case "sbix":
            case "STAT":
            case "SVG ":
            case "VDMX":
            case "vhea":
            case "vmtx":
            case "VORG":
            case "VVAR":

            // Apple-specific tables
            case "acnt":
            case "ankr":
            case "bdat":
            case "bhed":
            case "bloc":
            case "bsln":
            case "fdsc":
            case "feat":
            case "fmtx":
            case "fond":
            case "gcid":
            case "hsty":
            case "just":
            case "lcar":
            case "ltag":
            case "mort":
            case "morx":
            case "opbd":
            case "prop":
            case "trak":
            case "xref":
            case "Zapf":

            // Graphite-specific tables
            case "Feat":
            case "Glat":
            case "Gloc":
            case "Sill":
            case "Silf":

            // VOLT source table
            case "TSIV":

            // VTT source tables
            case "TSI0":
            case "TSI1":
            case "TSI2":
            case "TSI3":
            case "TSI5":
                return(true);

            default:
                return(false);
            }
        }
Exemplo n.º 10
0
        public void ReadFromFile(FileInfo fileInfo)
        {
            // save fileinfo for later reference
            _fi = fileInfo;

            // Check that file exists. (Handle FileStream exception in advance.)
            if (!_fi.Exists)
            {
                throw new FileNotFoundException("File " + _fi.FullName + " was not found.", _fi.FullName);
            }

            // read file as a byte array, then construct memory stream from byte array
            {
                byte[] bytes;
                try
                {
                    // File.ReadAllBytes opens a filestream and then ensures it is closed
                    bytes = File.ReadAllBytes(_fi.FullName);
                    _ms   = new MemoryStream(bytes, 0, bytes.Length, false, true);
                }
                catch (IOException e)
                {
                    throw e;
                }
            }

            // Read sfntVersion tag
            try
            {
                _sfntVersionTag = OTTag.ReadTag(_ms);
            }
            catch (OTDataIncompleteReadException e)
            {
                throw new OTFileParseException("OT parse error: unable to read sfnt tag", e);
            }

            // supported format?
            if (!IsSupportedSfntVersion(_sfntVersionTag))
            {
                _validationStatus = OTFileValidation_SfntVersionNotSupported;
                throw new OTFileParseException("OT parse error: not a known and supported sfnt type (sfnt tag = " + _sfntVersionTag.ToString());
            }

            _validationStatus = OTFile.OTFileValidation_PartialValidation;

            // TTC? get TTC header
            if (_sfntVersionTag == (OTTag)"ttcf")
            {
                try
                {
                    _ttcHeader.ReadTtcHeader(_ms); // will set read position to start of file
                }
                catch (OTException e)
                {
                    _validationStatus = _ttcHeader.ValidationStatus;
                    throw new OTFileParseException("OT parse error: unable to read TTC header", e);
                }

                _numFonts = _ttcHeader.NumFonts;
            }
            else
            {
                _numFonts = 1;
            }

            // initialize new font object(s); this will get the font headers for each font resource
            _fonts = new OTFont[_numFonts];
            if (_sfntVersionTag == (OTTag)"ttcf")
            {
                for (uint i = 0; i < _numFonts; i++)
                {
                    _fonts[i] = new OTFont(this, _ttcHeader.OffsetTableOffsets[i], i);
                }
            }
            else // single font
            {
                _fonts[0] = new OTFont(this);
            }

            // got TTC header; created font objects and got all the header info for each
            // caller can now poke individual fonts to get additional info as needed

            // finally, get validation status on ttc header, offset tables
            _validationStatusOfFonts = new UInt64[_numFonts];
            for (int i = 0; i < _numFonts; i++)
            {
                // simple validations -- full validation on a table-by-table basis
                _fonts[i].Validate(_ms.Length, true);
            }

            if (_sfntVersionTag == (OTTag)"ttcf")
            {
                // treat ttc header validation as part of the file validation
                _validationStatus |= _ttcHeader.Validate(_ms.Length);

                // check for validation issues in any of the font resources
                for (int i = 0; i < _numFonts; i++)
                {
                    if ((_fonts[i].ValidationStatus & OTFile.OTFileValidation_ValidationIssueMask) != 0)
                    {
                        _validationStatus |= OTFile.OTFileValidation_ValidationIssueInChildStructure;
                        break;
                    }
                }
            }
            else
            {
                // treat font resource validation as part of the file validation
                _validationStatus |= (_fonts[0].ValidationStatus & OTFile.OTFileValidation_ValidationIssueMask);
            }
        } // ReadFromFile