namespace IndexFormula.Finance { using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Xml.Serialization; [TypeConverter(typeof(ExpandableObjectConverter)), Editor(typeof(FontMapperEditor), typeof(UITypeEditor))] public class FontMapper { private StringAlignment alignment; private StringAlignment lineAlignment; private BrushMapper textBrush; private Font textFont; public FontMapper() : this("Verdana", 10f) { } public FontMapper(string familyName, float emSize) { this.textBrush = new BrushMapper(); this.alignment = StringAlignment.Center; this.lineAlignment = StringAlignment.Center; this.textFont = new Font(familyName, emSize); } public FontMapper Clone() { FontMapper mapper = new FontMapper(this.textFont.FontFamily.Name, this.textFont.Size); mapper.textBrush = this.textBrush.Clone(); mapper.alignment = this.alignment; mapper.lineAlignment = this.lineAlignment; return mapper; } public void DrawString(string s, Graphics g, RectangleF Rect) { g.DrawString(s, this.textFont, this.textBrush.GetBrush(Rect), Rect, this.GetStringFormat()); } private StringFormat GetStringFormat() { StringFormat format = new StringFormat(); format.Alignment = this.alignment; format.LineAlignment = this.lineAlignment; return format; } public RectangleF GetTextBackRect(string s, Graphics g, RectangleF Rect) { if (g != null) { SizeF size = g.MeasureString(s, this.textFont, Rect.Size, this.GetStringFormat()); RectangleF ef2 = new RectangleF(Rect.Location, size); if (this.alignment == StringAlignment.Center) { ef2.X += (Rect.Width - ef2.Width) / 2f; } else if (this.alignment == StringAlignment.Far) { ef2.X += Rect.Width - ef2.Width; } if (this.lineAlignment == StringAlignment.Center) { ef2.Y += (Rect.Height - ef2.Height) / 2f; return ef2; } if (this.lineAlignment == StringAlignment.Far) { ef2.Y += Rect.Height - ef2.Height; } return ef2; } return RectangleF.Empty; } public SizeF Measure(Graphics g, string s) { return this.Measure(g, s, 0x3e8); } public SizeF Measure(Graphics g, string s, int w) { if (g != null) { return g.MeasureString(s, this.textFont, w, this.GetStringFormat()); } return SizeF.Empty; } public bool ShouldSerializeTextBrush() { return BrushMapper.NotDefault(this.TextBrush); } public override string ToString() { return TypeDescriptor.GetConverter(typeof(Font)).ConvertToString(this.TextFont); } [XmlAttribute, RefreshProperties(RefreshProperties.All), DefaultValue(1)] public StringAlignment Alignment { get { return this.alignment; } set { this.alignment = value; } } [RefreshProperties(RefreshProperties.All), DefaultValue(1), XmlAttribute] public StringAlignment LineAlignment { get { return this.lineAlignment; } set { this.lineAlignment = value; } } public BrushMapper TextBrush { get { return this.textBrush; } set { this.textBrush = value; } } [XmlIgnore] public Font TextFont { get { return this.textFont; } set { this.textFont = value; } } [XmlElement("TextFont"), DefaultValue("Verdana, 10pt"), Browsable(false)] public string XmlTextFont { get { return TypeDescriptor.GetConverter(typeof(Font)).ConvertToString(null, FormulaHelper.enUS, this.TextFont); } set { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); this.TextFont = (Font) converter.ConvertFromString(null, FormulaHelper.enUS, value); } } } }