| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using System.Runtime.Serialization;
- using System.Runtime.InteropServices;
- using System.ComponentModel;
- using awt = java.awt;
- using TextAttribute = java.awt.font.TextAttribute;
- namespace System.Drawing {
- [Serializable]
- public sealed class Font: MarshalByRefObject, ISerializable, ICloneable, IDisposable {
- #region variables
- const byte DEFAULT_CHARSET = 1;
- private readonly GraphicsUnit _gUnit = GraphicsUnit.Point;
- private readonly FontFamily _fontFamily;
- private readonly awt.Font _jFont;
- private readonly byte _charset;
- #endregion
- internal awt.Font NativeObject {
- get {
- return _jFont;
- }
- }
- #region ISerializable
- void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
- info.AddValue("Name", Name);
- info.AddValue("Size", Size);
- info.AddValue("Style", Style, typeof(FontStyle));
- info.AddValue("Unit", Unit, typeof(GraphicsUnit));
- }
- #endregion
- #region ctors
- private Font (SerializationInfo info, StreamingContext context)
- : this(
- info.GetString("Name"),
- info.GetSingle("Size"),
- (FontStyle)info.GetValue("Style", typeof(FontStyle)),
- (GraphicsUnit)info.GetValue("Unit", typeof(GraphicsUnit)) ) {
- }
- public Font(Font original, FontStyle style) {
- _jFont = original.NativeObject.deriveFont( DeriveStyle(original.NativeObject.getAttributes(), style, true) );
- _gUnit = original._gUnit;
- _fontFamily = original._fontFamily;
- }
- public Font(FontFamily family, float emSize)
- : this(family, emSize, FontStyle.Regular, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
- }
- public Font(FontFamily family, float emSize, FontStyle style)
- : this(family, emSize, style, GraphicsUnit.Point, DEFAULT_CHARSET, false) {
- }
- public Font(FontFamily family, float emSize, GraphicsUnit unit)
- : this(family, emSize, FontStyle.Regular, unit, DEFAULT_CHARSET, false) {
- }
- public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit)
- : this(family, emSize, style, unit, DEFAULT_CHARSET, false) {
- }
- public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
- : this(family, emSize, style, unit, charSet, false) {
- }
-
- public Font(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical) {
- if (family == null)
- throw new ArgumentNullException("family");
- _gUnit = unit;
- _fontFamily = family;
- _charset = charSet;
- java.util.Hashtable attribs = new java.util.Hashtable();
- attribs.put(TextAttribute.FAMILY, family.Name/*TODO: family doungrade possibility*/);
- //init defaults
- attribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
- float newSize = emSize * Graphics.UnitConversion[ (int)_gUnit ];
- attribs.put(TextAttribute.SIZE, new java.lang.Float(newSize));
- DeriveStyle(attribs, style, false);
- _jFont = family.FamilyFont.deriveFont(attribs);
- }
- public Font(string familyName, float emSize)
- : this(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, (byte)0, false) {
- }
- public Font(string familyName, float emSize, FontStyle style)
- : this(familyName, emSize, style, GraphicsUnit.Point, (byte)0, false) {
- }
- public Font(string familyName, float emSize, GraphicsUnit unit)
- : this(familyName, emSize, FontStyle.Regular, unit, (byte)0, false) {
- }
-
- public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit)
- : this(familyName, emSize, style, unit, (byte)0, false) {
- }
-
- public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet)
- : this(familyName, emSize, style, unit, charSet, false) {
- }
-
- public Font(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
- :this(new FontFamily(familyName), emSize, style, unit, charSet, isVertical) {
- }
- #endregion
-
- #region IDisposable members
- public void Dispose() {
- }
- #endregion
- #region ICloneable
- public object Clone() {
- return (Font)MemberwiseClone();
- }
- #endregion
-
- #if INTPTR_SUPPORT
- [MonoTODO]
- public IntPtr ToHfont ()
- {
- throw new NotImplementedException();
- }
- #endif
-
- #region public properties
- public bool Bold {
- get {
- return _jFont.isBold();
- }
- }
-
- public FontFamily FontFamily {
- get {
- return _fontFamily;
- }
- }
-
- public byte GdiCharSet {
- get {
- return _charset;
- }
- }
-
- public bool GdiVerticalFont {
- get {
- return Name.StartsWith("@");
- }
- }
-
- public int Height {
- get {
- awt.Container c = new awt.Container();
- return c.getFontMetrics(NativeObject).getHeight();
- }
- }
- public bool Italic {
- get {
- return _jFont.isItalic();
- }
- }
- public string Name {
- get {
- return _jFont.getName();
- }
- }
- public float Size {
- get {
- return SizeInPoints / Graphics.UnitConversion[ (int)_gUnit ];
- }
- }
-
- public float SizeInPoints {
- get {
- return _jFont.getSize2D();
- }
- }
-
- public bool Strikeout {
- get {
- try {
- if((java.lang.Boolean)_jFont.getAttributes().get(TextAttribute.STRIKETHROUGH)
- == TextAttribute.STRIKETHROUGH_ON )
- return true;
- }
- catch {
- }
- return false;
- }
- }
-
- public FontStyle Style {
- get {
- FontStyle style = FontStyle.Regular;
- if (Bold)
- style |= FontStyle.Bold;
- if (Italic)
- style |= FontStyle.Italic;
- if (Underline)
- style |= FontStyle.Underline;
- if (Strikeout)
- style |= FontStyle.Strikeout;
- return style;
- }
- }
-
- public bool Underline {
- get {
- try {
- if((java.lang.Integer)_jFont.getAttributes().get(TextAttribute.UNDERLINE)
- == TextAttribute.UNDERLINE_ON )
- return true;
- }
- catch {
- }
- return false;
- }
- }
- [TypeConverter(typeof(FontConverter.FontUnitConverter))]
- public GraphicsUnit Unit {
- get {
- return _gUnit;
- }
- }
- #endregion
-
- public override System.String ToString() {
- return ("[Font: Name="+ Name +", Size="+ Size +", Style="+ Style +", Units="+ Unit +"]");
- }
- static internal java.util.Map DeriveStyle(java.util.Map attribs, FontStyle style, bool createNew) {
- java.util.Map newAttribs;
- if (createNew) {
- newAttribs = new java.util.Hashtable( attribs.size() );
- object [] keys = attribs.keySet().toArray();
- for (int i=0; i < keys.Length; i++)
- newAttribs.put( keys[i], attribs.get( keys[i] ) );
- }
- else
- newAttribs = attribs;
- //Bold
- if((style & FontStyle.Bold) == FontStyle.Bold)
- newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
- else
- newAttribs.remove(TextAttribute.WEIGHT);
- //Italic
- if((style & FontStyle.Italic) == FontStyle.Italic)
- newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
- else
- newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
- //Underline
- if((style & FontStyle.Underline) == FontStyle.Underline)
- newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
- else
- newAttribs.remove(TextAttribute.UNDERLINE);
- //Strikeout
- if((style & FontStyle.Strikeout) == FontStyle.Strikeout)
- newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
- else
- newAttribs.remove(TextAttribute.STRIKETHROUGH);
- return newAttribs;
- }
- }
- }
|