Compare commits

...

6 Commits

Author SHA1 Message Date
Eliot Jones
b5d90d1725 allow indirect references in xref streams as an attempt to fix #457 2022-05-29 16:26:56 -04:00
Eliot Jones
559f3af5f3 Merge pull request #459 from BobLd/master
Fix #458 - GrahamScan exception
2022-05-24 08:15:04 -04:00
BobLD
5eed9fd1bc Fix #458 Check if stack has element and invert sorting order in GrahamScan(), add tests 2022-05-24 07:47:55 +01:00
Eliot Jones
ddab53e456 remove stray debugging code 2022-05-10 10:14:36 -04:00
Eliot Jones
4e490d63be #444 handle invalid tounicode map objects in tt fonts 2022-05-02 16:17:05 -04:00
Eliot Jones
03692cf42f set version to alpha of 0.1.7 for future nightly builds 2022-04-25 10:06:46 -04:00
16 changed files with 132 additions and 34 deletions

View File

@@ -67,7 +67,7 @@
{
return new PdfPoint(X + dx, Y);
}
/// <summary>
/// Creates a new <see cref="PdfPoint"/> which is the current point moved in the y direction relative to its current position by a value.
/// </summary>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -528,6 +528,52 @@
}
}
};
public static IEnumerable<object[]> Issue458Data => new[]
{
new object[]
{
new PdfPoint[]
{
new PdfPoint(134.74199999999985, 1611.657),
new PdfPoint(277.58043749999985, 1611.657),
new PdfPoint(314.74199999999985, 1611.657),
new PdfPoint(507.0248828124999, 1611.657),
new PdfPoint(545.1419999999998, 1611.657),
new PdfPoint(632.9658281249997, 1611.657),
new PdfPoint(668.5709999999998, 1611.657),
new PdfPoint(831.3395078125002, 1611.657),
new PdfPoint(868.1139999999998, 1611.657),
new PdfPoint(892.8907187499999, 1611.657),
new PdfPoint(1010.0569999999999, 1611.6569999999997),
new PdfPoint(1046.2174003906248, 1611.7654812011715),
new PdfPoint(1010.0569999999999, 1611.657),
new PdfPoint(1046.2174003906248, 1611.7654812011717),
new PdfPoint(1085.145, 1611.8822639999996),
new PdfPoint(1255.484941406251, 1612.3932838242179),
new PdfPoint(1301.144, 1612.5302609999994),
new PdfPoint(1359.0006406250002, 1612.7038309218747),
new PdfPoint(1301.144, 1612.5302609999997),
new PdfPoint(1359.0006406250002, 1612.703830921875),
new PdfPoint(1400.915, 1612.8295739999996),
new PdfPoint(1505.379062499999, 1613.1429661874993),
new PdfPoint(1400.915, 1612.8295739999999),
new PdfPoint(1505.379062499999, 1613.1429661874995),
new PdfPoint(1543.886, 1613.2584869999996),
new PdfPoint(1600.9401015625003, 1613.4296493046872),
new PdfPoint(1641.597, 1613.5516199999997),
new PdfPoint(1764.5577421874998, 1613.9205022265612),
new PdfPoint(1641.597, 1613.55162),
new PdfPoint(1764.5577421874998, 1613.9205022265614)
},
new PdfPoint[]
{
new PdfPoint(134.74199999999985, 1611.657),
new PdfPoint(1010.0569999999999, 1611.6569999999997),
new PdfPoint(1764.5577421874998, 1613.9205022265614),
}
}
};
#endregion
[Fact]
@@ -570,7 +616,6 @@
}
}
[Theory]
[MemberData(nameof(MinimumAreaRectangleData))]
public void MinimumAreaRectangle(PdfPoint[] points, PdfPoint[] expected)
@@ -585,5 +630,51 @@
Assert.Equal(expected[i], mar[i], PointComparer);
}
}
[Theory]
[MemberData(nameof(Issue458Data))]
public void Issue458(PdfPoint[] points, PdfPoint[] expected)
{
/*
* https://github.com/UglyToad/PdfPig/issues/458
* An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Linq.dll: 'Specified argument was out of the range of valid values.'
* at System.Linq.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument)
* at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
*/
var result = GeometryExtensions.GrahamScan(points);
// Data is noisy so we just check it does not throw an exception
// and that key points are present (other points might be present,
// e.g. 'not enough equal dupplicates' or 'not collinear enough' points)
foreach (var point in expected)
{
Assert.Contains(point, result);
}
}
[Theory]
[MemberData(nameof(Issue458Data))]
public void Issue458_Inv(PdfPoint[] points, PdfPoint[] expected)
{
/*
* https://github.com/UglyToad/PdfPig/issues/458
* An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Linq.dll: 'Specified argument was out of the range of valid values.'
* at System.Linq.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument)
* at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
*/
var pointsInv = points.Select(p => new PdfPoint(p.Y, p.X)).ToArray();
var expectedInv = expected.Select(p => new PdfPoint(p.Y, p.X)).ToArray();
var result = GeometryExtensions.GrahamScan(pointsInv);
// Data is noisy so we just check it does not throw an exception
// and that key points are present (other points might be present,
// e.g. 'not enough equal dupplicates' or 'not collinear enough' points)
foreach (var point in expectedInv)
{
Assert.Contains(point, result);
}
}
}
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -1,7 +1,6 @@
namespace UglyToad.PdfPig.Encryption
{
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
@@ -29,11 +28,6 @@
var buffer = new byte[256];
if (data.Length > 256)
{
Debugger.Break();
}
using (var decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV))
using (var input = new MemoryStream(data))
using (var output = new MemoryStream())

View File

@@ -1,11 +1,11 @@
namespace UglyToad.PdfPig.Filters
{
using Core;
using System;
using System.Collections.Generic;
using System.Linq;
using Core;
using Tokens;
using UglyToad.PdfPig.Util;
using Util;
/// <inheritdoc />
/// <summary>

View File

@@ -269,11 +269,12 @@
double polarAngle(PdfPoint point1, PdfPoint point2)
{
// This is used for grouping, we could use Math.Round()
return Math.Atan2(point2.Y - point1.Y, point2.X - point1.X) % Math.PI;
}
Stack<PdfPoint> stack = new Stack<PdfPoint>();
var sortedPoints = points.OrderBy(p => p.Y).ThenBy(p => p.X).ToList();
var stack = new Stack<PdfPoint>();
var sortedPoints = points.OrderBy(p => p.X).ThenBy(p => p.Y).ToList();
var P0 = sortedPoints[0];
var groups = sortedPoints.Skip(1).GroupBy(p => polarAngle(P0, p)).OrderBy(g => g.Key);
@@ -309,7 +310,7 @@
for (int i = 2; i < sortedPoints.Count; i++)
{
var point = sortedPoints[i];
while (!ccw(stack.ElementAt(1), stack.Peek(), point))
while (stack.Count > 1 && !ccw(stack.ElementAt(1), stack.Peek(), point))
{
stack.Pop();
}

View File

@@ -269,7 +269,7 @@
return false;
}
xrefTablePart = crossReferenceStreamParser.Parse(objByteOffset, fromTableAtOffset, objectStream);
xrefTablePart = crossReferenceStreamParser.Parse(objByteOffset, fromTableAtOffset, objectStream, pdfScanner);
return true;
}

View File

@@ -4,6 +4,7 @@
using Core;
using Filters;
using PdfPig.CrossReference;
using Tokenization.Scanner;
using Tokens;
using Util;
@@ -19,9 +20,9 @@
/// <summary>
/// Parses through the unfiltered stream and populates the xrefTable HashMap.
/// </summary>
public CrossReferenceTablePart Parse(long streamOffset, long? fromTableAtOffset, StreamToken stream)
public CrossReferenceTablePart Parse(long streamOffset, long? fromTableAtOffset, StreamToken stream, IPdfTokenScanner pdfTokenScanner)
{
var decoded = stream.Decode(filterProvider);
var decoded = stream.DecodeWithLookup(filterProvider, pdfTokenScanner);
var fieldSizes = new CrossReferenceStreamFieldSize(stream.StreamDictionary);

View File

@@ -45,14 +45,18 @@
}
return typedToken;
}
}
/// <summary>
/// Get the decoded data from this stream.
/// </summary>
public static IReadOnlyList<byte> Decode(this StreamToken stream, IFilterProvider filterProvider)
=> DecodeWithLookup(stream, filterProvider, null);
internal static IReadOnlyList<byte> DecodeWithLookup(this StreamToken stream, IFilterProvider filterProvider, IPdfTokenScanner pdfTokenScanner)
{
var filters = filterProvider.GetFilters(stream.StreamDictionary);
var filters = pdfTokenScanner != null
? new FilterProviderWithLookup(filterProvider).GetFilters(stream.StreamDictionary, pdfTokenScanner)
: filterProvider.GetFilters(stream.StreamDictionary);
var transform = stream.Data;
for (var i = 0; i < filters.Count; i++)

View File

@@ -107,18 +107,25 @@
CMap toUnicodeCMap = null;
if (dictionary.TryGet(NameToken.ToUnicode, out var toUnicodeObj))
{
var toUnicode = DirectObjectFinder.Get<StreamToken>(toUnicodeObj, pdfScanner);
var decodedUnicodeCMap = toUnicode.Decode(filterProvider, pdfScanner);
if (decodedUnicodeCMap != null)
try
{
toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap));
var toUnicode = DirectObjectFinder.Get<StreamToken>(toUnicodeObj, pdfScanner);
var decodedUnicodeCMap = toUnicode.Decode(filterProvider, pdfScanner);
if (decodedUnicodeCMap != null)
{
toUnicodeCMap = CMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap));
}
}
catch (Exception ex)
{
log.Error("Failed to decode ToUnicode CMap for a TrueType font in file due to an exception.", ex);
}
}
Encoding encoding = encodingReader.Read(dictionary, descriptor);
if (encoding == null && font?.TableRegister?.CMapTable != null
&& font.TableRegister.PostScriptTable?.GlyphNames != null)
{
@@ -199,7 +206,7 @@
$"Expected a TrueType font in the TrueType font descriptor, instead it was {descriptor.FontFile.FileType}.");
}
}
var font = TrueTypeFontParser.Parse(new TrueTypeDataBytes(new ByteArrayInputBytes(fontFile)));
return font;

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net451;net452;net46;net461;net462;net47;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<IsTestProject>False</IsTestProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>

View File

@@ -11,7 +11,7 @@
<PackageTags>PDF;Reader;Document;Adobe;PDFBox;PdfPig;pdf-extract;pdf-to-text;pdf;file;text;C#;dotnet;.NET</PackageTags>
<RepositoryUrl>https://github.com/UglyToad/PdfPig</RepositoryUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.1.6</Version>
<Version>0.1.7-alpha-001</Version>
<AssemblyVersion>0.1.5.0</AssemblyVersion>
<FileVersion>0.1.5.0</FileVersion>
<PackageIconUrl>https://raw.githubusercontent.com/UglyToad/PdfPig/master/documentation/pdfpig.png</PackageIconUrl>