FontInfo.cs :  » GUI » Paint.net » PaintDotNet » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » Paint.net 
Paint.net » PaintDotNet » FontInfo.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.Windows.Forms;

namespace PaintDotNet{
    /// <summary>
    /// Carries information about the Font details that we support.
    /// Does not carry text alignment information.
    /// </summary>
    [Serializable]
    internal class FontInfo
        : IDisposable,
          ISerializable,
          ICloneable
    {
        private FontFamily family;
        private float size;
        private FontStyle style;

        private const string fontFamilyNameTag = "family.Name";
        private const string sizeTag = "size";
        private const string styleTag = "style";

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(fontFamilyNameTag, this.family.Name);
            info.AddValue(sizeTag, this.size);
            info.AddValue(styleTag, (int)this.style);
        }

        protected FontInfo(SerializationInfo info, StreamingContext context)
        {
            string familyName = info.GetString(fontFamilyNameTag);

            try
            {
                this.family = new FontFamily(familyName);
            }

            catch (ArgumentException)
            {
                this.family = FontFamily.GenericSansSerif;
            }

            this.size = info.GetSingle(sizeTag);

            int styleInt = info.GetInt32(styleTag);
            this.style = (FontStyle)styleInt;
        }

        public static bool operator ==(FontInfo lhs, FontInfo rhs)
        {
            if ((lhs.family == rhs.family) && (lhs.size == rhs.size) && (lhs.style == rhs. style))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool operator!= (FontInfo lhs, FontInfo rhs)
        {
            return !(lhs == rhs);
        }

        public override bool Equals(object obj)
        {
            return this == (FontInfo)obj;
        }

        public override int GetHashCode()
        {
            return unchecked(family.GetHashCode() + size.GetHashCode() + style.GetHashCode());
        }
        
        /// <summary>
        /// Constructs an instance of the FontInfo class.
        /// </summary>
        /// <param name="fontFamily">The FontFamily to associate with this class. The FontInfo instance takes ownership of this FontFamily instance: do not call Dispose() on it.</param>
        /// <param name="size">The Size of the font.</param>
        /// <param name="fontStyle">The FontStyle of the font.</param>
        public FontInfo(FontFamily fontFamily, float size, FontStyle fontStyle)
        {
            this.FontFamily = fontFamily;
            this.Size = size;
            this.FontStyle = fontStyle;
        }

        ~FontInfo()
        {
            Dispose(false);
        }

        /// <summary>
        /// The FontFamily property gets and sets the font family.
        /// </summary>
        public FontFamily FontFamily
        {
            get
            {
                return family;
            }

            set
            {
                family = value;
            }
        }

        /// <summary>
        /// The Size property gets and sets the size of the text.
        /// </summary>
        public float Size
        {
            get
            {
                return this.size;
            }

            set
            {
                this.size = value;
            }
        }

        /// <summary>
        /// The FontStyle property gets and sets the font style to bold and or italic and or underline.
        /// </summary>
        public FontStyle FontStyle
        {
            get 
            {
                return this.style;
            }

            set
            {
                this.style = value;
            }
        }

        public bool CanCreateFont()
        {
            if (this.FontFamily.IsStyleAvailable(this.FontStyle))   // check to see if style is available in family
            {
                return true;
            }
            else    // find the style it is available in (coersion)
            {
                if (this.FontFamily.IsStyleAvailable(FontStyle.Regular))
                {
                    return true;
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Italic))
                {
                    return true;
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Bold))
                {
                    return true;
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Underline))
                {
                    return true;
                }
            }

            return false;
        }

        public Font CreateFont()
        {   
            // it may be possible that the font style may not be available in the font returned.  But I am not sure.
            // I am checking for this possibility in the textconfig widget getFontInfo fucntion
            if (this.FontFamily.IsStyleAvailable(this.FontStyle))   // check to see if style is availble in family
            {
                return new Font(this.FontFamily, (float)Math.Max(1.0, (double)this.Size), this.FontStyle);
            }
            else    // find the style it is available in (coersion)
            {
                FontStyle fs = new FontStyle();

                if (this.FontFamily.IsStyleAvailable(FontStyle.Regular))
                {
                    fs = FontStyle.Regular;
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Italic))
                {
                    fs = FontStyle.Italic;
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Bold))
                {
                    fs = FontStyle.Bold;    
                }
                else if (this.FontFamily.IsStyleAvailable(FontStyle.Underline))
                {
                    fs = FontStyle.Underline;
                }

                try
                {
                    return new Font(this.FontFamily, (float)Math.Max(1.0, (double)this.Size), fs);
                }

                catch
                {
                    return null;
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.family != null)
                {
                    this.family.Dispose();
                    this.family = null;
                }
            }
        }

        public FontInfo Clone()
        {
            return new FontInfo(this.family, this.size, this.style);
        }

        object ICloneable.Clone()
        {
            return Clone();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.