mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-15 19:54:52 +08:00
#26 make all operations writable and test
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
namespace UglyToad.PdfPig.Tests.Graphics.Operations
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using PdfPig.Graphics.Operations;
|
||||
using PdfPig.Tokens;
|
||||
using Xunit;
|
||||
|
||||
public class GraphicsStateOperationTests
|
||||
@@ -10,14 +14,100 @@
|
||||
[Fact]
|
||||
public void AllOperationsArePublic()
|
||||
{
|
||||
var assembly = Assembly.GetAssembly(typeof(IGraphicsStateOperation));
|
||||
|
||||
var operationTypes = assembly.GetTypes().Where(x => typeof(IGraphicsStateOperation).IsAssignableFrom(x));
|
||||
|
||||
foreach (var operationType in operationTypes)
|
||||
foreach (var operationType in GetOperationTypes())
|
||||
{
|
||||
Assert.True(operationType.IsPublic, $"{operationType.Name} should be public.");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllOperationsCanBeWritten()
|
||||
{
|
||||
foreach (var operationType in GetOperationTypes())
|
||||
{
|
||||
IGraphicsStateOperation operation;
|
||||
|
||||
var constructors = operationType.GetConstructors();
|
||||
|
||||
if (constructors.Length == 0)
|
||||
{
|
||||
var field = operationType.GetFields(BindingFlags.Public | BindingFlags.Static)
|
||||
.FirstOrDefault(x => x.Name == "Value");
|
||||
|
||||
if (field == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not find singleton for type: {operationType.Name}.");
|
||||
}
|
||||
|
||||
operation = (IGraphicsStateOperation)field.GetValue(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var constructor = constructors[0];
|
||||
|
||||
var parameterTypes = constructor.GetParameters();
|
||||
|
||||
var parameters = GetConstructorParameters(parameterTypes);
|
||||
|
||||
operation = (IGraphicsStateOperation)constructor.Invoke(parameters);
|
||||
}
|
||||
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
operation.Write(memoryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Type> GetOperationTypes()
|
||||
{
|
||||
var assembly = Assembly.GetAssembly(typeof(IGraphicsStateOperation));
|
||||
|
||||
var operationTypes = assembly.GetTypes().Where(x => typeof(IGraphicsStateOperation).IsAssignableFrom(x)
|
||||
&& !x.IsInterface);
|
||||
|
||||
return operationTypes;
|
||||
}
|
||||
|
||||
private static object[] GetConstructorParameters(ParameterInfo[] parameters)
|
||||
{
|
||||
var result = new object[parameters.Length];
|
||||
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var param = parameters[i];
|
||||
var type = param.ParameterType;
|
||||
|
||||
if (type == typeof(string))
|
||||
{
|
||||
result[i] = "Toad";
|
||||
}
|
||||
else if (type == typeof(NameToken))
|
||||
{
|
||||
result[i] = NameToken.Create("Hog");
|
||||
}
|
||||
else if (type == typeof(decimal))
|
||||
{
|
||||
result[i] = 0.5m;
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
result[i] = 1;
|
||||
}
|
||||
else if (type == typeof(decimal[]) || type == typeof(IReadOnlyList<decimal>))
|
||||
{
|
||||
result[i] = new decimal[]
|
||||
{
|
||||
1, 0, 0, 1, 2, 5
|
||||
};
|
||||
}
|
||||
else if (type == typeof(IReadOnlyList<IToken>))
|
||||
{
|
||||
result[i] = new IToken[] { new StringToken("Text") };
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Operations
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using Util;
|
||||
|
||||
@@ -14,6 +15,12 @@
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
public static void WriteHex(this Stream stream, byte[] bytes)
|
||||
{
|
||||
var text = BitConverter.ToString(bytes).Replace("-", string.Empty);
|
||||
stream.WriteText($"<{text}>");
|
||||
}
|
||||
|
||||
public static void WriteWhiteSpace(this Stream stream)
|
||||
{
|
||||
stream.WriteByte(WhiteSpace);
|
||||
|
@@ -1,8 +1,6 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TextPositioning;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
@@ -71,8 +69,9 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
var hex = BitConverter.ToString(Bytes.ToArray()).Replace("-", string.Empty);
|
||||
stream.WriteText($"<{hex}> {Symbol}");
|
||||
stream.WriteHex(Bytes);
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteText(Symbol);
|
||||
stream.WriteNewLine();
|
||||
}
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Move to the next line and show a text string, using the first number as the word spacing and the second as the character spacing
|
||||
/// Move to the next line and show a text string, using the first number as the word spacing and the second as the character spacing.
|
||||
/// </summary>
|
||||
public class MoveToNextLineShowTextWithSpacing : IGraphicsStateOperation
|
||||
{
|
||||
@@ -84,7 +84,21 @@
|
||||
/// <inheritdoc />
|
||||
public void Write(Stream stream)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
stream.WriteDecimal(WordSpacing);
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteDecimal(CharacterSpacing);
|
||||
stream.WriteWhiteSpace();
|
||||
|
||||
if (Bytes != null)
|
||||
{
|
||||
stream.WriteHex(Bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.WriteText($"({Text})");
|
||||
}
|
||||
|
||||
stream.WriteNewLine();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
@@ -1,6 +1,5 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Operations.TextShowing
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using IO;
|
||||
using Util;
|
||||
@@ -72,12 +71,15 @@
|
||||
/// <inheritdoc />
|
||||
public void Write(Stream stream)
|
||||
{
|
||||
if (Text == null && Bytes != null)
|
||||
if (Bytes != null)
|
||||
{
|
||||
throw new NotImplementedException("Support for writing hex not done yet.");
|
||||
stream.WriteHex(Bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.WriteText($"({Text})");
|
||||
}
|
||||
|
||||
stream.WriteText($"({Text})");
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteText(Symbol);
|
||||
stream.WriteNewLine();
|
||||
|
Reference in New Issue
Block a user