fix typo
[mcs.git] / class / System.Drawing / System.Drawing / Font.jvm.cs
blob4a622f090f269ee40080bb8342acca1d8b4d0468
2 using System.Runtime.Serialization;
3 using System.Runtime.InteropServices;
4 using System.ComponentModel;
5 using awt = java.awt;
6 using TextAttribute = java.awt.font.TextAttribute;
8 namespace System.Drawing {
9 [Serializable]
10 public sealed class Font: MarshalByRefObject, ISerializable, ICloneable, IDisposable {
12 #region variables
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 = {
22 1, // World
23 1, // Display
24 1, // Pixel
25 Graphics.DefaultScreenResolution, // Point
26 Graphics.DefaultScreenResolution, // Inch
27 Graphics.DefaultScreenResolution, // Document
28 Graphics.DefaultScreenResolution // Millimeter
31 #if NET_2_0
32 private readonly string _systemFontName;
33 #endif
35 #endregion
37 internal awt.Font NativeObject {
38 get {
39 return _jFont;
43 #region ISerializable
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));
52 #endregion
54 #region ctors
56 private Font (SerializationInfo info, StreamingContext context)
57 : this(
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) {
91 if (family == null)
92 throw new ArgumentNullException("family");
94 _gUnit = unit;
95 _fontFamily = family;
96 _charset = charSet;
98 java.util.Hashtable attribs = new java.util.Hashtable();
99 attribs.put(TextAttribute.FAMILY, family.Name/*TODO: family doungrade possibility*/);
100 //init defaults
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) {
135 #if NET_2_0
136 internal Font (string familyName, float emSize, string systemName)
137 : this (familyName, emSize) {
138 _systemFontName = systemName;
140 #endif
142 static FontFamily GetFontFamily (string familyName) {
143 #if ONLY_1_1
144 if (familyName == null)
145 throw new ArgumentNullException ("familyName");
146 #endif
147 // NOTE: If family name is null, empty or invalid,
148 // MS creates Microsoft Sans Serif font.
149 try {
150 return new FontFamily (familyName);
152 catch {
153 return FontFamily.GenericSansSerif;
157 #endregion
159 #region IDisposable members
161 public void Dispose() {
164 #endregion
166 #region ICloneable
168 public object Clone() {
169 return (Font)MemberwiseClone();
172 #endregion
174 public override bool Equals (object obj)
176 Font other = obj as Font;
177 if (other == null) {
178 return false;
181 return NativeObject.Equals (other.NativeObject);
184 public override int GetHashCode ()
186 return NativeObject.GetHashCode ();
189 #if INTPTR_SUPPORT
190 [MonoTODO]
191 public IntPtr ToHfont ()
193 throw new NotImplementedException();
195 #endif
197 #region public properties
199 public bool Bold {
200 get {
201 return _jFont.isBold();
205 public FontFamily FontFamily {
206 get {
207 return _fontFamily;
211 public byte GdiCharSet {
212 get {
213 return _charset;
217 public bool GdiVerticalFont {
218 get {
219 return Name.StartsWith("@");
223 public int Height {
224 get {
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])
236 * dpi;
239 public float GetHeight (Graphics graphics) {
240 if (graphics == null)
241 throw new ArgumentNullException ("graphics");
242 return GetHeight (graphics.DpiY);
245 public bool Italic {
246 get {
247 return _jFont.isItalic();
251 public string Name {
252 get {
253 return _jFont.getName();
257 public float Size {
258 get {
259 return SizeInPoints / Graphics.UnitConversion[ (int)_gUnit ];
263 public float SizeInPoints {
264 get {
265 return _jFont.getSize2D();
269 public bool Strikeout {
270 get {
271 try {
272 if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH)
273 == TextAttribute.STRIKETHROUGH_ON )
274 return true;
276 catch {
278 return false;
282 public FontStyle Style {
283 get {
284 FontStyle style = FontStyle.Regular;
285 if (Bold)
286 style |= FontStyle.Bold;
287 if (Italic)
288 style |= FontStyle.Italic;
289 if (Underline)
290 style |= FontStyle.Underline;
291 if (Strikeout)
292 style |= FontStyle.Strikeout;
294 return style;
298 public bool Underline {
299 get {
300 try {
301 if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE)
302 == TextAttribute.UNDERLINE_ON )
303 return true;
305 catch {
307 return false;
311 [TypeConverter(typeof(FontConverter.FontUnitConverter))]
312 public GraphicsUnit Unit {
313 get {
314 return _gUnit;
318 #if NET_2_0
319 [Browsable (false)]
320 public bool IsSystemFont {
321 get {
322 return !string.IsNullOrEmpty (_systemFontName);
326 [Browsable (false)]
327 public string SystemFontName {
328 get {
329 return _systemFontName;
332 #endif
334 #endregion
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;
342 if (createNew) {
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);
348 if (value != null)
349 newAttribs.put (key, value);
352 else
353 newAttribs = attribs;
355 //Bold
356 if((style & FontStyle.Bold) == FontStyle.Bold)
357 newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
358 else
359 newAttribs.remove(TextAttribute.WEIGHT);
361 //Italic
362 if((style & FontStyle.Italic) == FontStyle.Italic)
363 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
364 else
365 newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
367 //Underline
368 if((style & FontStyle.Underline) == FontStyle.Underline)
369 newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
370 else
371 newAttribs.remove(TextAttribute.UNDERLINE);
373 //Strikeout
374 if((style & FontStyle.Strikeout) == FontStyle.Strikeout)
375 newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
376 else
377 newAttribs.remove(TextAttribute.STRIKETHROUGH);
379 return newAttribs;