Code cleanup for font table.
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#pragma warning disable IDE0049
|
||||
namespace Cryville.Common.Font {
|
||||
public abstract class FontTable<T> {
|
||||
protected UInt32 Offset { get; private set; }
|
||||
@@ -25,14 +26,17 @@ namespace Cryville.Common.Font {
|
||||
readonly UInt16 minorVersion;
|
||||
readonly UInt32 numFonts;
|
||||
readonly List<UInt32> tableDirectoryOffsets = new List<UInt32>();
|
||||
#pragma warning disable IDE0052 // Reserved
|
||||
readonly String dsigTag;
|
||||
readonly UInt32 dsigLength;
|
||||
readonly UInt32 dsigOffset;
|
||||
#pragma warning restore IDE0052 // Reserved
|
||||
public TTCHeader(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
ttcTag = reader.ReadTag();
|
||||
if (ttcTag != "ttcf") throw new NotImplementedException();
|
||||
if (ttcTag != "ttcf") throw new NotSupportedException();
|
||||
majorVersion = reader.ReadUInt16();
|
||||
minorVersion = reader.ReadUInt16();
|
||||
if (minorVersion != 0) throw new NotSupportedException();
|
||||
numFonts = reader.ReadUInt32();
|
||||
for (UInt32 i = 0; i < numFonts; i++) tableDirectoryOffsets.Add(reader.ReadUInt32());
|
||||
if (majorVersion == 2) {
|
||||
@@ -52,12 +56,16 @@ namespace Cryville.Common.Font {
|
||||
public sealed class TableDirectory : FontTable<TableRecord, object> {
|
||||
readonly UInt32 sfntVersion;
|
||||
readonly UInt16 numTables;
|
||||
#pragma warning disable IDE0052 // Reserved
|
||||
readonly UInt16 searchRange;
|
||||
readonly UInt16 entrySelector;
|
||||
readonly UInt16 rangeShift;
|
||||
#pragma warning restore IDE0052 // Reserved
|
||||
readonly List<TableRecord> tableRecords = new List<TableRecord>();
|
||||
public TableDirectory(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
sfntVersion = reader.ReadUInt32();
|
||||
if (sfntVersion != 0x00010000 && sfntVersion != 0x4F54544F &&
|
||||
sfntVersion != 0x74727565 && sfntVersion != 0x74797031) throw new NotSupportedException();
|
||||
numTables = reader.ReadUInt16();
|
||||
searchRange = reader.ReadUInt16();
|
||||
entrySelector = reader.ReadUInt16();
|
||||
@@ -99,48 +107,63 @@ namespace Cryville.Common.Font {
|
||||
count = reader.ReadUInt16();
|
||||
storageOffset = reader.ReadUInt16();
|
||||
for (UInt16 i = 0; i < count; i++)
|
||||
nameRecord.Add(new NameRecord {
|
||||
platformID = reader.ReadUInt16(),
|
||||
encodingID = reader.ReadUInt16(),
|
||||
languageID = reader.ReadUInt16(),
|
||||
nameID = (NameID)reader.ReadUInt16(),
|
||||
length = reader.ReadUInt16(),
|
||||
stringOffset = reader.ReadUInt16(),
|
||||
});
|
||||
nameRecord.Add(new NameRecord(
|
||||
reader.ReadUInt16(),
|
||||
reader.ReadUInt16(),
|
||||
reader.ReadUInt16(),
|
||||
(NameID)reader.ReadUInt16(),
|
||||
reader.ReadUInt16(),
|
||||
reader.ReadUInt16()
|
||||
));
|
||||
if (version == 1) {
|
||||
langTagCount = reader.ReadUInt16();
|
||||
for (UInt16 i = 0; i < langTagCount; i++)
|
||||
langTagRecord.Add(new LangTagRecord {
|
||||
length = reader.ReadUInt16(),
|
||||
langTagOffset = reader.ReadUInt16(),
|
||||
});
|
||||
langTagRecord.Add(new LangTagRecord(
|
||||
reader.ReadUInt16(),
|
||||
reader.ReadUInt16()
|
||||
));
|
||||
}
|
||||
foreach (var i in nameRecord)
|
||||
i.Load(reader, offset + storageOffset);
|
||||
if (version == 1) {
|
||||
foreach (var i in langTagRecord)
|
||||
i.Load(reader, offset + storageOffset);
|
||||
}
|
||||
UInt32 origin = (UInt32)reader.BaseStream.Position;
|
||||
for (int i = 0; i < nameRecord.Count; i++) nameRecord[i] = nameRecord[i].Load(reader, origin);
|
||||
for (int i = 0; i < langTagRecord.Count; i++) langTagRecord[i] = langTagRecord[i].Load(reader, origin);
|
||||
}
|
||||
public sealed override IReadOnlyList<NameRecord> GetItems() {
|
||||
return nameRecord;
|
||||
}
|
||||
}
|
||||
public struct NameRecord {
|
||||
public UInt16 platformID;
|
||||
public UInt16 encodingID;
|
||||
public UInt16 languageID;
|
||||
public NameID nameID;
|
||||
public UInt16 length;
|
||||
public UInt16 stringOffset;
|
||||
public String value { get; private set; }
|
||||
public NameRecord Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + stringOffset;
|
||||
public class NameRecord {
|
||||
public UInt16 PlatformID { get; private set; }
|
||||
public UInt16 EncodingID { get; private set; }
|
||||
public UInt16 LanguageID { get; private set; }
|
||||
public NameID NameID { get; private set; }
|
||||
public UInt16 Length { get; private set; }
|
||||
public UInt16 StringOffset { get; private set; }
|
||||
public String Value { get; private set; }
|
||||
public NameRecord(UInt16 platformID, UInt16 encodingID, UInt16 languageID, NameID nameID, UInt16 length, UInt16 stringOffset) {
|
||||
PlatformID = platformID;
|
||||
EncodingID = encodingID;
|
||||
LanguageID = languageID;
|
||||
NameID = nameID;
|
||||
Length = length;
|
||||
StringOffset = stringOffset;
|
||||
}
|
||||
public void Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + StringOffset;
|
||||
Encoding encoding;
|
||||
switch (platformID) {
|
||||
case 0: encoding = Encoding.BigEndianUnicode; break;
|
||||
case 3: encoding = Encoding.BigEndianUnicode; break;
|
||||
default: return this;
|
||||
try {
|
||||
switch (PlatformID) {
|
||||
case 0: encoding = Encoding.BigEndianUnicode; break;
|
||||
case 1: encoding = Encoding.GetEncoding(10000 + EncodingID); break;
|
||||
case 3: encoding = Encoding.BigEndianUnicode; break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
value = encoding.GetString(reader.ReadBytes(length));
|
||||
return this;
|
||||
catch (NotSupportedException) { return; }
|
||||
catch (ArgumentException) { return; }
|
||||
Value = encoding.GetString(reader.ReadBytes(Length));
|
||||
}
|
||||
}
|
||||
public enum NameID : UInt16 {
|
||||
@@ -171,47 +194,58 @@ namespace Cryville.Common.Font {
|
||||
DarkBackgroundPalette = 24,
|
||||
VariationsPostScriptNamePrefix = 25,
|
||||
}
|
||||
public struct LangTagRecord {
|
||||
public UInt16 length;
|
||||
public UInt16 langTagOffset;
|
||||
public String value { get; private set; }
|
||||
public LangTagRecord Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + langTagOffset;
|
||||
value = Encoding.BigEndianUnicode.GetString(reader.ReadBytes(length));
|
||||
return this;
|
||||
public class LangTagRecord {
|
||||
public UInt16 Length { get; private set; }
|
||||
public UInt16 LangTagOffset { get; private set; }
|
||||
public String Value { get; private set; }
|
||||
public LangTagRecord(UInt16 length, UInt16 langTagOffset) {
|
||||
Length = length;
|
||||
LangTagOffset = langTagOffset;
|
||||
}
|
||||
public void Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + LangTagOffset;
|
||||
Value = Encoding.BigEndianUnicode.GetString(reader.ReadBytes(Length));
|
||||
}
|
||||
}
|
||||
public sealed class MetaTable : FontTable<DataMap> {
|
||||
readonly UInt32 version;
|
||||
#pragma warning disable IDE0052 // Reserved
|
||||
readonly UInt32 flags;
|
||||
#pragma warning restore IDE0052 // Reserved
|
||||
readonly UInt32 dataMapCount;
|
||||
readonly List<DataMap> dataMaps = new List<DataMap>();
|
||||
public MetaTable(BinaryReader reader, UInt32 offset) : base(reader, offset) {
|
||||
version = reader.ReadUInt32();
|
||||
if (version != 1) throw new NotSupportedException();
|
||||
flags = reader.ReadUInt32();
|
||||
reader.ReadUInt32();
|
||||
dataMapCount = reader.ReadUInt32();
|
||||
for (UInt32 i = 0; i < dataMapCount; i++)
|
||||
dataMaps.Add(new DataMap {
|
||||
tag = reader.ReadTag(),
|
||||
dataOffset = reader.ReadUInt32(),
|
||||
dataLength = reader.ReadUInt32(),
|
||||
});
|
||||
for (int i = 0; i < dataMaps.Count; i++) dataMaps[i] = dataMaps[i].Load(reader, offset);
|
||||
dataMaps.Add(new DataMap (
|
||||
reader.ReadTag(),
|
||||
reader.ReadUInt32(),
|
||||
reader.ReadUInt32()
|
||||
));
|
||||
foreach (var i in dataMaps)
|
||||
i.Load(reader, offset);
|
||||
}
|
||||
public sealed override IReadOnlyList<DataMap> GetItems() {
|
||||
return dataMaps;
|
||||
}
|
||||
}
|
||||
public struct DataMap {
|
||||
public String tag;
|
||||
public UInt32 dataOffset;
|
||||
public UInt32 dataLength;
|
||||
public String value { get; private set; }
|
||||
public DataMap Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + dataOffset;
|
||||
value = Encoding.ASCII.GetString(reader.ReadBytes((int)dataLength));
|
||||
return this;
|
||||
public class DataMap {
|
||||
public String Tag { get; private set; }
|
||||
public UInt32 DataOffset { get; private set; }
|
||||
public UInt32 DataLength { get; private set; }
|
||||
public String Value { get; private set; }
|
||||
public DataMap(String tag, UInt32 dataOffset, UInt32 dataLength) {
|
||||
Tag = tag;
|
||||
DataOffset = dataOffset;
|
||||
DataLength = dataLength;
|
||||
}
|
||||
public void Load(BinaryReader reader, UInt32 origin) {
|
||||
reader.BaseStream.Position = origin + DataOffset;
|
||||
Value = Encoding.ASCII.GetString(reader.ReadBytes((int)DataLength));
|
||||
}
|
||||
}
|
||||
public static class BinaryReaderExtensions {
|
||||
|
Reference in New Issue
Block a user