using System.IO;
using System.Linq;

namespace Cryville.Common.Font {
	public abstract class Typeface {
		public FileInfo File { get; private set; }
		public int IndexInFile { get; private set; }
		public string FamilyName { get; protected set; }
		public string SubfamilyName { get; protected set; }
		public string FullName { get; protected set; }
		protected abstract void GetName(BinaryReader reader);

		public Typeface(BinaryReader reader, FileInfo file, int index) {
			File = file;
			IndexInFile = index;
			GetName(reader);
		}
	}
	public class TypefaceTTF : Typeface {
		public TypefaceTTF(BinaryReader reader, FileInfo file, int index)
			: base(reader, file, index) { }

		protected override void GetName(BinaryReader reader) {
			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());
			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();
			FullName = (from i in nameTable.GetItems() where i.NameID == NameID.FullFontName && i.Value != null select i.Value).First();
		}
	}
}
