VectSharp  2.2.1
A light library for C# vector graphics
Document.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 System;
19 using System.Collections.Generic;
20 using System.Text;
21 
22 namespace VectSharp
23 {
24  /// <summary>
25  /// Represents a collection of pages.
26  /// </summary>
27  public class Document
28  {
29  /// <summary>
30  /// The pages in the document.
31  /// </summary>
32  public List<Page> Pages = new List<Page>();
33 
34 
35  /// <summary>
36  /// Create a new document.
37  /// </summary>
38  public Document()
39  {
40 
41  }
42  }
43 
44  /// <summary>
45  /// Represents a <see cref="Graphics"/> object with a width and height.
46  /// </summary>
47  public class Page
48  {
49  /// <summary>
50  /// Width of the page.
51  /// </summary>
52  public double Width { get; set; }
53 
54  /// <summary>
55  /// Height of the page.
56  /// </summary>
57  public double Height { get; set; }
58 
59  /// <summary>
60  /// Graphics surface of the page.
61  /// </summary>
62  public Graphics Graphics { get; set; }
63 
64  /// <summary>
65  /// Background colour of the page.
66  /// </summary>
67  public Colour Background { get; set; } = Colour.FromRgba(255, 255, 255, 0);
68 
69  /// <summary>
70  /// Create a new page.
71  /// </summary>
72  /// <param name="width">The width of the page.</param>
73  /// <param name="height">The height of the page.</param>
74  public Page(double width, double height)
75  {
76  this.Width = width;
77  this.Height = height;
78 
79  this.Graphics = new Graphics();
80  this.Graphics.Translate(0, 0);
81  }
82 
83  /// <summary>
84  /// Translate and resize the <see cref="Page"/> so that it displays the rectangle defined by <paramref name="topLeft"/> and <paramref name="size"/>.
85  /// </summary>
86  /// <param name="topLeft">The top left corner of the area to include in the page.</param>
87  /// <param name="size">The size of the area to include in the page.</param>
88  public void Crop(Point topLeft, Size size)
89  {
90  if (this.Graphics.Actions[0] is TransformAction transf)
91  {
92  double[,] currMatrix = transf.GetMatrix();
93 
94  double[,] newMatrix = Graphics.Multiply(Graphics.TranslationMatrix(-topLeft.X, -topLeft.Y), currMatrix);
95 
96  this.Graphics.Actions[0] = new TransformAction(newMatrix);
97  }
98  else
99  {
100  this.Graphics.Actions.Insert(0, new TransformAction(new Point(-topLeft.X, -topLeft.Y)));
101  }
102 
103  this.Width = size.Width;
104  this.Height = size.Height;
105  }
106 
107  /// <summary>
108  /// Translate and resize the <see cref="Page"/> so that it displays the rectangle corresponding to the bounding box of its contents.
109  /// </summary>
110  public void Crop()
111  {
112  Rectangle bounds = this.Graphics.GetBounds();
113 
114  if (this.Graphics.Actions[0] is TransformAction transf)
115  {
116  double[,] currMatrix = transf.GetMatrix();
117 
118  double[,] newMatrix = Graphics.Multiply(Graphics.TranslationMatrix(-bounds.Location.X, -bounds.Location.Y), currMatrix);
119 
120  this.Graphics.Actions[0] = new TransformAction(newMatrix);
121  }
122  else
123  {
124  this.Graphics.Actions.Insert(0, new TransformAction(new Point(-bounds.Location.X, -bounds.Location.Y)));
125  }
126 
127  this.Width = bounds.Size.Width;
128  this.Height = bounds.Size.Height;
129  }
130  }
131 }
VectSharp.Rectangle
Represents a rectangle.
Definition: Point.cs:173
VectSharp.Graphics.Translate
void Translate(double x, double y)
Translate the coordinate system origin.
Definition: Graphics.cs:370
VectSharp.Colour
Represents an RGB colour.
Definition: Colour.cs:26
VectSharp.Page.Height
double Height
Height of the page.
Definition: Document.cs:57
VectSharp.Page.Background
Colour Background
Background colour of the page.
Definition: Document.cs:67
VectSharp
Definition: Brush.cs:26
VectSharp.Document
Represents a collection of pages.
Definition: Document.cs:28
VectSharp.Page.Width
double Width
Width of the page.
Definition: Document.cs:52
VectSharp.Page
Represents a Graphics object with a width and height.
Definition: Document.cs:48
VectSharp.Size.Height
double Height
Height of the object.
Definition: Point.cs:155
VectSharp.Document.Document
Document()
Create a new document.
Definition: Document.cs:38
VectSharp.Page.Page
Page(double width, double height)
Create a new page.
Definition: Document.cs:74
VectSharp.Graphics
Represents an abstract drawing surface.
Definition: Graphics.cs:262
VectSharp.Rectangle.Size
Size Size
The size of the rectangle.
Definition: Point.cs:187
VectSharp.Page.Graphics
Graphics Graphics
Graphics surface of the page.
Definition: Document.cs:62
VectSharp.Point.X
double X
Horizontal (x) coordinate, measured to the right of the origin.
Definition: Point.cs:32
VectSharp.Document.Pages
List< Page > Pages
The pages in the document.
Definition: Document.cs:32
VectSharp.Page.Crop
void Crop()
Translate and resize the Page so that it displays the rectangle corresponding to the bounding box of ...
Definition: Document.cs:110
VectSharp.Size
Represents the size of an object.
Definition: Point.cs:146
VectSharp.Rectangle.Location
Point Location
The top-left corner of the rectangle.
Definition: Point.cs:182
VectSharp.Size.Width
double Width
Width of the object.
Definition: Point.cs:150
VectSharp.Point
Represents a point relative to an origin in the top-left corner.
Definition: Point.cs:28
VectSharp.Graphics.GetBounds
Rectangle GetBounds()
Computes the rectangular bounds of the region affected by the drawing operations performed on the Gra...
Definition: Graphics.cs:1313
VectSharp.Page.Crop
void Crop(Point topLeft, Size size)
Translate and resize the Page so that it displays the rectangle defined by topLeft and size .
Definition: Document.cs:88
VectSharp.Point.Y
double Y
Vertical (y) coordinate, measured to the bottom of the origin.
Definition: Point.cs:37
VectSharp.Colour.FromRgba
static Colour FromRgba(double r, double g, double b, double a)
Create a new colour from RGBA (red, green, blue and alpha) values.
Definition: Colour.cs:99