Skip to content

Commit

Permalink
Added Margin setting helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaben committed Feb 19, 2025
1 parent 833c00f commit 6b9c589
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 18 deletions.
67 changes: 66 additions & 1 deletion lib/Domain/Builders/Faceted/PagePropertyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Security.Cryptography.X509Certificates;

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

namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
Expand All @@ -21,9 +23,18 @@ public sealed class PagePropertyBuilder(PageProperties pageProperties)
{
PageProperties _pageProperties = pageProperties;

/// <summary>
/// Set Margins to "default" values:
/// None: '0 0 0 0'
/// Normal: '1in 1in 1in 1in'
/// Large: '2in 2in 2in 2in'
/// </summary>
/// <param name="margins"></param>
/// <returns></returns>
public PagePropertyBuilder SetMargins(Margins margins)
{
var selected = margins.ToSelectedMargins();

this._pageProperties.MarginLeft = selected.Left;
this._pageProperties.MarginRight = selected.Right;
this._pageProperties.MarginTop = selected.Top;
Expand All @@ -35,6 +46,7 @@ public PagePropertyBuilder SetMargins(Margins margins)
public PagePropertyBuilder SetPaperSize(PaperSizes sizes)
{
var selected = sizes.ToSelectedSize();

this._pageProperties.PaperWidth = selected.Width;
this._pageProperties.PaperHeight = selected.Height;

Expand Down Expand Up @@ -92,7 +104,7 @@ public PagePropertyBuilder SetScale(double scale)

public PagePropertyBuilder SetMarginLeft(double marginLeftInches) => SetMarginLeft(Dimension.FromInches(marginLeftInches));

public PagePropertyBuilder SetMarginRight(double marginRightInches) => SetMarginRight(Dimension.FromInches(marginRightInches));
public PagePropertyBuilder SetMarginRight(double marginRightInches) => SetMarginRight(Dimension.FromInches(marginRightInches));

#endregion

Expand All @@ -110,6 +122,59 @@ public PagePropertyBuilder SetPaperHeight(Dimension height)
return this;
}

/// <summary>
/// Set margins like the CSS style '1.0in 0.25in 1.0in 0.25in'. (top, right, bottom, left) or
/// '1.0in 25.in' (top and bottom, right and left).
/// </summary>
/// <param name="margins"></param>
/// <returns></returns>
public PagePropertyBuilder SetMargins(string margins)
{
var parsedMargins = margins.Split([' '], StringSplitOptions.RemoveEmptyEntries);

var dimensions = parsedMargins.Select(Dimension.Parse).ToList();

if (dimensions.Count == 2)
{
// set top/bottom and right/left
SetMargins(dimensions[0], dimensions[1]);
}

if (dimensions.Count == 4)
{
SetMargins(dimensions[0], dimensions[1], dimensions[2], dimensions[3]);
}

return this;
}

public PagePropertyBuilder SetMargins(
Dimension marginTopBottom,
Dimension marginRightLeft)
{
this.SetMarginTop(marginTopBottom);
this.SetMarginBottom(marginTopBottom);

this.SetMarginRight(marginRightLeft);
this.SetMarginLeft(marginRightLeft);

return this;
}

public PagePropertyBuilder SetMargins(
Dimension marginTop,
Dimension marginRight,
Dimension marginBottom,
Dimension marginLeft)
{
this.SetMarginTop(marginTop);
this.SetMarginRight(marginTop);
this.SetMarginBottom(marginTop);
this.SetMarginLeft(marginTop);

return this;
}

public PagePropertyBuilder SetMarginTop(Dimension marginTop)
{
this._pageProperties.MarginTop = marginTop;
Expand Down
54 changes: 37 additions & 17 deletions lib/Domain/Dimensions/Dimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
Expand Down Expand Up @@ -54,9 +51,6 @@ namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
/// </summary>
public sealed class Dimension(double value, DimensionUnitType unitType) : IEquatable<Dimension?>
{
private static readonly Regex ValidDimensionRegex =
new(@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)?\s*$", RegexOptions.IgnoreCase);

/// <summary>
/// UnitType value
/// </summary>
Expand All @@ -67,39 +61,64 @@ public sealed class Dimension(double value, DimensionUnitType unitType) : IEquat
/// </summary>
public DimensionUnitType UnitType { get; init; } = unitType;

private static readonly Regex ValidDimensionRegex = new(
@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)?\s*$",
RegexOptions.IgnoreCase);

public bool Equals(Dimension? other)
{
return other is not null &&
Math.Abs(Value - other.Value) < 1e-6 &&
UnitType == other.UnitType;
return other is not null && Math.Abs(Value - other.Value) < 1e-6 && UnitType == other.UnitType;
}

/// <summary>
/// Parses a string like "200px", "11in", or defaults to inches if no unit is provided (e.g., "3.4").
/// </summary>
/// <param name="dimension"></param>
/// <param name="parseDimension"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static Dimension Parse(string dimension)
public static Dimension Parse(string parseDimension)
{
if (string.IsNullOrWhiteSpace(dimension))
throw new ArgumentException("Dimension cannot be null or empty.", nameof(dimension));
if (string.IsNullOrWhiteSpace(parseDimension))
throw new ArgumentException("Dimension cannot be null or empty.", nameof(parseDimension));

var match = ValidDimensionRegex.Match(dimension);
var match = ValidDimensionRegex.Match(parseDimension);
if (!match.Success)
throw new ArgumentException("Invalid dimension format. Expected formats: '200px', '11in', or default to inches.", nameof(dimension));
throw new ArgumentException(
"Invalid dimension format. Expected formats: '200px', '11in', or default to inches.",
nameof(parseDimension));

return CreateDimensionFromRegex(match);
}

static Dimension CreateDimensionFromRegex(Match match)
{
double value = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
string? unitStr = match.Groups[3].Success ? match.Groups[3].Value.ToLower() : null;

// Default to Inches if no unit is provided
DimensionUnitType unit = unitStr is not null && TryParseUnit(unitStr, out var parsedUnit)
? parsedUnit
DimensionUnitType unit = unitStr is not null && TryParseUnit(unitStr, out var parsedUnit)
? parsedUnit
: DimensionUnitType.Inches;

return new Dimension(value, unit);
}

public static bool TryParse(string parseDimension, out Dimension? dimensionOut)
{
dimensionOut = null;

if (string.IsNullOrWhiteSpace(parseDimension))
return false;

var match = ValidDimensionRegex.Match(parseDimension);
if (!match.Success)
return false;

dimensionOut = CreateDimensionFromRegex(match);

return true;
}

private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
{
foreach (DimensionUnitType type in Enum.GetValues(typeof(DimensionUnitType)))
Expand All @@ -110,6 +129,7 @@ private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
return true;
}
}

unitType = default;

return false;
Expand Down

0 comments on commit 6b9c589

Please sign in to comment.