OS2WindowsMetricsClass.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace FastReport.Fonts
  4. {
  5. /// <summary>
  6. /// OS/2 and Windows Metrics table
  7. /// </summary>
  8. public class OS2WindowsMetricsClass : TrueTypeTable
  9. {
  10. #region "Type definition"
  11. /// <summary>
  12. /// Description of FontTextMetric structure
  13. /// </summary>
  14. [StructLayout(LayoutKind.Sequential)]
  15. public struct FontTextMetric
  16. {
  17. public int tmHeight;
  18. public int tmAscent;
  19. public int tmDescent;
  20. public int tmInternalLeading;
  21. public int tmExternalLeading;
  22. public int tmAveCharWidth;
  23. public int tmMaxCharWidth;
  24. public int tmWeight;
  25. public int tmOverhang;
  26. public int tmDigitizedAspectX;
  27. public int tmDigitizedAspectY;
  28. public char tmFirstChar;
  29. public char tmLastChar;
  30. public char tmDefaultChar;
  31. public char tmBreakChar;
  32. public byte tmItalic;
  33. public byte tmUnderlined;
  34. public byte tmStruckOut;
  35. public byte tmPitchAndFamily;
  36. public byte tmCharSet;
  37. }
  38. /// <summary>
  39. /// Description of FontPanose structure
  40. /// </summary>
  41. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  42. public struct FontPanose
  43. {
  44. public byte bFamilyType;
  45. public byte bSerifStyle;
  46. public byte bWeight;
  47. public byte bProportion;
  48. public byte bContrast;
  49. public byte bStrokeVariation;
  50. public byte ArmStyle;
  51. public byte bLetterform;
  52. public byte bMidline;
  53. public byte bXHeight;
  54. }
  55. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  56. public struct OS2WindowsMetrics
  57. {
  58. public ushort Version; // version number 0x0004
  59. public short xAvgCharWidth;
  60. public ushort usWeightClass;
  61. public ushort usWidthClass;
  62. public ushort fsType;
  63. public short ySubscriptXSize;
  64. public short ySubscriptYSize;
  65. public short ySubscriptXOffset;
  66. public short ySubscriptYOffset;
  67. public short ySuperscriptXSize;
  68. public short ySuperscriptYSize;
  69. public short ySuperscriptXOffset;
  70. public short ySuperscriptYOffset;
  71. public short yStrikeoutSize;
  72. public short yStrikeoutPosition;
  73. public short sFamilyClass;
  74. #if false
  75. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  76. public byte[] panose; // = new byte[10];
  77. #else
  78. public FontPanose panose;
  79. #endif
  80. public uint ulUnicodeRange1;
  81. public uint ulUnicodeRange2;
  82. public uint ulUnicodeRange3;
  83. public uint ulUnicodeRange4;
  84. #if true
  85. public byte achVendID1;
  86. public byte achVendID2;
  87. public byte achVendID3;
  88. public byte achVendID4;
  89. #else
  90. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  91. public byte[] achVendID; // = new vyte[4];
  92. #endif
  93. public ushort fsSelection;
  94. public ushort usFirstCharIndex;
  95. public ushort usLastCharIndex;
  96. public short sTypoAscender;
  97. public short sTypoDescender;
  98. public short sTypoLineGap;
  99. public ushort usWinAscent;
  100. public ushort usWinDescent;
  101. public uint ulCodePageRange1;
  102. public uint ulCodePageRange2;
  103. public short sxHeight;
  104. public short sCapHeight;
  105. public ushort usDefaultChar;
  106. public ushort usBreakChar;
  107. public ushort usMaxContext;
  108. }
  109. #endregion
  110. internal OS2WindowsMetrics win_metrix;
  111. internal override void Load(FontStream stream)
  112. {
  113. stream.Position = this.Offset;
  114. win_metrix.Version = stream.ReadUInt16(); // version number 0x0004
  115. //public short
  116. win_metrix.xAvgCharWidth = stream.ReadInt16();
  117. win_metrix.usWeightClass = stream.ReadUInt16();
  118. win_metrix.usWidthClass = stream.ReadUInt16();
  119. win_metrix.fsType = stream.ReadUInt16();
  120. win_metrix.ySubscriptXSize = stream.ReadInt16();
  121. win_metrix.ySubscriptYSize = stream.ReadInt16();
  122. win_metrix.ySubscriptXOffset = stream.ReadInt16();
  123. win_metrix.ySubscriptYOffset = stream.ReadInt16();
  124. win_metrix.ySuperscriptXSize = stream.ReadInt16();
  125. win_metrix.ySuperscriptYSize = stream.ReadInt16();
  126. win_metrix.ySuperscriptXOffset = stream.ReadInt16();
  127. win_metrix.ySuperscriptYOffset = stream.ReadInt16();
  128. win_metrix.yStrikeoutSize = stream.ReadInt16();
  129. win_metrix.yStrikeoutPosition = stream.ReadInt16();
  130. win_metrix.sFamilyClass = stream.ReadInt16();
  131. //FontPanose
  132. win_metrix.panose.bFamilyType = stream.ReadByte();
  133. win_metrix.panose.bSerifStyle = stream.ReadByte();
  134. win_metrix.panose.bWeight = stream.ReadByte();
  135. win_metrix.panose.bProportion = stream.ReadByte();
  136. win_metrix.panose.bContrast = stream.ReadByte();
  137. win_metrix.panose.bStrokeVariation = stream.ReadByte();
  138. win_metrix.panose.ArmStyle = stream.ReadByte();
  139. win_metrix.panose.bLetterform = stream.ReadByte();
  140. win_metrix.panose.bMidline = stream.ReadByte();
  141. win_metrix.panose.bXHeight = stream.ReadByte();
  142. win_metrix.ulUnicodeRange1 = stream.ReadUInt32();
  143. win_metrix.ulUnicodeRange2 = stream.ReadUInt32();
  144. win_metrix.ulUnicodeRange3 = stream.ReadUInt32();
  145. win_metrix.ulUnicodeRange4 = stream.ReadUInt32();
  146. win_metrix.achVendID1 = stream.ReadByte();
  147. win_metrix.achVendID2 = stream.ReadByte();
  148. win_metrix.achVendID3 = stream.ReadByte();
  149. win_metrix.achVendID4 = stream.ReadByte();
  150. win_metrix.fsSelection = stream.ReadUInt16();
  151. win_metrix.usFirstCharIndex = stream.ReadUInt16();
  152. win_metrix.usLastCharIndex = stream.ReadUInt16();
  153. win_metrix.sTypoAscender = stream.ReadInt16();
  154. win_metrix.sTypoDescender = stream.ReadInt16();
  155. win_metrix.sTypoLineGap = stream.ReadInt16();
  156. win_metrix.usWinAscent = stream.ReadUInt16();
  157. win_metrix.usWinDescent = stream.ReadUInt16();
  158. win_metrix.ulCodePageRange1 = stream.ReadUInt32();
  159. win_metrix.ulCodePageRange2 = stream.ReadUInt32();
  160. win_metrix.sxHeight = stream.ReadInt16();
  161. win_metrix.sCapHeight = stream.ReadInt16();
  162. win_metrix.usDefaultChar = stream.ReadUInt16();
  163. win_metrix.usBreakChar = stream.ReadUInt16();
  164. win_metrix.usMaxContext = stream.ReadUInt16();
  165. }
  166. internal override uint Save(FontStream source_not_used, FontStream stream, uint offset)
  167. {
  168. stream.Position = offset;
  169. stream.WriteUInt16(win_metrix.Version); // version number 0x0004
  170. stream.WriteInt16(win_metrix.xAvgCharWidth);
  171. stream.WriteUInt16(win_metrix.usWeightClass);
  172. stream.WriteUInt16(win_metrix.usWidthClass);
  173. stream.WriteUInt16(win_metrix.fsType);
  174. stream.WriteInt16(win_metrix.ySubscriptXSize);
  175. stream.WriteInt16(win_metrix.ySubscriptYSize);
  176. stream.WriteInt16(win_metrix.ySubscriptXOffset);
  177. stream.WriteInt16(win_metrix.ySubscriptYOffset);
  178. stream.WriteInt16(win_metrix.ySuperscriptXSize);
  179. stream.WriteInt16(win_metrix.ySuperscriptYSize);
  180. stream.WriteInt16(win_metrix.ySuperscriptXOffset);
  181. stream.WriteInt16(win_metrix.ySuperscriptYOffset);
  182. stream.WriteInt16(win_metrix.yStrikeoutSize);
  183. stream.WriteInt16(win_metrix.yStrikeoutPosition);
  184. stream.WriteInt16(win_metrix.sFamilyClass);
  185. stream.WriteByte(win_metrix.panose.bFamilyType);
  186. stream.WriteByte(win_metrix.panose.bSerifStyle);
  187. stream.WriteByte(win_metrix.panose.bWeight);
  188. stream.WriteByte(win_metrix.panose.bProportion);
  189. stream.WriteByte(win_metrix.panose.bContrast);
  190. stream.WriteByte(win_metrix.panose.bStrokeVariation);
  191. stream.WriteByte(win_metrix.panose.ArmStyle);
  192. stream.WriteByte(win_metrix.panose.bLetterform);
  193. stream.WriteByte(win_metrix.panose.bMidline);
  194. stream.WriteByte(win_metrix.panose.bXHeight);
  195. stream.WriteUInt32(win_metrix.ulUnicodeRange1);
  196. stream.WriteUInt32(win_metrix.ulUnicodeRange2);
  197. stream.WriteUInt32(win_metrix.ulUnicodeRange3);
  198. stream.WriteUInt32(win_metrix.ulUnicodeRange4);
  199. #if true
  200. stream.WriteByte(win_metrix.achVendID1);
  201. stream.WriteByte(win_metrix.achVendID2);
  202. stream.WriteByte(win_metrix.achVendID3);
  203. stream.WriteByte(win_metrix.achVendID4);
  204. #else
  205. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  206. public byte[] achVendID; // = new vyte[4];
  207. #endif
  208. stream.WriteUInt16(win_metrix.fsSelection);
  209. stream.WriteUInt16(win_metrix.usFirstCharIndex);
  210. stream.WriteUInt16(win_metrix.usLastCharIndex);
  211. stream.WriteInt16(win_metrix.sTypoAscender);
  212. stream.WriteInt16(win_metrix.sTypoDescender);
  213. stream.WriteInt16(win_metrix.sTypoLineGap);
  214. stream.WriteUInt16(win_metrix.usWinAscent);
  215. stream.WriteUInt16(win_metrix.usWinDescent);
  216. stream.WriteUInt32(win_metrix.ulCodePageRange1);
  217. stream.WriteUInt32(win_metrix.ulCodePageRange2);
  218. stream.WriteInt16(win_metrix.sxHeight);
  219. stream.WriteInt16(win_metrix.sCapHeight);
  220. stream.WriteUInt16(win_metrix.usDefaultChar);
  221. stream.WriteUInt16(win_metrix.usBreakChar);
  222. stream.WriteUInt16(win_metrix.usMaxContext);
  223. offset = (offset + this.Length + 3) & 0xfffffffc;
  224. return offset;
  225. }
  226. // Public properties
  227. #pragma warning disable CS3001, CS3002, CS3003, CS1591
  228. public bool IsBold
  229. {
  230. get
  231. {
  232. // return win_metrix.usWeightClass >= 700;
  233. return (win_metrix.fsSelection & 0x20) != 0;
  234. }
  235. }
  236. public ushort Ascent { get { return win_metrix.usWinAscent; } }
  237. public ushort Descent { get { return win_metrix.usWinDescent; } }
  238. public short LineGap { get { return win_metrix.sTypoLineGap; } }
  239. public short AvgCharWidth { get { return win_metrix.xAvgCharWidth; } }
  240. public ushort Weight { get { return win_metrix.usWeightClass; } }
  241. public ushort WidthClass { get { return win_metrix.usWidthClass; } }
  242. public short Height { get { return win_metrix.sxHeight; } }
  243. public ushort BreakChar { get { return win_metrix.usBreakChar; } }
  244. public ushort DefaultChar { get { return win_metrix.usDefaultChar; } }
  245. public ushort FirstCharIndex { get { return win_metrix.usFirstCharIndex; } }
  246. public ushort LastCharIndex { get { return win_metrix.usLastCharIndex; } }
  247. public ushort LicensingRights { get { return win_metrix.fsType; } }
  248. public OS2WindowsMetricsClass.FontPanose Panose { get { return win_metrix.panose; } }
  249. public OS2WindowsMetricsClass(TrueTypeTable src) : base(src) { }
  250. public uint UnicodeRange1 { get { return win_metrix.ulUnicodeRange1; } }
  251. public uint UnicodeRange2 { get { return win_metrix.ulUnicodeRange2; } }
  252. public uint UnicodeRange3 { get { return win_metrix.ulUnicodeRange3; } }
  253. public uint UnicodeRange4 { get { return win_metrix.ulUnicodeRange4; } }
  254. public short FamilyClass { get { return win_metrix.sFamilyClass; } }
  255. public ushort Selection { get { return win_metrix.fsSelection; } }
  256. public uint CodepageRange1 { get { return win_metrix.ulCodePageRange1; } }
  257. public uint CodepageRange2 { get { return win_metrix.ulCodePageRange2; } }
  258. public string VendorID
  259. {
  260. get
  261. {
  262. return "" +
  263. (char)win_metrix.achVendID1 +
  264. (char)win_metrix.achVendID2 +
  265. (char)win_metrix.achVendID3 +
  266. (char)win_metrix.achVendID4;
  267. }
  268. }
  269. #pragma warning restore
  270. }
  271. }