Resolving page sizes did not work when the page orientation was landscape, or when

side lengths were not integer or off by one. Added unit tests.
This commit is contained in:
mvantzet 2023-03-09 16:09:14 +01:00
parent 999f9ee7dc
commit 3a0a6e1411
3 changed files with 24 additions and 6 deletions

View File

@ -1,9 +1,11 @@
namespace UglyToad.PdfPig.Tests.Geometry
{
using Content;
using PdfPig.Geometry;
using PdfPig.Core;
using Xunit;
using System.Collections.Generic;
using System.Drawing;
public class PdfRectangleTests
{
@ -1694,5 +1696,18 @@
Assert.True(rect.Height > 0);
}
[Theory]
[InlineData(595, 842, PageSize.A4)]
[InlineData(842, 595, PageSize.A4)]
[InlineData(595.3, 841.5, PageSize.A4)]
[InlineData(841.5, 595.3, PageSize.A4)]
[InlineData(1224, 792, PageSize.Ledger)]
[InlineData(792, 1224, PageSize.Tabloid)]
public void Parse(double w, double h, PageSize expectedPageSize)
{
var r = new PdfRectangle(0, 0, w, h);
Assert.Equal(expectedPageSize, r.GetPageSize());
}
}
}

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using Core;
using System;
/// <summary>
/// The corresponding named size of the <see cref="Page"/>.
@ -102,6 +103,7 @@
{new WidthHeight(74, 105), PageSize.A10},
{new WidthHeight(612, 792), PageSize.Letter},
{new WidthHeight(612, 1008), PageSize.Legal},
// Ledger and Tabloid differ by orientation
{new WidthHeight(1224, 792), PageSize.Ledger},
{new WidthHeight(792, 1224), PageSize.Tabloid},
// Again there is disagreement here
@ -111,11 +113,11 @@
public static PageSize GetPageSize(this PdfRectangle rectangle)
{
if (!Lookup.TryGetValue(new WidthHeight(rectangle.Width, rectangle.Height), out var size))
if (!Lookup.TryGetValue(new WidthHeight(rectangle.Width, rectangle.Height), out var size)
&& !Lookup.TryGetValue(new WidthHeight(rectangle.Height, rectangle.Width), out size))
{
return PageSize.Custom;
}
return size;
}
@ -148,15 +150,15 @@
public override bool Equals(object obj)
{
return obj is WidthHeight height &&
Width == height.Width &&
Height == height.Height;
Math.Abs(Width - height.Width) < 1 &&
Math.Abs(Height - height.Height) < 1;
}
public override int GetHashCode()
{
var hashCode = 859600377;
hashCode = hashCode * -1521134295 + Width.GetHashCode();
hashCode = hashCode * -1521134295 + Height.GetHashCode();
hashCode = hashCode * -1521134295 + Math.Round(Width).GetHashCode();
hashCode = hashCode * -1521134295 + Math.Round(Height).GetHashCode();
return hashCode;
}
}

View File

@ -178,6 +178,7 @@
DirectObjectFinder.Get<NumericToken>(array[3], tokenScanner).Double);
}
// TODO: Can this be removed? Seems unused but is public method
public static PdfRectangle ToIntRectangle(this ArrayToken array, IPdfTokenScanner tokenScanner)
{
if (array == null)