Refactoring and fixing according to Eliot's comments

This commit is contained in:
vadimy
2019-07-24 00:00:00 -04:00
parent 7c50733cbc
commit 7d3a0929b6
7 changed files with 45 additions and 23 deletions

View File

@@ -5,6 +5,7 @@
using PdfPig.Graphics; using PdfPig.Graphics;
using PdfPig.IO; using PdfPig.IO;
using PdfPig.Tokens; using PdfPig.Tokens;
using UglyToad.PdfPig.Core;
internal class TestOperationContext : IOperationContext internal class TestOperationContext : IOperationContext
{ {
@@ -16,6 +17,11 @@
public TextMatrices TextMatrices { get; set; } public TextMatrices TextMatrices { get; set; }
= new TextMatrices(); = new TextMatrices();
public TransformationMatrix CurrentTransformationMatrix
{
get { return GetCurrentState().CurrentTransformationMatrix; }
}
public PdfPath CurrentPath { get; set; } public PdfPath CurrentPath { get; set; }
public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext(); public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext();
@@ -25,7 +31,7 @@
public TestOperationContext() public TestOperationContext()
{ {
StateStack.Push(new CurrentGraphicsState()); StateStack.Push(new CurrentGraphicsState());
CurrentPath = new PdfPath(); CurrentPath = new PdfPath(CurrentTransformationMatrix);
} }
public CurrentGraphicsState GetCurrentState() public CurrentGraphicsState GetCurrentState()

View File

@@ -9,7 +9,7 @@
internal class Type2BuildCharContext internal class Type2BuildCharContext
{ {
private readonly Dictionary<int, decimal> transientArray = new Dictionary<int, decimal>(); private readonly Dictionary<int, decimal> transientArray = new Dictionary<int, decimal>();
/// <summary> /// <summary>
/// The numbers currently on the Type 2 Build Char stack. /// The numbers currently on the Type 2 Build Char stack.
/// </summary> /// </summary>
@@ -18,7 +18,7 @@
/// <summary> /// <summary>
/// The current path. /// The current path.
/// </summary> /// </summary>
public PdfPath Path { get; } = new PdfPath(); public PdfPath Path { get; } = new PdfPath(Core.TransformationMatrix.Identity);
/// <summary> /// <summary>
/// The current location of the active point. /// The current location of the active point.

View File

@@ -22,7 +22,7 @@
public bool IsFlexing { get; set; } public bool IsFlexing { get; set; }
[NotNull] [NotNull]
public PdfPath Path { get; private set; } = new PdfPath(); public PdfPath Path { get; private set; } = new PdfPath(Core.TransformationMatrix.Identity);
public PdfPoint CurrentPosition { get; set; } public PdfPoint CurrentPosition { get; set; }
@@ -63,7 +63,7 @@
public void ClearFlexPoints() public void ClearFlexPoints()
{ {
} }
} }
} }

View File

@@ -12,22 +12,29 @@ namespace UglyToad.PdfPig.Geometry
/// </summary> /// </summary>
public class PdfPath public class PdfPath
{ {
public readonly List<IPathCommand> Commands = new List<IPathCommand>(); private readonly List<IPathCommand> commands = new List<IPathCommand>();
public IReadOnlyList<IPathCommand> Commands => commands;
private PdfPoint? currentPosition; private PdfPoint? currentPosition;
internal TransformationMatrix CurrentTransformationMatrix; private TransformationMatrix currentTransformationMatrix = TransformationMatrix.Identity;
public PdfPath(TransformationMatrix transformationMatrix)
{
currentTransformationMatrix = transformationMatrix;
}
internal void MoveTo(decimal x, decimal y) internal void MoveTo(decimal x, decimal y)
{ {
currentPosition = CurrentTransformationMatrix.Transform(new PdfPoint(x, y)); currentPosition = currentTransformationMatrix.Transform(new PdfPoint(x, y));
Commands.Add(new Move(currentPosition.Value)); commands.Add(new Move(currentPosition.Value));
} }
internal void LineTo(decimal x, decimal y) internal void LineTo(decimal x, decimal y)
{ {
if (currentPosition.HasValue) if (currentPosition.HasValue)
{ {
var to = CurrentTransformationMatrix.Transform(new PdfPoint(x, y)); var to = currentTransformationMatrix.Transform(new PdfPoint(x, y));
Commands.Add(new Line(currentPosition.Value, to)); commands.Add(new Line(currentPosition.Value, to));
currentPosition = to; currentPosition = to;
} }
else else
@@ -42,9 +49,9 @@ namespace UglyToad.PdfPig.Geometry
{ {
if (currentPosition.HasValue) if (currentPosition.HasValue)
{ {
var to = CurrentTransformationMatrix.Transform(new PdfPoint(x3, y3)); var to = currentTransformationMatrix.Transform(new PdfPoint(x3, y3));
Commands.Add(new BezierCurve(currentPosition.Value, commands.Add(new BezierCurve(currentPosition.Value,
CurrentTransformationMatrix.Transform(new PdfPoint(x1, y1)), CurrentTransformationMatrix.Transform(new PdfPoint(x2, y2)), to)); currentTransformationMatrix.Transform(new PdfPoint(x1, y1)), currentTransformationMatrix.Transform(new PdfPoint(x2, y2)), to));
currentPosition = to; currentPosition = to;
} }
else else
@@ -57,12 +64,12 @@ namespace UglyToad.PdfPig.Geometry
internal void ClosePath() internal void ClosePath()
{ {
Commands.Add(new Close()); commands.Add(new Close());
} }
internal PdfRectangle? GetBoundingRectangle() internal PdfRectangle? GetBoundingRectangle()
{ {
if (Commands.Count == 0) if (commands.Count == 0)
{ {
return null; return null;
} }
@@ -73,7 +80,7 @@ namespace UglyToad.PdfPig.Geometry
var minY = decimal.MaxValue; var minY = decimal.MaxValue;
var maxY = decimal.MinValue; var maxY = decimal.MinValue;
foreach (var command in Commands) foreach (var command in commands)
{ {
var rect = command.GetBoundingRectangle(); var rect = command.GetBoundingRectangle();
if (rect == null) if (rect == null)
@@ -108,7 +115,7 @@ namespace UglyToad.PdfPig.Geometry
internal string ToSvg() internal string ToSvg()
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
foreach (var pathCommand in Commands) foreach (var pathCommand in commands)
{ {
pathCommand.WriteSvg(builder); pathCommand.WriteSvg(builder);
} }
@@ -138,7 +145,7 @@ namespace UglyToad.PdfPig.Geometry
var bbox = GetBoundingRectangle(); var bbox = GetBoundingRectangle();
var bboxes = new List<PdfRectangle>(); var bboxes = new List<PdfRectangle>();
foreach (var command in Commands) foreach (var command in commands)
{ {
var segBbox = command.GetBoundingRectangle(); var segBbox = command.GetBoundingRectangle();
if (segBbox.HasValue) if (segBbox.HasValue)
@@ -381,7 +388,7 @@ namespace UglyToad.PdfPig.Geometry
internal void Rectangle(decimal x, decimal y, decimal width, decimal height) internal void Rectangle(decimal x, decimal y, decimal width, decimal height)
{ {
currentPosition = CurrentTransformationMatrix.Transform(new PdfPoint(x, y)); currentPosition = currentTransformationMatrix.Transform(new PdfPoint(x, y));
LineTo(x + width, y); LineTo(x + width, y);
LineTo(x + width, y + height); LineTo(x + width, y + height);
LineTo(x, y + height); LineTo(x, y + height);

View File

@@ -32,6 +32,11 @@
public TextMatrices TextMatrices { get; } = new TextMatrices(); public TextMatrices TextMatrices { get; } = new TextMatrices();
public TransformationMatrix CurrentTransformationMatrix
{
get { return GetCurrentState().CurrentTransformationMatrix; }
}
public PdfPath CurrentPath { get; private set; } public PdfPath CurrentPath { get; private set; }
public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext(); public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext();
@@ -265,8 +270,7 @@
public void BeginSubpath() public void BeginSubpath()
{ {
CurrentPath = new PdfPath(); CurrentPath = new PdfPath(CurrentTransformationMatrix);
CurrentPath.CurrentTransformationMatrix = GetCurrentState().CurrentTransformationMatrix;
} }
public void StrokePath(bool close) public void StrokePath(bool close)

View File

@@ -4,6 +4,7 @@
using Geometry; using Geometry;
using IO; using IO;
using Tokens; using Tokens;
using UglyToad.PdfPig.Core;
using Util.JetBrains.Annotations; using Util.JetBrains.Annotations;
/// <summary> /// <summary>
@@ -38,6 +39,11 @@
/// </summary> /// </summary>
TextMatrices TextMatrices { get; } TextMatrices TextMatrices { get; }
/// <summary>
/// The current transformation matrix
/// </summary>
TransformationMatrix CurrentTransformationMatrix { get; }
/// <summary> /// <summary>
/// The number of graphics states on the stack. /// The number of graphics states on the stack.
/// </summary> /// </summary>

View File

@@ -52,7 +52,6 @@
{ {
operationContext.BeginSubpath(); operationContext.BeginSubpath();
operationContext.CurrentPath.Rectangle(LowerLeft.X, LowerLeft.Y, Width, Height); operationContext.CurrentPath.Rectangle(LowerLeft.X, LowerLeft.Y, Width, Height);
//operationContext.CurrentPath.ClosePath();
} }
/// <inheritdoc /> /// <inheritdoc />