Skip to content

Commit

Permalink
Added "Default" margins and paper sizes so it's
Browse files Browse the repository at this point in the history
much cleaner when working with our "baseline."
  • Loading branch information
Jaben committed Feb 19, 2025
1 parent 6b9c589 commit 94a85f8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
6 changes: 4 additions & 2 deletions lib/Domain/Builders/Faceted/Margins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public enum Margins
{
None = 0,

Normal = 1,
Default = 1,

Large = 2
Normal = 2,

Large = 3
}
4 changes: 4 additions & 0 deletions lib/Domain/Dimensions/Dimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,8 @@ public override int GetHashCode()
{
return !Equals(left, right);
}

// Implicit conversions from double and int to Dimension (defaulting to Inches)
public static implicit operator Dimension(double value) => new(value, DimensionUnitType.Inches);
public static implicit operator Dimension(int value) => new(value, DimensionUnitType.Inches);
}
27 changes: 21 additions & 6 deletions lib/Domain/Requests/Facets/PageProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ namespace Gotenberg.Sharp.API.Client.Domain.Requests.Facets
/// </remarks>
public sealed class PageProperties : IConvertToHttpContent
{
public PageProperties()
{
var defaultPaperSize = PaperSizes.Letter.ToSelectedSize();

PaperWidth = defaultPaperSize.Width;
PaperHeight = defaultPaperSize.Height;

var defaultMargins = Margins.Default.ToSelectedMargins();

MarginTop = defaultMargins.Top;
MarginBottom = defaultMargins.Bottom;
MarginLeft = defaultMargins.Left;
MarginRight = defaultMargins.Right;
}

#region Properties

/// <summary>
Expand All @@ -43,7 +58,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The width of the paper.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.PaperWidth)]
public Dimension? PaperWidth { get; set; }
public Dimension PaperWidth { get; set; }

/// <summary>
/// Gets or sets the height of the paper.
Expand All @@ -52,7 +67,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The height of the paper.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.PaperHeight)]
public Dimension? PaperHeight { get; set; }
public Dimension PaperHeight { get; set; }

/// <summary>
/// Gets or sets the margin top.
Expand All @@ -61,7 +76,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The margin top.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginTop)]
public Dimension? MarginTop { get; set; }
public Dimension MarginTop { get; set; }

/// <summary>
/// Gets or sets the margin bottom.
Expand All @@ -70,7 +85,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The margin bottom.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginBottom)]
public Dimension? MarginBottom { get; set; }
public Dimension MarginBottom { get; set; }

/// <summary>
/// Gets or sets the margin left.
Expand All @@ -79,7 +94,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The margin left.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginLeft)]
public Dimension? MarginLeft { get; set; }
public Dimension MarginLeft { get; set; }

/// <summary>
/// Gets or sets the margin right.
Expand All @@ -88,7 +103,7 @@ public sealed class PageProperties : IConvertToHttpContent
/// The margin right.
/// </value>
[MultiFormHeader(Constants.Gotenberg.Chromium.Shared.PageProperties.MarginRight)]
public Dimension? MarginRight { get; set; }
public Dimension MarginRight { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="PageProperties"/> is landscape.
Expand Down
68 changes: 68 additions & 0 deletions lib/Extensions/DimensionHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
// and GotenbergSharpApiClient Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using Gotenberg.Sharp.API.Client.Domain.Dimensions;

namespace Gotenberg.Sharp.API.Client.Extensions
{
public static class DimensionHelpers
{
private static readonly IEnumerable<(Margins MarginType, (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) Value)> MarginSizer =
[
(Margins.None, (Left: 0.0, Right: 0.0, Top: 0.0, Bottom: 0.0)),
(Margins.Default, (Left: 0.39, Right: 0.39, Top: 0.39, Bottom: 0.39)),
(Margins.Normal, (Left: 1.0, Right: 1.0, Top: 1.0, Bottom: 1.0)),
(Margins.Large, (Left: 2.0, Right: 2.0, Top: 2.0, Bottom: 2.0))
];

private static readonly IEnumerable<(PaperSizes Size, (Dimension Width, Dimension Height) Value)> PaperSizer =
[
(PaperSizes.A3, (Width: 11.7, Height: 16.5)),
(PaperSizes.A4, (Width: 8.27, Height: 11.7)),
(PaperSizes.A5, (Width: 5.8, Height: 8.2)),
(PaperSizes.A6, (Width: 4.1, Height: 5.8)),
(PaperSizes.Letter, (Width: 8.5, Height: 11.0)),
(PaperSizes.Legal, (Width: 8.5, Height: 14.0)),
(PaperSizes.Tabloid, (Width: 11.0, Height: 17.0))
];

internal static (Dimension Width, Dimension Height) ToSelectedSize(this PaperSizes selectedSize)
{
if (!Enum.IsDefined(typeof(PaperSizes), selectedSize))
throw new InvalidEnumArgumentException(
nameof(selectedSize),
(int)selectedSize,
typeof(PaperSizes));

if (selectedSize == default)
throw new InvalidOperationException(nameof(selectedSize));

return PaperSizer.First(s => s.Size == selectedSize).Value;
}

internal static (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) ToSelectedMargins(
this Margins selected)
{
if (!Enum.IsDefined(typeof(Margins), selected))
throw new InvalidEnumArgumentException(
nameof(selected),
(int)selected,
typeof(PaperSizes));

return MarginSizer.First(m => m.MarginType == selected).Value;
}
}
}
45 changes: 0 additions & 45 deletions lib/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,18 @@

using System.ComponentModel;
using System.Reflection;
using Gotenberg.Sharp.API.Client.Domain.Dimensions;

namespace Gotenberg.Sharp.API.Client.Extensions;

internal static class EnumExtensions
{
private static readonly IEnumerable<(Margins MarginType, (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) Value)> MarginSizer =
[
(Margins.None, (Left: Dimension.FromInches(0.0), Right: Dimension.FromInches(0.0), Top: Dimension.FromInches(0.0), Bottom: Dimension.FromInches(0.0))),
(Margins.Normal, (Left: Dimension.FromInches(1.0), Right: Dimension.FromInches(1.0), Top: Dimension.FromInches(1.0), Bottom: Dimension.FromInches(1.0))),
(Margins.Large, (Left: Dimension.FromInches(2.0), Right: Dimension.FromInches(2.0), Top: Dimension.FromInches(2.0), Bottom: Dimension.FromInches(2.0)))
];

private static readonly IEnumerable<(PaperSizes Size, (Dimension Width, Dimension Height) Value)> PaperSizer =
[
(PaperSizes.A3, (Width: Dimension.FromInches(11.7), Height: Dimension.FromInches(16.5))),
(PaperSizes.A4, (Width: Dimension.FromInches(8.27), Height: Dimension.FromInches(11.7))),
(PaperSizes.A5, (Width: Dimension.FromInches(5.8), Height: Dimension.FromInches(8.2))),
(PaperSizes.A6, (Width: Dimension.FromInches(4.1), Height: Dimension.FromInches(5.8))),
(PaperSizes.Letter, (Width: Dimension.FromInches(8.5), Height: Dimension.FromInches(11.0))),
(PaperSizes.Legal, (Width: Dimension.FromInches(8.5), Height: Dimension.FromInches(14.0))),
(PaperSizes.Tabloid, (Width: Dimension.FromInches(11.0), Height: Dimension.FromInches(17.0)))
];

internal static string ToFormDataValue(this PdfFormats format)
{
return format == default
? "PDF/A-1a"
: $"PDF/A-{format.ToString().Substring(1, 2)}";
}

internal static (Dimension Width, Dimension Height) ToSelectedSize(this PaperSizes selectedSize)
{
if (!Enum.IsDefined(typeof(PaperSizes), selectedSize))
throw new InvalidEnumArgumentException(
nameof(selectedSize),
(int)selectedSize,
typeof(PaperSizes));

if (selectedSize == default)
throw new InvalidOperationException(nameof(selectedSize));

return PaperSizer.First(s => s.Size == selectedSize).Value;
}

internal static (Dimension Left, Dimension Right, Dimension Top, Dimension Bottom) ToSelectedMargins(
this Margins selected)
{
if (!Enum.IsDefined(typeof(Margins), selected))
throw new InvalidEnumArgumentException(
nameof(selected),
(int)selected,
typeof(PaperSizes));

return MarginSizer.First(m => m.MarginType == selected).Value;
}

public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString())!;
Expand Down

0 comments on commit 94a85f8

Please sign in to comment.