VectSharp  2.2.1
A light library for C# vector graphics
GraphicsAction.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using VectSharp.Filters;
19 
20 namespace VectSharp
21 {
22  internal interface IGraphicsAction
23  {
24 
25  }
26 
27  internal interface IPrintableAction
28  {
29  Brush Fill { get; }
30  Brush Stroke { get; }
31  double LineWidth { get; }
32  LineCaps LineCap { get; }
33  LineJoins LineJoin { get; }
34  LineDash LineDash { get; }
35  string Tag { get; }
36  Rectangle GetBounds();
37  }
38 
39  internal class TransformAction : IGraphicsAction
40  {
41  public Point? Delta { get; } = null;
42 
43  public double? Angle { get; } = null;
44 
45  public Size? Scale { get; } = null;
46 
47  public double[,] Matrix { get; } = null;
48 
49  public TransformAction(Point delta)
50  {
51  this.Delta = delta;
52  }
53 
54  public TransformAction(double angle)
55  {
56  this.Angle = angle;
57  }
58 
59  public TransformAction(Size scale)
60  {
61  this.Scale = scale;
62  }
63 
64  public TransformAction(double[,] matrix)
65  {
66  this.Matrix = matrix;
67  }
68 
69  public double[,] GetMatrix()
70  {
71  if (this.Matrix != null)
72  {
73  return this.Matrix;
74  }
75  else if (this.Delta != null)
76  {
77  return Graphics.TranslationMatrix(this.Delta.Value.X, this.Delta.Value.Y);
78  }
79  else if (this.Angle != null)
80  {
81  return Graphics.RotationMatrix(this.Angle.Value);
82  }
83  else if (this.Scale != null)
84  {
85  return Graphics.ScaleMatrix(this.Scale.Value.Width, this.Scale.Value.Height);
86  }
87  else
88  {
89  return null;
90  }
91  }
92  }
93 
94  internal class StateAction : IGraphicsAction
95  {
96  public enum StateActionTypes
97  {
98  Save, Restore
99  }
100 
101  public StateActionTypes StateActionType { get; }
102 
103  public StateAction(StateActionTypes type)
104  {
105  this.StateActionType = type;
106  }
107  }
108 
109  internal class TextAction : IGraphicsAction, IPrintableAction
110  {
111  public Brush Fill { get; }
112  public Brush Stroke { get; }
113  public double LineWidth { get; }
114  public LineCaps LineCap { get; }
115  public LineJoins LineJoin { get; }
116  public LineDash LineDash { get; }
117  public string Tag { get; }
118  public string Text { get; }
119  public Point Origin { get; }
120  public TextBaselines TextBaseline { get; }
121  public Font Font { get; }
122 
123  public TextAction(Point origin, string text, Font font, TextBaselines textBaseLine, Brush fill, Brush stroke, double lineWidth, LineCaps lineCap, LineJoins lineJoin, LineDash lineDash, string tag)
124  {
125  this.Origin = origin;
126  this.Text = text;
127  this.Font = font;
128  this.TextBaseline = textBaseLine;
129  this.Fill = fill;
130  this.Stroke = stroke;
131  this.LineCap = lineCap;
132  this.LineJoin = lineJoin;
133  this.LineWidth = lineWidth;
134  this.Tag = tag;
135  this.LineDash = lineDash;
136  }
137 
138  public Rectangle GetBounds()
139  {
140  Font.DetailedFontMetrics metrics = this.Font.MeasureTextAdvanced(this.Text);
141 
142  switch (this.TextBaseline)
143  {
144  case TextBaselines.Top:
145  return new Rectangle(this.Origin, new Size(metrics.Width, metrics.Height));
146  case TextBaselines.Bottom:
147  return new Rectangle(this.Origin.X, this.Origin.Y - metrics.Height, metrics.Width, metrics.Height);
148  case TextBaselines.Middle:
149  return new Rectangle(this.Origin.X, this.Origin.Y - metrics.Height * 0.5, metrics.Width, metrics.Height);
150  case TextBaselines.Baseline:
151  return new Rectangle(this.Origin.X, this.Origin.Y - metrics.Top, metrics.Width, metrics.Height);
152  default:
153  throw new System.ArgumentOutOfRangeException(nameof(TextBaseline), this.TextBaseline, "Invalid text baseline!");
154  }
155  }
156  }
157 
158  internal class RectangleAction : IGraphicsAction, IPrintableAction
159  {
160  public Brush Fill { get; }
161  public Brush Stroke { get; }
162  public double LineWidth { get; }
163  public LineCaps LineCap { get; }
164  public LineJoins LineJoin { get; }
165  public LineDash LineDash { get; }
166  public string Tag { get; }
167  public Point TopLeft { get; }
168  public Size Size { get; }
169 
170  public RectangleAction(Point topLeft, Size size, Brush fill, Brush stroke, double lineWidth, LineCaps lineCap, LineJoins lineJoin, LineDash lineDash, string tag)
171  {
172  this.TopLeft = topLeft;
173  this.Size = size;
174  this.Fill = fill;
175  this.Stroke = stroke;
176  this.LineCap = lineCap;
177  this.LineJoin = lineJoin;
178  this.LineWidth = lineWidth;
179  this.LineDash = lineDash;
180  this.Tag = tag;
181  }
182 
183  public Rectangle GetBounds()
184  {
185  return new Rectangle(this.TopLeft, this.Size);
186  }
187  }
188 
189  internal class PathAction : IGraphicsAction, IPrintableAction
190  {
191  public GraphicsPath Path { get; }
192  public Brush Fill { get; }
193  public Brush Stroke { get; }
194  public string Tag { get; }
195  public double LineWidth { get; }
196  public LineCaps LineCap { get; }
197  public LineJoins LineJoin { get; }
198  public LineDash LineDash { get; }
199  public bool IsClipping { get; }
200  public Rectangle GetBounds()
201  {
202  return this.Path.GetBounds();
203  }
204  public PathAction(GraphicsPath path, Brush fill, Brush stroke, double lineWidth, LineCaps lineCap, LineJoins lineJoin, LineDash lineDash, string tag, bool isClipping)
205  {
206  this.Path = path;
207  this.Fill = fill;
208  this.Stroke = stroke;
209  this.LineCap = lineCap;
210  this.LineJoin = lineJoin;
211  this.LineWidth = lineWidth;
212  this.LineDash = lineDash;
213  this.Tag = tag;
214  this.IsClipping = isClipping;
215  }
216  }
217 
218  internal class RasterImageAction : IGraphicsAction, IPrintableAction
219  {
220  public Brush Fill { get; }
221  public Brush Stroke { get; }
222  public string Tag { get; }
223  public double LineWidth { get; }
224  public LineCaps LineCap { get; }
225  public LineJoins LineJoin { get; }
226  public LineDash LineDash { get; }
227  public int SourceX { get; }
228  public int SourceY { get; }
229  public int SourceWidth { get; }
230  public int SourceHeight { get; }
231  public double DestinationX { get; }
232  public double DestinationY { get; }
233  public double DestinationWidth { get; }
234  public double DestinationHeight { get; }
235  public RasterImage Image { get; }
236 
237  public RasterImageAction(int sourceX, int sourceY, int sourceWidth, int sourceHeight, double destinationX, double destinationY, double destinationWidth, double destinationHeight, RasterImage image, string tag)
238  {
239  this.SourceX = sourceX;
240  this.SourceY = sourceY;
241  this.SourceWidth = sourceWidth;
242  this.SourceHeight = sourceHeight;
243 
244  this.DestinationX = destinationX;
245  this.DestinationY = destinationY;
246  this.DestinationWidth = destinationWidth;
247  this.DestinationHeight = destinationHeight;
248 
249  this.Image = image;
250  this.Tag = tag;
251  }
252 
253  public Rectangle GetBounds()
254  {
255  return new Rectangle(this.DestinationX, this.DestinationY, this.DestinationWidth, this.DestinationHeight);
256  }
257  }
258 
259  internal class FilteredGraphicsAction : IGraphicsAction, IPrintableAction
260  {
261  public Brush Fill { get; }
262  public Brush Stroke { get; }
263  public string Tag { get; }
264  public double LineWidth { get; }
265  public LineCaps LineCap { get; }
266  public LineJoins LineJoin { get; }
267  public LineDash LineDash { get; }
268  public int SourceX { get; }
269  public int SourceY { get; }
270  public int SourceWidth { get; }
271  public int SourceHeight { get; }
272  public double DestinationX { get; }
273  public double DestinationY { get; }
274  public double DestinationWidth { get; }
275  public double DestinationHeight { get; }
276  public RasterImage Image { get; }
277  public Graphics Content { get; }
278  public IFilter Filter { get; }
279 
280  public FilteredGraphicsAction(Graphics graphics, IFilter filter)
281  {
282  this.Content = graphics;
283  this.Filter = filter;
284  }
285 
286  public Rectangle GetBounds()
287  {
288  Rectangle bounds = this.Content.GetBounds();
289 
290  return new Rectangle(bounds.Location.X - this.Filter.TopLeftMargin.X, bounds.Location.Y - this.Filter.TopLeftMargin.Y, bounds.Size.Width + this.Filter.TopLeftMargin.X + this.Filter.BottomRightMargin.X, bounds.Size.Height + this.Filter.TopLeftMargin.Y + this.Filter.BottomRightMargin.Y);
291  }
292  }
293 }
VectSharp
Definition: Brush.cs:26
VectSharp.Filters.IFilter
Represents a filter. Do not implement this interface directly; instead, implement ILocationInvariantF...
Definition: Filters.cs:26
VectSharp.LineCaps
LineCaps
Represents line caps.
Definition: Enums.cs:71
VectSharp.TextBaselines
TextBaselines
Represent text baselines.
Definition: Enums.cs:24
VectSharp.LineJoins
LineJoins
Represents line joining options.
Definition: Enums.cs:92
VectSharp.Filters
Definition: BoxBlurFilter.cs:22