FontHeaderClass.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. internal override void Load(FontStream stream)
  91. {
  92. stream.Position = Offset;
  93. font_header.version = stream.ReadUInt32(); // FIXED Table version number 0x00010000 for version 1.0.
  94. font_header.revision = stream.ReadUInt32(); // FIXED fontRevision Set by font manufacturer.
  95. font_header.checkSumAdjustment = stream.ReadUInt32(); // ULONG checkSumAdjustment To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum.
  96. font_header.magicNumber = stream.ReadUInt32(); // ULONG magicNumber Set to 0x5F0F3CF5.
  97. font_header.flags = stream.ReadUInt16(); // USHORT flags Bit 0 - baseline for font at y=0;
  98. // Bit 1 - left sidebearing at x=0;
  99. // Bit 2 - instructions may depend on point size;
  100. // Bit 3 - force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear;
  101. // Bit 4 - instructions may alter advance width (the advance widths might not scale linearly);
  102. // Note: All other bits must be zero.
  103. font_header.unitsPerEm = stream.ReadUInt16(); // USHORT unitsPerEm Valid range is from 16 to 16384
  104. font_header.CreatedDateTime = stream.ReadUInt64(); // created International date (8-byte field).
  105. font_header.ModifiedDateTime = stream.ReadUInt64(); // modified International date (8-byte field).
  106. font_header.xMin = stream.ReadInt16(); // For all glyph bounding boxes.
  107. font_header.yMin = stream.ReadInt16(); // For all glyph bounding boxes.
  108. font_header.xMax = stream.ReadInt16(); // For all glyph bounding boxes.
  109. font_header.yMax = stream.ReadInt16(); // For all glyph bounding boxes.
  110. font_header.macStyle = stream.ReadUInt16(); // Bit 0 bold (if set to 1); Bit 1 italic (if set to 1) Bits 2-15 reserved (set to 0).
  111. font_header.lowestRecPPEM = stream.ReadUInt16(); // Smallest readable size in pixels.
  112. font_header.fontDirectionHint = stream.ReadInt16();
  113. // 0 Fully mixed directional glyphs;
  114. // 1 Only strongly left to right;
  115. // 2 Like 1 but also contains neutrals ;
  116. //-1 Only strongly right to left;
  117. //-2 Like -1 but also contains neutrals.
  118. font_header.indexToLocFormat = stream.ReadInt16(); // 0 for short offsets, 1 for long.
  119. font_header.glyphDataFormat = stream.ReadInt16(); // 0 for current format.
  120. }
  121. internal void SaveFontHeader(FontStream stream, uint CheckSum)
  122. {
  123. // ChangeEndian();
  124. // header_ptr = Increment(header_ptr, (int)this.PackedOfffset);
  125. font_header.checkSumAdjustment = CheckSum;
  126. stream.Position = this.PackedOfffset;
  127. // font_header.checkSumAdjustment = SwapUInt32(CheckSum);
  128. //Marshal.StructureToPtr(font_header, header_ptr, true);
  129. stream.WriteUInt32(this.font_header.version); // FIXED Table version number 0x00010000 for version 1.0.
  130. stream.WriteUInt32(this.font_header.revision); // FIXED fontRevision Set by font manufacturer.
  131. stream.WriteUInt32(this.font_header.checkSumAdjustment); // ULONG checkSumAdjustment To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum.
  132. stream.WriteUInt32(this.font_header.magicNumber); // ULONG magicNumber Set to 0x5F0F3CF5.
  133. stream.WriteUInt16(font_header.flags); // USHORT flags Bit 0 - baseline for font at y=0;
  134. stream.WriteUInt16(font_header.unitsPerEm); // USHORT unitsPerEm Valid range is from 16 to 16384
  135. stream.WriteUInt64(font_header.CreatedDateTime); // created International date (8-byte field).
  136. stream.WriteUInt64(font_header.ModifiedDateTime); // modified International date (8-byte field).
  137. stream.WriteInt16(font_header.xMin); // For all glyph bounding boxes.
  138. stream.WriteInt16(font_header.yMin); // For all glyph bounding boxes.
  139. stream.WriteInt16(font_header.xMax); // For all glyph bounding boxes.
  140. stream.WriteInt16(font_header.yMax); // For all glyph bounding boxes.
  141. stream.WriteUInt16(font_header.macStyle); // Bit 0 bold (if set to 1); Bit 1 italic (if set to 1) Bits 2-15 reserved (set to 0).
  142. stream.WriteUInt16(font_header.lowestRecPPEM); // Smallest readable size in pixels.
  143. stream.WriteInt16(font_header.fontDirectionHint);
  144. stream.WriteInt16(font_header.indexToLocFormat); // 0 for short offsets, 1 for long.
  145. stream.WriteInt16(font_header.glyphDataFormat); // 0 for current format.
  146. }
  147. public FontHeaderClass(TrueTypeTable src) : base(src)
  148. {
  149. // This form is empty
  150. }
  151. public FontHeaderClass(FontHeaderClass src) : base(src)
  152. {
  153. this.font_header = (src as FontHeaderClass).font_header;
  154. }
  155. }
  156. }
  157. #pragma warning restore