Font.jvm.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System.Runtime.Serialization;
  2. using System.Runtime.InteropServices;
  3. using System.ComponentModel;
  4. using awt = java.awt;
  5. using TextAttribute = java.awt.font.TextAttribute;
  6. namespace System.Drawing {
  7. [Serializable]
  8. public sealed class Font: MarshalByRefObject, ISerializable, ICloneable, IDisposable {
  9. #region variables
  10. const byte DEFAULT_CHARSET = 1;
  11. private readonly GraphicsUnit _gUnit = GraphicsUnit.Point;
  12. private readonly FontFamily _fontFamily;
  13. private readonly awt.Font _jFont;
  14. private readonly byte _charset;
  15. #endregion
  16. internal awt.Font NativeObject {
  17. get {
  18. return _jFont;
  19. }
  20. }
  21. #region ISerializable
  22. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
  23. info.AddValue("Name", Name);
  24. info.AddValue("Size", Size);
  25. info.AddValue("Style", Style, typeof(FontStyle));
  26. info.AddValue("Unit", Unit, typeof(GraphicsUnit));
  27. }
  28. #endregion
  29. #region ctors
  30. private Font (SerializationInfo info, StreamingContext context)
  31. : this(
  32. info.GetString("Name"),
  33. info.GetSingle("Size"),
  34. (FontStyle)info.GetValue("Style", typeof(FontStyle)),
  35. (GraphicsUnit)info.GetValue("Unit", typeof(GraphicsUnit)) ) {
  36. }
  37. public Font(Font original, FontStyle style) {
  38. _jFont = original.NativeObject.deriveFont( DeriveStyle(original.NativeObject.getAttributes(), style, true) );
  39. _gUnit = original._gUnit;
  40. _fontFamily = original._fontFamily;
  41. }
  42. public Font(FontFamily family, float emSize)
  43. : this(family, emSize, FontStyle.Regular, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
  44. }
  45. public Font(FontFamily family, float emSize, FontStyle style)
  46. : this(family, emSize, style, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
  47. }
  48. public Font(FontFamily family, float emSize, GraphicsUnit unit)
  49. : this(family, emSize, FontStyle.Regular, unit, DEFAULT_CHARSET, false) {
  50. }
  51. public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)
  52. : this(family, emSize, style, unit, DEFAULT_CHARSET, false) {
  53. }
  54. public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
  55. : this(family, emSize, style, unit, charSet, false) {
  56. }
  57. public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical) {
  58. if (family == null)
  59. throw new ArgumentNullException("family");
  60. _gUnit = unit;
  61. _fontFamily = family;
  62. _charset = charSet;
  63. java.util.Hashtable attribs = new java.util.Hashtable();
  64. attribs.put(TextAttribute.FAMILY, family.Name/*TODO: family doungrade possibility*/);
  65. //init defaults
  66. attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
  67. float newSize = emSize * Graphics.UnitConversion[ (int)_gUnit ];
  68. attribs.put(TextAttribute.SIZE, new java.lang.Float(newSize));
  69. DeriveStyle(attribs, style, false);
  70. _jFont = family.FamilyFont.deriveFont(attribs);
  71. }
  72. public Font(string familyName, float emSize)
  73. : this(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false) {
  74. }
  75. public Font(string familyName, float emSize, FontStyle style)
  76. : this(familyName, emSize, style, GraphicsUnit.Point, (byte)0, false) {
  77. }
  78. public Font(string familyName, float emSize, GraphicsUnit unit)
  79. : this(familyName, emSize, FontStyle.Regular, unit, (byte)0, false) {
  80. }
  81. public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)
  82. : this(familyName, emSize, style, unit, (byte)0, false) {
  83. }
  84. public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
  85. : this(familyName, emSize, style, unit, charSet, false) {
  86. }
  87. public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
  88. :this(new FontFamily(familyName), emSize, style, unit, charSet, isVertical) {
  89. }
  90. #endregion
  91. #region IDisposable members
  92. public void Dispose() {
  93. }
  94. #endregion
  95. #region ICloneable
  96. public object Clone() {
  97. return (Font)MemberwiseClone();
  98. }
  99. #endregion
  100. #if INTPTR_SUPPORT
  101. [MonoTODO]
  102. public IntPtr ToHfont ()
  103. {
  104. throw new NotImplementedException();
  105. }
  106. #endif
  107. #region public properties
  108. public bool Bold {
  109. get {
  110. return _jFont.isBold();
  111. }
  112. }
  113. public FontFamily FontFamily {
  114. get {
  115. return _fontFamily;
  116. }
  117. }
  118. public byte GdiCharSet {
  119. get {
  120. return _charset;
  121. }
  122. }
  123. public bool GdiVerticalFont {
  124. get {
  125. return Name.StartsWith("@");
  126. }
  127. }
  128. public int Height {
  129. get {
  130. awt.Container c = new awt.Container();
  131. return c.getFontMetrics(NativeObject).getHeight();
  132. }
  133. }
  134. public bool Italic {
  135. get {
  136. return _jFont.isItalic();
  137. }
  138. }
  139. public string Name {
  140. get {
  141. return _jFont.getName();
  142. }
  143. }
  144. public float Size {
  145. get {
  146. return SizeInPoints / Graphics.UnitConversion[ (int)_gUnit ];
  147. }
  148. }
  149. public float SizeInPoints {
  150. get {
  151. return _jFont.getSize2D();
  152. }
  153. }
  154. public bool Strikeout {
  155. get {
  156. try {
  157. if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH)
  158. == TextAttribute.STRIKETHROUGH_ON )
  159. return true;
  160. }
  161. catch {
  162. }
  163. return false;
  164. }
  165. }
  166. public FontStyle Style {
  167. get {
  168. FontStyle style = FontStyle.Regular;
  169. if (Bold)
  170. style |= FontStyle.Bold;
  171. if (Italic)
  172. style |= FontStyle.Italic;
  173. if (Underline)
  174. style |= FontStyle.Underline;
  175. if (Strikeout)
  176. style |= FontStyle.Strikeout;
  177. return style;
  178. }
  179. }
  180. public bool Underline {
  181. get {
  182. try {
  183. if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE)
  184. == TextAttribute.UNDERLINE_ON )
  185. return true;
  186. }
  187. catch {
  188. }
  189. return false;
  190. }
  191. }
  192. [TypeConverter(typeof(FontConverter.FontUnitConverter))]
  193. public GraphicsUnit Unit {
  194. get {
  195. return _gUnit;
  196. }
  197. }
  198. #endregion
  199. public override System.String ToString() {
  200. return ("[Font: Name="+ Name +", Size="+ Size +", Style="+ Style +", Units="+ Unit +"]");
  201. }
  202. static internal java.util.Map DeriveStyle(java.util.Map attribs, FontStyle style, bool createNew) {
  203. java.util.Map newAttribs;
  204. if (createNew) {
  205. newAttribs = new java.util.Hashtable( attribs.size() );
  206. object [] keys = attribs.keySet().toArray();
  207. for (int i=0; i < keys.Length; i++)
  208. newAttribs.put( keys[i], attribs.get( keys[i] ) );
  209. }
  210. else
  211. newAttribs = attribs;
  212. //Bold
  213. if((style & FontStyle.Bold) == FontStyle.Bold)
  214. newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
  215. else
  216. newAttribs.remove(TextAttribute.WEIGHT);
  217. //Italic
  218. if((style & FontStyle.Italic) == FontStyle.Italic)
  219. newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
  220. else
  221. newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
  222. //Underline
  223. if((style & FontStyle.Underline) == FontStyle.Underline)
  224. newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
  225. else
  226. newAttribs.remove(TextAttribute.UNDERLINE);
  227. //Strikeout
  228. if((style & FontStyle.Strikeout) == FontStyle.Strikeout)
  229. newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
  230. else
  231. newAttribs.remove(TextAttribute.STRIKETHROUGH);
  232. return newAttribs;
  233. }
  234. }
  235. }