SvgBrush.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Brush SVG object.
  10. /// </summary>
  11. public class SvgBrush : SvgObject, Gdi.IGdiBrush
  12. {
  13. #region Local Variables
  14. private int _style;
  15. private int _color;
  16. private int _hatch;
  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 color.
  31. /// </summary>
  32. public int Color
  33. {
  34. get
  35. {
  36. return _color;
  37. }
  38. }
  39. /// <summary>
  40. /// Object hatch.
  41. /// </summary>
  42. public int Hatch
  43. {
  44. get
  45. {
  46. return _hatch;
  47. }
  48. }
  49. #endregion
  50. #region Constructors
  51. /// <summary>
  52. /// Default constructor.
  53. /// </summary>
  54. /// <param name="gdi"></param>
  55. /// <param name="style"></param>
  56. /// <param name="color"></param>
  57. /// <param name="hatch"></param>
  58. public SvgBrush(
  59. SvgGdi gdi,
  60. int style,
  61. int color,
  62. int hatch)
  63. : base(gdi)
  64. {
  65. _style = style;
  66. _color = color;
  67. _hatch = hatch;
  68. }
  69. #endregion
  70. #region Public Methods
  71. /// <summary>
  72. /// Create element FillPatern.
  73. /// </summary>
  74. /// <param name="id"></param>
  75. /// <returns></returns>
  76. public System.Xml.XmlElement CreateFillPattern(string id)
  77. {
  78. System.Xml.XmlElement pattern = GDI.Document.CreateElement("pattern");
  79. if (_style == (int)Gdi.BrushBSEnum.BS_HATCHED)
  80. {
  81. pattern.SetAttribute("id", id);
  82. pattern.SetAttribute("patternUnits", "userSpaceOnUse");
  83. pattern.SetAttribute("x", "" + ToRealSize(0));
  84. pattern.SetAttribute("y", "" + ToRealSize(0));
  85. pattern.SetAttribute("width", "" + ToRealSize(8));
  86. pattern.SetAttribute("height", "" + ToRealSize(8));
  87. GDI.Document.AppendChild(pattern);
  88. if (GDI.DC.BkMode == (int)Gdi.GdiEnum.OPAQUE)
  89. {
  90. System.Xml.XmlElement rect = GDI.Document.CreateElement("rect");
  91. rect.SetAttribute("fill", ToColor(GDI.DC.BkColor));
  92. rect.SetAttribute("x", "" + ToRealSize(0));
  93. rect.SetAttribute("y", "" + ToRealSize(0));
  94. rect.SetAttribute("width", "" + ToRealSize(8));
  95. rect.SetAttribute("height", "" + ToRealSize(8));
  96. pattern.AppendChild(rect);
  97. }
  98. switch (_hatch)
  99. {
  100. case (int)Gdi.BrushHSEnum.HS_HORIZONTAL:
  101. {
  102. System.Xml.XmlElement path = GDI.Document.CreateElement("line");
  103. path.SetAttribute("stroke", ToColor(_color));
  104. path.SetAttribute("x1", "" + ToRealSize(0));
  105. path.SetAttribute("y1", "" + ToRealSize(4));
  106. path.SetAttribute("x2", "" + ToRealSize(8));
  107. path.SetAttribute("y2", "" + ToRealSize(4));
  108. pattern.AppendChild(path);
  109. } break;
  110. case (int)Gdi.BrushHSEnum.HS_VERTICAL:
  111. {
  112. System.Xml.XmlElement path = GDI.Document.CreateElement("line");
  113. path.SetAttribute("stroke", ToColor(_color));
  114. path.SetAttribute("x1", "" + ToRealSize(4));
  115. path.SetAttribute("y1", "" + ToRealSize(0));
  116. path.SetAttribute("x2", "" + ToRealSize(4));
  117. path.SetAttribute("y2", "" + ToRealSize(8));
  118. pattern.AppendChild(path);
  119. } break;
  120. case (int)Gdi.BrushHSEnum.HS_FDIAGONAL:
  121. {
  122. System.Xml.XmlElement path = GDI.Document.CreateElement("line");
  123. path.SetAttribute("stroke", ToColor(_color));
  124. path.SetAttribute("x1", "" + ToRealSize(0));
  125. path.SetAttribute("y1", "" + ToRealSize(0));
  126. path.SetAttribute("x2", "" + ToRealSize(8));
  127. path.SetAttribute("y2", "" + ToRealSize(8));
  128. pattern.AppendChild(path);
  129. } break;
  130. case (int)Gdi.BrushHSEnum.HS_BDIAGONAL:
  131. {
  132. System.Xml.XmlElement path = GDI.Document.CreateElement("line");
  133. path.SetAttribute("stroke", ToColor(_color));
  134. path.SetAttribute("x1", "" + ToRealSize(0));
  135. path.SetAttribute("y1", "" + ToRealSize(8));
  136. path.SetAttribute("x2", "" + ToRealSize(8));
  137. path.SetAttribute("y2", "" + ToRealSize(0));
  138. pattern.AppendChild(path);
  139. } break;
  140. case (int)Gdi.BrushHSEnum.HS_CROSS:
  141. {
  142. System.Xml.XmlElement path1 = GDI.Document.CreateElement("line");
  143. path1.SetAttribute("stroke", ToColor(_color));
  144. path1.SetAttribute("x1", "" + ToRealSize(0));
  145. path1.SetAttribute("y1", "" + ToRealSize(4));
  146. path1.SetAttribute("x2", "" + ToRealSize(8));
  147. path1.SetAttribute("y2", "" + ToRealSize(4));
  148. pattern.AppendChild(path1);
  149. System.Xml.XmlElement path2 = GDI.Document.CreateElement("line");
  150. path2.SetAttribute("stroke", ToColor(_color));
  151. path2.SetAttribute("x1", "" + ToRealSize(4));
  152. path2.SetAttribute("y1", "" + ToRealSize(0));
  153. path2.SetAttribute("x2", "" + ToRealSize(4));
  154. path2.SetAttribute("y2", "" + ToRealSize(8));
  155. pattern.AppendChild(path2);
  156. } break;
  157. case (int)Gdi.BrushHSEnum.HS_DIAGCROSS:
  158. {
  159. System.Xml.XmlElement path1 = GDI.Document.CreateElement("line");
  160. path1.SetAttribute("stroke", ToColor(_color));
  161. path1.SetAttribute("x1", "" + ToRealSize(0));
  162. path1.SetAttribute("y1", "" + ToRealSize(0));
  163. path1.SetAttribute("x2", "" + ToRealSize(8));
  164. path1.SetAttribute("y2", "" + ToRealSize(8));
  165. pattern.AppendChild(path1);
  166. System.Xml.XmlElement path2 = GDI.Document.CreateElement("line");
  167. path2.SetAttribute("stroke", ToColor(_color));
  168. path2.SetAttribute("x1", "" + ToRealSize(0));
  169. path2.SetAttribute("y1", "" + ToRealSize(8));
  170. path2.SetAttribute("x2", "" + ToRealSize(8));
  171. path2.SetAttribute("y2", "" + ToRealSize(0));
  172. pattern.AppendChild(path2);
  173. } break;
  174. }
  175. }
  176. return pattern;
  177. }
  178. /// <summary>
  179. /// Serves as the default hash function.
  180. /// </summary>
  181. /// <returns></returns>
  182. public override int GetHashCode()
  183. {
  184. int PRIME = 31;
  185. int result = 1;
  186. result = PRIME * result + _color;
  187. result = PRIME * result + _hatch;
  188. result = PRIME * result + _style;
  189. return result;
  190. }
  191. /// <summary>
  192. /// Determines whether the specified object is equal to the current object.
  193. /// </summary>
  194. /// <param name="obj"></param>
  195. /// <returns></returns>
  196. public override bool Equals(Object obj)
  197. {
  198. if (this == obj)
  199. return true;
  200. if (obj == null)
  201. return false;
  202. if (typeof(SvgBrush) != obj.GetType())
  203. return false;
  204. SvgBrush other = (SvgBrush)obj;
  205. if (_color != other._color)
  206. return false;
  207. if (_hatch != other._hatch)
  208. return false;
  209. if (_style != other._style)
  210. return false;
  211. return true;
  212. }
  213. /// <summary>
  214. /// Create element inner text.
  215. /// </summary>
  216. /// <param name="id"></param>
  217. /// <returns></returns>
  218. public System.Xml.XmlText CreateTextNode(string id)
  219. {
  220. return GDI.Document.CreateTextNode("." + id + " { " + ToString() + " }\r\n");
  221. }
  222. /// <summary>
  223. /// Returns a string that represents the current object.
  224. /// </summary>
  225. /// <returns></returns>
  226. public override string ToString()
  227. {
  228. System.Text.StringBuilder buffer = new System.Text.StringBuilder();
  229. // fill
  230. switch (_style)
  231. {
  232. case (int)Gdi.BrushBSEnum.BS_SOLID:
  233. buffer.Append("fill: ").Append(ToColor(_color)).Append("; ");
  234. break;
  235. case (int)Gdi.BrushBSEnum.BS_HATCHED:
  236. break;
  237. default:
  238. buffer.Append("fill: none; ");
  239. break;
  240. }
  241. if (buffer.Length > 0)
  242. buffer.Length = buffer.Length - 1;
  243. return buffer.ToString();
  244. }
  245. #endregion
  246. }
  247. }