-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathCairoExtensions.Geometry.cs
More file actions
311 lines (253 loc) · 8.29 KB
/
Copy pathCairoExtensions.Geometry.cs
File metadata and controls
311 lines (253 loc) · 8.29 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Author:
// Jonathan Pobst <monkey@jpobst.com>
//
// Copyright (c) 2010 Jonathan Pobst
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Some functions are from Paint.NET:
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See license-pdn.txt for full licensing and attribution details. //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Immutable;
using Cairo;
namespace Pinta.Core;
// These are not intended to mutate the image surface
partial class CairoExtensions
{
/// <returns>Bounding rectangle of changed area</returns>
public static RectangleD StrokeExtents (this Context g)
{
g.StrokeExtents (
out double x1,
out double y1,
out double x2,
out double y2);
return new (
x1,
y1,
x2 - x1,
y2 - y1);
}
public static Region CreateRegion (in RectangleI rect)
{
CairoRectangleInt cairo_rect = new () {
X = rect.X,
Y = rect.Y,
Width = rect.Width,
Height = rect.Height,
};
return new (RegionCreateRectangle (ref cairo_rect));
}
public static bool ContainsPoint (this Region region, int x, int y)
=> RegionContainsPoint (region.Handle, x, y);
public static RectangleD PathExtents (this Context context)
{
context.PathExtents (
out double x1,
out double y1,
out double x2,
out double y2);
return new (
x1,
y1,
x2 - x1,
y2 - y1);
}
public static RectangleI GetBounds (this ImageSurface surf)
=> new (0, 0, surf.Width, surf.Height);
public static Size GetSize (this ImageSurface surf)
=> new (surf.Width, surf.Height);
public static void TranslatePointsInPlace (this Span<PointI> points, PointI delta)
{
for (int i = 0; i < points.Length; ++i)
points[i] += delta;
}
private struct Edge
{
public int miny { get; } // int
public int maxy { get; } // int
public int x { get; set; } // fixed point: 24.8
public int dxdy { get; } // fixed point: 24.8
public Edge (int miny, int maxy, int x, int dxdy)
{
this.miny = miny;
this.maxy = maxy;
this.x = x;
this.dxdy = dxdy;
}
}
public static ImmutableArray<Scanline> GetScans (this ReadOnlySpan<PointI> points)
{
int ymax = 0;
// Build edge table
Edge[] edgeTable = new Edge[points.Length];
int edgeCount = 0;
for (int i = 0; i < points.Length; ++i) {
PointI top = points[i];
PointI bottom = points[(i + 1) % points.Length];
if (top.Y > bottom.Y)
(bottom, top) = (top, bottom);
int dy = bottom.Y - top.Y;
if (dy != 0) {
edgeTable[edgeCount] = new Edge (top.Y, bottom.Y, top.X << 8, ((bottom.X - top.X) << 8) / dy);
ymax = Math.Max (ymax, bottom.Y);
++edgeCount;
}
}
// Sort edge table by miny
for (int i = 0; i < edgeCount - 1; ++i) {
int min = i;
for (int j = i + 1; j < edgeCount; ++j)
if (edgeTable[j].miny < edgeTable[min].miny)
min = j;
if (min != i)
(edgeTable[i], edgeTable[min]) = (edgeTable[min], edgeTable[i]);
}
// Compute how many scanlines we will be emitting
int scanCount = 0;
int activeLow = 0;
int activeHigh = 0;
int yscan1 = edgeTable[0].miny;
// we assume that edgeTable[0].miny == yscan
while (activeHigh < edgeCount - 1 && edgeTable[activeHigh + 1].miny == yscan1)
++activeHigh;
while (yscan1 <= ymax) {
// Find new edges where yscan == miny
while (activeHigh < edgeCount - 1 && edgeTable[activeHigh + 1].miny == yscan1)
++activeHigh;
int count = 0;
for (int i = activeLow; i <= activeHigh; ++i)
if (edgeTable[i].maxy > yscan1)
++count;
scanCount += count / 2;
++yscan1;
// Remove edges where yscan == maxy
while (activeLow < edgeCount - 1 && edgeTable[activeLow].maxy <= yscan1)
++activeLow;
if (activeLow > activeHigh)
activeHigh = activeLow;
}
// Allocate scanlines that we'll return
var scans = ImmutableArray.CreateBuilder<Scanline> (scanCount);
scans.Count = scanCount;
// Active Edge Table (AET): it is indices into the Edge Table (ET)
int[] active = new int[edgeCount];
int activeCount = 0;
int yscan2 = edgeTable[0].miny;
int scansIndex = 0;
// Repeat until both the ET and AET are empty
while (yscan2 <= ymax) {
// Move any edges from the ET to the AET where yscan == miny
for (int i = 0; i < edgeCount; ++i) {
if (edgeTable[i].miny != yscan2)
continue;
active[activeCount] = i;
++activeCount;
}
// Sort the AET on x
for (int i = 0; i < activeCount - 1; ++i) {
int min = i;
for (int j = i + 1; j < activeCount; ++j)
if (edgeTable[active[j]].x < edgeTable[active[min]].x)
min = j;
if (min != i)
(active[i], active[min]) = (active[min], active[i]);
}
// For each pair of entries in the AET, fill in pixels between their info
for (int i = 0; i < activeCount; i += 2) {
Edge el = edgeTable[active[i]];
Edge er = edgeTable[active[i + 1]];
int startx = (el.x + 0xff) >> 8; // ceil(x)
int endx = er.x >> 8; // floor(x)
scans[scansIndex] = new Scanline (startx, yscan2, endx - startx);
++scansIndex;
}
++yscan2;
// Remove from the AET any edge where yscan == maxy
int k = 0;
while (k < activeCount && activeCount > 0) {
if (edgeTable[active[k]].maxy == yscan2) {
// remove by shifting everything down one
for (int j = k + 1; j < activeCount; ++j)
active[j - 1] = active[j];
--activeCount;
} else {
++k;
}
}
// Update x for each entry in AET
for (int i = 0; i < activeCount; ++i)
edgeTable[active[i]].x += edgeTable[active[i]].dxdy;
}
return scans.MoveToImmutable ();
}
public static Matrix CreateIdentityMatrix ()
{
Matrix matrix = new ();
matrix.InitIdentity ();
return matrix;
}
public static Matrix CreateMatrix (
double xx,
double xy,
double yx,
double yy,
double x0,
double y0)
{
Matrix matrix = new ();
matrix.Init (xx, xy, yx, yy, x0, y0);
return matrix;
}
// TODO-GTK4 (bindings) - requires improvements to struct generation (https://github.com/gircore/gir.core/issues/622)
// This needs to have a proper copy operator in gir.core, or access to the 6 float fields.
// Should also audit all usages of Cairo.Matrix which changed from a struct to a class with gir.core
public static void InitMatrix (
this Matrix m,
Matrix other)
{
m.InitIdentity ();
m.Multiply (other);
}
// TODO-GTK4 (bindings) - requires improvements to struct generation (https://github.com/gircore/gir.core/issues/622)
// This needs to have a proper copy operator in gir.core, or access to the 6 float fields.
public static Matrix Clone (this Matrix m)
{
Matrix result = CreateIdentityMatrix ();
result.Multiply (m);
return result;
}
public static PointD TransformPoint (
this Matrix m,
in PointD p)
{
double newX = p.X;
double newY = p.Y;
m.TransformPoint (ref newX, ref newY);
return new (newX, newY);
}
private static void GetRectangle (this Region region, int i, out CairoRectangleInt rect)
=> RegionGetRectangle (region.Handle, i, out rect);
private static int GetNumRectangles (this Region region)
=> RegionNumRectangles (region.Handle);
}