Remove CurrentGraphicsState GetCurrentState() from IOperationContext.

This commit is contained in:
BobLd 2020-11-23 11:11:07 +00:00
parent 14465f0198
commit eb85f67b18
18 changed files with 272 additions and 54 deletions

View File

@ -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
{
@ -184,6 +186,91 @@
{
}
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)

View File

@ -1,9 +1,5 @@
namespace UglyToad.PdfPig.Graphics
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Colors;
using Content;
using Core;
@ -14,8 +10,13 @@
using Parser;
using PdfFonts;
using PdfPig.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Tokenization.Scanner;
using Tokens;
using UglyToad.PdfPig.Graphics.Operations.TextPositioning;
using XObjects;
using static PdfPig.Core.PdfSubpath;
@ -815,9 +816,86 @@
{
var matrix = TransformationMatrix.GetTranslationMatrix(tx, ty);
var newMatrix = matrix.Multiply(TextMatrices.TextMatrix);
TextMatrices.TextMatrix = matrix.Multiply(TextMatrices.TextMatrix);
}
TextMatrices.TextMatrix = newMatrix;
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;
}
}
}

View File

@ -3,6 +3,7 @@
using PdfPig.Core;
using System.Collections.Generic;
using Tokens;
using UglyToad.PdfPig.Graphics.Core;
/// <summary>
/// The current graphics state context when running a PDF content stream.
@ -19,12 +20,6 @@
/// </summary>
PdfPoint CurrentPosition { get; set; }
/// <summary>
/// Get the currently active <see cref="CurrentGraphicsState"/>. States are stored on a stack structure.
/// </summary>
/// <returns>The currently active graphics state.</returns>
CurrentGraphicsState GetCurrentState();
/// <summary>
/// The matrices for the current text state.
/// </summary>
@ -189,5 +184,90 @@
/// Modify the clipping rule of the current path.
/// </summary>
void ModifyClippingIntersect(FillingRule clippingRule);
/// <summary>
/// Set the flatness tolerance in the graphics state.
/// Flatness is a number in the range 0 to 100; a value of 0 specifies the output devices default flatness tolerance.
/// </summary>
/// <param name="tolerance"></param>
void SetFlatnessTolerance(decimal tolerance);
/// <summary>
/// Set the line cap style in the graphics state.
/// </summary>
void SetLineCap(LineCapStyle cap);
/// <summary>
/// Set the line dash pattern in the graphics state.
/// </summary>
void SetLineDashPattern(LineDashPattern pattern);
/// <summary>
/// Set the line join style in the graphics state.
/// </summary>
void SetLineJoin(LineJoinStyle join);
/// <summary>
/// Set the line width in the graphics state.
/// </summary>
void SetLineWidth(decimal width);
/// <summary>
/// Set the miter limit in the graphics state.
/// </summary>
void SetMiterLimit(decimal limit);
/// <summary>
/// Move to the start of the next line.
/// </summary>
/// <remarks>
/// This performs this operation: 0 -Tl Td
/// The offset is negative leading text (Tl) value, this is incorrect in the specification.
/// </remarks>
void MoveToNextLineWithOffset();
/// <summary>
/// Set the font and the font size.
/// Font is the name of a font resource in the Font subdictionary of the current resource dictionary.
/// Size is a number representing a scale factor.
/// </summary>
void SetFontAndSize(NameToken font, double size);
/// <summary>
/// Set the horizontal scaling.
/// </summary>
/// <param name="scale"></param>
void SetHorizontalScaling(double scale);
/// <summary>
/// Set the text leading.
/// </summary>
void SetTextLeading(double leading);
/// <summary>
/// Set the text rendering mode.
/// </summary>
void SetTextRenderingMode(TextRenderingMode mode);
/// <summary>
/// Set text rise.
/// </summary>
void SetTextRise(double rise);
/// <summary>
/// Sets the word spacing.
/// </summary>
void SetWordSpacing(double spacing);
/// <summary>
/// Modify the current transformation matrix by concatenating the specified matrix.
/// </summary>
void ModifyCurrentTransformationMatrix(double[] value);
/// <summary>
/// Set the character spacing to a number expressed in unscaled text space units.
/// Initial value: 0.
/// </summary>
void SetCharacterSpacing(double spacing);
}
}

View File

@ -35,7 +35,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
operationContext.GetCurrentState().Flatness = Tolerance;
operationContext.SetFlatnessTolerance(Tolerance);
}
/// <inheritdoc />

View File

@ -45,7 +45,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
operationContext.GetCurrentState().CapStyle = Cap;
operationContext.SetLineCap(Cap);
}
/// <inheritdoc />

View File

@ -33,7 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
operationContext.GetCurrentState().LineDashPattern = Pattern;
operationContext.SetLineDashPattern(Pattern);
}
/// <inheritdoc />

View File

@ -40,7 +40,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
operationContext.GetCurrentState().JoinStyle = Join;
operationContext.SetLineJoin(Join);
}
/// <inheritdoc />

View File

@ -33,9 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.LineWidth = Width;
operationContext.SetLineWidth(Width);
}
/// <inheritdoc />

View File

@ -33,9 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.MiterLimit = Limit;
operationContext.SetMiterLimit(Limit);
}
/// <inheritdoc />

View File

@ -44,13 +44,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var newMatrix = TransformationMatrix.FromArray(Value);
var ctm = operationContext.GetCurrentState().CurrentTransformationMatrix;
var newCtm = newMatrix.Multiply(ctm);
operationContext.GetCurrentState().CurrentTransformationMatrix = newCtm;
operationContext.ModifyCurrentTransformationMatrix(Array.ConvertAll(Value, x => (double)x));
}
/// <inheritdoc />

View File

@ -32,9 +32,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var tdOperation = new MoveToNextLineWithOffset(0, -1 * (decimal)operationContext.GetCurrentState().FontState.Leading);
tdOperation.Run(operationContext);
operationContext.MoveToNextLineWithOffset();
}
/// <inheritdoc />

View File

@ -34,9 +34,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.CharacterSpacing = (double)Spacing;
operationContext.SetCharacterSpacing((double)Spacing);
}
/// <inheritdoc />

View File

@ -48,10 +48,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.FontSize = (double)Size;
currentState.FontState.FontName = Font;
operationContext.SetFontAndSize(Font, (double)Size);
}
/// <inheritdoc />

View File

@ -30,9 +30,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.HorizontalScaling = (double)Scale;
operationContext.SetHorizontalScaling((double)Scale);
}
/// <inheritdoc />

View File

@ -33,9 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.Leading = (double)Leading;
operationContext.SetTextLeading((double)Leading);
}
/// <inheritdoc />

View File

@ -33,9 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.TextRenderingMode = Mode;
operationContext.SetTextRenderingMode(Mode);
}
/// <inheritdoc />

View File

@ -33,9 +33,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.Rise = (double)Rise;
operationContext.SetTextRise((double)Rise);
}
/// <inheritdoc />

View File

@ -34,9 +34,7 @@
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentState = operationContext.GetCurrentState();
currentState.FontState.WordSpacing = (double)Spacing;
operationContext.SetWordSpacing((double)Spacing);
}
/// <inheritdoc />