Skip to content

Commit

Permalink
Fix the interactions between Bounds and Width/Height (ignoring DPI)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Mar 14, 2024
1 parent 87335f3 commit 744e66c
Show file tree
Hide file tree
Showing 38 changed files with 205 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/PDFtoImage/Internals/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
if (_disposed)
throw new ObjectDisposedException(GetType().Name);

var originalWidth = PageSizes[page].Width;
var originalHeight = PageSizes[page].Height;

if (rotate == PdfRotation.Rotate90 || rotate == PdfRotation.Rotate270)
{
(width, height) = (height, width);
(originalWidth, originalHeight) = (originalHeight, originalWidth);
(dpiX, dpiY) = (dpiY, dpiX);
}

Expand All @@ -84,6 +88,9 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
width *= dpiX / 72f;
height *= dpiY / 72f;

originalWidth *= dpiX / 72f;
originalHeight *= dpiY / 72f;

if (bounds != null)
{
bounds = new RectangleF(
Expand All @@ -97,10 +104,13 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

if (bounds != null)
{
var factorX = width / originalWidth;
var factorY = height / originalHeight;

if (rotate == PdfRotation.Rotate90)
{
bounds = new RectangleF(
width - bounds.Value.Height - bounds.Value.Y,
((originalWidth - bounds.Value.Height) * factorX) - bounds.Value.Y,
bounds.Value.X,
bounds.Value.Height,
bounds.Value.Width
Expand All @@ -110,16 +120,16 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp
{
bounds = new RectangleF(
bounds.Value.Y,
height - bounds.Value.Width - bounds.Value.X,
((originalHeight - bounds.Value.Width) * factorY) - bounds.Value.X,
bounds.Value.Height,
bounds.Value.Width
);
}
else if (rotate == PdfRotation.Rotate180)
{
bounds = new RectangleF(
width - bounds.Value.Width - bounds.Value.X,
height - bounds.Value.Height - bounds.Value.Y,
((originalWidth - bounds.Value.Width) * factorX) - bounds.Value.X,
((originalHeight - bounds.Value.Height) * factorY) - bounds.Value.Y,
bounds.Value.Width,
bounds.Value.Height
);
Expand All @@ -133,7 +143,7 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

if (!useTiling || (horizontalTileCount == 1 && verticalTileCount == 1))
{
bitmap = RenderSubset(_file!, page, width, height, rotate, flags, renderFormFill, backgroundColor, bounds, width, height, cancellationToken);
bitmap = RenderSubset(_file!, page, width, height, rotate, flags, renderFormFill, backgroundColor, bounds, originalWidth, originalHeight, cancellationToken);
}
else
{
Expand All @@ -143,8 +153,8 @@ public SKBitmap Render(int page, float width, float height, float dpiX, float dp

float currentTileWidth = width / horizontalTileCount;
float currentTileHeight = height / verticalTileCount;
float boundsWidthFactor = bounds != null ? bounds.Value.Width / width : 0f;
float boundsHeightFactor = bounds != null ? bounds.Value.Height / height : 0f;
float boundsWidthFactor = bounds != null ? bounds.Value.Width / originalWidth : 0f;
float boundsHeightFactor = bounds != null ? bounds.Value.Height / originalHeight : 0f;

using var canvas = new SKCanvas(bitmap);
canvas.Clear(backgroundColor);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/Tests/BoundsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,74 @@ public void WithWidthAndHeight(float x, float y, float width, float height, int?
CompareStreams(expectedPath, outputStream);
}

[TestMethod]
[DataRow(0f, 0f, 419.528f, 595.276f, default, default, default)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, default)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, default)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, default)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, default)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, default)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, default)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, default)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate0)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate0)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate90)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate90)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate180)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate180)]

[DataRow(0f, 0f, 419.528f, 595.276f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, default, default, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 419.528f, 595.276f, 500, 500, PdfRotation.Rotate270)]
[DataRow(0f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(209.764f, 0f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(0f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
[DataRow(209.764f, 297.638f, 209.764f, 297.638f, 500, 500, PdfRotation.Rotate270)]
public void WithWidthAndHeightAndRotation(float x, float y, float width, float height, int? outputWidth = null, int? outputHeight = null, PdfRotation? rotation = null)
{
var bounds = new RectangleF(x, y, width, height);
var expectedPath = Path.Combine("Assets", "Expected", GetPlatformAsString(), "Bounds", $"Wikimedia_Commons_web_{GetFileName(bounds, rotation, default, default)}_{outputWidth?.ToString() ?? "null"}x{outputHeight?.ToString() ?? "null"}.png");

using var inputStream = GetInputStream(Path.Combine("Assets", "Wikimedia_Commons_web.pdf"));
using var outputStream = CreateOutputStream(expectedPath);

SavePng(outputStream, inputStream, options: new(Width: outputWidth, Height: outputHeight, Bounds: bounds, Rotation: rotation ?? default));

CompareStreams(expectedPath, outputStream);
}

private static string GetFileName(RectangleF? input, PdfRotation? rotation, bool? withAnnotations, bool? withFormFill)
{
if (input == null)
Expand Down
Loading

0 comments on commit 744e66c

Please sign in to comment.