Skip to content

Commit

Permalink
Merge pull request #237 from BobLd/operationcontext-refactor
Browse files Browse the repository at this point in the history
Using `byte[]` for `IPageImageRenderer` and keeping the point on `ModifyCurrentTransformationMatrix.cs` in mind.
  • Loading branch information
BobLd authored Nov 27, 2020
2 parents cbd6eeb + 2b60474 commit 6e69160
Show file tree
Hide file tree
Showing 32 changed files with 505 additions and 122 deletions.
6 changes: 2 additions & 4 deletions src/UglyToad.PdfPig.Core/PdfSubpath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public void LineTo(double x, double y)
throw new ArgumentNullException("LineTo(): currentPosition is null.");
}
}

/// <summary>
/// Adds 4 <see cref="Line"/>s forming a rectangle to the path.
/// Add a rectangle following the pdf specification (m, l, l, l, c) path. A new subpath is created.
/// </summary>
public void Rectangle(double x, double y, double width, double height)
{
Expand All @@ -174,8 +174,6 @@ public void Rectangle(double x, double y, double width, double height)
CloseSubpath(); // h
IsDrawnAsRectangle = true;
}

internal void QuadraticCurveTo(double x1, double y1, double x2, double y2) { }

/// <summary>
/// Add a <see cref="BezierCurve"/> to the path.
Expand Down
147 changes: 147 additions & 0 deletions src/UglyToad.PdfPig.Tests/Graphics/TestOperationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using PdfPig.Tokens;
using PdfPig.Core;
using Tokens;
using UglyToad.PdfPig.Graphics.Core;
using UglyToad.PdfPig.Graphics.Operations.TextPositioning;

internal class TestOperationContext : IOperationContext
{
Expand Down Expand Up @@ -88,6 +90,66 @@ public void FillStrokePath(FillingRule fillingRule, bool close)

}

public void MoveTo(double x, double y)
{
BeginSubpath();
var point = CurrentTransformationMatrix.Transform(new PdfPoint(x, y));
CurrentPosition = point;
CurrentSubpath.MoveTo(point.X, point.Y);
}

public void BezierCurveTo(double x2, double y2, double x3, double y3)
{
if (CurrentSubpath == null)
{
return;
}

var controlPoint2 = CurrentTransformationMatrix.Transform(new PdfPoint(x2, y2));
var end = CurrentTransformationMatrix.Transform(new PdfPoint(x3, y3));

CurrentSubpath.BezierCurveTo(CurrentPosition.X, CurrentPosition.Y, controlPoint2.X, controlPoint2.Y, end.X, end.Y);
CurrentPosition = end;
}

public void BezierCurveTo(double x1, double y1, double x2, double y2, double x3, double y3)
{
if (CurrentSubpath == null)
{
return;
}

var controlPoint1 = CurrentTransformationMatrix.Transform(new PdfPoint(x1, y1));
var controlPoint2 = CurrentTransformationMatrix.Transform(new PdfPoint(x2, y2));
var end = CurrentTransformationMatrix.Transform(new PdfPoint(x3, y3));

CurrentSubpath.BezierCurveTo(controlPoint1.X, controlPoint1.Y, controlPoint2.X, controlPoint2.Y, end.X, end.Y);
CurrentPosition = end;
}

public void LineTo(double x, double y)
{
if (CurrentSubpath == null)
{
return;
}

var endPoint = CurrentTransformationMatrix.Transform(new PdfPoint(x, y));

CurrentSubpath.LineTo(endPoint.X, endPoint.Y);
CurrentPosition = endPoint;
}

public void Rectangle(double x, double y, double width, double height)
{
BeginSubpath();
var lowerLeft = CurrentTransformationMatrix.Transform(new PdfPoint(x, y));
var upperRight = CurrentTransformationMatrix.Transform(new PdfPoint(x + width, y + height));

CurrentSubpath.Rectangle(lowerLeft.X, lowerLeft.Y, upperRight.X - lowerLeft.X, upperRight.Y - lowerLeft.Y);
AddCurrentSubpath();
}

public void EndPath()
{
}
Expand Down Expand Up @@ -124,6 +186,91 @@ public void ModifyClippingIntersect(FillingRule clippingRule)
{
}

private void AdjustTextMatrix(double tx, double ty)
{
var matrix = TransformationMatrix.GetTranslationMatrix(tx, ty);
TextMatrices.TextMatrix = matrix.Multiply(TextMatrices.TextMatrix);
}

public void SetFlatnessTolerance(decimal tolerance)
{
GetCurrentState().Flatness = tolerance;
}

public void SetLineCap(LineCapStyle cap)
{
GetCurrentState().CapStyle = cap;
}

public void SetLineDashPattern(LineDashPattern pattern)
{
GetCurrentState().LineDashPattern = pattern;
}

public void SetLineJoin(LineJoinStyle join)
{
GetCurrentState().JoinStyle = join;
}

public void SetLineWidth(decimal width)
{
GetCurrentState().LineWidth = width;
}

public void SetMiterLimit(decimal limit)
{
GetCurrentState().MiterLimit = limit;
}

public void MoveToNextLineWithOffset()
{
var tdOperation = new MoveToNextLineWithOffset(0, -1 * (decimal)GetCurrentState().FontState.Leading);
tdOperation.Run(this);
}

public void SetFontAndSize(NameToken font, double size)
{
var currentState = GetCurrentState();
currentState.FontState.FontSize = size;
currentState.FontState.FontName = font;
}

public void SetHorizontalScaling(double scale)
{
GetCurrentState().FontState.HorizontalScaling = scale;
}

public void SetTextLeading(double leading)
{
GetCurrentState().FontState.Leading = leading;
}

public void SetTextRenderingMode(TextRenderingMode mode)
{
GetCurrentState().FontState.TextRenderingMode = mode;
}

public void SetTextRise(double rise)
{
GetCurrentState().FontState.Rise = rise;
}

public void SetWordSpacing(double spacing)
{
GetCurrentState().FontState.WordSpacing = spacing;
}

public void ModifyCurrentTransformationMatrix(double[] value)
{
var ctm = GetCurrentState().CurrentTransformationMatrix;
GetCurrentState().CurrentTransformationMatrix = TransformationMatrix.FromArray(value).Multiply(ctm);
}

public void SetCharacterSpacing(double spacing)
{
GetCurrentState().FontState.CharacterSpacing = spacing;
}

private class TestFontFactory : IFontFactory
{
public IFont Get(DictionaryToken dictionary)
Expand Down
3 changes: 3 additions & 0 deletions src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Content.IPdfImage",
"UglyToad.PdfPig.Content.Letter",
"UglyToad.PdfPig.Content.MarkedContentElement",
"UglyToad.PdfPig.Content.MediaBox",
"UglyToad.PdfPig.Content.Page",
"UglyToad.PdfPig.Content.PageRotationDegrees",
"UglyToad.PdfPig.Content.PageSize",
Expand Down Expand Up @@ -197,6 +198,8 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.ParsingOptions",
"UglyToad.PdfPig.PdfDocument",
"UglyToad.PdfPig.PdfExtensions",
"UglyToad.PdfPig.Rendering.IPageImageRenderer",
"UglyToad.PdfPig.Rendering.PdfRendererImageFormat",
"UglyToad.PdfPig.Structure",
"UglyToad.PdfPig.Util.Adler32Checksum",
"UglyToad.PdfPig.Util.IWordExtractor",
Expand Down
1 change: 1 addition & 0 deletions src/UglyToad.PdfPig.Tokens/NameToken.Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public partial class NameToken
public static readonly NameToken MarkInfo = new NameToken("MarkInfo");
public static readonly NameToken Mask = new NameToken("Mask");
public static readonly NameToken Matrix = new NameToken("Matrix");
public static readonly NameToken Matte = new NameToken("Matte");
public static readonly NameToken MaxLen = new NameToken("MaxLen");
public static readonly NameToken MaxWidth = new NameToken("MaxWidth");
public static readonly NameToken Mcid = new NameToken("MCID");
Expand Down
8 changes: 7 additions & 1 deletion src/UglyToad.PdfPig/Content/MediaBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// <remarks>
/// See table 3.27 from the PDF specification version 1.7.
/// </remarks>
internal class MediaBox
public class MediaBox
{
///<summary>
/// User space units per inch.
Expand Down Expand Up @@ -66,8 +66,14 @@ internal class MediaBox
/// </summary>
public static readonly MediaBox A6 = new MediaBox(new PdfRectangle(0, 0, 105 * PointsPerMm, 148 * PointsPerMm));

/// <summary>
/// A rectangle, expressed in default user space units, that defines the boundaries of the physical medium on which the page shall be displayed or printed.
/// </summary>
public PdfRectangle Bounds { get; }

/// <summary>
/// Create a new <see cref="MediaBox"/>.
/// </summary>
public MediaBox(PdfRectangle? bounds)
{
Bounds = bounds ?? throw new ArgumentNullException(nameof(bounds));
Expand Down
5 changes: 4 additions & 1 deletion src/UglyToad.PdfPig/Content/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public class Page
/// </summary>
public CropBox CropBox { get; }

internal MediaBox MediaBox { get; }
/// <summary>
/// Defines the boundaries of the physical medium on which the page shall be displayed or printed.
/// </summary>
public MediaBox MediaBox { get; }

internal PageContent Content { get; }

Expand Down
Loading

0 comments on commit 6e69160

Please sign in to comment.