2 using System
.Runtime
.Serialization
;
3 using System
.Runtime
.InteropServices
;
4 using System
.ComponentModel
;
6 using TextAttribute
= java
.awt
.font
.TextAttribute
;
8 namespace System
.Drawing
{
10 public sealed class Font
: MarshalByRefObject
, ISerializable
, ICloneable
, IDisposable
{
14 const byte DEFAULT_CHARSET
= 1;
16 private readonly GraphicsUnit _gUnit
= GraphicsUnit
.Point
;
17 private readonly FontFamily _fontFamily
;
18 private readonly awt
.Font _jFont
;
19 private readonly byte _charset
;
21 static readonly float [] _screenResolutionConverter
= {
25 Graphics
.DefaultScreenResolution
, // Point
26 Graphics
.DefaultScreenResolution
, // Inch
27 Graphics
.DefaultScreenResolution
, // Document
28 Graphics
.DefaultScreenResolution
// Millimeter
32 private readonly string _systemFontName
;
37 internal awt
.Font NativeObject
{
45 void ISerializable
.GetObjectData(SerializationInfo info
, StreamingContext context
) {
46 info
.AddValue("Name", Name
);
47 info
.AddValue("Size", Size
);
48 info
.AddValue("Style", Style
, typeof(FontStyle
));
49 info
.AddValue("Unit", Unit
, typeof(GraphicsUnit
));
56 private Font (SerializationInfo info
, StreamingContext context
)
58 info
.GetString("Name"),
59 info
.GetSingle("Size"),
60 (FontStyle
)info
.GetValue("Style", typeof(FontStyle
)),
61 (GraphicsUnit
)info
.GetValue("Unit", typeof(GraphicsUnit
)) ) {
64 public Font(Font original
, FontStyle style
) {
65 _jFont
= original
.NativeObject
.deriveFont( DeriveStyle(original
.NativeObject
.getAttributes(), style
, true) );
66 _gUnit
= original
._gUnit
;
67 _fontFamily
= original
._fontFamily
;
68 _charset
= original
._charset
;
71 public Font(FontFamily family
, float emSize
)
72 : this(family
, emSize
, FontStyle
.Regular
, GraphicsUnit
.Point
, DEFAULT_CHARSET
, false) {
75 public Font(FontFamily family
, float emSize
, FontStyle style
)
76 : this(family
, emSize
, style
, GraphicsUnit
.Point
, DEFAULT_CHARSET
, false) {
78 public Font(FontFamily family
, float emSize
, GraphicsUnit unit
)
79 : this(family
, emSize
, FontStyle
.Regular
, unit
, DEFAULT_CHARSET
, false) {
82 public Font(FontFamily family
, float emSize
, FontStyle style
, GraphicsUnit unit
)
83 : this(family
, emSize
, style
, unit
, DEFAULT_CHARSET
, false) {
86 public Font(FontFamily family
, float emSize
, FontStyle style
, GraphicsUnit unit
, byte charSet
)
87 : this(family
, emSize
, style
, unit
, charSet
, false) {
90 public Font(FontFamily family
, float emSize
, FontStyle style
, GraphicsUnit unit
, byte charSet
, bool isVertical
) {
92 throw new ArgumentNullException("family");
98 java
.util
.Hashtable attribs
= new java
.util
.Hashtable();
99 attribs
.put(TextAttribute
.FAMILY
, family
.Name
/*TODO: family doungrade possibility*/);
101 attribs
.put(TextAttribute
.POSTURE
, TextAttribute
.POSTURE_REGULAR
);
103 float newSize
= emSize
* Graphics
.UnitConversion
[ (int)_gUnit
];
104 attribs
.put(TextAttribute
.SIZE
, new java
.lang
.Float(newSize
));
106 DeriveStyle(attribs
, style
, false);
108 _jFont
= family
.FamilyFont
.deriveFont(attribs
);
111 public Font(string familyName
, float emSize
)
112 : this(familyName
, emSize
, FontStyle
.Regular
, GraphicsUnit
.Point
, (byte)0, false) {
115 public Font(string familyName
, float emSize
, FontStyle style
)
116 : this(familyName
, emSize
, style
, GraphicsUnit
.Point
, (byte)0, false) {
119 public Font(string familyName
, float emSize
, GraphicsUnit unit
)
120 : this(familyName
, emSize
, FontStyle
.Regular
, unit
, (byte)0, false) {
123 public Font(string familyName
, float emSize
, FontStyle style
, GraphicsUnit unit
)
124 : this(familyName
, emSize
, style
, unit
, (byte)0, false) {
127 public Font(string familyName
, float emSize
, FontStyle style
, GraphicsUnit unit
, byte charSet
)
128 : this(familyName
, emSize
, style
, unit
, charSet
, false) {
131 public Font(string familyName
, float emSize
, FontStyle style
, GraphicsUnit unit
, byte charSet
, bool isVertical
)
132 : this (GetFontFamily (familyName
), emSize
, style
, unit
, charSet
, isVertical
) {
136 internal Font (string familyName
, float emSize
, string systemName
)
137 : this (familyName
, emSize
) {
138 _systemFontName
= systemName
;
142 static FontFamily
GetFontFamily (string familyName
) {
144 if (familyName
== null)
145 throw new ArgumentNullException ("familyName");
147 // NOTE: If family name is null, empty or invalid,
148 // MS creates Microsoft Sans Serif font.
150 return new FontFamily (familyName
);
153 return FontFamily
.GenericSansSerif
;
159 #region IDisposable members
161 public void Dispose() {
168 public object Clone() {
169 return (Font
)MemberwiseClone();
174 public override bool Equals (object obj
)
176 Font other
= obj
as Font
;
181 return NativeObject
.Equals (other
.NativeObject
);
184 public override int GetHashCode ()
186 return NativeObject
.GetHashCode ();
191 public IntPtr
ToHfont ()
193 throw new NotImplementedException();
197 #region public properties
201 return _jFont
.isBold();
205 public FontFamily FontFamily
{
211 public byte GdiCharSet
{
217 public bool GdiVerticalFont
{
219 return Name
.StartsWith("@");
225 return FontFamily
.Container
.getFontMetrics(NativeObject
).getHeight();
229 public float GetHeight () {
230 return GetHeight (Graphics
.DefaultScreenResolution
);
233 public float GetHeight (float dpi
) {
234 return (FontFamily
.GetLineSpacing (Style
) / FontFamily
.GetEmHeight (Style
))
235 * (SizeInPoints
/ _screenResolutionConverter
[(int) Unit
])
239 public float GetHeight (Graphics graphics
) {
240 if (graphics
== null)
241 throw new ArgumentNullException ("graphics");
242 return GetHeight (graphics
.DpiY
);
247 return _jFont
.isItalic();
253 return _jFont
.getName();
259 return SizeInPoints
/ Graphics
.UnitConversion
[ (int)_gUnit
];
263 public float SizeInPoints
{
265 return _jFont
.getSize2D();
269 public bool Strikeout
{
272 if((java
.lang
.Boolean
)_jFont
.getAttributes().get(TextAttribute
.STRIKETHROUGH
)
273 == TextAttribute
.STRIKETHROUGH_ON
)
282 public FontStyle Style
{
284 FontStyle style
= FontStyle
.Regular
;
286 style
|= FontStyle
.Bold
;
288 style
|= FontStyle
.Italic
;
290 style
|= FontStyle
.Underline
;
292 style
|= FontStyle
.Strikeout
;
298 public bool Underline
{
301 if((java
.lang
.Integer
)_jFont
.getAttributes().get(TextAttribute
.UNDERLINE
)
302 == TextAttribute
.UNDERLINE_ON
)
311 [TypeConverter(typeof(FontConverter
.FontUnitConverter
))]
312 public GraphicsUnit Unit
{
320 public bool IsSystemFont
{
322 return !string.IsNullOrEmpty (_systemFontName
);
327 public string SystemFontName
{
329 return _systemFontName
;
336 public override System
.String
ToString() {
337 return ("[Font: Name="+ Name
+", Size="+ Size
+", Style="+ Style
+", Units="+ Unit
+"]");
340 static internal java
.util
.Map
DeriveStyle(java
.util
.Map attribs
, FontStyle style
, bool createNew
) {
341 java
.util
.Map newAttribs
;
343 newAttribs
= new java
.util
.Hashtable( attribs
.size() );
344 java
.util
.Iterator it
= attribs
.keySet().iterator();
345 while (it
.hasNext ()) {
346 object key
= it
.next ();
347 object value = attribs
.get (key
);
349 newAttribs
.put (key
, value);
353 newAttribs
= attribs
;
356 if((style
& FontStyle
.Bold
) == FontStyle
.Bold
)
357 newAttribs
.put(TextAttribute
.WEIGHT
, TextAttribute
.WEIGHT_BOLD
);
359 newAttribs
.remove(TextAttribute
.WEIGHT
);
362 if((style
& FontStyle
.Italic
) == FontStyle
.Italic
)
363 newAttribs
.put(TextAttribute
.POSTURE
, TextAttribute
.POSTURE_OBLIQUE
);
365 newAttribs
.put(TextAttribute
.POSTURE
, TextAttribute
.POSTURE_REGULAR
);
368 if((style
& FontStyle
.Underline
) == FontStyle
.Underline
)
369 newAttribs
.put(TextAttribute
.UNDERLINE
, TextAttribute
.UNDERLINE_ON
);
371 newAttribs
.remove(TextAttribute
.UNDERLINE
);
374 if((style
& FontStyle
.Strikeout
) == FontStyle
.Strikeout
)
375 newAttribs
.put(TextAttribute
.STRIKETHROUGH
, TextAttribute
.STRIKETHROUGH_ON
);
377 newAttribs
.remove(TextAttribute
.STRIKETHROUGH
);