Skip to content

Commit

Permalink
Added Dimension Support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaben committed Feb 13, 2025
1 parent e900c1e commit a6a7278
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 77 deletions.
1 change: 1 addition & 0 deletions GotenbergSharpApiClient.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jaben/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Libre/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pdfs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Picas/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=potm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Rpcc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=suppressions/@EntryIndexedValue">True</s:Boolean>
Expand Down
3 changes: 0 additions & 3 deletions lib/Domain/Builders/Faceted/Margins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.



namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;


public enum Margins
{
None = 0,
Expand Down
76 changes: 39 additions & 37 deletions 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 Gotenberg.Sharp.API.Client.Domain.Dimensions;

namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;

public sealed class PagePropertyBuilder(PageProperties pageProperties)
Expand Down Expand Up @@ -56,83 +58,83 @@ public PagePropertyBuilder SetScale(double scale)
return this;
}

#region Obsolete Helper Functions

[Obsolete("Use SetPaperWidth")]
public PagePropertyBuilder PaperWidth(double width)
{
this._pageProperties.PaperWidth = width;
return this;
}
public PagePropertyBuilder PaperWidth(double width) => SetPaperWidth(width);

[Obsolete("Use SetPaperHeight")]
public PagePropertyBuilder PaperHeight(double height)
{
this._pageProperties.PaperHeight = height;
return this;
}
public PagePropertyBuilder PaperHeight(double height) => SetPaperHeight(height);

[Obsolete("Use SetMarginTop")]
public PagePropertyBuilder MarginTop(double marginTop)
{
this._pageProperties.MarginTop = marginTop;
return this;
}
public PagePropertyBuilder MarginTop(double marginTop) => SetMarginTop(marginTop);

[Obsolete("Use SetMarginBottom")]
public PagePropertyBuilder MarginBottom(double marginBottom)
{
this._pageProperties.MarginBottom = marginBottom;
return this;
}
public PagePropertyBuilder MarginBottom(double marginBottom) => SetMarginBottom(marginBottom);

[Obsolete("Use SetMarginLeft")]
public PagePropertyBuilder MarginLeft(double marginLeft)
{
this._pageProperties.MarginLeft = marginLeft;
return this;
}
public PagePropertyBuilder MarginLeft(double marginLeft) => SetMarginLeft(marginLeft);

[Obsolete("Use SetMarginRight")]
public PagePropertyBuilder MarginRight(double marginRight)
{
this._pageProperties.MarginRight = marginRight;
return this;
}
public PagePropertyBuilder MarginRight(double marginRight) => SetMarginRight(marginRight);

#endregion

#region Dimension Helpers for Inches

public PagePropertyBuilder SetPaperWidth(double widthInches) => SetPaperWidth(Dimension.FromInches(widthInches));

public PagePropertyBuilder SetPaperHeight(double heightInches) => SetPaperHeight(Dimension.FromInches(heightInches));

public PagePropertyBuilder SetPaperWidth(double width)
public PagePropertyBuilder SetMarginTop(double marginTopInches) => SetMarginTop(Dimension.FromInches(marginTopInches));

public PagePropertyBuilder SetMarginBottom(double marginBottomInches) => SetMarginBottom(Dimension.FromInches(marginBottomInches));

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

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

#endregion

#region Dimension Helpers

public PagePropertyBuilder SetPaperWidth(Dimension width)
{
this._pageProperties.PaperWidth = width;
return this;
}

public PagePropertyBuilder SetPaperHeight(double height)
public PagePropertyBuilder SetPaperHeight(Dimension height)
{
this._pageProperties.PaperHeight = height;
return this;
}

public PagePropertyBuilder SetMarginTop(double marginTop)
public PagePropertyBuilder SetMarginTop(Dimension marginTop)
{
this._pageProperties.MarginTop = marginTop;
return this;
}

public PagePropertyBuilder SetMarginBottom(double marginBottom)
public PagePropertyBuilder SetMarginBottom(Dimension marginBottom)
{
this._pageProperties.MarginBottom = marginBottom;
return this;
}

public PagePropertyBuilder SetMarginLeft(double marginLeft)
public PagePropertyBuilder SetMarginLeft(Dimension marginLeft)
{
this._pageProperties.MarginLeft = marginLeft;
return this;
}

public PagePropertyBuilder SetMarginRight(double marginRight)
public PagePropertyBuilder SetMarginRight(Dimension marginRight)
{
this._pageProperties.MarginRight = marginRight;
return this;
}
}

#endregion

[Obsolete("Use SetLandscape()")]
public PagePropertyBuilder LandScape(bool landscape = true)
Expand Down
141 changes: 141 additions & 0 deletions lib/Domain/Dimensions/Dimension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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 System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

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

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>
public double Value { get; init; } = value;

/// <summary>
/// pt|px|in|mm|cm|pc
/// </summary>
public DimensionUnitType UnitType { get; init; } = unitType;

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

public static Dimension Parse(string dimension)
{
if (string.IsNullOrWhiteSpace(dimension))
{
throw new ArgumentException("Dimension cannot be null or empty.",
nameof(dimension));
}

var match = ValidDimensionRegex.Match(dimension);
if (!match.Success)
{
throw new ArgumentException(
"Invalid dimension format. Expected formats: '200px', '11in', etc.",
nameof(dimension));
}

var value = double.Parse(match.Groups[1].Value);
var unitStr = match.Groups[3].Value.ToLower();

if (!TryParseUnit(unitStr, out var unit))
{
throw new ArgumentException($"Unknown unitType '{unitStr}'", nameof(dimension));
}

return new Dimension(value, unit);
}

private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
{
foreach (DimensionUnitType type in Enum.GetValues(typeof(DimensionUnitType)))
{
if (type.GetDescription() == unitStr)
{
unitType = type;
return true;
}
}
unitType = default;

return false;
}

public static Dimension FromPoints(double points)
{
return new Dimension(points, DimensionUnitType.Points);
}

public static Dimension FromPixels(double pixels)
{
return new Dimension(pixels, DimensionUnitType.Pixels);
}

public static Dimension FromInches(double inches)
{
return new Dimension(inches, DimensionUnitType.Inches);
}

public static Dimension FromMillimeters(double millimeters)
{
return new Dimension(millimeters, DimensionUnitType.Millimeters);
}

public static Dimension FromCentimeters(double centimeters)
{
return new Dimension(centimeters, DimensionUnitType.Centimeters);
}

public static Dimension FromPicas(double picas)
{
return new Dimension(picas, DimensionUnitType.Picas);
}

public override string ToString()
{
return $"{Value}{UnitType.GetDescription()}";
}

public override bool Equals(object? obj)
{
return Equals(obj as Dimension);
}

public override int GetHashCode()
{
return HashCode.Combine(Value, UnitType);
}

public static bool operator ==(Dimension? left, Dimension? right)
{
return Equals(left, right);
}

public static bool operator !=(Dimension? left, Dimension? right)
{
return !Equals(left, right);
}
}
29 changes: 29 additions & 0 deletions lib/Domain/Dimensions/DimensionUnitType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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;

namespace Gotenberg.Sharp.API.Client.Domain.Dimensions
{
public enum DimensionUnitType
{
[Description("pt")] Points, // Points
[Description("px")] Pixels, // Pixels
[Description("in")] Inches, // Inches
[Description("mm")] Millimeters, // Millimeters
[Description("cm")] Centimeters, // Centimeters
[Description("pc")] Picas // Picas
}
}
Loading

0 comments on commit a6a7278

Please sign in to comment.