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