SvgRenderer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Text;
  8. #pragma warning disable
  9. namespace Svg
  10. {
  11. /// <summary>
  12. /// Convenience wrapper around a graphics object
  13. /// </summary>
  14. public sealed class SvgRenderer : IDisposable, IGraphicsProvider, ISvgRenderer
  15. {
  16. private Graphics _innerGraphics;
  17. private Stack<ISvgBoundable> _boundables = new Stack<ISvgBoundable>();
  18. public void SetBoundable(ISvgBoundable boundable)
  19. {
  20. _boundables.Push(boundable);
  21. }
  22. public ISvgBoundable GetBoundable()
  23. {
  24. return _boundables.Peek();
  25. }
  26. public ISvgBoundable PopBoundable()
  27. {
  28. return _boundables.Pop();
  29. }
  30. public float DpiY
  31. {
  32. get { return _innerGraphics.DpiY; }
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ISvgRenderer"/> class.
  36. /// </summary>
  37. private SvgRenderer(Graphics graphics)
  38. {
  39. this._innerGraphics = graphics;
  40. }
  41. public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit graphicsUnit)
  42. {
  43. _innerGraphics.DrawImage(image, destRect, srcRect, graphicsUnit);
  44. }
  45. public void DrawImageUnscaled(Image image, Point location)
  46. {
  47. this._innerGraphics.DrawImageUnscaled(image, location);
  48. }
  49. public void DrawPath(Pen pen, GraphicsPath path)
  50. {
  51. this._innerGraphics.DrawPath(pen, path);
  52. }
  53. public void FillPath(Brush brush, GraphicsPath path)
  54. {
  55. this._innerGraphics.FillPath(brush, path);
  56. }
  57. public Region GetClip()
  58. {
  59. return this._innerGraphics.Clip;
  60. }
  61. public void RotateTransform(float fAngle, MatrixOrder order = MatrixOrder.Append)
  62. {
  63. this._innerGraphics.RotateTransform(fAngle, order);
  64. }
  65. public void ScaleTransform(float sx, float sy, MatrixOrder order = MatrixOrder.Append)
  66. {
  67. this._innerGraphics.ScaleTransform(sx, sy, order);
  68. }
  69. public void SetClip(Region region, CombineMode combineMode = CombineMode.Replace)
  70. {
  71. this._innerGraphics.SetClip(region, combineMode);
  72. }
  73. public void SetClip(GraphicsPath path, CombineMode combineMode = CombineMode.Replace)
  74. {
  75. this._innerGraphics.SetClip(path, combineMode);
  76. }
  77. public void EndClip()
  78. {
  79. }
  80. public void TranslateTransform(float dx, float dy, MatrixOrder order = MatrixOrder.Append)
  81. {
  82. this._innerGraphics.TranslateTransform(dx, dy, order);
  83. }
  84. public SmoothingMode SmoothingMode
  85. {
  86. get { return this._innerGraphics.SmoothingMode; }
  87. set { this._innerGraphics.SmoothingMode = value; }
  88. }
  89. public Matrix Transform
  90. {
  91. get { return this._innerGraphics.Transform; }
  92. set { this._innerGraphics.Transform = value; }
  93. }
  94. public void Dispose()
  95. {
  96. this._innerGraphics.Dispose();
  97. }
  98. Graphics IGraphicsProvider.GetGraphics()
  99. {
  100. return _innerGraphics;
  101. }
  102. /// <summary>
  103. /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Image"/>.
  104. /// </summary>
  105. /// <param name="image"><see cref="Image"/> from which to create the new <see cref="ISvgRenderer"/>.</param>
  106. public static ISvgRenderer FromImage(Image image)
  107. {
  108. var g = Graphics.FromImage(image);
  109. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  110. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
  111. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  112. g.TextContrast = 1;
  113. return new SvgRenderer(g);
  114. }
  115. /// <summary>
  116. /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Graphics"/>.
  117. /// </summary>
  118. /// <param name="graphics">The <see cref="Graphics"/> to create the renderer from.</param>
  119. public static ISvgRenderer FromGraphics(Graphics graphics)
  120. {
  121. return new SvgRenderer(graphics);
  122. }
  123. public static ISvgRenderer FromNull()
  124. {
  125. var img = new Bitmap(1, 1);
  126. return SvgRenderer.FromImage(img);
  127. }
  128. }
  129. }
  130. #pragma warning restore