using System.Drawing; using System.Xml.Serialization; namespace GeoSigma.SigmaDrawerStyle { /// /// 字体 /// public class WellFontEx { /// 字体名称,如“宋体” [XmlAttribute("FN")] public string FontName { get; set; } /// [XmlAttribute("W")] public float Width { get; set; } /// [XmlAttribute("H")] public float Height { get; set; } /// 粗细 [XmlAttribute("B")] public int Weight { get; set; } /// 斜体 [XmlAttribute("I")] public bool Italic { get; set; } /// 下划线 [XmlAttribute("U")] public bool Underline { get; set; } /// /// 删除线 /// [XmlAttribute("S")] public bool StrikeOut { get; set; } /// /// 间距 /// [XmlAttribute("PF")] public double Space { get; set; } /// /// 用于 xml 序列化反序列化,它不支持 Color,这里玩个技巧 /// [XmlAttribute("C")] public string FontColorString { get => ColorTranslator.ToHtml(FontColor); set => FontColor = ColorTranslator.FromHtml(value); } /// 字体颜色 [XmlIgnore] public Color FontColor { get; set; } /// /// 显示方式:0常规,1上标,2下标 /// [XmlAttribute("SP")] public int Script { get; set; } /// /// 字体内部文本内容 /// [XmlText] public string Text { get; set; } /// public override string ToString() { return FontName; } /// /// 转换到 Font /// /// 封装的字体对象 /// winform 内置字体对象 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; } /// /// 从 winform 字体对象转换为封装的字体对象 /// /// winform 字体对象 /// 封装的字体对象 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; } } }