FontHeaderClass.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Runtime.InteropServices;
  3. #pragma warning disable CS3001, CS3002, CS3003, CS1591
  4. namespace FastReport.Fonts
  5. {
  6. /// <summary>
  7. /// FontHeader table
  8. /// </summary>
  9. public class FontHeaderClass : TrueTypeTable
  10. {
  11. #region "Type definitions"
  12. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  13. public struct FontHeader
  14. {
  15. [FieldOffset(0)]
  16. public uint version; // FIXED Table version number 0x00010000 for version 1.0.
  17. [FieldOffset(4)]
  18. public uint revision; // FIXED fontRevision Set by font manufacturer.
  19. [FieldOffset(8)]
  20. public uint checkSumAdjustment; // ULONG checkSumAdjustment To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum.
  21. [FieldOffset(12)]
  22. public uint magicNumber; // ULONG magicNumber Set to 0x5F0F3CF5.
  23. [FieldOffset(16)]
  24. public ushort flags; // USHORT flags Bit 0 - baseline for font at y=0;
  25. // Bit 1 - left sidebearing at x=0;
  26. // Bit 2 - instructions may depend on point size;
  27. // Bit 3 - force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear;
  28. // Bit 4 - instructions may alter advance width (the advance widths might not scale linearly);
  29. // Note: All other bits must be zero.
  30. [FieldOffset(18)]
  31. public ushort unitsPerEm; // USHORT unitsPerEm Valid range is from 16 to 16384
  32. [FieldOffset(20)]
  33. public ulong CreatedDateTime; // created International date (8-byte field).
  34. [FieldOffset(28)]
  35. public ulong ModifiedDateTime; // modified International date (8-byte field).
  36. [FieldOffset(36)]
  37. public short xMin; // For all glyph bounding boxes.
  38. [FieldOffset(38)]
  39. public short yMin; // For all glyph bounding boxes.
  40. [FieldOffset(40)]
  41. public short xMax; // For all glyph bounding boxes.
  42. [FieldOffset(42)]
  43. public short yMax; // For all glyph bounding boxes.
  44. [FieldOffset(44)]
  45. public ushort macStyle; // Bit 0 bold (if set to 1); Bit 1 italic (if set to 1) Bits 2-15 reserved (set to 0).
  46. [FieldOffset(46)]
  47. public ushort lowestRecPPEM; // Smallest readable size in pixels.
  48. [FieldOffset(48)]
  49. public short fontDirectionHint;
  50. // 0 Fully mixed directional glyphs;
  51. // 1 Only strongly left to right;
  52. // 2 Like 1 but also contains neutrals ;
  53. //-1 Only strongly right to left;
  54. //-2 Like -1 but also contains neutrals.
  55. [FieldOffset(50)]
  56. public short indexToLocFormat; // 0 for short offsets, 1 for long.
  57. [FieldOffset(52)]
  58. public short glyphDataFormat; // 0 for current format.
  59. }
  60. public enum IndexToLoc
  61. {
  62. ShortType = 0,
  63. LongType = 1
  64. }
  65. #endregion
  66. private FontHeader font_header;
  67. public IndexToLoc indexToLocFormat
  68. {
  69. get
  70. {
  71. return (IndexToLoc)font_header.indexToLocFormat;
  72. }
  73. set
  74. {
  75. font_header.indexToLocFormat = (short) value;
  76. }
  77. }
  78. internal uint checkSumAdjustment { set { font_header.checkSumAdjustment = value; } }
  79. public ushort unitsPerEm { get { return font_header.unitsPerEm; } }
  80. public ushort Flags { get { return font_header.flags; } }
  81. public ushort MacStyle { get { return font_header.macStyle; } set { font_header.macStyle = value; } }
  82. public short xMin { get { return font_header.xMin; } }
  83. public short xMax { get { return font_header.xMax; } }
  84. public short yMin { get { return font_header.yMin; } }
  85. public short yMax { get { return font_header.yMax; } }
  86. public ushort LowestRecPPEM { get { return font_header.lowestRecPPEM; } }
  87. public short FontDirectionHint { get { return font_header.fontDirectionHint; } }
  88. public ulong CreatedDateTime { get { return font_header.CreatedDateTime; } }
  89. public ulong ModifiedDateTime { get { return font_header.ModifiedDateTime; } }
  90. private void ChangeEndian()
  91. {
  92. font_header.indexToLocFormat = SwapInt16(font_header.indexToLocFormat);
  93. font_header.magicNumber = SwapUInt32(font_header.magicNumber);
  94. font_header.unitsPerEm = SwapUInt16(font_header.unitsPerEm);
  95. font_header.xMin = SwapInt16(font_header.xMin);
  96. font_header.xMax = SwapInt16(font_header.xMax);
  97. font_header.yMin = SwapInt16(font_header.yMin);
  98. font_header.yMax = SwapInt16(font_header.yMax);
  99. font_header.lowestRecPPEM = SwapUInt16(font_header.lowestRecPPEM);
  100. font_header.fontDirectionHint = SwapInt16(font_header.fontDirectionHint);
  101. font_header.CreatedDateTime = SwapUInt64(font_header.CreatedDateTime);
  102. font_header.ModifiedDateTime = SwapUInt64(font_header.ModifiedDateTime);
  103. font_header.checkSumAdjustment = 0;
  104. if (font_header.macStyle != 0)
  105. {
  106. font_header.macStyle = SwapUInt16(font_header.macStyle);
  107. }
  108. }
  109. internal override void Load(IntPtr font)
  110. {
  111. IntPtr header_ptr = Increment(font, (int)this.Offset);
  112. font_header = (FontHeader)Marshal.PtrToStructure(header_ptr, typeof(FontHeader));
  113. font_header.checkSumAdjustment = 0;
  114. Marshal.StructureToPtr(font_header, header_ptr, false);
  115. ChangeEndian();
  116. }
  117. internal void SaveFontHeader(IntPtr header_ptr, uint CheckSum)
  118. {
  119. ChangeEndian();
  120. header_ptr = Increment(header_ptr, (int)this.PackedOfffset);
  121. font_header.checkSumAdjustment = SwapUInt32(CheckSum);
  122. Marshal.StructureToPtr(font_header, header_ptr, true);
  123. }
  124. public FontHeaderClass(TrueTypeTable src) : base(src)
  125. {
  126. // This form is empty
  127. }
  128. public FontHeaderClass(FontHeaderClass src) : base(src)
  129. {
  130. this.font_header = (src as FontHeaderClass).font_header;
  131. }
  132. }
  133. }
  134. #pragma warning restore