Ejemplo n.º 1
0
        public void Serialize(BinaryWriter writer, long startOffset, SerializationInfo additionalInfo)
        {
            writer.BaseStream.Position = startOffset;
            DataTypeConverter.WriteUShort(writer, Version);
            var environments =
                Subtables.SelectMany(subtable => subtable.Environments)
                .OrderBy(environment => environment.PlatformId)
                .ThenBy(environment => environment.EncodingId)
                .ToList();

            DataTypeConverter.WriteUShort(writer, (ushort)environments.Count);
            var startOffsetOfEnvironments = writer.BaseStream.Position;

            writer.BaseStream.Position += environments.Count *
                                          (DataTypeLength.UShort + DataTypeLength.UShort + DataTypeLength.ULong);

            foreach (var subtable in Subtables)
            {
                // 4byte padding
                if (writer.BaseStream.Position % 4 != 0)
                {
                    writer.BaseStream.Position += (4 - writer.BaseStream.Position % 4);
                }

                subtable.Environments.ForEach(
                    environment => environment.SubtableOffset = (uint)(writer.BaseStream.Position - startOffset));

                subtable.Serialize(writer, writer.BaseStream.Position, additionalInfo);
            }

            var endOffset = writer.BaseStream.Position;

            writer.BaseStream.Position = startOffsetOfEnvironments;
            environments.ForEach(environment =>
            {
                DataTypeConverter.WriteUShort(writer, environment.PlatformId);
                DataTypeConverter.WriteUShort(writer, environment.EncodingId);
                DataTypeConverter.WriteULong(writer, (ushort)environment.SubtableOffset);
            });

            // Restore writer position
            writer.BaseStream.Position = endOffset;
        }