-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathGraphicsPath.cs
More file actions
183 lines (158 loc) · 5.62 KB
/
GraphicsPath.cs
File metadata and controls
183 lines (158 loc) · 5.62 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
//
// Swiss QR Bill Generator for .NET
// Copyright (c) 2020 Manuel Bleichenbacher
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//
using PdfSharp.Drawing;
using System.Collections.Generic;
namespace PDFsharp
{
/// <summary>
/// Wrapper for PDFsharp's XGraphicsPath to work around a bug
/// <para>
/// <c>XGraphicsPath.StartFigure()</c> is not correctly implemented. It does nothing.
/// This prevents the use of subpaths that are not closed.
/// </para>
/// <para>
/// This wrapper records the entire path. Once it's known if the path is filled or stroked,
/// it is replayed 1:1 (filled path) or split into separate paths (stroked path).
/// </para>
/// </summary>
class GraphicsPath
{
enum Command
{
MoveTo,
LineTo,
CubicCurveTo,
CloseSubpath,
Rectangle
}
private readonly List<XPoint> Points = new List<XPoint>();
private readonly List<Command> Commands = new List<Command>();
public void MoveTo(XPoint pt)
{
Points.Add(pt);
Commands.Add(Command.MoveTo);
}
public void LineTo(XPoint pt)
{
Points.Add(pt);
Commands.Add(Command.LineTo);
}
public void CubicCurveTo(XPoint pt1, XPoint pt2, XPoint pt3)
{
Points.Add(pt1);
Points.Add(pt2);
Points.Add(pt3);
Commands.Add(Command.CubicCurveTo);
}
public void AddRectangle(XPoint pt, double width, double height)
{
Points.Add(new XPoint(width, height));
Points.Add(pt);
Commands.Add(Command.Rectangle);
}
public void CloseSubpath()
{
Commands.Add(Command.CloseSubpath);
}
public void Fill(XGraphics graphics, XBrush brush)
{
XGraphicsPath path = new XGraphicsPath
{
FillMode = XFillMode.Winding
};
bool hasFigure = false;
int ptIndex = 0;
foreach (Command command in Commands)
{
switch (command)
{
case Command.MoveTo:
if (hasFigure)
{
path.CloseFigure();
hasFigure = false;
}
ptIndex++;
break;
case Command.LineTo:
path.AddLine(Points[ptIndex - 1], Points[ptIndex]);
ptIndex += 1;
hasFigure = true;
break;
case Command.CubicCurveTo:
path.AddBezier(Points[ptIndex - 1], Points[ptIndex], Points[ptIndex + 1], Points[ptIndex + 2]);
ptIndex += 3;
hasFigure = true;
break;
case Command.CloseSubpath:
if (hasFigure)
{
path.CloseFigure();
hasFigure = false;
}
break;
case Command.Rectangle:
path.AddRectangle(Points[ptIndex + 1].X, Points[ptIndex + 1].Y, Points[ptIndex].X, Points[ptIndex].Y);
ptIndex += 2;
path.CloseFigure();
hasFigure = false;
break;
}
}
if (hasFigure)
path.CloseFigure();
graphics.DrawPath(brush, path);
}
public void Stroke(XGraphics graphics, XPen pen)
{
XGraphicsPath path = new XGraphicsPath();
bool hasFigure = false;
int ptIndex = 0;
foreach (Command command in Commands)
{
switch (command)
{
case Command.MoveTo:
if (hasFigure)
{
graphics.DrawPath(pen, path);
path = new XGraphicsPath();
hasFigure = false;
}
ptIndex++;
break;
case Command.LineTo:
path.AddLine(Points[ptIndex - 1], Points[ptIndex]);
ptIndex += 1;
hasFigure = true;
break;
case Command.CubicCurveTo:
path.AddBezier(Points[ptIndex - 1], Points[ptIndex], Points[ptIndex + 1], Points[ptIndex + 2]);
ptIndex += 3;
hasFigure = true;
break;
case Command.CloseSubpath:
if (hasFigure)
{
path.CloseFigure();
hasFigure = false;
}
graphics.DrawPath(pen, path);
path = new XGraphicsPath();
break;
case Command.Rectangle:
path.AddRectangle(Points[ptIndex + 1].X, Points[ptIndex + 1].Y, Points[ptIndex].X, Points[ptIndex].Y);
ptIndex += 2;
path.CloseFigure();
hasFigure = false;
break;
}
}
graphics.DrawPath(pen, path);
}
}
}