* Brush.cs: added texture brush with transform tests
[mono.git] / mcs / class / System.Drawing / System.Drawing.Drawing2D / GraphicsState.jvm.cs
1 //
2 // System.Drawing.Drawing2D.ExtendedGraphicsState.jvm.cs
3 //
4 // Author:
5 //   Vladimir Krasnov (vladimirk@mainsoft.com)
6 //
7 // Copyright (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Drawing.Text;
32 using geom = java.awt.geom;
33 using awt = java.awt;
34
35 namespace System.Drawing.Drawing2D \r
36 {
37         /// <summary>
38         /// Summary description for GraphicsState.
39         /// </summary>
40         public sealed class GraphicsState : MarshalByRefObject
41         {
42                 readonly CompositingMode _compositingMode;
43                 readonly CompositingQuality _compositingQuality;
44                 readonly Region _clip;
45                 readonly awt.Shape _baseClip;
46                 readonly InterpolationMode _interpolationMode;
47                 readonly float _pageScale;
48                 readonly GraphicsUnit _pageUnit;
49                 readonly PixelOffsetMode _pixelOffsetMode;
50                 readonly Point _renderingOrigin;
51                 readonly SmoothingMode _smoothingMode;
52                 readonly int _textContrast;
53                 readonly TextRenderingHint _textRenderingHint;
54
55                 // additional transform in case that new container has one
56                 readonly Matrix _transform;
57                 readonly Matrix _baseTransform;
58
59                 GraphicsState _next = null;
60
61                 internal GraphicsState(Graphics graphics, bool resetState) 
62                         : this(graphics, Matrix.IdentityTransform, resetState) {}
63
64                 internal GraphicsState Next {
65                         get {
66                                 return _next;
67                         }
68                         set {
69                                 _next = value;
70                         }
71                 }
72
73                 internal GraphicsState(Graphics graphics, Matrix matrix, bool resetState)
74                 {
75                         _compositingMode = graphics.CompositingMode;
76                         _compositingQuality = graphics.CompositingQuality;
77                         _clip = graphics.ScaledClip;
78                         _baseClip = graphics.NativeObject.getClip();
79                         _interpolationMode = graphics.InterpolationMode;
80                         _pageScale = graphics.PageScale;
81                         _pageUnit = graphics.PageUnit;
82                         _pixelOffsetMode = graphics.PixelOffsetMode;
83                         
84                         // FIXME: render orign is not implemented yet
85                         //_renderingOrigin = new Point( g.RenderingOrigin.X, g.RenderingOrigin.Y );
86
87                         _smoothingMode = graphics.SmoothingMode;
88                         _transform = graphics.Transform;
89                         _baseTransform = graphics.BaseTransform;
90
91                         _textContrast = graphics.TextContrast;
92                         _textRenderingHint = graphics.TextRenderingHint;
93
94                         if (resetState)
95                                 ResetState(graphics, matrix);
96                 }
97
98                 internal void RestoreState(Graphics graphics)
99                 {
100                         graphics.CompositingMode = _compositingMode;
101                         graphics.CompositingQuality = _compositingQuality;
102                         graphics.ScaledClip = _clip;
103                         graphics.InterpolationMode = _interpolationMode;
104                         graphics.PageScale = _pageScale;
105                         graphics.PageUnit = _pageUnit;
106                         graphics.PixelOffsetMode = _pixelOffsetMode;
107         
108                         // FIXME: render orign is not implemented yet
109                         //graphics.RenderingOrigin = new Point( _renderingOrigin.X, _renderingOrigin.Y );
110
111                         graphics.SmoothingMode = _smoothingMode;
112                         graphics.Transform = _transform;
113                         graphics.BaseTransform = _baseTransform;
114                         graphics.TextContrast = _textContrast;
115                         graphics.TextRenderingHint = _textRenderingHint;
116
117                         // must be set after the base transform is restored
118                         graphics.NativeObject.setClip(_baseClip);
119                 }
120
121                 void ResetState(Graphics graphics, Matrix matrix)
122                 {
123                         //should be set before the base transform is changed
124                         if (_baseClip == null)
125                                 graphics.IntersectScaledClipWithBase(graphics.VisibleShape);
126
127                         graphics.CompositingMode = CompositingMode.SourceOver;
128                         graphics.CompositingQuality = CompositingQuality.Default;
129                         graphics.ScaledClip = Region.InfiniteRegion;
130                         graphics.InterpolationMode = InterpolationMode.Bilinear;
131                         graphics.PageScale = 1.0f;
132                         graphics.PageUnit = GraphicsUnit.Display;
133                         graphics.PixelOffsetMode = PixelOffsetMode.Default;
134                         
135                         // FIXME: render orign is not implemented yet
136                         //graphics.RenderingOrigin = new Point(0, 0);
137
138                         graphics.SmoothingMode = SmoothingMode.None;
139                         graphics.ResetTransform();
140                         graphics.PrependBaseTransform(Graphics.GetFinalTransform(_transform.NativeObject, _pageUnit, _pageScale));
141                         graphics.PrependBaseTransform(matrix.NativeObject);
142                         graphics.TextContrast = 4;
143                         graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
144                 }
145
146                 internal void RestoreBaseClip(Graphics graphics) {
147                         graphics.NativeObject.setClip(_baseClip);
148                 }
149         }
150 }