Skip to content

Commit 6b9c589

Browse files
committed
Added Margin setting helpers.
1 parent 833c00f commit 6b9c589

2 files changed

Lines changed: 103 additions & 18 deletions

File tree

lib/Domain/Builders/Faceted/PagePropertyBuilder.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
using System.Security.Cryptography.X509Certificates;
17+
1618
using Gotenberg.Sharp.API.Client.Domain.Dimensions;
1719

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

26+
/// <summary>
27+
/// Set Margins to "default" values:
28+
/// None: '0 0 0 0'
29+
/// Normal: '1in 1in 1in 1in'
30+
/// Large: '2in 2in 2in 2in'
31+
/// </summary>
32+
/// <param name="margins"></param>
33+
/// <returns></returns>
2434
public PagePropertyBuilder SetMargins(Margins margins)
2535
{
2636
var selected = margins.ToSelectedMargins();
37+
2738
this._pageProperties.MarginLeft = selected.Left;
2839
this._pageProperties.MarginRight = selected.Right;
2940
this._pageProperties.MarginTop = selected.Top;
@@ -35,6 +46,7 @@ public PagePropertyBuilder SetMargins(Margins margins)
3546
public PagePropertyBuilder SetPaperSize(PaperSizes sizes)
3647
{
3748
var selected = sizes.ToSelectedSize();
49+
3850
this._pageProperties.PaperWidth = selected.Width;
3951
this._pageProperties.PaperHeight = selected.Height;
4052

@@ -92,7 +104,7 @@ public PagePropertyBuilder SetScale(double scale)
92104

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

95-
public PagePropertyBuilder SetMarginRight(double marginRightInches) => SetMarginRight(Dimension.FromInches(marginRightInches));
107+
public PagePropertyBuilder SetMarginRight(double marginRightInches) => SetMarginRight(Dimension.FromInches(marginRightInches));
96108

97109
#endregion
98110

@@ -110,6 +122,59 @@ public PagePropertyBuilder SetPaperHeight(Dimension height)
110122
return this;
111123
}
112124

125+
/// <summary>
126+
/// Set margins like the CSS style '1.0in 0.25in 1.0in 0.25in'. (top, right, bottom, left) or
127+
/// '1.0in 25.in' (top and bottom, right and left).
128+
/// </summary>
129+
/// <param name="margins"></param>
130+
/// <returns></returns>
131+
public PagePropertyBuilder SetMargins(string margins)
132+
{
133+
var parsedMargins = margins.Split([' '], StringSplitOptions.RemoveEmptyEntries);
134+
135+
var dimensions = parsedMargins.Select(Dimension.Parse).ToList();
136+
137+
if (dimensions.Count == 2)
138+
{
139+
// set top/bottom and right/left
140+
SetMargins(dimensions[0], dimensions[1]);
141+
}
142+
143+
if (dimensions.Count == 4)
144+
{
145+
SetMargins(dimensions[0], dimensions[1], dimensions[2], dimensions[3]);
146+
}
147+
148+
return this;
149+
}
150+
151+
public PagePropertyBuilder SetMargins(
152+
Dimension marginTopBottom,
153+
Dimension marginRightLeft)
154+
{
155+
this.SetMarginTop(marginTopBottom);
156+
this.SetMarginBottom(marginTopBottom);
157+
158+
this.SetMarginRight(marginRightLeft);
159+
this.SetMarginLeft(marginRightLeft);
160+
161+
return this;
162+
}
163+
164+
public PagePropertyBuilder SetMargins(
165+
Dimension marginTop,
166+
Dimension marginRight,
167+
Dimension marginBottom,
168+
Dimension marginLeft)
169+
{
170+
this.SetMarginTop(marginTop);
171+
this.SetMarginRight(marginTop);
172+
this.SetMarginBottom(marginTop);
173+
this.SetMarginLeft(marginTop);
174+
175+
return this;
176+
}
177+
113178
public PagePropertyBuilder SetMarginTop(Dimension marginTop)
114179
{
115180
this._pageProperties.MarginTop = marginTop;

lib/Domain/Dimensions/Dimension.cs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
using System.ComponentModel;
1716
using System.Globalization;
18-
using System.Reflection;
19-
using System.Runtime.CompilerServices;
2017
using System.Text.RegularExpressions;
2118

2219
namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
@@ -54,9 +51,6 @@ namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
5451
/// </summary>
5552
public sealed class Dimension(double value, DimensionUnitType unitType) : IEquatable<Dimension?>
5653
{
57-
private static readonly Regex ValidDimensionRegex =
58-
new(@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)?\s*$", RegexOptions.IgnoreCase);
59-
6054
/// <summary>
6155
/// UnitType value
6256
/// </summary>
@@ -67,39 +61,64 @@ public sealed class Dimension(double value, DimensionUnitType unitType) : IEquat
6761
/// </summary>
6862
public DimensionUnitType UnitType { get; init; } = unitType;
6963

64+
private static readonly Regex ValidDimensionRegex = new(
65+
@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)?\s*$",
66+
RegexOptions.IgnoreCase);
67+
7068
public bool Equals(Dimension? other)
7169
{
72-
return other is not null &&
73-
Math.Abs(Value - other.Value) < 1e-6 &&
74-
UnitType == other.UnitType;
70+
return other is not null && Math.Abs(Value - other.Value) < 1e-6 && UnitType == other.UnitType;
7571
}
7672

7773
/// <summary>
7874
/// Parses a string like "200px", "11in", or defaults to inches if no unit is provided (e.g., "3.4").
7975
/// </summary>
80-
/// <param name="dimension"></param>
76+
/// <param name="parseDimension"></param>
8177
/// <returns></returns>
8278
/// <exception cref="ArgumentException"></exception>
83-
public static Dimension Parse(string dimension)
79+
public static Dimension Parse(string parseDimension)
8480
{
85-
if (string.IsNullOrWhiteSpace(dimension))
86-
throw new ArgumentException("Dimension cannot be null or empty.", nameof(dimension));
81+
if (string.IsNullOrWhiteSpace(parseDimension))
82+
throw new ArgumentException("Dimension cannot be null or empty.", nameof(parseDimension));
8783

88-
var match = ValidDimensionRegex.Match(dimension);
84+
var match = ValidDimensionRegex.Match(parseDimension);
8985
if (!match.Success)
90-
throw new ArgumentException("Invalid dimension format. Expected formats: '200px', '11in', or default to inches.", nameof(dimension));
86+
throw new ArgumentException(
87+
"Invalid dimension format. Expected formats: '200px', '11in', or default to inches.",
88+
nameof(parseDimension));
89+
90+
return CreateDimensionFromRegex(match);
91+
}
9192

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

9598
// Default to Inches if no unit is provided
96-
DimensionUnitType unit = unitStr is not null && TryParseUnit(unitStr, out var parsedUnit)
97-
? parsedUnit
99+
DimensionUnitType unit = unitStr is not null && TryParseUnit(unitStr, out var parsedUnit)
100+
? parsedUnit
98101
: DimensionUnitType.Inches;
99102

100103
return new Dimension(value, unit);
101104
}
102105

106+
public static bool TryParse(string parseDimension, out Dimension? dimensionOut)
107+
{
108+
dimensionOut = null;
109+
110+
if (string.IsNullOrWhiteSpace(parseDimension))
111+
return false;
112+
113+
var match = ValidDimensionRegex.Match(parseDimension);
114+
if (!match.Success)
115+
return false;
116+
117+
dimensionOut = CreateDimensionFromRegex(match);
118+
119+
return true;
120+
}
121+
103122
private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
104123
{
105124
foreach (DimensionUnitType type in Enum.GetValues(typeof(DimensionUnitType)))
@@ -110,6 +129,7 @@ private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
110129
return true;
111130
}
112131
}
132+
113133
unitType = default;
114134

115135
return false;

0 commit comments

Comments
 (0)