IndexToLocationClass.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Runtime.InteropServices;
  3. #pragma warning disable CS3001, CS3002, CS3003, CS1591
  4. namespace FastReport.Fonts
  5. {
  6. /// <summary>
  7. /// IndexToLocation table
  8. /// </summary>
  9. public class IndexToLocationClass : TrueTypeTable
  10. {
  11. private ushort[] shortIndexToLocation = null;
  12. private uint[] longIndexToLocation = null;
  13. internal ushort[] Short { get { return shortIndexToLocation; } }
  14. internal uint[] Long { get { return longIndexToLocation; } }
  15. internal void LoadIndexToLocation(IntPtr font, FontHeaderClass font_header)
  16. {
  17. int count;
  18. IntPtr i2l_ptr = Increment(font, (int)(this.Offset));
  19. switch (font_header.indexToLocFormat)
  20. {
  21. case FontHeaderClass.IndexToLoc.ShortType:
  22. count = (int)this.Length / 2;
  23. short[] ShortTemp = new short[count];
  24. Marshal.Copy(i2l_ptr, ShortTemp, 0, count);
  25. shortIndexToLocation = new ushort[count];
  26. for (int i = 0; i < count; i++)
  27. {
  28. byte[] buf = BitConverter.GetBytes(ShortTemp[i]);
  29. if (BitConverter.IsLittleEndian)
  30. Array.Reverse(buf);
  31. shortIndexToLocation[i] = BitConverter.ToUInt16(buf, 0);
  32. }
  33. ShortTemp = null;
  34. break;
  35. case FontHeaderClass.IndexToLoc.LongType:
  36. count = (int)this.Length / 4;
  37. int[] LongTemp = new int[count];
  38. Marshal.Copy(i2l_ptr, LongTemp, 0, count);
  39. longIndexToLocation = new uint[count];
  40. for (int j = 0; j < count; j++)
  41. {
  42. byte[] buf = BitConverter.GetBytes(LongTemp[j]);
  43. if (BitConverter.IsLittleEndian)
  44. Array.Reverse(buf);
  45. longIndexToLocation[j] = BitConverter.ToUInt32(buf, 0);
  46. }
  47. LongTemp = null;
  48. break;
  49. default:
  50. throw new Exception("Unsupported Index to Location format");
  51. }
  52. }
  53. public ushort GetGlyph(ushort i2l_idx, FontHeaderClass font_header, out uint location)
  54. {
  55. location = 0;
  56. ushort length = 0;
  57. switch (font_header.indexToLocFormat)
  58. {
  59. case FontHeaderClass.IndexToLoc.ShortType:
  60. if (i2l_idx >= shortIndexToLocation.Length)
  61. {
  62. location = 0;
  63. break;
  64. }
  65. location = (uint)(2 * shortIndexToLocation[i2l_idx]);
  66. length = (ushort)(2 * (shortIndexToLocation[i2l_idx + 1] - shortIndexToLocation[i2l_idx]));
  67. break;
  68. case FontHeaderClass.IndexToLoc.LongType:
  69. location = longIndexToLocation[i2l_idx];
  70. length = (ushort)(longIndexToLocation[i2l_idx + 1] - longIndexToLocation[i2l_idx]);
  71. break;
  72. }
  73. return length;
  74. }
  75. internal override uint Save(IntPtr font, uint offset)
  76. // internal override uint Save(IntPtr src, IntPtr dst, uint offset)
  77. {
  78. this.Offset = offset;
  79. return base.Save(font, offset);
  80. }
  81. public IndexToLocationClass(TrueTypeTable src) : base(src) { }
  82. }
  83. }
  84. #pragma warning restore