ShareX/ShareX.HelpersLib/XmlFont.cs

104 lines
2.8 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Drawing;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
[Serializable]
public class XmlFont
{
public string FontFamily { get; set; }
public float Size { get; set; }
public FontStyle Style { get; set; }
public GraphicsUnit GraphicsUnit { get; set; }
public XmlFont()
{
}
public XmlFont(Font font)
{
Init(font);
}
public XmlFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular)
{
Font font = CreateFont(fontName, fontSize, fontStyle);
Init(font);
}
private void Init(Font font)
{
2013-11-14 08:55:04 +13:00
using (font)
{
FontFamily = font.FontFamily.Name;
Size = font.Size;
Style = font.Style;
GraphicsUnit = font.Unit;
}
2013-11-03 23:53:49 +13:00
}
private Font CreateFont(string fontName, float fontSize, FontStyle fontStyle)
{
try
{
return new Font(fontName, fontSize, fontStyle);
}
catch
{
return new Font(SystemFonts.DefaultFont.FontFamily, fontSize, fontStyle);
}
}
public static implicit operator Font(XmlFont font)
{
2013-11-14 08:55:04 +13:00
return font.ToFont();
2013-11-03 23:53:49 +13:00
}
public static implicit operator XmlFont(Font font)
{
return new XmlFont(font);
}
2013-11-14 08:55:04 +13:00
public Font ToFont()
{
return new Font(FontFamily, Size, Style, GraphicsUnit);
}
public override string ToString()
{
2013-11-14 21:01:21 +13:00
string text = string.Format("{0}; {1}", FontFamily, Size);
2013-11-14 08:55:04 +13:00
if (Style != FontStyle.Regular)
{
2013-11-14 21:01:21 +13:00
text += "; " + Style;
2013-11-14 08:55:04 +13:00
}
return text;
}
2013-11-03 23:53:49 +13:00
}
}