SvgRenderer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 TranslateTransform(float dx, float dy, MatrixOrder order = MatrixOrder.Append)
  74. {
  75. this._innerGraphics.TranslateTransform(dx, dy, order);
  76. }
  77. public SmoothingMode SmoothingMode
  78. {
  79. get { return this._innerGraphics.SmoothingMode; }
  80. set { this._innerGraphics.SmoothingMode = value; }
  81. }
  82. public Matrix Transform
  83. {
  84. get { return this._innerGraphics.Transform; }
  85. set { this._innerGraphics.Transform = value; }
  86. }
  87. public void Dispose()
  88. {
  89. this._innerGraphics.Dispose();
  90. }
  91. Graphics IGraphicsProvider.GetGraphics()
  92. {
  93. return _innerGraphics;
  94. }
  95. /// <summary>
  96. /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Image"/>.
  97. /// </summary>
  98. /// <param name="image"><see cref="Image"/> from which to create the new <see cref="ISvgRenderer"/>.</param>
  99. public static ISvgRenderer FromImage(Image image)
  100. {
  101. var g = Graphics.FromImage(image);
  102. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  103. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
  104. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  105. g.TextContrast = 1;
  106. return new SvgRenderer(g);
  107. }
  108. /// <summary>
  109. /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Graphics"/>.
  110. /// </summary>
  111. /// <param name="graphics">The <see cref="Graphics"/> to create the renderer from.</param>
  112. public static ISvgRenderer FromGraphics(Graphics graphics)
  113. {
  114. return new SvgRenderer(graphics);
  115. }
  116. public static ISvgRenderer FromNull()
  117. {
  118. var img = new Bitmap(1, 1);
  119. return SvgRenderer.FromImage(img);
  120. }
  121. }
  122. }
  123. #pragma warning restore