CapSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.ComponentModel;
  2. using System.Drawing.Drawing2D;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. namespace FastReport
  6. {
  7. /// <summary>
  8. /// Specifies a line cap style.
  9. /// </summary>
  10. public enum CapStyle
  11. {
  12. /// <summary>
  13. /// Specifies a line without a cap.
  14. /// </summary>
  15. None,
  16. /// <summary>
  17. /// Specifies a line with a circle cap.
  18. /// </summary>
  19. Circle,
  20. /// <summary>
  21. /// Specifies a line with a square cap.
  22. /// </summary>
  23. Square,
  24. /// <summary>
  25. /// Specifies a line with a diamond cap.
  26. /// </summary>
  27. Diamond,
  28. /// <summary>
  29. /// Specifies a line with an arrow cap.
  30. /// </summary>
  31. Arrow
  32. }
  33. /// <summary>
  34. /// Specifies a start and end line caps.
  35. /// </summary>
  36. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  37. public class CapSettings
  38. {
  39. private float width;
  40. private float height;
  41. private CapStyle style;
  42. /// <summary>
  43. /// Gets or sets a width of the cap.
  44. /// </summary>
  45. [DefaultValue(8f)]
  46. public float Width
  47. {
  48. get { return width; }
  49. set { width = value; }
  50. }
  51. /// <summary>
  52. /// Gets or sets a height of the cap.
  53. /// </summary>
  54. [DefaultValue(8f)]
  55. public float Height
  56. {
  57. get { return height; }
  58. set { height = value; }
  59. }
  60. /// <summary>
  61. /// Gets or sets a cap style.
  62. /// </summary>
  63. [DefaultValue(CapStyle.None)]
  64. public CapStyle Style
  65. {
  66. get { return style; }
  67. set { style = value; }
  68. }
  69. internal void GetCustomCapPath(out GraphicsPath path, out float inset)
  70. {
  71. path = new GraphicsPath();
  72. inset = 0;
  73. switch (Style)
  74. {
  75. case CapStyle.Arrow:
  76. path.AddLine(new PointF(0, 0), new PointF(-Width, -Height));
  77. path.AddLine(new PointF(0, 0), new PointF(Width, -Height));
  78. break;
  79. case CapStyle.Circle:
  80. path.AddEllipse(-Width / 2, -Height / 2, Width, Height);
  81. inset = Height / 2;
  82. break;
  83. case CapStyle.Square:
  84. path.AddRectangle(new RectangleF(-Width / 2, -Height / 2, Width, Height));
  85. inset = Height / 2;
  86. break;
  87. case CapStyle.Diamond:
  88. path.AddLine(new PointF(0, -Height / 1.4f), new PointF(-Width / 1.4f, 0));
  89. path.AddLine(new PointF(-Width / 1.4f, 0), new PointF(0, Height / 1.4f));
  90. path.AddLine(new PointF(0, Height / 1.4f), new PointF(Width / 1.4f, 0));
  91. path.AddLine(new PointF(Width / 1.4f, 0), new PointF(0, -Height / 1.4f));
  92. inset = Height / 1.4f;
  93. break;
  94. }
  95. }
  96. /// <summary>
  97. /// Assigns values from another source.
  98. /// </summary>
  99. /// <param name="source">Source to assign from.</param>
  100. public void Assign(CapSettings source)
  101. {
  102. Width = source.Width;
  103. Height = source.Height;
  104. Style = source.Style;
  105. }
  106. /// <summary>
  107. /// Creates exact copy of this object.
  108. /// </summary>
  109. /// <returns>Copy of this object.</returns>
  110. public CapSettings Clone()
  111. {
  112. CapSettings result = new CapSettings();
  113. result.Assign(this);
  114. return result;
  115. }
  116. /// <inheritdoc/>
  117. public override bool Equals(object obj)
  118. {
  119. CapSettings c = obj as CapSettings;
  120. return c != null && Width == c.Width && Height == c.Height && Style == c.Style;
  121. }
  122. /// <inheritdoc/>
  123. public override int GetHashCode()
  124. {
  125. return base.GetHashCode();
  126. }
  127. /// <summary>
  128. /// Serializes the cap settings.
  129. /// </summary>
  130. /// <param name="prefix">Name of the cap property.</param>
  131. /// <param name="writer">Writer object.</param>
  132. /// <param name="diff">Another cap to compare with.</param>
  133. /// <remarks>
  134. /// This method is for internal use only.
  135. /// </remarks>
  136. public void Serialize(string prefix, FRWriter writer, CapSettings diff)
  137. {
  138. if (Width != diff.Width)
  139. writer.WriteFloat(prefix + ".Width", Width);
  140. if (Height != diff.Height)
  141. writer.WriteFloat(prefix + ".Height", Height);
  142. if (Style != diff.Style)
  143. writer.WriteValue(prefix + ".Style", Style);
  144. }
  145. /// <summary>
  146. /// Initializes a new instance of the <b>CapSettings</b> class with default settings.
  147. /// </summary>
  148. public CapSettings()
  149. {
  150. width = 8;
  151. height = 8;
  152. }
  153. }
  154. }