You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.6 KiB
C#

1 month ago
using System.Drawing;
using System.Xml.Serialization;
namespace GeoSigma.SigmaDrawerStyle
{
/// <summary>
/// 字体
/// </summary>
public class WellFontEx
{
/// <summary>字体名称,如“宋体”</summary>
[XmlAttribute("FN")]
public string FontName { get; set; }
/// <summary>宽</summary>
[XmlAttribute("W")]
public float Width { get; set; }
/// <summary>高</summary>
[XmlAttribute("H")]
public float Height { get; set; }
/// <summary>粗细</summary>
[XmlAttribute("B")]
public int Weight { get; set; }
/// <summary>斜体</summary>
[XmlAttribute("I")]
public bool Italic { get; set; }
/// <summary>下划线</summary>
[XmlAttribute("U")]
public bool Underline { get; set; }
/// <summary>
/// 删除线
/// </summary>
[XmlAttribute("S")]
public bool StrikeOut { get; set; }
/// <summary>
/// 间距
/// </summary>
[XmlAttribute("PF")]
public double Space { get; set; }
/// <summary>
/// 用于 xml 序列化反序列化,它不支持 Color这里玩个技巧
/// </summary>
[XmlAttribute("C")]
public string FontColorString
{
get => ColorTranslator.ToHtml(FontColor);
set => FontColor = ColorTranslator.FromHtml(value);
}
/// <summary>字体颜色</summary>
[XmlIgnore]
public Color FontColor { get; set; }
/// <summary>
/// 显示方式0常规1上标2下标
/// </summary>
[XmlAttribute("SP")]
public int Script { get; set; }
/// <summary>
/// 字体内部文本内容
/// </summary>
[XmlText]
public string Text { get; set; }
/// <inheritdoc/>
public override string ToString()
{
return FontName;
}
/// <summary>
/// 转换到 Font
/// </summary>
/// <param name="fontEx">封装的字体对象</param>
/// <returns>winform 内置字体对象</returns>
public static Font ToFont(WellFontEx fontEx)
{
FontStyle style = FontStyle.Regular;
// 粗体,暂时参照着 LogFont.cs 里面写的
if (fontEx.Weight != 800)
{
style |= FontStyle.Bold;
}
if (fontEx.Italic)
{
style |= FontStyle.Italic;
}
if (fontEx.Underline)
{
style |= FontStyle.Underline;
}
if (fontEx.StrikeOut)
{
style |= FontStyle.Strikeout;
}
var font = new Font(fontEx.FontName ?? "宋体", fontEx.Height/ 2.8f, style);
return font;
}
/// <summary>
/// 从 winform 字体对象转换为封装的字体对象
/// </summary>
/// <param name="font">winform 字体对象</param>
/// <returns>封装的字体对象</returns>
public static WellFontEx FromFont(Font font)
{
var fontEx = new WellFontEx();
fontEx.FontName = font.Name;
fontEx.Width = font.Size * 0.4f * 2.8f; // 我也不知道为什么是乘0.4 (乘以系数2.8正好接近井模块中的字体 gaofeng)
fontEx.Height = font.Size * 2.8f;
fontEx.Weight = font.Bold ? 800 : 0;
fontEx.Italic = font.Italic;
fontEx.Underline = font.Underline;
fontEx.StrikeOut = font.Strikeout;
return fontEx;
}
}
}