SvgFragment.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Xml;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. /// <summary>
  9. /// An <see cref="SvgFragment"/> represents an SVG fragment that can be the root element or an embedded fragment of an SVG document.
  10. /// </summary>
  11. [SvgElement("svg")]
  12. public class SvgFragment : SvgElement, ISvgViewPort, ISvgBoundable
  13. {
  14. /// <summary>
  15. /// Gets the SVG namespace string.
  16. /// </summary>
  17. public static readonly Uri Namespace = new Uri("http://www.w3.org/2000/svg");
  18. PointF ISvgBoundable.Location
  19. {
  20. get
  21. {
  22. return PointF.Empty;
  23. }
  24. }
  25. SizeF ISvgBoundable.Size
  26. {
  27. get
  28. {
  29. return GetDimensions();
  30. }
  31. }
  32. RectangleF ISvgBoundable.Bounds
  33. {
  34. get
  35. {
  36. return new RectangleF(((ISvgBoundable)this).Location, ((ISvgBoundable)this).Size);
  37. }
  38. }
  39. private SvgUnit _x;
  40. private SvgUnit _y;
  41. /// <summary>
  42. /// Gets or sets the position where the left point of the svg should start.
  43. /// </summary>
  44. [SvgAttribute("x")]
  45. public SvgUnit X
  46. {
  47. get { return _x; }
  48. set
  49. {
  50. if (_x != value)
  51. {
  52. _x = value;
  53. OnAttributeChanged(new AttributeEventArgs { Attribute = "x", Value = value });
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Gets or sets the position where the top point of the svg should start.
  59. /// </summary>
  60. [SvgAttribute("y")]
  61. public SvgUnit Y
  62. {
  63. get { return _y; }
  64. set
  65. {
  66. if (_y != value)
  67. {
  68. _y = value;
  69. OnAttributeChanged(new AttributeEventArgs { Attribute = "y", Value = value });
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Gets or sets the width of the fragment.
  75. /// </summary>
  76. /// <value>The width.</value>
  77. [SvgAttribute("width")]
  78. public SvgUnit Width
  79. {
  80. get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
  81. set { this.Attributes["width"] = value; }
  82. }
  83. /// <summary>
  84. /// Gets or sets the height of the fragment.
  85. /// </summary>
  86. /// <value>The height.</value>
  87. [SvgAttribute("height")]
  88. public SvgUnit Height
  89. {
  90. get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
  91. set { this.Attributes["height"] = value; }
  92. }
  93. [SvgAttribute("overflow")]
  94. public virtual SvgOverflow Overflow
  95. {
  96. get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
  97. set { this.Attributes["overflow"] = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the viewport of the element.
  101. /// </summary>
  102. /// <value></value>
  103. [SvgAttribute("viewBox")]
  104. public SvgViewBox ViewBox
  105. {
  106. get { return this.Attributes.GetAttribute<SvgViewBox>("viewBox"); }
  107. set { this.Attributes["viewBox"] = value; }
  108. }
  109. /// <summary>
  110. /// Gets or sets the aspect of the viewport.
  111. /// </summary>
  112. /// <value></value>
  113. [SvgAttribute("preserveAspectRatio")]
  114. public SvgAspectRatio AspectRatio
  115. {
  116. get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
  117. set { this.Attributes["preserveAspectRatio"] = value; }
  118. }
  119. /// <summary>
  120. /// Refers to the size of the font from baseline to baseline when multiple lines of text are set solid in a multiline layout environment.
  121. /// </summary>
  122. [SvgAttribute("font-size")]
  123. public override SvgUnit FontSize
  124. {
  125. get { return (this.Attributes["font-size"] == null) ? SvgUnit.Empty : (SvgUnit)this.Attributes["font-size"]; }
  126. set { this.Attributes["font-size"] = value; }
  127. }
  128. /// <summary>
  129. /// Indicates which font family is to be used to render the text.
  130. /// </summary>
  131. [SvgAttribute("font-family")]
  132. public override string FontFamily
  133. {
  134. get { return this.Attributes["font-family"] as string; }
  135. set { this.Attributes["font-family"] = value; }
  136. }
  137. /// <summary>
  138. /// Applies the required transforms to <see cref="ISvgRenderer"/>.
  139. /// </summary>
  140. /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
  141. protected internal override bool PushTransforms(ISvgRenderer renderer)
  142. {
  143. if (!base.PushTransforms(renderer)) return false;
  144. this.ViewBox.AddViewBoxTransform(this.AspectRatio, renderer, this);
  145. return true;
  146. }
  147. protected override void Render(ISvgRenderer renderer)
  148. {
  149. switch (this.Overflow)
  150. {
  151. case SvgOverflow.Auto:
  152. case SvgOverflow.Visible:
  153. case SvgOverflow.Inherit:
  154. base.Render(renderer);
  155. break;
  156. default:
  157. var prevClip = renderer.GetClip();
  158. try
  159. {
  160. var size = (this.Parent == null ? renderer.GetBoundable().Bounds.Size : GetDimensions());
  161. var clip = new RectangleF(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  162. this.Y.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
  163. size.Width, size.Height);
  164. renderer.SetClip(new Region(clip), CombineMode.Intersect);
  165. base.Render(renderer);
  166. }
  167. finally
  168. {
  169. renderer.SetClip(prevClip, CombineMode.Replace);
  170. }
  171. break;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the <see cref="GraphicsPath"/> for this element.
  176. /// </summary>
  177. /// <value></value>
  178. public GraphicsPath Path
  179. {
  180. get
  181. {
  182. var path = new GraphicsPath();
  183. AddPaths(this, path);
  184. return path;
  185. }
  186. }
  187. /// <summary>
  188. /// Gets the bounds of the svg element.
  189. /// </summary>
  190. /// <value>The bounds.</value>
  191. public RectangleF Bounds
  192. {
  193. get
  194. {
  195. var bounds = new RectangleF();
  196. foreach (var child in this.Children)
  197. {
  198. RectangleF childBounds = new RectangleF();
  199. if (child is SvgFragment)
  200. {
  201. childBounds = ((SvgFragment)child).Bounds;
  202. childBounds.Offset(((SvgFragment)child).X, ((SvgFragment)child).Y);
  203. }
  204. else if (child is SvgVisualElement)
  205. {
  206. childBounds = ((SvgVisualElement)child).Bounds;
  207. }
  208. if (!childBounds.IsEmpty)
  209. {
  210. if (bounds.IsEmpty)
  211. {
  212. bounds = childBounds;
  213. }
  214. else
  215. {
  216. bounds = RectangleF.Union(bounds, childBounds);
  217. }
  218. }
  219. }
  220. return bounds;
  221. }
  222. }
  223. /// <summary>
  224. /// Initializes a new instance of the <see cref="SvgFragment"/> class.
  225. /// </summary>
  226. public SvgFragment()
  227. {
  228. _x = 0.0f;
  229. _y = 0.0f;
  230. this.Height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
  231. this.Width = new SvgUnit(SvgUnitType.Percentage, 100.0f);
  232. this.ViewBox = SvgViewBox.Empty;
  233. this.AspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
  234. }
  235. public SizeF GetDimensions()
  236. {
  237. float w, h;
  238. var isWidthperc = Width.Type == SvgUnitType.Percentage;
  239. var isHeightperc = Height.Type == SvgUnitType.Percentage;
  240. RectangleF bounds = new RectangleF();
  241. if (isWidthperc || isHeightperc)
  242. {
  243. if (ViewBox.Width > 0 && ViewBox.Height > 0)
  244. {
  245. bounds = new RectangleF(0, 0, ViewBox.Width, ViewBox.Height);
  246. }
  247. else
  248. {
  249. bounds = this.Bounds; //do just one call to the recursive bounds property
  250. }
  251. }
  252. if (isWidthperc)
  253. {
  254. w = (bounds.Width + bounds.X) * (Width.Value * 0.01f);
  255. }
  256. else
  257. {
  258. w = Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this);
  259. }
  260. if (isHeightperc)
  261. {
  262. h = (bounds.Height + bounds.Y) * (Height.Value * 0.01f);
  263. }
  264. else
  265. {
  266. h = Height.ToDeviceValue(null, UnitRenderingType.Vertical, this);
  267. }
  268. return new SizeF(w, h);
  269. }
  270. public override SvgElement DeepCopy()
  271. {
  272. return DeepCopy<SvgFragment>();
  273. }
  274. public override SvgElement DeepCopy<T>()
  275. {
  276. var newObj = base.DeepCopy<T>() as SvgFragment;
  277. newObj.Height = this.Height;
  278. newObj.Width = this.Width;
  279. newObj.Overflow = this.Overflow;
  280. newObj.ViewBox = this.ViewBox;
  281. newObj.AspectRatio = this.AspectRatio;
  282. return newObj;
  283. }
  284. //Override the default behavior, writing out the namespaces.
  285. protected override void WriteStartElement(XmlTextWriter writer)
  286. {
  287. base.WriteStartElement(writer);
  288. foreach (var ns in SvgAttributeAttribute.Namespaces)
  289. {
  290. if (string.IsNullOrEmpty(ns.Key))
  291. writer.WriteAttributeString("xmlns", ns.Value);
  292. else
  293. writer.WriteAttributeString("xmlns:" + ns.Key, ns.Value);
  294. }
  295. writer.WriteAttributeString("version", "1.1");
  296. }
  297. }
  298. }
  299. #pragma warning restore