Skip to content

Commit 8e3661d

Browse files
authored
Merge pull request #54 from synercoder/develop
Added spotcolors, AddPageAsync & page numbers
2 parents ad11438 + 490bdde commit 8e3661d

File tree

24 files changed

+716
-270
lines changed

24 files changed

+716
-270
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Synercoding.FileFormats.Pdf.Extensions;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
33
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
4+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors.ColorSpaces;
45
using Synercoding.FileFormats.Pdf.LowLevel.Text;
56
using Synercoding.Primitives;
67
using Synercoding.Primitives.Extensions;
@@ -151,8 +152,8 @@ public static void Main(string[] args)
151152
{
152153
var scale = (double)forestImage.Width / forestImage.Height;
153154

154-
var matrix = Matrix.CreateScaleMatrix(new Value(scale * 303, Unit.Millimeters), new Value(303, Unit.Millimeters))
155-
.Translate(new Value(-100, Unit.Millimeters), new Value(0, Unit.Millimeters));
155+
var matrix = Matrix.CreateScaleMatrix(new Value(scale * 303, Unit.Millimeters).AsRaw(Unit.Points), new Value(303, Unit.Millimeters).AsRaw(Unit.Points))
156+
.Translate(new Value(-100, Unit.Millimeters).AsRaw(Unit.Points), new Value(0, Unit.Millimeters).AsRaw(Unit.Points));
156157

157158
page.AddImage(forestImage, matrix);
158159
}
@@ -175,6 +176,26 @@ public static void Main(string[] args)
175176
page.AddImage(reusedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
176177
});
177178
}
179+
180+
181+
writer.AddPage(page =>
182+
{
183+
page.MediaBox = mediaBox;
184+
page.TrimBox = trimBox;
185+
186+
var scale = (double)blurImage.Width / blurImage.Height;
187+
188+
page.AddImage(reusedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
189+
190+
page.AddShapes(trimBox, static (trim, context) =>
191+
{
192+
context.DefaultState(state =>
193+
{
194+
state.Stroke = new SpotColor(new Separation(LowLevel.PdfName.Get("CutContour"), PredefinedColors.Magenta), 1);
195+
});
196+
context.NewPath().Rectangle(trim);
197+
});
198+
});
178199
}
179200
}
180201
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Synercoding.FileFormats.Pdf.Internals;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
33
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
4-
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color;
54
using Synercoding.FileFormats.Pdf.LowLevel.Operators.State;
65
using Synercoding.FileFormats.Pdf.LowLevel.Text;
76
using Synercoding.Primitives;

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

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,19 @@ internal void FinishPath()
4040
{
4141
if (GraphicsState.Stroke is not null && GraphicsState.Fill is not null)
4242
{
43-
if (GraphicsState.Stroke is GrayColor gs)
44-
_contentStream.Write(new GrayStrokingColorOperator(gs));
45-
else if (GraphicsState.Stroke is RgbColor rs)
46-
_contentStream.Write(new RgbStrokingColorOperator(rs));
47-
else if (GraphicsState.Stroke is CmykColor cs)
48-
_contentStream.Write(new CmykStrokingColorOperator(cs));
49-
else
50-
throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented.");
51-
52-
if (GraphicsState.Fill is GrayColor gf)
53-
_contentStream.Write(new GrayNonStrokingColorOperator(gf));
54-
else if (GraphicsState.Fill is RgbColor rf)
55-
_contentStream.Write(new RgbNonStrokingColorOperator(rf));
56-
else if (GraphicsState.Fill is CmykColor cf)
57-
_contentStream.Write(new CmykNonStrokingColorOperator(cf));
58-
else
59-
throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented.");
43+
_contentStream.SetColorFill(GraphicsState.Fill);
44+
_contentStream.SetColorStroke(GraphicsState.Stroke);
6045

6146
_contentStream.Write(new FillAndStrokeOperator(GraphicsState.FillRule));
6247
}
6348
else if (GraphicsState.Fill is not null)
6449
{
65-
if (GraphicsState.Fill is GrayColor gf)
66-
_contentStream.Write(new GrayNonStrokingColorOperator(gf));
67-
else if (GraphicsState.Fill is RgbColor rf)
68-
_contentStream.Write(new RgbNonStrokingColorOperator(rf));
69-
else if (GraphicsState.Fill is CmykColor cf)
70-
_contentStream.Write(new CmykNonStrokingColorOperator(cf));
71-
else
72-
throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented.");
73-
50+
_contentStream.SetColorFill(GraphicsState.Fill);
7451
_contentStream.Write(new FillOperator(GraphicsState.FillRule));
7552
}
7653
else if (GraphicsState.Stroke is not null)
7754
{
78-
if (GraphicsState.Stroke is GrayColor gs)
79-
_contentStream.Write(new GrayStrokingColorOperator(gs));
80-
else if (GraphicsState.Stroke is RgbColor rs)
81-
_contentStream.Write(new RgbStrokingColorOperator(rs));
82-
else if (GraphicsState.Stroke is CmykColor cs)
83-
_contentStream.Write(new CmykStrokingColorOperator(cs));
84-
else
85-
throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented.");
86-
55+
_contentStream.SetColorStroke(GraphicsState.Stroke);
8756
_contentStream.Write(new StrokeOperator());
8857
}
8958
else

src/Synercoding.FileFormats.Pdf/LowLevel/ContentStream.cs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,14 @@ public sealed class ContentStream : IPdfObject, IDisposable
2626
private const string UNKNOWN_FILL_RULE = "Unknown fill rule";
2727
private readonly PdfStream _streamWrapper;
2828

29-
/// <summary>
30-
/// Constructor for <see cref="ContentStream"/>
31-
/// </summary>
32-
/// <param name="id">The <see cref="PdfReference"/> of this content stream</param>
33-
public ContentStream(PdfReference id)
34-
: this(id, new PdfStream(new MemoryStream()))
35-
{ }
36-
37-
/// <summary>
38-
/// Constructor for <see cref="ContentStream"/>
39-
/// </summary>
40-
/// <param name="id">The <see cref="PdfReference"/> of this content stream</param>
41-
/// <param name="pdfStream">The <see cref="PdfStream"/> to write to</param>
42-
public ContentStream(PdfReference id, PdfStream pdfStream)
29+
internal ContentStream(PdfReference id, PageResources pageResources)
4330
{
31+
Resources = pageResources;
32+
_streamWrapper = new PdfStream(new MemoryStream());
33+
4434
Reference = id;
45-
_streamWrapper = pdfStream;
4635
}
36+
internal PageResources Resources { get; }
4737

4838
/// <inheritdoc />
4939
public PdfReference Reference { get; }
@@ -572,6 +562,7 @@ public ContentStream SetColorFill(Color color)
572562
GrayColor gray => Write(new GrayNonStrokingColorOperator(gray)),
573563
RgbColor rgb => Write(new RgbNonStrokingColorOperator(rgb)),
574564
CmykColor cmyk => Write(new CmykNonStrokingColorOperator(cmyk)),
565+
SpotColor spot => Write(new SpotNonStrokingColorOperator(spot)),
575566
_ => throw new NotImplementedException($"The color type {color.GetType().Name} is not implemented.")
576567
};
577568
}
@@ -589,6 +580,7 @@ public ContentStream SetColorStroke(Color color)
589580
GrayColor gray => Write(new GrayStrokingColorOperator(gray)),
590581
RgbColor rgb => Write(new RgbStrokingColorOperator(rgb)),
591582
CmykColor cmyk => Write(new CmykStrokingColorOperator(cmyk)),
583+
SpotColor spot => Write(new SpotStrokingColorOperator(spot)),
592584
_ => throw new NotImplementedException($"The color type {color.GetType().Name} is not implemented.")
593585
};
594586
}
@@ -743,6 +735,50 @@ public ContentStream Write(CmykNonStrokingColorOperator op)
743735
return this;
744736
}
745737

738+
/// <summary>
739+
/// Write the operator (SCN) to the stream
740+
/// </summary>
741+
/// <param name="op">The operator to write</param>
742+
/// <returns>The <see cref="ContentStream"/> to support chaining operations.</returns>
743+
public ContentStream Write(SpotStrokingColorOperator op)
744+
{
745+
var name = Resources.AddSeparation(op.Color.Separation);
746+
747+
_streamWrapper
748+
.Write(name)
749+
.Space()
750+
.Write("CS")
751+
.Space()
752+
.Write(op.Color.Tint)
753+
.Space()
754+
.Write("SCN")
755+
.NewLine();
756+
757+
return this;
758+
}
759+
760+
/// <summary>
761+
/// Write the operator (scn) to the stream
762+
/// </summary>
763+
/// <param name="op">The operator to write</param>
764+
/// <returns>The <see cref="ContentStream"/> to support chaining operations.</returns>
765+
public ContentStream Write(SpotNonStrokingColorOperator op)
766+
{
767+
var name = Resources.AddSeparation(op.Color.Separation);
768+
769+
_streamWrapper
770+
.Write(name)
771+
.Space()
772+
.Write("cs")
773+
.Space()
774+
.Write(op.Color.Tint)
775+
.Space()
776+
.Write("scn")
777+
.NewLine();
778+
779+
return this;
780+
}
781+
746782
/// <summary>
747783
/// Write the operator (w) to the stream
748784
/// </summary>

0 commit comments

Comments
 (0)