123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /////////////////////////////////////////////////////////////////////////////////
- // 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.Drawing;
- using System.Runtime.Serialization;
- namespace PaintDotNet.Measurement.ObjInfo
- {
- /// <summary>
- /// Carries information about the Font details that we support.
- /// Does not carry text alignment information.
- /// </summary>
- [Serializable]
- public 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();
- }
- }
- }
|