forked from cogneco/Kean
/
Canvas.cs
170 lines (166 loc) · 4.22 KB
/
Canvas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Canvas.cs
//
// Author:
// Simon Mika <smika@hx.se>
//
// Copyright (c) 2011 Simon Mika
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using Kean;
using Kean.Extension;
using Geometry2D = Kean.Math.Geometry2D;
namespace Kean.Draw
{
public abstract class Canvas :
Surface
{
Surface surface;
public Image Image { get; private set; }
public override Geometry2D.Integer.Size Size { get { return this.Image.Size; } }
public override CoordinateSystem CoordinateSystem { get { return this.Image.CoordinateSystem; } }
protected Canvas(Surface surface, Image image) :
base(image.Size, image.CoordinateSystem)
{
this.surface = surface;
this.Image = image;
}
#region Create
public abstract Canvas CreateSubcanvas(Geometry2D.Single.Box bounds);
#endregion
#region Render
public void Render(Action<Surface> render)
{
this.Use();
render.Call(this.surface);
this.Unuse();
}
public void Render(params Action<Surface>[] render)
{
this.Use();
foreach (Action<Surface> r in render)
r.Call(this.surface);
this.Unuse();
}
#endregion
#region Surface Implementation
#region Use, Unuse
protected internal override void Use()
{
this.surface.Use();
}
protected internal override void Unuse()
{
this.surface.Unuse();
}
#endregion
#region Clip, Transform
protected override Geometry2D.Single.Box OnClipChange(Geometry2D.Single.Box clip)
{
return this.surface.Clip = clip;
}
protected override Geometry2D.Single.Transform OnTransformChange(Geometry2D.Single.Transform transform)
{
return this.surface.Transform = transform;
}
#endregion
#region Convert
public override Image Convert(Image image)
{
return this.surface.Convert(image);
}
#endregion
#region Draw Image
public override void Draw(Image image)
{
this.Render(s => s.Draw(image));
}
public override void Draw(Map map, Image image)
{
this.Render(s => s.Draw(map, image));
}
public override void Draw(Map map, Image image, Geometry2D.Single.Point position)
{
this.Render(s => s.Draw(map, image, position));
}
public override void Draw(Map map, Image image, Geometry2D.Single.Box source, Geometry2D.Single.Box destination)
{
this.Render(s => s.Draw(map, image, source, destination));
}
#endregion
#region Draw Box
public override void Draw(IColor color)
{
this.Render(s => s.Draw(color));
}
public override void Draw(IColor color, Geometry2D.Single.Box region)
{
this.Render(s => s.Draw(color, region));
}
#endregion
#region Draw Path
public override void Draw(IPaint fill, Stroke stroke, Path path)
{
this.Render(s => s.Draw(fill, stroke, path));
}
#endregion
#region Draw Text
public override void Draw(IPaint fill, Stroke stroke, Text text, Geometry2D.Single.Point position)
{
this.Render(s => s.Draw(fill, stroke, text, position));
}
#endregion
#region Blend
public override void Blend(float factor)
{
this.Render(s => s.Blend(factor));
}
#endregion
#region Clear
public override void Clear()
{
this.Render(s => s.Clear());
}
public override void Clear(Geometry2D.Single.Box region)
{
this.Render(s => s.Clear(region));
}
#endregion
#region Flush, Finish
public override void Flush()
{
this.surface.Flush();
}
public override bool Finish()
{
return this.surface.Finish();
}
#endregion
public override T Read<T>()
{
return this.Image.Convert<T>();
}
#endregion
public override void Dispose()
{
if (this.surface.NotNull())
{
this.surface.Dispose();
this.surface = null;
}
base.Dispose();
}
}
}