HorizontalMetrixClass.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. // #pragma warning disable CS3001, CS3002, CS3003, CS1591
  5. namespace FastReport.Fonts
  6. {
  7. /// <summary>
  8. /// HorizontalMetrix table
  9. /// </summary>
  10. public class HorizontalMetrixClass : TrueTypeTable
  11. {
  12. #region "Type definition"
  13. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  14. public struct longHorMetric
  15. {
  16. [FieldOffset(0)]
  17. public ushort advanceWidth;
  18. [FieldOffset(2)]
  19. public short lsb;
  20. };
  21. #endregion
  22. longHorMetric[] metrixTable;
  23. short[] lsbExt;
  24. public ushort numberOfMetrics;
  25. public longHorMetric this[int index]
  26. {
  27. get
  28. {
  29. // Microsoft say: "If the font is monospaced, only one entry need be in the array, but that entry is required"
  30. if (index >= metrixTable.Length) index = 0;
  31. return metrixTable[index];
  32. }
  33. }
  34. public void RepackWithDictionary(ref Dictionary<ushort, GlyphChar> dict, int source_glyph_count)
  35. {
  36. longHorMetric[] metrix = new longHorMetric[dict.Count];
  37. ushort[] lsb = new ushort[source_glyph_count - metrixTable.Length];
  38. int i = 0;
  39. foreach (ushort key in dict.Keys)
  40. {
  41. GlyphChar gc = dict[key];
  42. if (key < metrixTable.Length)
  43. {
  44. metrix[gc.Glyph] = metrixTable[key];
  45. }
  46. else
  47. {
  48. // TODO: Correct LSB
  49. throw new Exception("Check me in RepackWithDictionary");
  50. // short lsb = lsbExt[key];
  51. }
  52. }
  53. metrixTable = metrix;
  54. numberOfMetrics = (ushort)metrix.Length;
  55. }
  56. internal override void Load(FontStream stream)
  57. {
  58. metrixTable = new longHorMetric[numberOfMetrics];
  59. stream.Position = this.Offset;
  60. for (int i = 0; i < numberOfMetrics; i++)
  61. {
  62. metrixTable[i].advanceWidth = stream.ReadUInt16();
  63. metrixTable[i].lsb = stream.ReadInt16();
  64. }
  65. // Magic of calculation of leftSideBearings items count
  66. int lsbCount = ((int)this.Length - (numberOfMetrics * 4)) / 2;
  67. if (lsbCount == 0)
  68. lsbExt = null;
  69. else
  70. {
  71. lsbExt = new short[lsbCount];
  72. for (int i = 0; i < lsbCount; i++)
  73. {
  74. lsbExt[i] = stream.ReadInt16();
  75. }
  76. }
  77. }
  78. internal override uint Save(FontStream source_not_used, FontStream stream, uint offset)
  79. {
  80. //this.Offset = offset;
  81. //IntPtr horizontal_header_ptr = Increment(font, (int)offset);
  82. stream.Position = offset;
  83. // IntPtr h_metrix_ptr = Increment(font, (int)offset);
  84. for (int i = 0; i < numberOfMetrics; i++)
  85. {
  86. longHorMetric rec;
  87. //rec.advanceWidth =
  88. stream.WriteUInt16(this.metrixTable[i].advanceWidth); // SwapUInt16(metrixTable[i].advanceWidth);
  89. // rec.lsb =
  90. stream.WriteInt16(this.metrixTable[i].lsb); // SwapInt16(metrixTable[i].lsb);
  91. //Marshal.StructureToPtr(rec, h_metrix_ptr, false);
  92. //h_metrix_ptr = Increment(h_metrix_ptr, 4);
  93. }
  94. uint lsb_size = 0;
  95. if (lsbExt != null)
  96. {
  97. for (int j = 0; j < lsbExt.Length; j++)
  98. {
  99. // Marshal.WriteInt16(h_metrix_ptr, j * 2, lsbExt[j]);
  100. stream.WriteInt16(lsbExt[j]);
  101. }
  102. lsb_size = ((uint)lsbExt.Length * 2);
  103. lsb_size = ((lsb_size + 3) / 4) * 4;
  104. }
  105. uint len = (uint)numberOfMetrics * 4 + lsb_size;
  106. SetLenght(len);
  107. return offset + len;
  108. }
  109. public HorizontalMetrixClass(TrueTypeTable src)
  110. : base(src)
  111. {
  112. }
  113. }
  114. }
  115. //#pragma warning restore