1 | using System;
|
---|
2 | using System.Xml;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Drawing.Imaging;
|
---|
5 | using System.Drawing.Drawing2D;
|
---|
6 |
|
---|
7 | using SharpVectors.Dom.Svg;
|
---|
8 |
|
---|
9 | namespace SharpVectors.Renderers.Gdi
|
---|
10 | {
|
---|
11 | /// <summary>
|
---|
12 | /// Wraps a Graphics object since it's sealed
|
---|
13 | /// </summary>
|
---|
14 | public sealed class GdiGraphicsWrapper : IDisposable
|
---|
15 | {
|
---|
16 | #region Private Fields
|
---|
17 |
|
---|
18 | private bool _isStatic;
|
---|
19 | private Graphics _graphics;
|
---|
20 | private Graphics _idMapGraphics;
|
---|
21 | private Bitmap _idMapImage;
|
---|
22 |
|
---|
23 | #endregion
|
---|
24 |
|
---|
25 | #region Constructors
|
---|
26 |
|
---|
27 | private GdiGraphicsWrapper(Image image, bool isStatic)
|
---|
28 | {
|
---|
29 | this._isStatic = isStatic;
|
---|
30 | if (!IsStatic)
|
---|
31 | {
|
---|
32 | _idMapImage = new Bitmap(image.Width, image.Height);
|
---|
33 | _idMapGraphics = Graphics.FromImage(_idMapImage);
|
---|
34 | _idMapGraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
|
---|
35 | _idMapGraphics.SmoothingMode = SmoothingMode.None;
|
---|
36 | _idMapGraphics.CompositingQuality = CompositingQuality.Invalid;
|
---|
37 | }
|
---|
38 | _graphics = Graphics.FromImage(image);
|
---|
39 | }
|
---|
40 |
|
---|
41 | private GdiGraphicsWrapper(IntPtr hdc, bool isStatic)
|
---|
42 | {
|
---|
43 | this._isStatic = isStatic;
|
---|
44 | if (!IsStatic)
|
---|
45 | {
|
---|
46 | // This will get resized when the actual size is known
|
---|
47 | _idMapImage = new Bitmap(0, 0);
|
---|
48 | _idMapGraphics = Graphics.FromImage(_idMapImage);
|
---|
49 | _idMapGraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
|
---|
50 | _idMapGraphics.SmoothingMode = SmoothingMode.None;
|
---|
51 | _idMapGraphics.CompositingQuality = CompositingQuality.Invalid;
|
---|
52 | }
|
---|
53 | _graphics = Graphics.FromHdc(hdc);
|
---|
54 | }
|
---|
55 |
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | public static GdiGraphicsWrapper FromImage(Image image, bool isStatic)
|
---|
59 | {
|
---|
60 | return new GdiGraphicsWrapper(image, isStatic);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static GdiGraphicsWrapper FromHdc(IntPtr hdc, bool isStatic)
|
---|
64 | {
|
---|
65 | return new GdiGraphicsWrapper(hdc, isStatic);
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region Properties
|
---|
69 |
|
---|
70 | public bool IsStatic
|
---|
71 | {
|
---|
72 | get { return _isStatic; }
|
---|
73 | set
|
---|
74 | {
|
---|
75 | _isStatic = value;
|
---|
76 | _idMapGraphics.Dispose();
|
---|
77 | _idMapGraphics = null;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Graphics Graphics
|
---|
82 | {
|
---|
83 | get { return _graphics; }
|
---|
84 | set { _graphics = value; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public Graphics IdMapGraphics
|
---|
88 | {
|
---|
89 | get { return _graphics; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public Bitmap IdMapRaster
|
---|
93 | {
|
---|
94 | get { return _idMapImage; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | #endregion
|
---|
98 |
|
---|
99 | #region Graphics members
|
---|
100 |
|
---|
101 | public void Clear(Color color)
|
---|
102 | {
|
---|
103 | _graphics.Clear(color);
|
---|
104 | if (_idMapGraphics != null) _idMapGraphics.Clear(Color.Empty);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void Dispose()
|
---|
108 | {
|
---|
109 | _graphics.Dispose();
|
---|
110 | if (_idMapGraphics != null) _idMapGraphics.Dispose();
|
---|
111 | }
|
---|
112 |
|
---|
113 | public GdiGraphicsContainer BeginContainer()
|
---|
114 | {
|
---|
115 | GdiGraphicsContainer container = new GdiGraphicsContainer();
|
---|
116 | if (_idMapGraphics != null)
|
---|
117 | container.IdMap = _idMapGraphics.BeginContainer();
|
---|
118 |
|
---|
119 | container.Main = _graphics.BeginContainer();
|
---|
120 |
|
---|
121 | return container;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void EndContainer(GdiGraphicsContainer container)
|
---|
125 | {
|
---|
126 | if (_idMapGraphics != null)
|
---|
127 | _idMapGraphics.EndContainer(container.IdMap);
|
---|
128 |
|
---|
129 | _graphics.EndContainer(container.Main);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public SmoothingMode SmoothingMode
|
---|
133 | {
|
---|
134 | get { return _graphics.SmoothingMode; }
|
---|
135 | set { _graphics.SmoothingMode = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public Matrix Transform
|
---|
139 | {
|
---|
140 | get { return _graphics.Transform; }
|
---|
141 | set
|
---|
142 | {
|
---|
143 | if (_idMapGraphics != null) _idMapGraphics.Transform = value;
|
---|
144 | _graphics.Transform = value;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void SetClip(GraphicsPath path)
|
---|
149 | {
|
---|
150 | _graphics.SetClip(path);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void SetClip(RectangleF rect)
|
---|
154 | {
|
---|
155 | if (_idMapGraphics != null) _idMapGraphics.SetClip(rect);
|
---|
156 | _graphics.SetClip(rect);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void SetClip(Region region, CombineMode combineMode)
|
---|
160 | {
|
---|
161 | if (_idMapGraphics != null) _idMapGraphics.SetClip(region, combineMode);
|
---|
162 | _graphics.SetClip(region, combineMode);
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void TranslateClip(float x, float y)
|
---|
166 | {
|
---|
167 | if (_idMapGraphics != null) _idMapGraphics.TranslateClip(x, y);
|
---|
168 | _graphics.TranslateClip(x, y);
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void ResetClip()
|
---|
172 | {
|
---|
173 | if (_idMapGraphics != null) _idMapGraphics.ResetClip();
|
---|
174 | _graphics.ResetClip();
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void FillPath(GdiRendering grNode, Brush brush, GraphicsPath path)
|
---|
178 | {
|
---|
179 | if (_idMapGraphics != null)
|
---|
180 | {
|
---|
181 | Brush idBrush = new SolidBrush(grNode.UniqueColor);
|
---|
182 | if (grNode.Element is SvgTextContentElement)
|
---|
183 | {
|
---|
184 | _idMapGraphics.FillRectangle(idBrush, path.GetBounds());
|
---|
185 | }
|
---|
186 | else
|
---|
187 | {
|
---|
188 | _idMapGraphics.FillPath(idBrush, path);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | _graphics.FillPath(brush, path);
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void DrawPath(GdiRendering grNode, Pen pen, GraphicsPath path)
|
---|
195 | {
|
---|
196 | if (_idMapGraphics != null)
|
---|
197 | {
|
---|
198 | Pen idPen = new Pen(grNode.UniqueColor, pen.Width);
|
---|
199 | _idMapGraphics.DrawPath(idPen, path);
|
---|
200 | }
|
---|
201 | _graphics.DrawPath(pen, path);
|
---|
202 | }
|
---|
203 |
|
---|
204 | public void TranslateTransform(float dx, float dy)
|
---|
205 | {
|
---|
206 | if (_idMapGraphics != null) _idMapGraphics.TranslateTransform(dx, dy);
|
---|
207 | _graphics.TranslateTransform(dx, dy);
|
---|
208 | }
|
---|
209 |
|
---|
210 | public void ScaleTransform(float sx, float sy)
|
---|
211 | {
|
---|
212 | if (_idMapGraphics != null) _idMapGraphics.ScaleTransform(sx, sy);
|
---|
213 | _graphics.ScaleTransform(sx, sy);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void RotateTransform(float angle)
|
---|
217 | {
|
---|
218 | if (_idMapGraphics != null) _idMapGraphics.RotateTransform(angle);
|
---|
219 | _graphics.RotateTransform(angle);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void DrawImage(GdiRendering grNode, Image image, Rectangle destRect,
|
---|
223 | float srcX, float srcY, float srcWidth, float srcHeight,
|
---|
224 | GraphicsUnit graphicsUnit, ImageAttributes imageAttributes)
|
---|
225 | {
|
---|
226 | if (_idMapGraphics != null)
|
---|
227 | {
|
---|
228 | // This handles pointer-events for visibleFill visibleStroke and visible
|
---|
229 | /*Brush idBrush = new SolidBrush(grNode.UniqueColor);
|
---|
230 | GraphicsPath gp = new GraphicsPath();
|
---|
231 | gp.AddRectangle(destRect);
|
---|
232 | _idMapGraphics.FillPath(idBrush, gp);*/
|
---|
233 | Color unique = grNode.UniqueColor;
|
---|
234 | float r = (float)unique.R / 255;
|
---|
235 | float g = (float)unique.G / 255;
|
---|
236 | float b = (float)unique.B / 255;
|
---|
237 | ColorMatrix colorMatrix = new ColorMatrix(
|
---|
238 | new float[][] { new float[] {0f, 0f, 0f, 0f, 0f},
|
---|
239 | new float[] {0f, 0f, 0f, 0f, 0f},
|
---|
240 | new float[] {0f, 0f, 0f, 0f, 0f},
|
---|
241 | new float[] {0f, 0f, 0f, 1f, 0f},
|
---|
242 | new float[] {r, g, b, 0f, 1f} });
|
---|
243 | ImageAttributes ia = new ImageAttributes();
|
---|
244 | ia.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
|
---|
245 | _idMapGraphics.DrawImage(image, destRect, srcX, srcY, srcWidth, srcHeight, graphicsUnit, ia);
|
---|
246 | }
|
---|
247 | _graphics.DrawImage(image, destRect, srcX, srcY, srcWidth, srcHeight, graphicsUnit, imageAttributes);
|
---|
248 | }
|
---|
249 |
|
---|
250 | #endregion
|
---|
251 | }
|
---|
252 |
|
---|
253 | /// <summary>
|
---|
254 | /// Wraps a GraphicsContainer because it is sealed.
|
---|
255 | /// This is a helper for GraphicsWrapper so that it can save
|
---|
256 | /// multiple container states. It holds the containers
|
---|
257 | /// for both the idMapGraphics and the main graphics
|
---|
258 | /// being rendered in the GraphicsWrapper.
|
---|
259 | /// </summary>
|
---|
260 | public sealed class GdiGraphicsContainer
|
---|
261 | {
|
---|
262 | internal GraphicsContainer IdMap;
|
---|
263 | internal GraphicsContainer Main;
|
---|
264 |
|
---|
265 | public GdiGraphicsContainer()
|
---|
266 | {
|
---|
267 | }
|
---|
268 |
|
---|
269 | public GdiGraphicsContainer(GraphicsContainer idmap, GraphicsContainer main)
|
---|
270 | {
|
---|
271 | this.IdMap = idmap;
|
---|
272 | this.Main = main;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|