using System.Linq;
using System.IO;
using System.Xml;
using FSO.Files.Formats.IFF;
namespace FSO.Files.Formats.OTF
{
///
/// Object Tuning File (OTF) is an SGML format which defines tuning constants.
///
public class OTFFile
{
public XmlDocument Document;
///
/// Constructs an OTF instance from a filepath.
///
/// Path to the OTF.
public OTFFile(string filepath)
{
using (var stream = File.OpenRead(filepath))
{
this.Read(stream);
}
}
public OTFFile()
{
//you can also create empty OTFs!
}
public OTFTable[] Tables;
///
/// Gets an OTFTable instance from an ID.
///
/// The ID of the table.
/// An OTFTable instance.
public OTFTable GetTable(int ID)
{
return Tables.FirstOrDefault(x => x?.ID == ID);
}
///
/// Reads an OTF from a stream.
///
/// The stream to read from.
public void Read(Stream stream)
{
var doc = new XmlDocument();
doc.Load(stream);
if (IffFile.RETAIN_CHUNK_DATA) Document = doc;
var tables = doc.GetElementsByTagName("T");
Tables = new OTFTable[tables.Count];
for (var i = 0; i < tables.Count; i++)
{
var table = tables.Item(i);
if (table.NodeType == XmlNodeType.Comment) continue;
var tableEntry = new OTFTable();
tableEntry.ID = int.Parse(table.Attributes["i"].Value);
tableEntry.Name = table.Attributes["n"].Value;
var numKeys = table.ChildNodes.Count;
tableEntry.Keys = new OTFTableKey[numKeys];
for (var x = 0; x < numKeys; x++)
{
var key = table.ChildNodes[x];
if (key.NodeType == XmlNodeType.Comment) continue;
var keyEntry = new OTFTableKey();
keyEntry.ID = int.Parse(key.Attributes["i"].Value);
keyEntry.Label = key.Attributes["l"].Value;
keyEntry.Value = int.Parse(key.Attributes["v"].Value);
tableEntry.Keys[x] = keyEntry;
}
Tables[i] = tableEntry;
}
}
}
public class OTFTable
{
public int ID;
public string Name;
public OTFTableKey[] Keys;
public OTFTableKey GetKey(int id)
{
return Keys.FirstOrDefault(x => x?.ID == id);
}
}
public class OTFTableKey
{
public int ID;
public string Label;
public int Value;
}
}