Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DpiRelativeToBounds option #83

Merged
merged 12 commits into from
Jun 27, 2024
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ body:
attributes:
label: PDFtoImage version
description: Which version of PDFtoImage is affected?
value: 4.0.1
value: 4.0.2
validations:
required: true
- type: dropdown
Expand All @@ -33,7 +33,7 @@ body:
attributes:
label: OS version
description: Which operating system version was used?
placeholder: e.g. Windows 11 23H2, Ubuntu 23.10, macOS 14.1, Android 14, ...
placeholder: e.g. Windows 11 24H2, Ubuntu 24.04, macOS 14.5, Android 14, ...
validations:
required: false
- type: dropdown
Expand All @@ -58,6 +58,7 @@ body:
- .NET (Core)
- .NET Framework
- Mono
- Unity
- (Other)
default: 0
validations:
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>PDFtoImage.Console</AssemblyName>
<RootNamespace>PDFtoImage.Console</RootNamespace>
<StartupObject>PDFtoImage.Console.Program</StartupObject>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
<Configurations>Debug;Release;ReleaseSigned</Configurations>
</PropertyGroup>

Expand Down
438 changes: 223 additions & 215 deletions src/FrameworkTests/Unity2D/Assembly-CSharp.csproj

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/FrameworkTests/Unity2D/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"dependencies": {
"com.unity.collab-proxy": "2.2.0",
"com.unity.feature.2d": "2.0.0",
"com.unity.ide.rider": "3.0.27",
"com.unity.collab-proxy": "2.3.1",
"com.unity.feature.2d": "2.0.1",
"com.unity.ide.rider": "3.0.28",
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.6",
"com.unity.ugui": "1.0.0",
"com.unity.visualscripting": "1.9.1",
"com.unity.visualscripting": "1.9.4",
"de.sungaila.pdftoimage": "file:X:/Repos/sungaila/PDFtoImage/etc/UnityPackage",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
32 changes: 16 additions & 16 deletions src/FrameworkTests/Unity2D/Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"dependencies": {
"com.unity.2d.animation": {
"version": "9.0.4",
"version": "9.1.1",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.common": "8.0.1",
"com.unity.2d.common": "8.0.2",
"com.unity.2d.sprite": "1.0.0",
"com.unity.collections": "1.1.0",
"com.unity.modules.animation": "1.0.0",
Expand All @@ -14,7 +14,7 @@
"url": "https://packages.unity.com"
},
"com.unity.2d.aseprite": {
"version": "1.1.0",
"version": "1.1.4",
"depth": 1,
"source": "registry",
"dependencies": {
Expand Down Expand Up @@ -46,11 +46,11 @@
"url": "https://packages.unity.com"
},
"com.unity.2d.psdimporter": {
"version": "8.0.3",
"version": "8.0.5",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.animation": "9.0.4",
"com.unity.2d.animation": "9.1.1",
"com.unity.2d.common": "8.0.2",
"com.unity.2d.sprite": "1.0.0"
},
Expand All @@ -63,12 +63,12 @@
"dependencies": {}
},
"com.unity.2d.spriteshape": {
"version": "9.0.2",
"version": "9.0.3",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.1.0",
"com.unity.2d.common": "8.0.1",
"com.unity.2d.common": "8.0.2",
"com.unity.modules.physics2d": "1.0.0"
},
"url": "https://packages.unity.com"
Expand All @@ -95,7 +95,7 @@
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.8.12",
"version": "1.8.15",
"depth": 3,
"source": "registry",
"dependencies": {
Expand All @@ -105,7 +105,7 @@
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": {
"version": "2.2.0",
"version": "2.3.1",
"depth": 0,
"source": "registry",
"dependencies": {},
Expand All @@ -129,22 +129,22 @@
"url": "https://packages.unity.com"
},
"com.unity.feature.2d": {
"version": "2.0.0",
"version": "2.0.1",
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.2d.animation": "9.0.4",
"com.unity.2d.animation": "9.1.1",
"com.unity.2d.pixel-perfect": "5.0.3",
"com.unity.2d.psdimporter": "8.0.3",
"com.unity.2d.psdimporter": "8.0.5",
"com.unity.2d.sprite": "1.0.0",
"com.unity.2d.spriteshape": "9.0.2",
"com.unity.2d.spriteshape": "9.0.3",
"com.unity.2d.tilemap": "1.0.0",
"com.unity.2d.tilemap.extras": "3.1.2",
"com.unity.2d.aseprite": "1.1.0"
"com.unity.2d.aseprite": "1.1.4"
}
},
"com.unity.ide.rider": {
"version": "3.0.27",
"version": "3.0.28",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down Expand Up @@ -210,7 +210,7 @@
}
},
"com.unity.visualscripting": {
"version": "1.9.1",
"version": "1.9.4",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/FrameworkTests/Unity2D/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.18f1
m_EditorVersionWithRevision: 2022.3.18f1 (d29bea25151d)
m_EditorVersion: 2022.3.34f1
m_EditorVersionWithRevision: 2022.3.34f1 (4886f5360533)
94 changes: 43 additions & 51 deletions src/PDFtoImage/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ public static SKBitmap ToImage(Stream pdfStream, bool leaveOpen = false, string?
if (options == default)
options = new();

// correct the width and height for the given dpi
// but only if both width and height are not specified (so the original sizes are corrected)
var correctFromDpi = options.Width == null && options.Height == null;

NativeMethods.FPDF renderFlags = default;

if (options.WithAnnotations)
Expand All @@ -347,16 +343,21 @@ public static SKBitmap ToImage(Stream pdfStream, bool leaveOpen = false, string?
if (page >= pdfDocument.PageSizes.Count)
throw new ArgumentOutOfRangeException(nameof(page), $"The page number {page} does not exist. Highest page number available is {pdfDocument.PageSizes.Count - 1}.");

var currentWidth = (float?)options.Width;
var currentHeight = (float?)options.Height;
var pageSize = pdfDocument.PageSizes[page];

// correct aspect ratio if requested
if (options.WithAspectRatio)
AdjustForAspectRatio(ref currentWidth, ref currentHeight, pageSize);

// Internals.PdfDocument -> Image
return pdfDocument.Render(page, currentWidth ?? pageSize.Width, currentHeight ?? pageSize.Height, options.Dpi, options.Dpi, options.Rotation, renderFlags, options.WithFormFill, correctFromDpi, options.BackgroundColor ?? SKColors.White, options.Bounds, options.UseTiling);
return pdfDocument.Render(
page,
options.Width,
options.Height,
options.Dpi,
options.Dpi,
options.Rotation,
renderFlags,
options.WithFormFill,
options.BackgroundColor ?? SKColors.White,
options.Bounds,
options.UseTiling,
options.WithAspectRatio,
options.DpiRelativeToBounds);
}

/// <summary>
Expand Down Expand Up @@ -564,10 +565,6 @@ public static IEnumerable<SKBitmap> ToImages(Stream pdfStream, bool leaveOpen =
if (options == default)
options = new();

// correct the width and height for the given dpi
// but only if both width and height are not specified (so the original sizes are corrected)
var correctFromDpi = options.Width == null && options.Height == null;

NativeMethods.FPDF renderFlags = default;

if (options.WithAnnotations)
Expand All @@ -585,16 +582,21 @@ public static IEnumerable<SKBitmap> ToImages(Stream pdfStream, bool leaveOpen =

for (int i = 0; i < pdfDocument.PageSizes.Count; i++)
{
var currentWidth = (float?)options.Width;
var currentHeight = (float?)options.Height;
var pageSize = pdfDocument.PageSizes[i];

// correct aspect ratio if requested
if (options.WithAspectRatio)
AdjustForAspectRatio(ref currentWidth, ref currentHeight, pageSize);

// Internals.PdfDocument -> Image
yield return pdfDocument.Render(i, currentWidth ?? pageSize.Width, currentHeight ?? pageSize.Height, options.Dpi, options.Dpi, options.Rotation, renderFlags, options.WithFormFill, correctFromDpi, options.BackgroundColor ?? SKColors.White, options.Bounds, options.UseTiling);
yield return pdfDocument.Render(
i,
options.Width,
options.Height,
options.Dpi,
options.Dpi,
options.Rotation,
renderFlags,
options.WithFormFill,
options.BackgroundColor ?? SKColors.White,
options.Bounds,
options.UseTiling,
options.WithAspectRatio,
options.DpiRelativeToBounds);
}
}

Expand Down Expand Up @@ -657,10 +659,6 @@ public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, b
if (options == default)
options = new();

// correct the width and height for the given dpi
// but only if both width and height are not specified (so the original sizes are corrected)
var correctFromDpi = options.Width == null && options.Height == null;

NativeMethods.FPDF renderFlags = default;

if (options.WithAnnotations)
Expand All @@ -680,16 +678,22 @@ public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, b
{
cancellationToken.ThrowIfCancellationRequested();

var currentWidth = (float?)options.Width;
var currentHeight = (float?)options.Height;
var pageSize = pdfDocument.PageSizes[i];

// correct aspect ratio if requested
if (options.WithAspectRatio)
AdjustForAspectRatio(ref currentWidth, ref currentHeight, pageSize);

// Internals.PdfDocument -> Image
yield return await Task.Run(() => pdfDocument.Render(i, currentWidth ?? pageSize.Width, currentHeight ?? pageSize.Height, options.Dpi, options.Dpi, options.Rotation, renderFlags, options.WithFormFill, correctFromDpi, options.BackgroundColor ?? SKColors.White, options.Bounds, options.UseTiling, cancellationToken), cancellationToken);
yield return await Task.Run(() => pdfDocument.Render(
i,
options.Width,
options.Height,
options.Dpi,
options.Dpi,
options.Rotation,
renderFlags,
options.WithFormFill,
options.BackgroundColor ?? SKColors.White,
options.Bounds,
options.UseTiling,
options.WithAspectRatio,
options.DpiRelativeToBounds,
cancellationToken), cancellationToken);
}
}
#endif
Expand Down Expand Up @@ -741,17 +745,5 @@ internal static void SaveImpl(Stream stream, SKEncodedImageFormat format, Stream
using var bitmap = ToImage(pdfStream, leaveOpen, password, page, options);
bitmap.Encode(stream, format, 100);
}

private static void AdjustForAspectRatio(ref float? width, ref float? height, SizeF pageSize)
{
if (width == null && height != null)
{
width = pageSize.Width / pageSize.Height * height.Value;
}
else if (width != null && height == null)
{
height = pageSize.Height / pageSize.Width * width.Value;
}
}
}
}
7 changes: 6 additions & 1 deletion src/PDFtoImage/IRenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ public interface IRenderOptions
SKColor? BackgroundColor { get; init; }

/// <summary>
/// Specifies the bounds for the page relative to <see cref="Conversion.GetPageSizes(string,string)"/>. This can be used for clipping (bounds inside of page) or additional margins (bounds outside of page).
/// Specifies the bounds for the page relative to <see cref="Conversion.GetPageSizes(string,string)"/>. This can be used for clipping (bounds inside of page) or additional margins (bounds outside of page). The bound units are relative to the PDF size (at 72 DPI).
/// </summary>
RectangleF? Bounds { get; init; }

/// <summary>
/// Specifies that the PDF should be rendered as several segments and merged into the final image. This can help in cases where the output image is too large, causing corrupted images (e.g. missing text) or crashes.
/// </summary>
bool UseTiling { get; init; }

/// <summary>
/// Specifies that <see cref="Dpi"/> and <see cref="WithAspectRatio"/> will be calculated relative to <see cref="Bounds"/> instead of the original PDF.
/// </summary>
bool DpiRelativeToBounds { get; init; }
}
}
Loading
Loading