FontInfo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Drawing;
  11. using System.Runtime.Serialization;
  12. namespace PaintDotNet.Measurement.ObjInfo
  13. {
  14. /// <summary>
  15. /// Carries information about the Font details that we support.
  16. /// Does not carry text alignment information.
  17. /// </summary>
  18. [Serializable]
  19. public class FontInfo
  20. : IDisposable,
  21. ISerializable,
  22. ICloneable
  23. {
  24. private FontFamily family;
  25. private float size;
  26. private FontStyle style;
  27. private const string fontFamilyNameTag = "family.Name";
  28. private const string sizeTag = "size";
  29. private const string styleTag = "style";
  30. public void GetObjectData(SerializationInfo info, StreamingContext context)
  31. {
  32. info.AddValue(fontFamilyNameTag, this.family.Name);
  33. info.AddValue(sizeTag, this.size);
  34. info.AddValue(styleTag, (int)this.style);
  35. }
  36. protected FontInfo(SerializationInfo info, StreamingContext context)
  37. {
  38. string familyName = info.GetString(fontFamilyNameTag);
  39. try
  40. {
  41. this.family = new FontFamily(familyName);
  42. }
  43. catch (ArgumentException)
  44. {
  45. this.family = FontFamily.GenericSansSerif;
  46. }
  47. this.size = info.GetSingle(sizeTag);
  48. int styleInt = info.GetInt32(styleTag);
  49. this.style = (FontStyle)styleInt;
  50. }
  51. public static bool operator ==(FontInfo lhs, FontInfo rhs)
  52. {
  53. if ((lhs.family == rhs.family) && (lhs.size == rhs.size) && (lhs.style == rhs.style))
  54. {
  55. return true;
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. }
  62. public static bool operator !=(FontInfo lhs, FontInfo rhs)
  63. {
  64. return !(lhs == rhs);
  65. }
  66. public override bool Equals(object obj)
  67. {
  68. return this == (FontInfo)obj;
  69. }
  70. public override int GetHashCode()
  71. {
  72. return unchecked(family.GetHashCode() + size.GetHashCode() + style.GetHashCode());
  73. }
  74. /// <summary>
  75. /// Constructs an instance of the FontInfo class.
  76. /// </summary>
  77. /// <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>
  78. /// <param name="size">The Size of the font.</param>
  79. /// <param name="fontStyle">The FontStyle of the font.</param>
  80. public FontInfo(FontFamily fontFamily, float size, FontStyle fontStyle)
  81. {
  82. this.FontFamily = fontFamily;
  83. this.Size = size;
  84. this.FontStyle = fontStyle;
  85. }
  86. ~FontInfo()
  87. {
  88. Dispose(false);
  89. }
  90. /// <summary>
  91. /// The FontFamily property gets and sets the font family.
  92. /// </summary>
  93. public FontFamily FontFamily
  94. {
  95. get
  96. {
  97. return family;
  98. }
  99. set
  100. {
  101. family = value;
  102. }
  103. }
  104. /// <summary>
  105. /// The Size property gets and sets the size of the text.
  106. /// </summary>
  107. public float Size
  108. {
  109. get
  110. {
  111. return this.size;
  112. }
  113. set
  114. {
  115. this.size = value;
  116. }
  117. }
  118. /// <summary>
  119. /// The FontStyle property gets and sets the font style to bold and or italic and or underline.
  120. /// </summary>
  121. public FontStyle FontStyle
  122. {
  123. get
  124. {
  125. return this.style;
  126. }
  127. set
  128. {
  129. this.style = value;
  130. }
  131. }
  132. public bool CanCreateFont()
  133. {
  134. if (this.FontFamily.IsStyleAvailable(this.FontStyle)) // check to see if style is available in family
  135. {
  136. return true;
  137. }
  138. else // find the style it is available in (coersion)
  139. {
  140. if (this.FontFamily.IsStyleAvailable(FontStyle.Regular))
  141. {
  142. return true;
  143. }
  144. else if (this.FontFamily.IsStyleAvailable(FontStyle.Italic))
  145. {
  146. return true;
  147. }
  148. else if (this.FontFamily.IsStyleAvailable(FontStyle.Bold))
  149. {
  150. return true;
  151. }
  152. else if (this.FontFamily.IsStyleAvailable(FontStyle.Underline))
  153. {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. public Font CreateFont()
  160. {
  161. // it may be possible that the font style may not be available in the font returned. But I am not sure.
  162. // I am checking for this possibility in the textconfig widget getFontInfo fucntion
  163. if (this.FontFamily.IsStyleAvailable(this.FontStyle)) // check to see if style is availble in family
  164. {
  165. return new Font(this.FontFamily, (float)Math.Max(1.0, (double)this.Size), this.FontStyle);
  166. }
  167. else // find the style it is available in (coersion)
  168. {
  169. FontStyle fs = new FontStyle();
  170. if (this.FontFamily.IsStyleAvailable(FontStyle.Regular))
  171. {
  172. fs = FontStyle.Regular;
  173. }
  174. else if (this.FontFamily.IsStyleAvailable(FontStyle.Italic))
  175. {
  176. fs = FontStyle.Italic;
  177. }
  178. else if (this.FontFamily.IsStyleAvailable(FontStyle.Bold))
  179. {
  180. fs = FontStyle.Bold;
  181. }
  182. else if (this.FontFamily.IsStyleAvailable(FontStyle.Underline))
  183. {
  184. fs = FontStyle.Underline;
  185. }
  186. try
  187. {
  188. return new Font(this.FontFamily, (float)Math.Max(1.0, (double)this.Size), fs);
  189. }
  190. catch
  191. {
  192. return null;
  193. }
  194. }
  195. }
  196. public void Dispose()
  197. {
  198. Dispose(true);
  199. GC.SuppressFinalize(this);
  200. }
  201. private void Dispose(bool disposing)
  202. {
  203. if (disposing)
  204. {
  205. if (this.family != null)
  206. {
  207. this.family.Dispose();
  208. this.family = null;
  209. }
  210. }
  211. }
  212. public FontInfo Clone()
  213. {
  214. return new FontInfo(this.family, this.size, this.style);
  215. }
  216. object ICloneable.Clone()
  217. {
  218. return Clone();
  219. }
  220. }
  221. }