HorizontalMetrixClass.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(IntPtr font)
  57. {
  58. metrixTable = new longHorMetric[numberOfMetrics];
  59. IntPtr h_metrix_ptr = Increment(font, (int)this.Offset);
  60. for (int i = 0; i < numberOfMetrics; i++)
  61. {
  62. metrixTable[i] = (longHorMetric)Marshal.PtrToStructure(h_metrix_ptr, typeof(longHorMetric));
  63. metrixTable[i].advanceWidth = SwapUInt16(metrixTable[i].advanceWidth);
  64. metrixTable[i].lsb = SwapInt16(metrixTable[i].lsb);
  65. h_metrix_ptr = Increment(h_metrix_ptr, 4);
  66. }
  67. // Magic of calculation of leftSideBearings items count
  68. int lsbCount = ((int)this.Length - (numberOfMetrics * 4)) / 2;
  69. if (lsbCount == 0)
  70. lsbExt = null;
  71. else
  72. {
  73. lsbExt = new short[lsbCount];
  74. for (int i = 0; i < lsbCount; i++)
  75. {
  76. lsbExt[i] = Marshal.ReadInt16(h_metrix_ptr, i * 2);
  77. }
  78. }
  79. }
  80. internal override uint Save(IntPtr font, uint offset)
  81. {
  82. this.Offset = offset;
  83. IntPtr horizontal_header_ptr = Increment(font, (int)offset);
  84. IntPtr h_metrix_ptr = Increment(font, (int)offset);
  85. for (int i = 0; i < numberOfMetrics; i++)
  86. {
  87. longHorMetric rec;
  88. rec.advanceWidth = SwapUInt16(metrixTable[i].advanceWidth);
  89. rec.lsb = SwapInt16(metrixTable[i].lsb);
  90. Marshal.StructureToPtr(rec, h_metrix_ptr, false);
  91. h_metrix_ptr = Increment(h_metrix_ptr, 4);
  92. }
  93. uint lsb_size = 0;
  94. if(lsbExt != null)
  95. {
  96. for (int j = 0; j < lsbExt.Length; j++)
  97. {
  98. Marshal.WriteInt16(h_metrix_ptr, j * 2, lsbExt[j]);
  99. }
  100. lsb_size = ((uint)lsbExt.Length * 2);
  101. lsb_size = ((lsb_size + 3) / 4) * 4;
  102. }
  103. uint len = (uint)numberOfMetrics * 4 + lsb_size;
  104. SetLenght(len);
  105. return offset + len;
  106. }
  107. internal override uint Save(IntPtr src, IntPtr dst, uint offset)
  108. {
  109. return base.Save(src, dst, offset);
  110. }
  111. public HorizontalMetrixClass(TrueTypeTable src)
  112. : base(src)
  113. {
  114. }
  115. }
  116. }
  117. #pragma warning restore