FontMapper.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Design;
  7. using System.Xml.Serialization;
  8. [TypeConverter(typeof(ExpandableObjectConverter)), Editor(typeof(FontMapperEditor), typeof(UITypeEditor))]
  9. public class FontMapper
  10. {
  11. private StringAlignment alignment;
  12. private StringAlignment lineAlignment;
  13. private BrushMapper textBrush;
  14. private Font textFont;
  15. public FontMapper() : this("Verdana", 10f)
  16. {
  17. }
  18. public FontMapper(string familyName, float emSize)
  19. {
  20. this.textBrush = new BrushMapper();
  21. this.alignment = StringAlignment.Center;
  22. this.lineAlignment = StringAlignment.Center;
  23. this.textFont = new Font(familyName, emSize);
  24. }
  25. public FontMapper Clone()
  26. {
  27. FontMapper mapper = new FontMapper(this.textFont.FontFamily.Name, this.textFont.Size);
  28. mapper.textBrush = this.textBrush.Clone();
  29. mapper.alignment = this.alignment;
  30. mapper.lineAlignment = this.lineAlignment;
  31. return mapper;
  32. }
  33. public void DrawString(string s, Graphics g, RectangleF Rect)
  34. {
  35. g.DrawString(s, this.textFont, this.textBrush.GetBrush(Rect), Rect, this.GetStringFormat());
  36. }
  37. private StringFormat GetStringFormat()
  38. {
  39. StringFormat format = new StringFormat();
  40. format.Alignment = this.alignment;
  41. format.LineAlignment = this.lineAlignment;
  42. return format;
  43. }
  44. public RectangleF GetTextBackRect(string s, Graphics g, RectangleF Rect)
  45. {
  46. if (g != null)
  47. {
  48. SizeF size = g.MeasureString(s, this.textFont, Rect.Size, this.GetStringFormat());
  49. RectangleF ef2 = new RectangleF(Rect.Location, size);
  50. if (this.alignment == StringAlignment.Center)
  51. {
  52. ef2.X += (Rect.Width - ef2.Width) / 2f;
  53. }
  54. else if (this.alignment == StringAlignment.Far)
  55. {
  56. ef2.X += Rect.Width - ef2.Width;
  57. }
  58. if (this.lineAlignment == StringAlignment.Center)
  59. {
  60. ef2.Y += (Rect.Height - ef2.Height) / 2f;
  61. return ef2;
  62. }
  63. if (this.lineAlignment == StringAlignment.Far)
  64. {
  65. ef2.Y += Rect.Height - ef2.Height;
  66. }
  67. return ef2;
  68. }
  69. return RectangleF.Empty;
  70. }
  71. public SizeF Measure(Graphics g, string s)
  72. {
  73. return this.Measure(g, s, 0x3e8);
  74. }
  75. public SizeF Measure(Graphics g, string s, int w)
  76. {
  77. if (g != null)
  78. {
  79. return g.MeasureString(s, this.textFont, w, this.GetStringFormat());
  80. }
  81. return SizeF.Empty;
  82. }
  83. public bool ShouldSerializeTextBrush()
  84. {
  85. return BrushMapper.NotDefault(this.TextBrush);
  86. }
  87. public override string ToString()
  88. {
  89. return TypeDescriptor.GetConverter(typeof(Font)).ConvertToString(this.TextFont);
  90. }
  91. [XmlAttribute, RefreshProperties(RefreshProperties.All), DefaultValue(1)]
  92. public StringAlignment Alignment
  93. {
  94. get
  95. {
  96. return this.alignment;
  97. }
  98. set
  99. {
  100. this.alignment = value;
  101. }
  102. }
  103. [RefreshProperties(RefreshProperties.All), DefaultValue(1), XmlAttribute]
  104. public StringAlignment LineAlignment
  105. {
  106. get
  107. {
  108. return this.lineAlignment;
  109. }
  110. set
  111. {
  112. this.lineAlignment = value;
  113. }
  114. }
  115. public BrushMapper TextBrush
  116. {
  117. get
  118. {
  119. return this.textBrush;
  120. }
  121. set
  122. {
  123. this.textBrush = value;
  124. }
  125. }
  126. [XmlIgnore]
  127. public Font TextFont
  128. {
  129. get
  130. {
  131. return this.textFont;
  132. }
  133. set
  134. {
  135. this.textFont = value;
  136. }
  137. }
  138. [XmlElement("TextFont"), DefaultValue("Verdana, 10pt"), Browsable(false)]
  139. public string XmlTextFont
  140. {
  141. get
  142. {
  143. return TypeDescriptor.GetConverter(typeof(Font)).ConvertToString(null, FormulaHelper.enUS, this.TextFont);
  144. }
  145. set
  146. {
  147. TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
  148. this.TextFont = (Font) converter.ConvertFromString(null, FormulaHelper.enUS, value);
  149. }
  150. }
  151. }
  152. }