Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Synercoding.FileFormats.Pdf.Extensions;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors.ColorSpaces;
using Synercoding.FileFormats.Pdf.LowLevel.Text;
using Synercoding.Primitives;
using Synercoding.Primitives.Extensions;
Expand Down Expand Up @@ -151,8 +152,8 @@ public static void Main(string[] args)
{
var scale = (double)forestImage.Width / forestImage.Height;

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

page.AddImage(forestImage, matrix);
}
Expand All @@ -175,6 +176,26 @@ public static void Main(string[] args)
page.AddImage(reusedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
});
}


writer.AddPage(page =>
{
page.MediaBox = mediaBox;
page.TrimBox = trimBox;

var scale = (double)blurImage.Width / blurImage.Height;

page.AddImage(reusedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));

page.AddShapes(trimBox, static (trim, context) =>
{
context.DefaultState(state =>
{
state.Stroke = new SpotColor(new Separation(LowLevel.PdfName.Get("CutContour"), PredefinedColors.Magenta), 1);
});
context.NewPath().Rectangle(trim);
});
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Synercoding.FileFormats.Pdf.Internals;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color;
using Synercoding.FileFormats.Pdf.LowLevel.Operators.State;
using Synercoding.FileFormats.Pdf.LowLevel.Text;
using Synercoding.Primitives;
Expand Down
39 changes: 4 additions & 35 deletions src/Synercoding.FileFormats.Pdf/Internals/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,50 +40,19 @@ internal void FinishPath()
{
if (GraphicsState.Stroke is not null && GraphicsState.Fill is not null)
{
if (GraphicsState.Stroke is GrayColor gs)
_contentStream.Write(new GrayStrokingColorOperator(gs));
else if (GraphicsState.Stroke is RgbColor rs)
_contentStream.Write(new RgbStrokingColorOperator(rs));
else if (GraphicsState.Stroke is CmykColor cs)
_contentStream.Write(new CmykStrokingColorOperator(cs));
else
throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented.");

if (GraphicsState.Fill is GrayColor gf)
_contentStream.Write(new GrayNonStrokingColorOperator(gf));
else if (GraphicsState.Fill is RgbColor rf)
_contentStream.Write(new RgbNonStrokingColorOperator(rf));
else if (GraphicsState.Fill is CmykColor cf)
_contentStream.Write(new CmykNonStrokingColorOperator(cf));
else
throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented.");
_contentStream.SetColorFill(GraphicsState.Fill);
_contentStream.SetColorStroke(GraphicsState.Stroke);

_contentStream.Write(new FillAndStrokeOperator(GraphicsState.FillRule));
}
else if (GraphicsState.Fill is not null)
{
if (GraphicsState.Fill is GrayColor gf)
_contentStream.Write(new GrayNonStrokingColorOperator(gf));
else if (GraphicsState.Fill is RgbColor rf)
_contentStream.Write(new RgbNonStrokingColorOperator(rf));
else if (GraphicsState.Fill is CmykColor cf)
_contentStream.Write(new CmykNonStrokingColorOperator(cf));
else
throw new NotImplementedException($"The color type {GraphicsState.Fill.GetType().Name} is not implemented.");

_contentStream.SetColorFill(GraphicsState.Fill);
_contentStream.Write(new FillOperator(GraphicsState.FillRule));
}
else if (GraphicsState.Stroke is not null)
{
if (GraphicsState.Stroke is GrayColor gs)
_contentStream.Write(new GrayStrokingColorOperator(gs));
else if (GraphicsState.Stroke is RgbColor rs)
_contentStream.Write(new RgbStrokingColorOperator(rs));
else if (GraphicsState.Stroke is CmykColor cs)
_contentStream.Write(new CmykStrokingColorOperator(cs));
else
throw new NotImplementedException($"The color type {GraphicsState.Stroke.GetType().Name} is not implemented.");

_contentStream.SetColorStroke(GraphicsState.Stroke);
_contentStream.Write(new StrokeOperator());
}
else
Expand Down
66 changes: 51 additions & 15 deletions src/Synercoding.FileFormats.Pdf/LowLevel/ContentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ public sealed class ContentStream : IPdfObject, IDisposable
private const string UNKNOWN_FILL_RULE = "Unknown fill rule";
private readonly PdfStream _streamWrapper;

/// <summary>
/// Constructor for <see cref="ContentStream"/>
/// </summary>
/// <param name="id">The <see cref="PdfReference"/> of this content stream</param>
public ContentStream(PdfReference id)
: this(id, new PdfStream(new MemoryStream()))
{ }

/// <summary>
/// Constructor for <see cref="ContentStream"/>
/// </summary>
/// <param name="id">The <see cref="PdfReference"/> of this content stream</param>
/// <param name="pdfStream">The <see cref="PdfStream"/> to write to</param>
public ContentStream(PdfReference id, PdfStream pdfStream)
internal ContentStream(PdfReference id, PageResources pageResources)
{
Resources = pageResources;
_streamWrapper = new PdfStream(new MemoryStream());

Reference = id;
_streamWrapper = pdfStream;
}
internal PageResources Resources { get; }

/// <inheritdoc />
public PdfReference Reference { get; }
Expand Down Expand Up @@ -572,6 +562,7 @@ public ContentStream SetColorFill(Color color)
GrayColor gray => Write(new GrayNonStrokingColorOperator(gray)),
RgbColor rgb => Write(new RgbNonStrokingColorOperator(rgb)),
CmykColor cmyk => Write(new CmykNonStrokingColorOperator(cmyk)),
SpotColor spot => Write(new SpotNonStrokingColorOperator(spot)),
_ => throw new NotImplementedException($"The color type {color.GetType().Name} is not implemented.")
};
}
Expand All @@ -589,6 +580,7 @@ public ContentStream SetColorStroke(Color color)
GrayColor gray => Write(new GrayStrokingColorOperator(gray)),
RgbColor rgb => Write(new RgbStrokingColorOperator(rgb)),
CmykColor cmyk => Write(new CmykStrokingColorOperator(cmyk)),
SpotColor spot => Write(new SpotStrokingColorOperator(spot)),
_ => throw new NotImplementedException($"The color type {color.GetType().Name} is not implemented.")
};
}
Expand Down Expand Up @@ -743,6 +735,50 @@ public ContentStream Write(CmykNonStrokingColorOperator op)
return this;
}

/// <summary>
/// Write the operator (SCN) to the stream
/// </summary>
/// <param name="op">The operator to write</param>
/// <returns>The <see cref="ContentStream"/> to support chaining operations.</returns>
public ContentStream Write(SpotStrokingColorOperator op)
{
var name = Resources.AddSeparation(op.Color.Separation);

_streamWrapper
.Write(name)
.Space()
.Write("CS")
.Space()
.Write(op.Color.Tint)
.Space()
.Write("SCN")
.NewLine();

return this;
}

/// <summary>
/// Write the operator (scn) to the stream
/// </summary>
/// <param name="op">The operator to write</param>
/// <returns>The <see cref="ContentStream"/> to support chaining operations.</returns>
public ContentStream Write(SpotNonStrokingColorOperator op)
{
var name = Resources.AddSeparation(op.Color.Separation);

_streamWrapper
.Write(name)
.Space()
.Write("cs")
.Space()
.Write(op.Color.Tint)
.Space()
.Write("scn")
.NewLine();

return this;
}

/// <summary>
/// Write the operator (w) to the stream
/// </summary>
Expand Down
Loading