SvgDeferredPaintServer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. #pragma warning disable
  7. namespace Svg
  8. {
  9. /// <summary>
  10. /// A wrapper for a paint server which isn't defined currently in the parse process, but
  11. /// should be defined by the time the image needs to render.
  12. /// </summary>
  13. public class SvgDeferredPaintServer : SvgPaintServer
  14. {
  15. private bool _serverLoaded = false;
  16. private SvgPaintServer _concreteServer;
  17. public SvgDocument Document { get; set; }
  18. public string DeferredId { get; set; }
  19. public SvgDeferredPaintServer() { }
  20. public SvgDeferredPaintServer(SvgDocument document, string id)
  21. {
  22. this.Document = document;
  23. this.DeferredId = id;
  24. }
  25. public void EnsureServer(SvgElement styleOwner)
  26. {
  27. if (!_serverLoaded)
  28. {
  29. if (this.DeferredId == "currentColor" && styleOwner != null)
  30. {
  31. var colorElement = (from e in styleOwner.ParentsAndSelf.OfType<SvgElement>()
  32. where e.Color != SvgPaintServer.None && e.Color != SvgColourServer.NotSet &&
  33. e.Color != SvgColourServer.Inherit && e.Color != SvgColourServer.None
  34. select e).FirstOrDefault();
  35. _concreteServer = (colorElement == null ? SvgPaintServer.None : colorElement.Color);
  36. }
  37. else
  38. {
  39. _concreteServer = this.Document.IdManager.GetElementById(this.DeferredId) as SvgPaintServer;
  40. }
  41. _serverLoaded = true;
  42. }
  43. }
  44. public override Brush GetBrush(SvgVisualElement styleOwner, ISvgRenderer renderer, float opacity, bool forStroke = false)
  45. {
  46. EnsureServer(styleOwner);
  47. return _concreteServer.GetBrush(styleOwner, renderer, opacity, forStroke);
  48. }
  49. public override SvgElement DeepCopy()
  50. {
  51. return DeepCopy<SvgDeferredPaintServer>();
  52. }
  53. public override SvgElement DeepCopy<T>()
  54. {
  55. var newObj = base.DeepCopy<T>() as SvgDeferredPaintServer;
  56. newObj.Document = this.Document;
  57. newObj.DeferredId = this.DeferredId;
  58. return newObj;
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. var other = obj as SvgDeferredPaintServer;
  63. if (other == null)
  64. return false;
  65. return this.Document == other.Document && this.DeferredId == other.DeferredId;
  66. }
  67. public override int GetHashCode()
  68. {
  69. if (this.Document == null || this.DeferredId == null) return 0;
  70. return this.Document.GetHashCode() ^ this.DeferredId.GetHashCode();
  71. }
  72. public override string ToString()
  73. {
  74. if (this.DeferredId == "currentColor")
  75. {
  76. return this.DeferredId;
  77. }
  78. else
  79. {
  80. return string.Format("url({0})", this.DeferredId);
  81. }
  82. }
  83. public static T TryGet<T>(SvgPaintServer server, SvgElement parent) where T : SvgPaintServer
  84. {
  85. var deferred = server as SvgDeferredPaintServer;
  86. if (deferred == null)
  87. {
  88. return server as T;
  89. }
  90. else
  91. {
  92. deferred.EnsureServer(parent);
  93. return deferred._concreteServer as T;
  94. }
  95. }
  96. }
  97. }
  98. #pragma warning restore