Font.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using Biff8Excel.Interfaces;
  6. namespace Biff8Excel.Records
  7. {
  8. [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  9. struct RecordFont
  10. {
  11. public ushort opcode ;
  12. public ushort length;
  13. public ushort size;
  14. public ushort attributes;
  15. public ushort colourIndex;
  16. public ushort boldWeight;
  17. public ushort superSubScript;
  18. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte underline;
  19. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte family;
  20. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte charset;
  21. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte zero ; // must be zero
  22. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte fontNameLen;
  23. [MarshalAs(UnmanagedType.U1, SizeConst = 1)]public byte isunicode; // Flag to store as unicode
  24. }
  25. internal class Font : IRecords
  26. {
  27. RecordFont font;
  28. ushort recLen;
  29. byte[] m_fontName;
  30. public Font()
  31. {
  32. font.opcode = 0x31;
  33. this.Size = 10;
  34. font.boldWeight = 0x190;
  35. font.zero = 0x0;
  36. this.FontName = "ËÎÌå";
  37. //this.FontName = "Tahoma";
  38. //font.charset = (byte)enumFontCharacterSet.csANSI_Chinese_Simplified;
  39. }
  40. public ushort ID
  41. {
  42. get { return font.opcode; }
  43. }
  44. public ushort RecordSize
  45. {
  46. get { return recLen; }
  47. }
  48. public ushort Size
  49. {
  50. set {
  51. font.size = (ushort)(value * 20);
  52. }
  53. get
  54. {
  55. if ( font.size > 0)
  56. return (ushort)(font.size / 20);
  57. return 0;
  58. }
  59. }
  60. public ushort Attributes
  61. {
  62. set
  63. {
  64. font.attributes = value;
  65. if ((value & 0x1) == 1)
  66. font.boldWeight = 0x2BC;
  67. }
  68. }
  69. public ushort ColourIndex
  70. {
  71. set { font.colourIndex = value; }
  72. }
  73. public ushort BoldWeight
  74. {
  75. set { font.boldWeight = value; }
  76. }
  77. public ushort Escapement
  78. {
  79. set { font.superSubScript = value; }
  80. }
  81. public byte Underline
  82. {
  83. set { font.underline = value; }
  84. }
  85. public byte Family
  86. {
  87. set { font.family = value; }
  88. }
  89. public byte Charset
  90. {
  91. set { font.charset = value; }
  92. }
  93. public string FontName
  94. {
  95. set
  96. {
  97. font.fontNameLen = (byte)value.Length;
  98. //m_fontName = Globals.GetUnicodeBytes(value);
  99. if (font.fontNameLen != (byte)Globals.GetDefaultBytesLength(value))
  100. {
  101. font.isunicode = 0x1;
  102. m_fontName = Globals.GetUnicodeBytes(value);
  103. }
  104. else
  105. {
  106. font.isunicode = 0x0;
  107. m_fontName = Globals.GetDefaultBytes(value);
  108. }
  109. }
  110. }
  111. #region IRecords ³ÉÔ±
  112. public byte[] GetByte()
  113. {
  114. //font.length = (ushort)((font.fontNameLen * 2) + 16);
  115. //byte[] b = new byte[(font.fontNameLen * 2) + 20];
  116. font.length = (ushort)(m_fontName.Length + 16);
  117. byte[] b = new byte[m_fontName.Length + 20];
  118. Globals.GetStructToBytes(font).CopyTo(b, 0);
  119. m_fontName.CopyTo(b, 20);
  120. return b;
  121. }
  122. #endregion
  123. }
  124. }