Beispiel #1
0
        public unsafe static NameData ReadNames(DataReader reader, TableRecord[] tables)
        {
            if (!SeekToTable(reader, tables, FourCC.Name))
            {
                return(default(NameData));
            }

            // read header
            var currentOffset = reader.Position;
            var format        = reader.ReadUInt16BE();
            var count         = reader.ReadUInt16BE();
            var dataOffset    = currentOffset + reader.ReadUInt16BE();

            // read name records, filtering out non-Unicode and platforms we don't know about
            var stringData      = stackalloc StringData[count];
            var stringDataCount = 0;

            for (int i = 0; i < count; i++)
            {
                var platform = reader.ReadUInt16BE();
                var encoding = reader.ReadUInt16BE();
                var language = reader.ReadUInt16BE();
                var name     = reader.ReadUInt16BE();
                var length   = reader.ReadUInt16BE();
                var offset   = reader.ReadUInt16BE();

                // we only support Unicode strings
                if (platform == PlatformID.Microsoft)
                {
                    if (encoding != WindowsEncoding.UnicodeBmp && encoding != WindowsEncoding.UnicodeFull)
                    {
                        continue;
                    }
#if !NETCORE
                    if (language != CultureInfo.CurrentCulture.LCID)
                    {
                        continue;
                    }
#endif
                }
                else if (platform != PlatformID.Unicode)
                {
                    continue;
                }

                stringData[stringDataCount++] = new StringData {
                    Name   = name,
                    Offset = offset,
                    Length = length
                };
            }

            // find strings we care about and extract them from the blob
            var nameData = new NameData();
            for (int i = 0; i < stringDataCount; i++)
            {
                var data = stringData[i];
                switch (data.Name)
                {
                case NameID.FamilyName: nameData.FamilyName = ExtractString(reader, dataOffset, data); break;

                case NameID.SubfamilyName: nameData.SubfamilyName = ExtractString(reader, dataOffset, data); break;

                case NameID.UniqueID: nameData.UniqueID = ExtractString(reader, dataOffset, data); break;

                case NameID.FullName: nameData.FullName = ExtractString(reader, dataOffset, data); break;

                case NameID.Version: nameData.Version = ExtractString(reader, dataOffset, data); break;

                case NameID.Description: nameData.Description = ExtractString(reader, dataOffset, data); break;

                case NameID.TypographicFamilyName: nameData.TypographicFamilyName = ExtractString(reader, dataOffset, data); break;

                case NameID.TypographicSubfamilyName: nameData.TypographicSubfamilyName = ExtractString(reader, dataOffset, data); break;
                }
            }

            return(nameData);
        }
Beispiel #2
0
        public unsafe static NameData ReadNames(DataReader reader, TableRecord[] tables)
        {
            if (!SeekToTable(reader, tables, FourCC.Name))
                return default(NameData);

            // read header
            var currentOffset = reader.Position;
            var format = reader.ReadUInt16BE();
            var count = reader.ReadUInt16BE();
            var dataOffset = currentOffset + reader.ReadUInt16BE();

            // read name records, filtering out non-Unicode and platforms we don't know about
            var stringData = stackalloc StringData[count];
            var stringDataCount = 0;
            for (int i = 0; i < count; i++)
            {
                var platform = reader.ReadUInt16BE();
                var encoding = reader.ReadUInt16BE();
                var language = reader.ReadUInt16BE();
                var name = reader.ReadUInt16BE();
                var length = reader.ReadUInt16BE();
                var offset = reader.ReadUInt16BE();

                // we only support Unicode strings
                if (platform == PlatformID.Microsoft)
                {
                    if (encoding != WindowsEncoding.UnicodeBmp && encoding != WindowsEncoding.UnicodeFull)
                        continue;

                    if (language != CultureInfo.CurrentCulture.LCID)
                        continue;
                }
                else if (platform != PlatformID.Unicode)
                    continue;

                stringData[stringDataCount++] = new StringData
                {
                    Name = name,
                    Offset = offset,
                    Length = length
                };
            }

            // find strings we care about and extract them from the blob
            var nameData = new NameData();
            for (int i = 0; i < stringDataCount; i++)
            {
                var data = stringData[i];
                switch (data.Name)
                {
                    case NameID.FamilyName: nameData.FamilyName = ExtractString(reader, dataOffset, data); break;
                    case NameID.SubfamilyName: nameData.SubfamilyName = ExtractString(reader, dataOffset, data); break;
                    case NameID.UniqueID: nameData.UniqueID = ExtractString(reader, dataOffset, data); break;
                    case NameID.FullName: nameData.FullName = ExtractString(reader, dataOffset, data); break;
                    case NameID.Version: nameData.Version = ExtractString(reader, dataOffset, data); break;
                    case NameID.Description: nameData.Description = ExtractString(reader, dataOffset, data); break;
                    case NameID.TypographicFamilyName: nameData.TypographicFamilyName = ExtractString(reader, dataOffset, data); break;
                    case NameID.TypographicSubfamilyName: nameData.TypographicSubfamilyName = ExtractString(reader, dataOffset, data); break;
                }
            }

            return nameData;
        }