Skip to content

Commit ad2270a

Browse files
committed
Moved images to pageresources & added xml comments
1 parent 274a259 commit ad2270a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2061
-495
lines changed

samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Synercoding.FileFormats.Pdf.Extensions;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
3+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
34
using Synercoding.Primitives;
45
using Synercoding.Primitives.Extensions;
6+
using System;
57
using System.IO;
68

79
namespace Synercoding.FileFormats.Pdf.ConsoleTester
@@ -66,26 +68,26 @@ public static void Main(string[] args)
6668
g.Stroke = null;
6769
g.Dash = new Dash()
6870
{
69-
Array = new double[0],
71+
Array = Array.Empty<double>(),
7072
Phase = 0
7173
};
7274
g.MiterLimit = 10;
7375
g.LineCap = LineCapStyle.ButtCap;
7476
g.LineJoin = LineJoinStyle.MiterJoin;
7577
});
7678

77-
ctx.NewPath(g => { g.Fill = Colors.Red; g.Stroke = Colors.Black; g.LineWidth = 5; })
79+
ctx.NewPath(g => { g.Fill = PredefinedColors.Red; g.Stroke = PredefinedColors.Black; g.LineWidth = 5; })
7880
.Move(100, 100)
7981
.LineTo(200, 100)
8082
.LineTo(200, 200)
8183
.LineTo(100, 200);
82-
ctx.NewPath(g => { g.Fill = Colors.Blue; g.Stroke = null; })
84+
ctx.NewPath(g => { g.Fill = PredefinedColors.Blue; g.Stroke = null; })
8385
.Move(50, 50)
8486
.LineTo(150, 50)
8587
.LineTo(150, 150)
8688
.LineTo(50, 150)
8789
.Close();
88-
ctx.NewPath(g => { g.Fill = null; g.Stroke = Colors.Yellow; g.LineWidth = 3; g.Dash = new Dash() { Array = new[] { 5d } }; })
90+
ctx.NewPath(g => { g.Fill = null; g.Stroke = PredefinedColors.Yellow; g.LineWidth = 3; g.Dash = new Dash() { Array = new[] { 5d } }; })
8991
.Move(150, 150)
9092
.LineTo(250, 150)
9193
.LineTo(250, 250)

src/Synercoding.FileFormats.Pdf/Extensions/PdfPageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static PdfPage AddShapes(this PdfPage page, Action<IShapeContext> paintAc
146146
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
147147
public static PdfPage AddShapes<T>(this PdfPage page, T data, Action<T, IShapeContext> paintAction)
148148
{
149-
using (var context = new ShapeContext(page.ContentStream))
149+
using (var context = new ShapeContext(page.ContentStream, page.Resources))
150150
paintAction(data, context);
151151

152152
return page;

src/Synercoding.FileFormats.Pdf/Extensions/PrimitiveExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
using Synercoding.Primitives;
1+
using Synercoding.Primitives;
22

33
namespace Synercoding.FileFormats.Pdf.Extensions
44
{
5+
/// <summary>
6+
/// Extension methods for primitives
7+
/// </summary>
58
public static class PrimitiveExtensions
69
{
10+
/// <summary>
11+
/// Convert a <see cref="Rectangle"/> to a transformation matrix
12+
/// </summary>
13+
/// <param name="rectangle">The <see cref="Rectangle"/> to use</param>
14+
/// <returns>Returns a <see cref="Matrix"/> representing the provided <see cref="Rectangle"/>.</returns>
715
public static Matrix AsPlacementMatrix(this Rectangle rectangle)
816
{
917
rectangle = rectangle.ConvertTo(Unit.Points);

src/Synercoding.FileFormats.Pdf/IPath.cs

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,132 @@
22

33
namespace Synercoding.FileFormats.Pdf
44
{
5+
/// <summary>
6+
/// Interface representing a path in the content stream of a page
7+
/// </summary>
58
public interface IPath
69
{
10+
/// <summary>
11+
/// Begin a new subpath by moving the current point to the coordinates (<paramref name="x"/>, <paramref name="y"/>),
12+
/// ommitting any connecting line segment. Appends an (m) operator to the content stream
13+
/// </summary>
14+
/// <param name="x">The X coordinate of the move</param>
15+
/// <param name="y">The Y coordinate of the move</param>
16+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
717
IPath Move(double x, double y);
18+
19+
/// <summary>
20+
/// Begin a new subpath by moving the current point to the coordinates (<paramref name="point"/>),
21+
/// ommitting any connecting line segment. Appends an (m) operator to the content stream
22+
/// </summary>
23+
/// <param name="point">The point to where to move</param>
24+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
825
IPath Move(Point point);
26+
27+
/// <summary>
28+
/// Add a line (l) operator to the content stream
29+
/// </summary>
30+
/// <param name="x">The X coordinate of the line end point</param>
31+
/// <param name="y">The Y coordinate of the line end point</param>
32+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
933
IPath LineTo(double x, double y);
34+
35+
/// <summary>
36+
/// Add a line (l) operator to the content stream
37+
/// </summary>
38+
/// <param name="point">The point to where to line to</param>
39+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
1040
IPath LineTo(Point point);
41+
42+
/// <summary>
43+
/// Add a rectangle (re) operator to the content stream
44+
/// </summary>
45+
/// <param name="x">The X coordinate of the rectangle</param>
46+
/// <param name="y">The Y coordinate of the rectangle</param>
47+
/// <param name="width">The width of the rectangle</param>
48+
/// <param name="height">The height of the rectangle</param>
49+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
1150
IPath Rectangle(double x, double y, double width, double height);
51+
52+
/// <summary>
53+
/// Add a rectangle (re) operator to the content stream
54+
/// </summary>
55+
/// <param name="rectangle">The rectangle to add to the content stream</param>
56+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
1257
IPath Rectangle(Rectangle rectangle);
58+
59+
/// <summary>
60+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="finalX"/>, <paramref name="finalY"/>),
61+
/// using (<paramref name="cpX1"/>, <paramref name="cpY1"/>) and (<paramref name="cpX2"/>, <paramref name="cpY2"/>) as the Bézier control points.
62+
/// Adds a Cubic Bézier Curve (c) operator to the content stream.
63+
/// </summary>
64+
/// <param name="cpX1">The X coordinate of the first control point</param>
65+
/// <param name="cpY1">The Y coordinate of the first control point</param>
66+
/// <param name="cpX2">The X coordinate of the second control point</param>
67+
/// <param name="cpY2">The Y coordinate of the second control point</param>
68+
/// <param name="finalX">The X coordinate of the endpoint of the curve</param>
69+
/// <param name="finalY">The Y coordinate of the endpoint of the curve</param>
70+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
1371
IPath CurveTo(double cpX1, double cpY1, double cpX2, double cpY2, double finalX, double finalY);
72+
73+
/// <summary>
74+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="final"/>),
75+
/// using (<paramref name="cp1"/>) and (<paramref name="cp2"/>) as the Bézier control points.
76+
/// Adds a Cubic Bézier Curve (c) operator to the content stream.
77+
/// </summary>
78+
/// <param name="cp1">The first control point</param>
79+
/// <param name="cp2">The second control point</param>
80+
/// <param name="final">The endpoint of the curve</param>
81+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
1482
IPath CurveTo(Point cp1, Point cp2, Point final);
15-
IPath CurveToWithStartAnker(double cpX1, double cpY1, double finalX, double finalY);
16-
IPath CurveToWithStartAnker(Point cp, Point final);
83+
84+
/// <summary>
85+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="finalX"/>, <paramref name="finalY"/>),
86+
/// using the current point and (<paramref name="cpX2"/>, <paramref name="cpY2"/>) as the Bézier control points.
87+
/// Adds a Cubic Bézier Curve (v) operator to the content stream.
88+
/// </summary>
89+
/// <param name="cpX2">The X coordinate of the second control point</param>
90+
/// <param name="cpY2">The Y coordinate of the second control point</param>
91+
/// <param name="finalX">The X coordinate of the endpoint of the curve</param>
92+
/// <param name="finalY">The Y coordinate of the endpoint of the curve</param>
93+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
94+
IPath CurveToWithStartAnker(double cpX2, double cpY2, double finalX, double finalY);
95+
96+
/// <summary>
97+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="final"/>),
98+
/// using the current point and (<paramref name="cp2"/>) as the Bézier control points.
99+
/// Adds a Cubic Bézier Curve (v) operator to the content stream.
100+
/// </summary>
101+
/// <param name="cp2">The second control point</param>
102+
/// <param name="final">The endpoint of the curve</param>
103+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
104+
IPath CurveToWithStartAnker(Point cp2, Point final);
105+
106+
/// <summary>
107+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="finalX"/>, <paramref name="finalY"/>),
108+
/// using (<paramref name="cpX1"/>, <paramref name="cpY1"/>) and (<paramref name="finalX"/>, <paramref name="finalY"/>) as the Bézier control points.
109+
/// Adds a Cubic Bézier Curve (y) operator to the content stream.
110+
/// </summary>
111+
/// <param name="cpX1">The X coordinate of the first control point</param>
112+
/// <param name="cpY1">The Y coordinate of the first control point</param>
113+
/// <param name="finalX">The X coordinate of the endpoint of the curve</param>
114+
/// <param name="finalY">The Y coordinate of the endpoint of the curve</param>
115+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
17116
IPath CurveToWithEndAnker(double cpX1, double cpY1, double finalX, double finalY);
18-
IPath CurveToWithEndAnker(Point cp, Point final);
19117

20-
IShapeContext Close();
118+
/// <summary>
119+
/// Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (<paramref name="final"/>),
120+
/// using (<paramref name="cp1"/>) and (<paramref name="final"/>) as the Bézier control points.
121+
/// Adds a Cubic Bézier Curve (y) operator to the content stream.
122+
/// </summary>
123+
/// <param name="cp1">The first control point</param>
124+
/// <param name="final">The endpoint of the curve</param>
125+
/// <returns>The calling <see cref="IPath"/> to support chaining operations.</returns>
126+
IPath CurveToWithEndAnker(Point cp1, Point final);
21127

128+
/// <summary>
129+
/// Close the <see cref="IPath"/>
130+
/// </summary>
131+
void Close();
22132
}
23133
}

src/Synercoding.FileFormats.Pdf/IShapeContext.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@
33

44
namespace Synercoding.FileFormats.Pdf
55
{
6+
/// <summary>
7+
/// Interface representing a context which can be used to draw shapes on a page
8+
/// </summary>
69
public interface IShapeContext
710
{
11+
/// <summary>
12+
/// Change the default graphics state
13+
/// </summary>
14+
/// <param name="configureState">Action used to configure the <see cref="GraphicsState"/></param>
15+
/// <returns>The calling <see cref="IShapeContext"/> to support chaining</returns>
816
IShapeContext DefaultState(Action<GraphicsState> configureState);
17+
18+
/// <summary>
19+
/// Start a new <see cref="IPath"/>
20+
/// </summary>
21+
/// <returns>The new <see cref="IPath"/> object to chain pathing operators</returns>
922
IPath NewPath();
23+
24+
/// <summary>
25+
/// Start a new <see cref="IPath"/> with a different graphics state
26+
/// </summary>
27+
/// <param name="configureState">The action used to change the <see cref="GraphicsState"/></param>
28+
/// <returns>The new <see cref="IPath"/> object to chain pathing operators</returns>
1029
IPath NewPath(Action<GraphicsState> configureState);
1130
}
1231
}

src/Synercoding.FileFormats.Pdf/Image.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using SixLabors.ImageSharp;
1+
using SixLabors.ImageSharp;
22
using Synercoding.FileFormats.Pdf.LowLevel;
33
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
44
using System;
55
using System.IO;
66

77
namespace Synercoding.FileFormats.Pdf
88
{
9-
public class Image : IPdfObject, IDisposable
9+
/// <summary>
10+
/// Class representing an image inside a pdf
11+
/// </summary>
12+
public sealed class Image : IPdfObject, IDisposable
1013
{
1114
private readonly Stream _imageStream;
1215
private readonly int _width;

src/Synercoding.FileFormats.Pdf/Internals/Map.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34

@@ -37,7 +38,7 @@ IEnumerator IEnumerable.GetEnumerator()
3738
public Indexer<T1, T2> Forward { get; }
3839
public Indexer<T2, T1> Reverse { get; }
3940

40-
public sealed class Indexer<T3, T4>
41+
public sealed class Indexer<T3, T4> : IReadOnlyDictionary<T3, T4>
4142
where T3 : notnull
4243
where T4 : notnull
4344
{
@@ -54,8 +55,26 @@ public T4 this[T3 index]
5455
set => _dictionary[index] = value;
5556
}
5657

58+
public IEnumerable<T3> Keys => _dictionary.Keys;
59+
60+
public IEnumerable<T4> Values => _dictionary.Values;
61+
62+
public int Count => _dictionary.Count;
63+
5764
public bool Contains(T3 value)
5865
=> _dictionary.ContainsKey(value);
66+
67+
public bool ContainsKey(T3 key)
68+
=> _dictionary.ContainsKey(key);
69+
70+
public IEnumerator<KeyValuePair<T3, T4>> GetEnumerator()
71+
=> _dictionary.GetEnumerator();
72+
73+
public bool TryGetValue(T3 key, out T4 value)
74+
=> throw new InvalidOperationException("TryGetValue is not supported on types with non-nullable values.");
75+
76+
IEnumerator IEnumerable.GetEnumerator()
77+
=> _dictionary.GetEnumerator();
5978
}
6079
}
6180
}

src/Synercoding.FileFormats.Pdf/Internals/Path.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Synercoding.FileFormats.Pdf.LowLevel;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
3+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
34
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color;
45
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Pathing.Construction;
56
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Pathing.Painting;
@@ -11,12 +12,10 @@ namespace Synercoding.FileFormats.Pdf.Internals
1112
{
1213
internal class Path : IPath
1314
{
14-
private readonly ShapeContext _context;
1515
private readonly ContentStream _contentStream;
1616

17-
public Path(ShapeContext context, ContentStream contentStream, GraphicsState graphicsState)
17+
public Path(ContentStream contentStream, GraphicsState graphicsState)
1818
{
19-
_context = context;
2019
_contentStream = contentStream;
2120
GraphicsState = graphicsState;
2221

@@ -35,14 +34,10 @@ private void _startPath()
3534
.Write(new LineJoinOperator(GraphicsState.LineJoin))
3635
.Write(new MiterLimitOperator(GraphicsState.MiterLimit))
3736
.Write(new DashOperator(GraphicsState.Dash.Array, GraphicsState.Dash.Phase));
38-
39-
// write graphic state to stream
4037
}
4138

4239
internal void FinishPath()
4340
{
44-
// TODO: set colorspace for stroke and/or fill
45-
4641
if (GraphicsState.Stroke is not null && GraphicsState.Fill is not null)
4742
{
4843
if (GraphicsState.Stroke is GrayColor gs)
@@ -99,11 +94,9 @@ internal void FinishPath()
9994
_contentStream.RestoreState();
10095
}
10196

102-
public IShapeContext Close()
97+
public void Close()
10398
{
10499
_contentStream.Write(new CloseOperator());
105-
106-
return _context;
107100
}
108101

109102
public IPath CurveTo(double cpX1, double cpY1, double cpX2, double cpY2, double finalX, double finalY)
@@ -118,27 +111,27 @@ public IPath CurveTo(Point cp1, Point cp2, Point final)
118111
return this;
119112
}
120113

121-
public IPath CurveToWithEndAnker(double cpX, double cpY, double finalX, double finalY)
114+
public IPath CurveToWithEndAnker(double cpX1, double cpY1, double finalX, double finalY)
122115
{
123-
_contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cpX, cpY, finalX, finalY));
116+
_contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cpX1, cpY1, finalX, finalY));
124117
return this;
125118
}
126119

127-
public IPath CurveToWithEndAnker(Point cp, Point final)
120+
public IPath CurveToWithEndAnker(Point cp1, Point final)
128121
{
129-
_contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cp, final));
122+
_contentStream.Write(new CubicBezierCurveFinalControlPointsOperator(cp1, final));
130123
return this;
131124
}
132125

133-
public IPath CurveToWithStartAnker(double cpX, double cpY, double finalX, double finalY)
126+
public IPath CurveToWithStartAnker(double cpX2, double cpY2, double finalX, double finalY)
134127
{
135-
_contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cpX, cpY, finalX, finalY));
128+
_contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cpX2, cpY2, finalX, finalY));
136129
return this;
137130
}
138131

139-
public IPath CurveToWithStartAnker(Point cp, Point final)
132+
public IPath CurveToWithStartAnker(Point cp2, Point final)
140133
{
141-
_contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cp, final));
134+
_contentStream.Write(new CubicBezierCurveInitialControlPointsOperator(cp2, final));
142135
return this;
143136
}
144137

0 commit comments

Comments
 (0)