mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-23 04:36:44 +08:00
Refactoring and fixing according to Eliot's comments
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
using PdfPig.Graphics;
|
||||
using PdfPig.IO;
|
||||
using PdfPig.Tokens;
|
||||
using UglyToad.PdfPig.Core;
|
||||
|
||||
internal class TestOperationContext : IOperationContext
|
||||
{
|
||||
@@ -16,6 +17,11 @@
|
||||
public TextMatrices TextMatrices { get; set; }
|
||||
= new TextMatrices();
|
||||
|
||||
public TransformationMatrix CurrentTransformationMatrix
|
||||
{
|
||||
get { return GetCurrentState().CurrentTransformationMatrix; }
|
||||
}
|
||||
|
||||
public PdfPath CurrentPath { get; set; }
|
||||
|
||||
public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext();
|
||||
@@ -25,7 +31,7 @@
|
||||
public TestOperationContext()
|
||||
{
|
||||
StateStack.Push(new CurrentGraphicsState());
|
||||
CurrentPath = new PdfPath();
|
||||
CurrentPath = new PdfPath(CurrentTransformationMatrix);
|
||||
}
|
||||
|
||||
public CurrentGraphicsState GetCurrentState()
|
||||
|
@@ -9,7 +9,7 @@
|
||||
internal class Type2BuildCharContext
|
||||
{
|
||||
private readonly Dictionary<int, decimal> transientArray = new Dictionary<int, decimal>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The numbers currently on the Type 2 Build Char stack.
|
||||
/// </summary>
|
||||
@@ -18,7 +18,7 @@
|
||||
/// <summary>
|
||||
/// The current path.
|
||||
/// </summary>
|
||||
public PdfPath Path { get; } = new PdfPath();
|
||||
public PdfPath Path { get; } = new PdfPath(Core.TransformationMatrix.Identity);
|
||||
|
||||
/// <summary>
|
||||
/// The current location of the active point.
|
||||
|
@@ -22,7 +22,7 @@
|
||||
public bool IsFlexing { get; set; }
|
||||
|
||||
[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; }
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
|
||||
public void ClearFlexPoints()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,22 +12,29 @@ namespace UglyToad.PdfPig.Geometry
|
||||
/// </summary>
|
||||
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;
|
||||
internal TransformationMatrix CurrentTransformationMatrix;
|
||||
private TransformationMatrix currentTransformationMatrix = TransformationMatrix.Identity;
|
||||
|
||||
public PdfPath(TransformationMatrix transformationMatrix)
|
||||
{
|
||||
currentTransformationMatrix = transformationMatrix;
|
||||
}
|
||||
|
||||
internal void MoveTo(decimal x, decimal y)
|
||||
{
|
||||
currentPosition = CurrentTransformationMatrix.Transform(new PdfPoint(x, y));
|
||||
Commands.Add(new Move(currentPosition.Value));
|
||||
currentPosition = currentTransformationMatrix.Transform(new PdfPoint(x, y));
|
||||
commands.Add(new Move(currentPosition.Value));
|
||||
}
|
||||
|
||||
internal void LineTo(decimal x, decimal y)
|
||||
{
|
||||
if (currentPosition.HasValue)
|
||||
{
|
||||
var to = CurrentTransformationMatrix.Transform(new PdfPoint(x, y));
|
||||
Commands.Add(new Line(currentPosition.Value, to));
|
||||
var to = currentTransformationMatrix.Transform(new PdfPoint(x, y));
|
||||
commands.Add(new Line(currentPosition.Value, to));
|
||||
currentPosition = to;
|
||||
}
|
||||
else
|
||||
@@ -42,9 +49,9 @@ namespace UglyToad.PdfPig.Geometry
|
||||
{
|
||||
if (currentPosition.HasValue)
|
||||
{
|
||||
var to = CurrentTransformationMatrix.Transform(new PdfPoint(x3, y3));
|
||||
Commands.Add(new BezierCurve(currentPosition.Value,
|
||||
CurrentTransformationMatrix.Transform(new PdfPoint(x1, y1)), CurrentTransformationMatrix.Transform(new PdfPoint(x2, y2)), to));
|
||||
var to = currentTransformationMatrix.Transform(new PdfPoint(x3, y3));
|
||||
commands.Add(new BezierCurve(currentPosition.Value,
|
||||
currentTransformationMatrix.Transform(new PdfPoint(x1, y1)), currentTransformationMatrix.Transform(new PdfPoint(x2, y2)), to));
|
||||
currentPosition = to;
|
||||
}
|
||||
else
|
||||
@@ -57,12 +64,12 @@ namespace UglyToad.PdfPig.Geometry
|
||||
|
||||
internal void ClosePath()
|
||||
{
|
||||
Commands.Add(new Close());
|
||||
commands.Add(new Close());
|
||||
}
|
||||
|
||||
internal PdfRectangle? GetBoundingRectangle()
|
||||
{
|
||||
if (Commands.Count == 0)
|
||||
if (commands.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -73,7 +80,7 @@ namespace UglyToad.PdfPig.Geometry
|
||||
var minY = decimal.MaxValue;
|
||||
var maxY = decimal.MinValue;
|
||||
|
||||
foreach (var command in Commands)
|
||||
foreach (var command in commands)
|
||||
{
|
||||
var rect = command.GetBoundingRectangle();
|
||||
if (rect == null)
|
||||
@@ -108,7 +115,7 @@ namespace UglyToad.PdfPig.Geometry
|
||||
internal string ToSvg()
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
foreach (var pathCommand in Commands)
|
||||
foreach (var pathCommand in commands)
|
||||
{
|
||||
pathCommand.WriteSvg(builder);
|
||||
}
|
||||
@@ -138,7 +145,7 @@ namespace UglyToad.PdfPig.Geometry
|
||||
var bbox = GetBoundingRectangle();
|
||||
var bboxes = new List<PdfRectangle>();
|
||||
|
||||
foreach (var command in Commands)
|
||||
foreach (var command in commands)
|
||||
{
|
||||
var segBbox = command.GetBoundingRectangle();
|
||||
if (segBbox.HasValue)
|
||||
@@ -381,7 +388,7 @@ namespace UglyToad.PdfPig.Geometry
|
||||
|
||||
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 + height);
|
||||
LineTo(x, y + height);
|
||||
|
@@ -32,6 +32,11 @@
|
||||
|
||||
public TextMatrices TextMatrices { get; } = new TextMatrices();
|
||||
|
||||
public TransformationMatrix CurrentTransformationMatrix
|
||||
{
|
||||
get { return GetCurrentState().CurrentTransformationMatrix; }
|
||||
}
|
||||
|
||||
public PdfPath CurrentPath { get; private set; }
|
||||
|
||||
public IColorspaceContext ColorspaceContext { get; } = new ColorspaceContext();
|
||||
@@ -265,8 +270,7 @@
|
||||
|
||||
public void BeginSubpath()
|
||||
{
|
||||
CurrentPath = new PdfPath();
|
||||
CurrentPath.CurrentTransformationMatrix = GetCurrentState().CurrentTransformationMatrix;
|
||||
CurrentPath = new PdfPath(CurrentTransformationMatrix);
|
||||
}
|
||||
|
||||
public void StrokePath(bool close)
|
||||
|
@@ -4,6 +4,7 @@
|
||||
using Geometry;
|
||||
using IO;
|
||||
using Tokens;
|
||||
using UglyToad.PdfPig.Core;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
/// <summary>
|
||||
@@ -38,6 +39,11 @@
|
||||
/// </summary>
|
||||
TextMatrices TextMatrices { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The current transformation matrix
|
||||
/// </summary>
|
||||
TransformationMatrix CurrentTransformationMatrix { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of graphics states on the stack.
|
||||
/// </summary>
|
||||
|
@@ -52,7 +52,6 @@
|
||||
{
|
||||
operationContext.BeginSubpath();
|
||||
operationContext.CurrentPath.Rectangle(LowerLeft.X, LowerLeft.Y, Width, Height);
|
||||
//operationContext.CurrentPath.ClosePath();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
Reference in New Issue
Block a user