SvgPen.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace WMFConverter.Svg
  7. {
  8. /// <summary>
  9. /// Scalable Vector Graphics - Represents a Pen object.
  10. /// </summary>
  11. public class SvgPen: SvgObject,Gdi.IGdiPen
  12. {
  13. #region Local Variables
  14. private int _style;
  15. private int _width;
  16. private int _color;
  17. #endregion
  18. #region Properties
  19. /// <summary>
  20. /// Object style.
  21. /// </summary>
  22. public int Style
  23. {
  24. get
  25. {
  26. return _style;
  27. }
  28. }
  29. /// <summary>
  30. /// Object width.
  31. /// </summary>
  32. public int Width
  33. {
  34. get
  35. {
  36. return _width;
  37. }
  38. }
  39. /// <summary>
  40. /// Object color.
  41. /// </summary>
  42. public int Color
  43. {
  44. get
  45. {
  46. return _color;
  47. }
  48. }
  49. #endregion
  50. #region Constructors
  51. /// <summary>
  52. /// Construdor padrão.
  53. /// </summary>
  54. /// <param name="gdi"></param>
  55. /// <param name="style"></param>
  56. /// <param name="width"></param>
  57. /// <param name="color"></param>
  58. public SvgPen(
  59. SvgGdi gdi,
  60. int style,
  61. int width,
  62. int color)
  63. :base(gdi)
  64. {
  65. _style = style;
  66. _width = (width > 0) ? width : 1;
  67. _color = color;
  68. }
  69. #endregion
  70. #region Public Methods
  71. /// <summary>
  72. /// Serves as the default hash function.
  73. /// </summary>
  74. /// <returns></returns>
  75. public override int GetHashCode()
  76. {
  77. int PRIME = 31;
  78. int result = 1;
  79. result = PRIME * result + _color;
  80. result = PRIME * result + _style;
  81. result = PRIME * result + _width;
  82. return result;
  83. }
  84. /// <summary>
  85. /// Determines whether the specified object is equal to the current object.
  86. /// </summary>
  87. /// <param name="obj"></param>
  88. /// <returns></returns>
  89. public override bool Equals(Object obj)
  90. {
  91. if (this == obj)
  92. return true;
  93. if (obj == null)
  94. return false;
  95. if (typeof(SvgPen) != obj.GetType())
  96. return false;
  97. SvgPen other = (SvgPen) obj;
  98. if (_color != other._color)
  99. return false;
  100. if (_style != other._style)
  101. return false;
  102. if (_width != other._width)
  103. return false;
  104. return true;
  105. }
  106. /// <summary>
  107. /// Create inner text element.
  108. /// </summary>
  109. /// <param name="id"></param>
  110. /// <returns></returns>
  111. public System.Xml.XmlText CreateTextNode(String id)
  112. {
  113. return GDI.Document.CreateTextNode("." + id + " { " + ToString() + " }\r\n");
  114. }
  115. /// <summary>
  116. /// Returns a string that represents the current object.
  117. /// </summary>
  118. /// <returns></returns>
  119. public override string ToString()
  120. {
  121. System.Text.StringBuilder buffer = new System.Text.StringBuilder();
  122. if (_style == (int)Gdi.PenPSEnum.PS_NULL)
  123. {
  124. buffer.Append("stroke: none; ");
  125. }
  126. else
  127. {
  128. // stroke
  129. buffer.Append("stroke: " + ToColor(_color) + "; ");
  130. // stroke-width
  131. buffer.Append("stroke-width: " + _width + "; ");
  132. // stroke-linejoin
  133. buffer.Append("stroke-linejoin: round; ");
  134. // stroke-dasharray
  135. if (_width == 1 && (int)Gdi.PenPSEnum.PS_DASH <= _style && _style <= (int)Gdi.PenPSEnum.PS_DASHDOTDOT)
  136. {
  137. buffer.Append("stroke-dasharray: ");
  138. switch (_style)
  139. {
  140. case (int)Gdi.PenPSEnum.PS_DASH:
  141. buffer.Append(
  142. "" + ToRealSize(18) + "," + ToRealSize(6));
  143. break;
  144. case (int)Gdi.PenPSEnum.PS_DOT:
  145. buffer.Append("" + ToRealSize(3) + "," + ToRealSize(3));
  146. break;
  147. case (int)Gdi.PenPSEnum.PS_DASHDOT:
  148. buffer.Append(
  149. ""
  150. + ToRealSize(9)
  151. + ","
  152. + ToRealSize(3)
  153. + ","
  154. + ToRealSize(3)
  155. + ","
  156. + ToRealSize(3));
  157. break;
  158. case (int)Gdi.PenPSEnum.PS_DASHDOTDOT:
  159. buffer.Append(
  160. ""
  161. + ToRealSize(9)
  162. + ","
  163. + ToRealSize(3)
  164. + ","
  165. + ToRealSize(3)
  166. + ","
  167. + ToRealSize(3)
  168. + ","
  169. + ToRealSize(3)
  170. + ","
  171. + ToRealSize(3));
  172. break;
  173. }
  174. buffer.Append("; ");
  175. }
  176. }
  177. if (buffer.Length > 0)
  178. buffer.Length = buffer.Length - 1;
  179. return buffer.ToString();
  180. }
  181. #endregion
  182. }
  183. }