HorizontalHeaderClass.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace FastReport.Fonts
  4. {
  5. /////////////////////////////////////////////////////////////////////////////////////////////////
  6. // HorizontalHeader table
  7. /////////////////////////////////////////////////////////////////////////////////////////////////
  8. class HorizontalHeaderClass : TrueTypeTable
  9. {
  10. #region "Type definition"
  11. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  12. public struct HorizontalHeader
  13. {
  14. [FieldOffset(0)]
  15. public uint Version; // version number 0x00010000 for version 1.0.
  16. [FieldOffset(4)]
  17. public short Ascender; // Typographic ascent.
  18. [FieldOffset(6)]
  19. public short Descender; // Typographic descent.
  20. [FieldOffset(8)]
  21. public short LineGap; // Typographic line gap. Negative LineGap values are treated as zero in Windows 3.1, System 6, and System 7.
  22. [FieldOffset(10)]
  23. public ushort advanceWidthMax; // Maximum advance width value in ‘hmtx’ table.
  24. [FieldOffset(12)]
  25. public short minLeftSideBearing; // Minimum left sidebearing value in ‘hmtx’ table.
  26. [FieldOffset(14)]
  27. public short minRightSideBearing; // Minimum right sidebearing value; calculated as Min(aw - lsb - (xMax - xMin)).
  28. [FieldOffset(16)]
  29. public short xMaxExtent; // Max(lsb + (xMax - xMin)).
  30. [FieldOffset(18)]
  31. public short caretSlopeRise; // Used to calculate the slope of the cursor (rise/run); 1 for vertical.
  32. [FieldOffset(20)]
  33. public short caretSlopeRun; // 0 for vertical.
  34. [FieldOffset(22)]
  35. public short reserved1; // set to 0
  36. [FieldOffset(24)]
  37. public short reserved2; // set to 0
  38. [FieldOffset(26)]
  39. public short reserved3; // set to 0
  40. [FieldOffset(28)]
  41. public short reserved4; // set to 0
  42. [FieldOffset(30)]
  43. public short reserved5; // set to 0
  44. [FieldOffset(32)]
  45. public short metricDataFormat; // 0 for current format.
  46. [FieldOffset(34)]
  47. public ushort numberOfHMetrics; // Number of hMetric entries in ‘hmtx’ table; may be smaller than the total number of glyphs in the font.
  48. }
  49. #endregion
  50. private HorizontalHeader horizontal_header;
  51. private void ChangeEndian()
  52. {
  53. horizontal_header.Version = SwapUInt32(horizontal_header.Version);
  54. horizontal_header.Ascender = SwapInt16(horizontal_header.Ascender);
  55. horizontal_header.Descender = SwapInt16(horizontal_header.Descender);
  56. horizontal_header.LineGap = SwapInt16(horizontal_header.LineGap);
  57. horizontal_header.advanceWidthMax = SwapUInt16(horizontal_header.advanceWidthMax);
  58. horizontal_header.minLeftSideBearing = SwapInt16(horizontal_header.minLeftSideBearing);
  59. horizontal_header.minRightSideBearing = SwapInt16(horizontal_header.minRightSideBearing);
  60. horizontal_header.xMaxExtent = SwapInt16(horizontal_header.xMaxExtent);
  61. horizontal_header.caretSlopeRise = SwapInt16(horizontal_header.caretSlopeRise);
  62. horizontal_header.caretSlopeRun = SwapInt16(horizontal_header.caretSlopeRun);
  63. horizontal_header.metricDataFormat = SwapInt16(horizontal_header.metricDataFormat);
  64. horizontal_header.numberOfHMetrics = SwapUInt16(horizontal_header.numberOfHMetrics);
  65. }
  66. internal override void Load(IntPtr font)
  67. {
  68. IntPtr horizontal_header_ptr = Increment(font, (int)this.Offset);
  69. horizontal_header = (HorizontalHeader)Marshal.PtrToStructure(horizontal_header_ptr, typeof(HorizontalHeader));
  70. ChangeEndian();
  71. }
  72. internal override uint Save(IntPtr font, uint offset)
  73. {
  74. this.Offset = offset;
  75. ChangeEndian();
  76. IntPtr horizontal_header_ptr = Increment(font, (int)offset);
  77. Marshal.StructureToPtr(horizontal_header, horizontal_header_ptr, false);
  78. ChangeEndian();
  79. return offset + (uint)this.Length;
  80. }
  81. public short Ascender { get { return horizontal_header.Ascender; } }
  82. public short Descender { get { return horizontal_header.Descender; } }
  83. public short LineGap { get { return horizontal_header.LineGap; } }
  84. public ushort MaxWidth { get { return horizontal_header.advanceWidthMax; } }
  85. public short MinLeftSideBearing { get { return horizontal_header.minLeftSideBearing; } }
  86. public ushort NumberOfHMetrics { get { return horizontal_header.numberOfHMetrics; } set { horizontal_header.numberOfHMetrics = value; } }
  87. public HorizontalHeaderClass(TrueTypeTable src) : base(src) { }
  88. }
  89. }