5
// Eric Maupin <ermau@xamarin.com>
6
// Lytico (http://limada.sourceforge.net)
8
// Copyright (c) 2012 Xamarin, Inc.
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
11
// of this software and associated documentation files (the "Software"), to deal
12
// in the Software without restriction, including without limitation the rights
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
// copies of the Software, and to permit persons to whom the Software is
15
// furnished to do so, subject to the following conditions:
17
// The above copyright notice and this permission notice shall be included in
18
// all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
30
using System.Drawing.Drawing2D;
32
namespace Xwt.WPFBackend
34
internal class DrawingContext:IDisposable
36
internal DrawingContext (Graphics graphics)
39
throw new ArgumentNullException ("graphics");
41
graphics.SmoothingMode = SmoothingMode.None;
42
graphics.PixelOffsetMode = PixelOffsetMode.Half;
43
graphics.CompositingQuality = CompositingQuality.HighSpeed;
45
// necessary for correct text rendering with System.Drawing
46
//graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
47
// necessary for none-pixelated text drawing in images, revert to above line if it introduces a performance problem
48
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
53
internal DrawingContext (DrawingContext context)
56
context.CopyTo (this, false);
58
CurrentX = context.CurrentX;
59
CurrentY = context.CurrentY;
62
internal readonly Graphics Graphics;
66
get { return font ?? (font = new Font (FontFamily.GenericSansSerif, 12));}
70
Color color = Color.Black;
75
get { return pen ?? (pen = new Pen (color, width));}
81
internal Brush Brush {
82
get { return brush ?? (brush = new SolidBrush (color));}
86
internal GraphicsState State;
87
internal float CurrentX;
88
internal float CurrentY;
89
GraphicsPath path = null;
91
internal GraphicsPath Path {
92
get { return path ?? (path = new GraphicsPath ());}
96
internal void SetColor (Color color)
98
if (this.color == color)
105
SolidBrush solidBrush = brush as SolidBrush;
106
if (solidBrush == null) {
110
solidBrush.Color = color;
116
internal void SetWidth (float width)
118
if (this.width != width) {
126
internal void CopyTo (DrawingContext dc, bool toCurrent)
129
dc.Graphics.Restore (this.State);
131
dc.State = this.Graphics.Save ();
133
dc.Brush = this.brush;
135
dc.SetWidth (this.width);
136
dc.SetColor (this.color);
137
dc.CurrentX = this.CurrentX;
138
dc.CurrentY = this.CurrentY;
139
if (this.path != null && this.path.PointCount > 0)
140
dc.Path = (GraphicsPath) this.path.Clone ();
143
internal void Save ()
145
if (this.contexts == null)
146
this.contexts = new Stack<DrawingContext> ();
148
this.contexts.Push (new DrawingContext (this));
151
internal void Restore ()
153
if (this.contexts == null || this.contexts.Count == 0)
154
throw new InvalidOperationException ();
156
var dc = this.contexts.Pop ();
158
dc.CopyTo (this, true);
163
private Stack<DrawingContext> contexts;
165
public void Dispose (bool stacked)
179
if (contexts != null)
180
while (contexts.Count!=0) {
181
var c = contexts.Pop ();
190
public void Dispose ()
b'\\ No newline at end of file'