Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(presets): store custom sizing as its own object so older versions keep the file

choksi221286c0fe7c20f8
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
index 919abf3e..74ad3c2f 100644
--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs
+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs
@@ -113,7 +113,7 @@ public sealed partial class PresetEditorViewModel : ObservableObject
_name = string.IsNullOrWhiteSpace(_edit.Name) ? loc.DisplayName(_edit) : _edit.Name;
_target = Targets.FirstOrDefault(t => t.Value == _edit.TargetFormat) ?? Targets[1];
- _resize = ResizeModes.First(r => r.Value == _edit.Image.Resize);
+ _resize = ResizeModes.First(r => r.Value == (_edit.Image.CustomSize is null ? _edit.Image.Resize : ResizeMode.Custom));
_rotation = Rotations.FirstOrDefault(r => r.Value == _edit.Image.Rotation) ?? Rotations[0];
_flip = Flips.FirstOrDefault(f => f.Value == _edit.Image.Flip) ?? Flips[0];
_pageSize = PageSizes.First(p => p.Value == _edit.Pdf.PageSize);
@@ -160,9 +160,9 @@ public sealed partial class PresetEditorViewModel : ObservableObject
_resizePercent = _edit.Image.ResizePercent;
_longEdge = _edit.Image.LongEdge;
_width = _edit.Image.Width;
- _customWidth = _edit.Image.CustomWidth;
- _customHeight = _edit.Image.CustomHeight;
- _lockAspect = _edit.Image.LockAspect;
+ _customWidth = _edit.Image.CustomSize?.Width ?? 1920;
+ _customHeight = _edit.Image.CustomSize?.Height ?? 1080;
+ _lockAspect = _edit.Image.CustomSize?.LockAspect ?? false;
_setDpi = _edit.Image.Dpi.HasValue;
_dpi = _edit.Image.Dpi ?? 300;
_keepMetadata = _edit.Image.KeepMetadata;
@@ -366,7 +366,7 @@ public sealed partial class PresetEditorViewModel : ObservableObject
}
}
- [ObservableProperty, NotifyPropertyChangedFor(nameof(ShowPercent), nameof(ShowLongEdge), nameof(ShowWidth))]
+ [ObservableProperty, NotifyPropertyChangedFor(nameof(ShowPercent), nameof(ShowLongEdge), nameof(ShowWidth), nameof(ShowCustom))]
private Choice<ResizeMode> _resize;
[ObservableProperty] private Choice<ImageRotation> _rotation;
@@ -664,7 +664,19 @@ public sealed partial class PresetEditorViewModel : ObservableObject
_original.TargetFormat = TargetId;
var image = _original.Image;
image.Quality = Math.Clamp(Quality, 1, 100);
- image.Resize = Resize.Value;
+ if (Resize.Value == ResizeMode.Custom)
+ {
+ // Custom sizing is stored as CustomSize with Resize None, never as a resize name an older version rejects
+ // (it would read presets.json as broken and fall back to the built-in presets, and the next save would
+ // write those over the user's own; issue #576).
+ image.Resize = ResizeMode.None;
+ image.CustomSize = new CustomImageSize { Width = Math.Clamp(CustomWidth, 16, 16384), Height = Math.Clamp(CustomHeight, 16, 16384), LockAspect = LockAspect };
+ }
+ else
+ {
+ image.Resize = Resize.Value;
+ image.CustomSize = null;
+ }
// Only images and PDFs are turned; for any other target a rotation left over from an earlier target would make
// every file fail ("can only turn images") with nothing on screen to turn it off.
image.Rotation = CanTurn ? Rotation.Value : ImageRotation.None;
@@ -672,9 +684,6 @@ public sealed partial class PresetEditorViewModel : ObservableObject
image.ResizePercent = Math.Clamp(ResizePercent, 1, 1000);
image.LongEdge = Math.Clamp(LongEdge, 16, 20000);
image.Width = Math.Clamp(Width, 16, 20000);
- image.CustomWidth = Math.Clamp(CustomWidth, 16, 16384);
- image.CustomHeight = Math.Clamp(CustomHeight, 16, 16384);
- image.LockAspect = LockAspect;
image.Dpi = SetDpi ? Math.Clamp(Dpi, 36, 2400) : null;
image.KeepMetadata = KeepMetadata;
image.KeepColorProfile = KeepColorProfile;
diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.cs
index 76b4ef1a..2b6388ea 100644
--- a/src/Filee.Core/Presets/Preset.cs
+++ b/src/Filee.Core/Presets/Preset.cs
@@ -60,6 +60,18 @@ public sealed class PresetKindJsonConverter : LenientEnumJsonConverter<PresetKin
protected override PresetKind Unknown => PresetKind.Group;
}
+/// <summary>
+/// Reads like the lenient converter and, when writing, never emits <see cref="ResizeMode.Custom"/> (an editor-only
+/// choice): a preset with custom sizing stores Resize None plus CustomSize, so a version without the mode keeps
+/// reading presets.json instead of rejecting it as broken and overwriting the user's presets with the built-in
+/// ones (issue #576).
+/// </summary>
+public sealed class ResizeModeJsonConverter : LenientEnumJsonConverter<ResizeMode>
+{
+ public override void Write(Utf8JsonWriter writer, ResizeMode value, JsonSerializerOptions options) =>
+ writer.WriteStringValue((value == ResizeMode.Custom ? ResizeMode.None : value).ToString());
+}
+
/// <summary>
/// A named conversion recipe. The donut toolbar shows presets; dropping files on one runs it.
/// </summary>
@@ -266,8 +278,10 @@ public enum ResizeMode
/// <summary>Scale to <see cref="ImageOptions.Width"/> pixels wide, height in proportion (never upscales).</summary>
Width,
/// <summary>
- /// Scale to <see cref="ImageOptions.CustomWidth"/> x <see cref="ImageOptions.CustomHeight"/> pixels: stretched to exactly that
- /// size, or with <see cref="ImageOptions.LockAspect"/> scaled (up or down) to the largest size that fits inside.
+ /// Scale to the free width x height in <see cref="ImageOptions.CustomSize"/>: stretched to exactly that size, or
+ /// with the aspect kept, scaled (up or down) to the largest size that fits inside (issue #576). An editor choice
+ /// only: a preset with custom sizing stores <see cref="ImageOptions.Resize"/> None plus
+ /// <see cref="ImageOptions.CustomSize"/>, so a version without the mode still reads the file.
/// </summary>
Custom,
}
@@ -316,6 +330,13 @@ public sealed class ImageOptions
/// <summary>Lossy quality 1-100 (JPG, WEBP, AVIF, HEIC).</summary>
public int Quality { get; set; } = 90;
+ /// <summary>
+ /// How the picture is scaled. A preset with <see cref="CustomSize"/> set stores None here (custom sizing wins),
+ /// so a version without custom sizing simply does not resize instead of rejecting a name it does not know. The
+ /// lenient converter also reads a name a future version might add as None, and the writer maps Custom down to
+ /// None (see <see cref="ResizeModeJsonConverter"/>).
+ /// </summary>
+ [JsonConverter(typeof(ResizeModeJsonConverter))]
public ResizeMode Resize { get; set; } = ResizeMode.None;
/// <summary>Turns the image (after its EXIF orientation is applied).</summary>
@@ -339,17 +360,14 @@ public sealed class ImageOptions
/// <summary>Target width in pixels when <see cref="Resize"/> is <see cref="ResizeMode.Width"/>.</summary>
public int Width { get; set; } = 1200;
- /// <summary>Target width in pixels when <see cref="Resize"/> is <see cref="ResizeMode.Custom"/> (issue #576).</summary>
- public int CustomWidth { get; set; } = 1920;
-
- /// <summary>Target height in pixels when <see cref="Resize"/> is <see cref="ResizeMode.Custom"/> (issue #576).</summary>
- public int CustomHeight { get; set; } = 1080;
-
/// <summary>
- /// With <see cref="ResizeMode.Custom"/>: keep each file's own shape and scale it to the largest size that fits inside
- /// <see cref="CustomWidth"/> x <see cref="CustomHeight"/>, instead of stretching it to exactly that size.
+ /// Free width x height resizing (issue #576): stretched to exactly that size, or with
+ /// <see cref="CustomImageSize.LockAspect"/> scaled (up or down) to the largest size that fits inside. When set it
+ /// wins over <see cref="Resize"/>; null leaves <see cref="Resize"/> in charge. Stored as one object so a
+ /// presets.json written by this version never carries a resize name an older version rejects: older versions skip
+ /// the unknown property and simply do not resize.
/// </summary>
- public bool LockAspect { get; set; }
+ public CustomImageSize? CustomSize { get; set; }
/// <summary>Output DPI written into the file. <c>null</c> keeps the source value.</summary>
public int? Dpi { get; set; }
@@ -453,10 +471,29 @@ public sealed class ImageOptions
{
var copy = (ImageOptions)MemberwiseClone();
copy.IcoSizes = [.. IcoSizes];
+ copy.CustomSize = CustomSize is { } size
+ ? new CustomImageSize { Width = size.Width, Height = size.Height, LockAspect = size.LockAspect }
+ : null;
return copy;
}
}
+/// <summary>The free width x height a preset resizes to: <see cref="ImageOptions.CustomSize"/> (issue #576).</summary>
+public sealed class CustomImageSize
+{
+ /// <summary>Target width in pixels.</summary>
+ public int Width { get; set; } = 1920;
+
+ /// <summary>Target height in pixels.</summary>
+ public int Height { get; set; } = 1080;
+
+ /// <summary>
+ /// Keep each file's own shape and scale it to the largest size that fits inside Width x Height, instead of
+ /// stretching it to exactly that size.
+ /// </summary>
+ public bool LockAspect { get; set; }
+}
+
/// <summary>Page size used when images are placed into a PDF.</summary>
public enum PdfPageSize
{
diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs
index c2b2d9eb..a494738a 100644
--- a/src/Filee.Engines/Magick/ImageEncoder.cs
+++ b/src/Filee.Engines/Magick/ImageEncoder.cs
@@ -199,16 +199,22 @@ internal static class ImageEncoder
}
/// <summary>
- /// How much <see cref="ApplyOptions"/> resizes a <paramref name="width"/> × <paramref name="height"/> image before
- /// <see cref="FitFormat"/> (1 when it does not).
+ /// How much <see cref="ApplyOptions"/> resizes a width x height image before <see cref="FitFormat"/> (1 when it
+ /// does not). Custom sizing with the aspect kept takes one scale for both sides; stretched, the height's factor
+ /// is what a tall run of pages has to fit.
/// </summary>
- public static double ResizeScale(double width, double height, ImageOptions options) => options.Resize switch
- {
- ResizeMode.Percent when options.ResizePercent > 0 => options.ResizePercent / 100.0,
- ResizeMode.LongEdge when options.LongEdge > 0 => Math.Min(1, options.LongEdge / Math.Max(width, height)),
- ResizeMode.Width when options.Width > 0 => Math.Min(1, options.Width / width),
- _ => 1,
- };
+ public static double ResizeScale(double width, double height, ImageOptions options) =>
+ options.CustomSize is { } custom
+ ? custom.LockAspect
+ ? Math.Min(custom.Width / width, custom.Height / height)
+ : custom.Height / height
+ : options.Resize switch
+ {
+ ResizeMode.Percent when options.ResizePercent > 0 => options.ResizePercent / 100.0,
+ ResizeMode.LongEdge when options.LongEdge > 0 => Math.Min(1, options.LongEdge / Math.Max(width, height)),
+ ResizeMode.Width when options.Width > 0 => Math.Min(1, options.Width / width),
+ _ => 1,
+ };
/// <summary>Turns and mirrors an image as <paramref name="options"/> say (part of <see cref="ApplyOptions"/>).</summary>
public static void Turn(IMagickImage<byte> image, ImageOptions options)
@@ -278,7 +284,24 @@ internal static class ImageEncoder
image.SetProfile(exif);
}
- switch (options.Resize)
+ if (options.CustomSize is { } custom)
+ {
+ if (custom.LockAspect)
+ {
+ // The file's own shape is kept: scaled up or down to the largest size that fits inside the box. Both
+ // sides come from one scale, so the aspect already matches and Resize keeps it, as it always does.
+ var scale = Math.Min(custom.Width / (double)image.Width, custom.Height / (double)image.Height);
+ if (scale != 1)
+ image.Resize((uint)Math.Max(1, Math.Round(image.Width * scale)), (uint)Math.Max(1, Math.Round(image.Height * scale)));
+ }
+ else if (image.Width != (uint)custom.Width || image.Height != (uint)custom.Height)
+ {
+ // No lock: exactly the asked size, stretching or squeezing the picture on purpose (issue #576). Resize
+ // fits inside a box and keeps the aspect on its own, so the flag is what makes this a real stretch.
+ image.Resize(new MagickGeometry((uint)custom.Width, (uint)custom.Height) { IgnoreAspectRatio = true });
+ }
+ }
+ else switch (options.Resize)
{
case ResizeMode.Percent when options.ResizePercent is > 0 and not 100:
image.Resize(new Percentage(options.ResizePercent));
@@ -290,21 +313,6 @@ internal static class ImageEncoder
case ResizeMode.Width when options.Width > 0 && image.Width > options.Width:
image.Resize((uint)options.Width, (uint)Math.Max(1, Math.Round(image.Height * (double)options.Width / image.Width)));
break;
- case ResizeMode.Custom when options.CustomWidth > 0 && options.CustomHeight > 0 && options.LockAspect:
- {
- // The file's own shape is kept: scaled up or down to the largest size that fits inside the box. Both
- // sides come from one scale, so the aspect already matches and Resize keeps it, as it always does.
- var scale = Math.Min(options.CustomWidth / (double)image.Width, options.CustomHeight / (double)image.Height);
- if (scale != 1)
- image.Resize((uint)Math.Max(1, Math.Round(image.Width * scale)), (uint)Math.Max(1, Math.Round(image.Height * scale)));
- break;
- }
- case ResizeMode.Custom when options.CustomWidth > 0 && options.CustomHeight > 0:
- // No lock: exactly the asked size, stretching or squeezing the picture on purpose (issue #576). Resize
- // fits inside a box and keeps the aspect on its own, so the flag is what makes this a real stretch.
- if (image.Width != (uint)options.CustomWidth || image.Height != (uint)options.CustomHeight)
- image.Resize(new MagickGeometry((uint)options.CustomWidth, (uint)options.CustomHeight) { IgnoreAspectRatio = true });
- break;
}
FitFormat(image, targetFormat);
diff --git a/src/Filee.Engines/Vector/SvgRenderer.cs b/src/Filee.Engines/Vector/SvgRenderer.cs
index be7e3529..47c81862 100644
--- a/src/Filee.Engines/Vector/SvgRenderer.cs
+++ b/src/Filee.Engines/Vector/SvgRenderer.cs
@@ -165,7 +165,16 @@ internal static class SvgRenderer
var longEdge = Math.Max(bounds.Width, bounds.Height);
var dpi = Math.Clamp(pdf.RenderDpi, 36, 1200);
var scale = Math.Max(dpi / CssDpi, MinimumLongEdge / longEdge);
- switch (image.Resize)
+ if (image.CustomSize is { } custom)
+ {
+ // Render at the size the custom resize asks for, so the box is not reached by stretching a default-size
+ // bitmap, which comes out soft. With the lock, at the fit-inside scale; without, at the larger side's
+ // scale, so the stretch that follows only ever shrinks the other side.
+ scale = custom.LockAspect
+ ? Math.Min(custom.Width / bounds.Width, custom.Height / bounds.Height)
+ : Math.Max(custom.Width / bounds.Width, custom.Height / bounds.Height);
+ }
+ else switch (image.Resize)
{
case ResizeMode.LongEdge when image.LongEdge > 0:
scale = image.LongEdge / longEdge;
diff --git a/src/Filee.Engines/Vector/VectorConverter.cs b/src/Filee.Engines/Vector/VectorConverter.cs
index 7d2a7459..550c649b 100644
--- a/src/Filee.Engines/Vector/VectorConverter.cs
+++ b/src/Filee.Engines/Vector/VectorConverter.cs
@@ -120,11 +120,11 @@ public sealed class VectorConverter : IConverter, IImageTransformer
ct.ThrowIfCancellationRequested();
progress?.Report(0.6);
- // The drawing was rendered at its final size, so the preset's resize must not scale the bitmap again. A custom
- // width x height is different: it is an exact stretch of whatever the render produced, so it applies afterwards.
+ // The drawing was rendered at its final size, so the preset's resize must not scale the bitmap again. Custom
+ // sizing rides on CustomSize, not on Resize: with the lock the raster already fits the box, without it this is
+ // the exact stretch of what the render produced.
var options = step.Preset.Image.Clone();
- if (options.Resize is not Core.Presets.ResizeMode.Custom)
- options.Resize = Core.Presets.ResizeMode.None;
+ options.Resize = Core.Presets.ResizeMode.None;
ImageEncoder.ApplyOptions(image, options, step.To);
return ImageEncoder.Write([image], step.To, step.Output, options);
}
diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.cs
index 9e4aa783..d0a592cb 100644
--- a/tests/Filee.App.Tests/RenderTests.cs
+++ b/tests/Filee.App.Tests/RenderTests.cs
@@ -1147,6 +1147,66 @@ public class RenderTests
Assert.Equal("jpg", preset.TargetFormat);
}
+ [AvaloniaTheory]
+ [InlineData("en")]
+ [InlineData("de")]
+ [InlineData("ru")]
+ public void Custom_resize_shows_its_fields_and_never_writes_a_name_older_versions_reject(string language)
+ {
+ TestServices.EnsureInitialized(language);
+ var loc = AppHost.Get<ILocalizer>();
+ var preset = Filee.Core.Presets.BuiltInData.CreatePresets().Single(p => p.Id == "jpg-70");
+ var vm = new PresetEditorViewModel(preset, loc);
+
+ vm.Resize = vm.ResizeModes.Single(r => r.Value == Filee.Core.Presets.ResizeMode.Percent);
+ Assert.False(vm.ShowCustom);
+
+ var raised = new List<string>();
+ vm.PropertyChanged += (_, e) => raised.Add(e.PropertyName!);
+ vm.Resize = vm.ResizeModes.Single(r => r.Value == Filee.Core.Presets.ResizeMode.Custom);
+ Assert.True(vm.ShowCustom);
+ Assert.Contains(nameof(vm.ShowCustom), raised); // the boxes appear without reopening the editor
+
+ vm.CustomWidth = 777;
+ vm.CustomHeight = 333;
+ vm.LockAspect = true;
+ var window = new PresetEditorWindow { DataContext = vm, Height = 900 };
+ window.Show();
+ Pump();
+ Save(window, $"preset-custom-{language}.png");
+ window.Close();
+
+ Assert.True(vm.Apply());
+ // Stored as customSize with resize None, so an older Filee keeps reading presets.json instead of treating it
+ // as broken and overwriting the user's presets with the built-in ones (issue #576).
+ Assert.Equal(Filee.Core.Presets.ResizeMode.None, preset.Image.Resize);
+ }
+
+ [AvaloniaFact]
+ public void Custom_resize_stores_a_size_that_switching_the_mode_clears()
+ {
+ TestServices.EnsureInitialized("en");
+ var loc = AppHost.Get<ILocalizer>();
+ var preset = Filee.Core.Presets.BuiltInData.CreatePresets().Single(p => p.Id == "jpg-70");
+ var vm = new PresetEditorViewModel(preset, loc);
+ vm.Resize = vm.ResizeModes.Single(r => r.Value == Filee.Core.Presets.ResizeMode.Custom);
+ vm.CustomWidth = 777;
+ vm.CustomHeight = 333;
+ vm.LockAspect = true;
+ Assert.True(vm.Apply());
+ Assert.NotNull(preset.Image.CustomSize);
+ Assert.Equal((777, 333, true), (preset.Image.CustomSize!.Width, preset.Image.CustomSize!.Height, preset.Image.CustomSize.LockAspect));
+
+ // Reads back as Custom, and switching the mode clears the custom size.
+ var reloaded = new PresetEditorViewModel(preset, loc);
+ Assert.Equal(Filee.Core.Presets.ResizeMode.Custom, reloaded.Resize.Value);
+ Assert.Equal(333, reloaded.CustomHeight);
+ vm.Resize = vm.ResizeModes.Single(r => r.Value == Filee.Core.Presets.ResizeMode.Percent);
+ Assert.True(vm.Apply());
+ Assert.Null(preset.Image.CustomSize);
+ Assert.Equal(Filee.Core.Presets.ResizeMode.Percent, preset.Image.Resize);
+ }
+
[AvaloniaTheory]
[InlineData("en", false)]
[InlineData("ru", false)]
diff --git a/tests/Filee.Core.Tests/UserDataStoreTests.cs b/tests/Filee.Core.Tests/UserDataStoreTests.cs
index 15d907f1..039b2752 100644
--- a/tests/Filee.Core.Tests/UserDataStoreTests.cs
+++ b/tests/Filee.Core.Tests/UserDataStoreTests.cs
@@ -1,3 +1,4 @@
+using System.Text.Json;
using Filee.Core.Presets;
using Filee.Core.Profiles;
using Filee.Core.Settings;
@@ -53,6 +54,79 @@ public class UserDataStoreTests
Assert.DoesNotContain("log", reloaded.Profiles.Single(p => p.Id == "text").Extensions);
}
+ [Fact]
+ public void A_preset_with_custom_sizing_never_writes_a_resize_name_an_older_version_rejects()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ var preset = store.Presets[0];
+ preset.Image.Resize = ResizeMode.None;
+ preset.Image.CustomSize = new CustomImageSize { Width = 777, Height = 333, LockAspect = true };
+ store.SaveLibrary();
+
+ // What a version without custom sizing reads: every resize stays a name it knows (so presets.json is not
+ // dropped as broken and overwritten with the built-in presets) and the unknown customSize is simply skipped.
+ var stored = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir.Path, "presets.json")));
+ foreach (var p in stored.RootElement.EnumerateArray())
+ {
+ var name = p.GetProperty("image").GetProperty("resize").GetString();
+ Assert.Contains(name, new[] { "None", "Percent", "LongEdge", "Width" });
+ }
+ var image = stored.RootElement.EnumerateArray().Single(p => p.GetProperty("id").GetString() == preset.Id).GetProperty("image");
+ var size = image.GetProperty("customSize");
+ Assert.Equal(777, size.GetProperty("width").GetInt32());
+ Assert.Equal(333, size.GetProperty("height").GetInt32());
+ Assert.True(size.GetProperty("lockAspect").GetBoolean());
+
+ // And this version reads the custom sizing back.
+ var reloaded = new UserDataStore(dir.Path);
+ reloaded.Load();
+ var back = reloaded.Presets.Single(p => p.Id == preset.Id);
+ Assert.Equal(ResizeMode.None, back.Image.Resize);
+ Assert.Equal((777, 333, true), (back.Image.CustomSize!.Width, back.Image.CustomSize!.Height, back.Image.CustomSize!.LockAspect));
+ }
+
+ [Fact]
+ public void A_resize_mode_of_Custom_is_never_written_to_presets_json()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ store.Presets[0].Image.Resize = ResizeMode.Custom;
+ store.SaveLibrary();
+
+ // However custom sizing reaches the model, the file keeps a resize name every version knows.
+ var stored = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir.Path, "presets.json")));
+ foreach (var p in stored.RootElement.EnumerateArray())
+ {
+ var name = p.GetProperty("image").GetProperty("resize").GetString();
+ Assert.Contains(name, new[] { "None", "Percent", "LongEdge", "Width" });
+ }
+ }
+
+ [Fact]
+ public void A_resize_name_this_version_does_not_know_reads_as_no_resize()
+ {
+ using var dir = new TempDir();
+ var store = new UserDataStore(dir.Path);
+ store.Load();
+ store.Presets[0].Name = "mine";
+ store.SaveLibrary();
+
+ // A preset from a future version, or a hand-edited one: a resize name this version has never heard of.
+ var path = Path.Combine(dir.Path, "presets.json");
+ var original = File.ReadAllText(path);
+ var edited = original.Replace("\"resize\": \"None\"", "\"resize\": \"Quantum\"");
+ Assert.NotEqual(original, edited);
+ File.WriteAllText(path, edited);
+
+ var reloaded = new UserDataStore(dir.Path);
+ reloaded.Load();
+ Assert.Equal(ResizeMode.None, reloaded.Presets[0].Image.Resize); // the lenient converter, not a broken file
+ Assert.Equal("mine", reloaded.Presets[0].Name); // and the library was kept, not replaced
+ }
+
[Fact]
public void Groups_keep_only_conversions_once_and_at_most_six()
{
diff --git a/tests/Filee.Engines.Tests/GraphicsTests.cs b/tests/Filee.Engines.Tests/GraphicsTests.cs
index be34091a..1b3b1f4d 100644
--- a/tests/Filee.Engines.Tests/GraphicsTests.cs
+++ b/tests/Filee.Engines.Tests/GraphicsTests.cs
@@ -3,6 +3,7 @@
using System.IO.Compression;
using System.Text;
+using SkiaSharp;
using Filee.Core.Conversion;
using Filee.Core.Presets;
using Filee.Engines.Icns;
@@ -432,7 +433,7 @@ public class GraphicsTests(EngineFixture fx) : IClassFixture<EngineFixture>
var rendered = await fx.ConvertAsync([svg], new Preset
{
TargetFormat = "png",
- Image = { Resize = ResizeMode.Custom, CustomWidth = 777, CustomHeight = 333 },
+ Image = { CustomSize = new() { Width = 777, Height = 333 } },
});
AssertDone(rendered);
@@ -440,6 +441,36 @@ public class GraphicsTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal((777u, 333u), (png.Width, png.Height));
}
+ [Fact]
+ public async Task Custom_resize_with_the_lock_renders_a_vector_picture_at_the_asked_size()
+ {
+ var dir = fx.NewFolder();
+ var svg = Write(dir, "logo.svg", GraphicsBuilders.Svg);
+
+ var rendered = await fx.ConvertAsync([svg], new Preset
+ {
+ TargetFormat = "png",
+ Image = { CustomSize = new() { Width = 777, Height = 333, LockAspect = true } },
+ });
+
+ AssertDone(rendered);
+ using var png = new MagickImage(rendered.Outputs.Single());
+ // The 400 x 200 drawing is rendered at min(777/400, 333/200) = 1.665: the largest 2:1 shape inside the box.
+ Assert.Equal((666u, 333u), (png.Width, png.Height));
+ }
+
+ [Fact]
+ public void Custom_resize_renders_vectors_at_the_asked_scale_instead_of_stretching_a_default_raster()
+ {
+ var image = new ImageOptions { CustomSize = new() { Width = 777, Height = 333, LockAspect = true } };
+ // With the lock, the fit-inside scale: the raster already keeps the file's shape at the box's edge.
+ Assert.Equal(1.665f, SvgRenderer.RasterScale(new SKRect(0, 0, 400, 200), image, new PdfOptions()), 3);
+
+ // Without, the larger side's scale, so the stretch that follows only ever shrinks the other side.
+ image.CustomSize = new() { Width = 777, Height = 333 };
+ Assert.Equal(1.9425f, SvgRenderer.RasterScale(new SKRect(0, 0, 400, 200), image, new PdfOptions()), 3);
+ }
+
[Fact]
public async Task Files_that_are_not_svg_fail_with_a_message()
{
diff --git a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs b/tests/Filee.Engines.Tests/ImageAndPdfTests.cs
index 4384dae5..456b5daa 100644
--- a/tests/Filee.Engines.Tests/ImageAndPdfTests.cs
+++ b/tests/Filee.Engines.Tests/ImageAndPdfTests.cs
@@ -308,7 +308,7 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>
public void Custom_resize_stretches_to_exactly_the_asked_size()
{
// The case from the issue: a 1080 x 1920 photo asked for a 500 x 2000 banner, distorted on purpose.
- var options = new ImageOptions { Resize = ResizeMode.Custom, CustomWidth = 500, CustomHeight = 2000 };
+ var options = new ImageOptions { CustomSize = new() { Width = 500, Height = 2000 } };
using var portrait = new MagickImage(MagickColors.White, 1080, 1920);
Filee.Engines.Magick.ImageEncoder.ApplyOptions(portrait, options, "png");
@@ -319,7 +319,7 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>
[Fact]
public void Custom_resize_with_the_lock_fits_inside_without_distortion()
{
- var options = new ImageOptions { Resize = ResizeMode.Custom, CustomWidth = 500, CustomHeight = 2000, LockAspect = true };
+ var options = new ImageOptions { CustomSize = new() { Width = 500, Height = 2000, LockAspect = true } };
// The width is the limit: 1080 x 1920 shrinks to the largest 9:16 fit inside the box, 500 x 889.
using var portrait = new MagickImage(MagickColors.White, 1080, 1920);
@@ -335,7 +335,7 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>
[Fact]
public void Custom_resize_leaves_a_picture_that_already_has_the_asked_size()
{
- var options = new ImageOptions { Resize = ResizeMode.Custom, CustomWidth = 500, CustomHeight = 2000 };
+ var options = new ImageOptions { CustomSize = new() { Width = 500, Height = 2000 } };
using var exact = new MagickImage(MagickColors.White, 500, 2000);
Filee.Engines.Magick.ImageEncoder.ApplyOptions(exact, options, "png");
@@ -343,6 +343,19 @@ public class ImageAndPdfTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal((500u, 2000u), (exact.Width, exact.Height));
}
+ [Fact]
+ public void Custom_resize_plans_pdf_page_runs_at_the_scale_the_resize_will_use()
+ {
+ // How PdfiumConverter.Split plans runs: the factor the stacked strip is scaled by (issue #576).
+ var locked = new ImageOptions { CustomSize = new() { Width = 500, Height = 2000, LockAspect = true } };
+ Assert.Equal(1.25, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, locked), 3);
+
+ var stretched = new ImageOptions { CustomSize = new() { Width = 500, Height = 2000 } };
+ Assert.Equal(2.5, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, stretched), 3);
+
+ Assert.Equal(1, Filee.Engines.Magick.ImageEncoder.ResizeScale(400, 800, new ImageOptions()), 3);
+ }
+
[Fact]
public void A_long_image_splits_between_pages_into_parts_the_format_holds()
{