NameTableClass.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. #pragma warning disable CS3001, CS3002, CS3003, CS1591
  7. namespace FastReport.Fonts
  8. {
  9. /// <summary>
  10. /// Name table keep human friendly description about font properties, including font names, author and copyright notes
  11. /// </summary>
  12. public class NameTableClass : TrueTypeTable, ICollection
  13. {
  14. #region "Structure dfinition"
  15. public enum NameID
  16. {
  17. CopyrightNotice = 0,
  18. FamilyName = 1,
  19. SubFamilyName = 2,
  20. UniqueID = 3,
  21. FullName = 4,
  22. Version = 5,
  23. PostscriptName = 6,
  24. Trademark = 7,
  25. Manufacturer = 8,
  26. Designer = 9,
  27. Description = 10,
  28. URL_Vendor = 11,
  29. URL_Designer = 12,
  30. LicenseDescription = 13,
  31. LicenseInfoURL = 14,
  32. PreferredFamily = 16,
  33. PreferredSubFamily = 17,
  34. CompatibleFull = 18,
  35. SampleText = 19,
  36. PostscriptCID = 20,
  37. WWS_Family_Name = 21,
  38. WWS_SubFamily_Name = 22
  39. }
  40. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  41. public struct NamingTableHeader
  42. {
  43. [FieldOffset(0)]
  44. public ushort TableVersion;
  45. [FieldOffset(2)]
  46. public ushort Count;
  47. [FieldOffset(4)]
  48. public ushort stringOffset;
  49. }
  50. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  51. public struct NamingRecord
  52. {
  53. [FieldOffset(0)]
  54. public ushort PlatformID;
  55. [FieldOffset(2)]
  56. public ushort EncodingID;
  57. [FieldOffset(4)]
  58. public ushort LanguageID;
  59. [FieldOffset(6)]
  60. public ushort NameID;
  61. [FieldOffset(8)]
  62. public ushort Length;
  63. [FieldOffset(10)]
  64. public ushort Offset;
  65. }
  66. public readonly struct LangTagReccord
  67. {
  68. public readonly ushort lenght;
  69. public readonly ushort offset;
  70. public LangTagReccord(ushort lenght, ushort offset)
  71. {
  72. this.lenght = lenght;
  73. this.offset = offset;
  74. }
  75. };
  76. #endregion
  77. NamingTableHeader name_header;
  78. int langTagCount = 0;
  79. NamingRecord[] name_rec;
  80. LangTagReccord[] tags;
  81. Dictionary<NameID, List<string>> names = new Dictionary<NameID, List<string>>();
  82. public Dictionary<NameID, List<string>> NamesDict
  83. {
  84. get
  85. {
  86. return names;
  87. }
  88. }
  89. public int Count
  90. {
  91. get
  92. {
  93. return name_header.Count;
  94. }
  95. }
  96. public object SyncRoot
  97. {
  98. get
  99. {
  100. return null; // Names.SyncRoot;
  101. }
  102. }
  103. public bool IsSynchronized
  104. {
  105. get
  106. {
  107. return true; // Names.IsSynchronized;
  108. }
  109. }
  110. private void ChangeEndian()
  111. {
  112. name_header.TableVersion = SwapUInt16(name_header.TableVersion);
  113. name_header.Count = SwapUInt16(name_header.Count);
  114. name_header.stringOffset = SwapUInt16(name_header.stringOffset);
  115. }
  116. internal override void Load(FontStream stream)
  117. {
  118. stream.Position = this.Offset;
  119. name_header.TableVersion = stream.ReadUInt16();
  120. name_header.Count = stream.ReadUInt16();
  121. name_header.stringOffset = stream.ReadUInt16();
  122. name_rec = new NamingRecord[name_header.Count];
  123. for (int i = 0; i < name_header.Count; i++)
  124. {
  125. name_rec[i].PlatformID = stream.ReadUInt16();
  126. name_rec[i].EncodingID = stream.ReadUInt16();
  127. name_rec[i].LanguageID = stream.ReadUInt16();
  128. name_rec[i].NameID = stream.ReadUInt16();
  129. name_rec[i].Length = stream.ReadUInt16();
  130. name_rec[i].Offset = stream.ReadUInt16();
  131. }
  132. if (name_header.TableVersion == 1)
  133. {
  134. this.langTagCount = stream.ReadUInt16();
  135. tags = new LangTagReccord[langTagCount];
  136. for (int i = 0; i < this.langTagCount; i++)
  137. {
  138. tags[i] = new LangTagReccord(stream.ReadUInt16(), stream.ReadUInt16());
  139. }
  140. }
  141. // Tables ready. Let's look into them
  142. byte[] Temp;
  143. string value;
  144. int j;
  145. for (int i = 0; i < name_header.Count; i++)
  146. {
  147. if (name_rec[i].PlatformID == 1)
  148. {
  149. Temp = new byte[name_rec[i].Length];
  150. stream.Position = this.Offset + name_header.stringOffset + name_rec[i].Offset;
  151. stream.Read(Temp, name_rec[i].Length);
  152. value = Encoding.GetEncoding("utf-8").GetString(Temp);
  153. }
  154. else
  155. {
  156. Temp = new byte[name_rec[i].Length];
  157. stream.Position = this.Offset + name_header.stringOffset + name_rec[i].Offset;
  158. stream.Read(Temp, name_rec[i].Length);
  159. value = Encoding.GetEncoding(1201).GetString(Temp);
  160. }
  161. NameID id = (NameID)name_rec[i].NameID;
  162. List<string> values;
  163. if (!names.ContainsKey(id))
  164. {
  165. values = new List<string>();
  166. names.Add(id, values);
  167. }
  168. else
  169. {
  170. values = (List<string>)names[id];
  171. }
  172. if (!values.Contains(value))
  173. {
  174. values.Add(value);
  175. }
  176. }
  177. }
  178. public void CopyTo(Array array, int index)
  179. {
  180. throw new NotImplementedException();
  181. }
  182. public IEnumerator GetEnumerator()
  183. {
  184. return names.Keys.GetEnumerator();
  185. }
  186. public ICollection this[NameID Index]
  187. {
  188. get
  189. {
  190. ICollection rec = null;
  191. if (names.ContainsKey(Index))
  192. {
  193. rec = names[Index] as ICollection;
  194. }
  195. return rec;
  196. }
  197. }
  198. public NameTableClass(TrueTypeTable src) : base(src) { }
  199. }
  200. }
  201. #pragma warning restore