Skip to content

Commit a6a7278

Browse files
committed
Added Dimension Support.
1 parent e900c1e commit a6a7278

8 files changed

Lines changed: 258 additions & 77 deletions

File tree

GotenbergSharpApiClient.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jaben/@EntryIndexedValue">True</s:Boolean>
2929
<s:Boolean x:Key="/Default/UserDictionary/Words/=Libre/@EntryIndexedValue">True</s:Boolean>
3030
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pdfs/@EntryIndexedValue">True</s:Boolean>
31+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Picas/@EntryIndexedValue">True</s:Boolean>
3132
<s:Boolean x:Key="/Default/UserDictionary/Words/=potm/@EntryIndexedValue">True</s:Boolean>
3233
<s:Boolean x:Key="/Default/UserDictionary/Words/=Rpcc/@EntryIndexedValue">True</s:Boolean>
3334
<s:Boolean x:Key="/Default/UserDictionary/Words/=suppressions/@EntryIndexedValue">True</s:Boolean>

lib/Domain/Builders/Faceted/Margins.cs

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

16-
17-
1816
namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
1917

20-
2118
public enum Margins
2219
{
2320
None = 0,

lib/Domain/Builders/Faceted/PagePropertyBuilder.cs

Lines changed: 39 additions & 37 deletions
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 Gotenberg.Sharp.API.Client.Domain.Dimensions;
17+
1618
namespace Gotenberg.Sharp.API.Client.Domain.Builders.Faceted;
1719

1820
public sealed class PagePropertyBuilder(PageProperties pageProperties)
@@ -56,83 +58,83 @@ public PagePropertyBuilder SetScale(double scale)
5658
return this;
5759
}
5860

61+
#region Obsolete Helper Functions
62+
5963
[Obsolete("Use SetPaperWidth")]
60-
public PagePropertyBuilder PaperWidth(double width)
61-
{
62-
this._pageProperties.PaperWidth = width;
63-
return this;
64-
}
64+
public PagePropertyBuilder PaperWidth(double width) => SetPaperWidth(width);
6565

6666
[Obsolete("Use SetPaperHeight")]
67-
public PagePropertyBuilder PaperHeight(double height)
68-
{
69-
this._pageProperties.PaperHeight = height;
70-
return this;
71-
}
67+
public PagePropertyBuilder PaperHeight(double height) => SetPaperHeight(height);
7268

7369
[Obsolete("Use SetMarginTop")]
74-
public PagePropertyBuilder MarginTop(double marginTop)
75-
{
76-
this._pageProperties.MarginTop = marginTop;
77-
return this;
78-
}
70+
public PagePropertyBuilder MarginTop(double marginTop) => SetMarginTop(marginTop);
7971

8072
[Obsolete("Use SetMarginBottom")]
81-
public PagePropertyBuilder MarginBottom(double marginBottom)
82-
{
83-
this._pageProperties.MarginBottom = marginBottom;
84-
return this;
85-
}
73+
public PagePropertyBuilder MarginBottom(double marginBottom) => SetMarginBottom(marginBottom);
8674

8775
[Obsolete("Use SetMarginLeft")]
88-
public PagePropertyBuilder MarginLeft(double marginLeft)
89-
{
90-
this._pageProperties.MarginLeft = marginLeft;
91-
return this;
92-
}
76+
public PagePropertyBuilder MarginLeft(double marginLeft) => SetMarginLeft(marginLeft);
9377

9478
[Obsolete("Use SetMarginRight")]
95-
public PagePropertyBuilder MarginRight(double marginRight)
96-
{
97-
this._pageProperties.MarginRight = marginRight;
98-
return this;
99-
}
79+
public PagePropertyBuilder MarginRight(double marginRight) => SetMarginRight(marginRight);
80+
81+
#endregion
82+
83+
#region Dimension Helpers for Inches
84+
85+
public PagePropertyBuilder SetPaperWidth(double widthInches) => SetPaperWidth(Dimension.FromInches(widthInches));
86+
87+
public PagePropertyBuilder SetPaperHeight(double heightInches) => SetPaperHeight(Dimension.FromInches(heightInches));
10088

101-
public PagePropertyBuilder SetPaperWidth(double width)
89+
public PagePropertyBuilder SetMarginTop(double marginTopInches) => SetMarginTop(Dimension.FromInches(marginTopInches));
90+
91+
public PagePropertyBuilder SetMarginBottom(double marginBottomInches) => SetMarginBottom(Dimension.FromInches(marginBottomInches));
92+
93+
public PagePropertyBuilder SetMarginLeft(double marginLeftInches) => SetMarginLeft(Dimension.FromInches(marginLeftInches));
94+
95+
public PagePropertyBuilder SetMarginRight(double marginRightInches) => SetMarginRight(Dimension.FromInches(marginRightInches));
96+
97+
#endregion
98+
99+
#region Dimension Helpers
100+
101+
public PagePropertyBuilder SetPaperWidth(Dimension width)
102102
{
103103
this._pageProperties.PaperWidth = width;
104104
return this;
105105
}
106106

107-
public PagePropertyBuilder SetPaperHeight(double height)
107+
public PagePropertyBuilder SetPaperHeight(Dimension height)
108108
{
109109
this._pageProperties.PaperHeight = height;
110110
return this;
111111
}
112112

113-
public PagePropertyBuilder SetMarginTop(double marginTop)
113+
public PagePropertyBuilder SetMarginTop(Dimension marginTop)
114114
{
115115
this._pageProperties.MarginTop = marginTop;
116116
return this;
117117
}
118118

119-
public PagePropertyBuilder SetMarginBottom(double marginBottom)
119+
public PagePropertyBuilder SetMarginBottom(Dimension marginBottom)
120120
{
121121
this._pageProperties.MarginBottom = marginBottom;
122122
return this;
123123
}
124124

125-
public PagePropertyBuilder SetMarginLeft(double marginLeft)
125+
public PagePropertyBuilder SetMarginLeft(Dimension marginLeft)
126126
{
127127
this._pageProperties.MarginLeft = marginLeft;
128128
return this;
129129
}
130130

131-
public PagePropertyBuilder SetMarginRight(double marginRight)
131+
public PagePropertyBuilder SetMarginRight(Dimension marginRight)
132132
{
133133
this._pageProperties.MarginRight = marginRight;
134134
return this;
135-
}
135+
}
136+
137+
#endregion
136138

137139
[Obsolete("Use SetLandscape()")]
138140
public PagePropertyBuilder LandScape(bool landscape = true)

lib/Domain/Dimensions/Dimension.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
2+
// and GotenbergSharpApiClient Contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System.ComponentModel;
17+
using System.Reflection;
18+
using System.Runtime.CompilerServices;
19+
using System.Text.RegularExpressions;
20+
21+
namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
22+
23+
public sealed class Dimension(double value, DimensionUnitType unitType) : IEquatable<Dimension?>
24+
{
25+
private static readonly Regex ValidDimensionRegex =
26+
new(@"^\s*(\d+(\.\d+)?)\s*(pt|px|in|mm|cm|pc)\s*$", RegexOptions.IgnoreCase);
27+
28+
/// <summary>
29+
/// UnitType value
30+
/// </summary>
31+
public double Value { get; init; } = value;
32+
33+
/// <summary>
34+
/// pt|px|in|mm|cm|pc
35+
/// </summary>
36+
public DimensionUnitType UnitType { get; init; } = unitType;
37+
38+
public bool Equals(Dimension? other)
39+
{
40+
return other is not null &&
41+
Math.Abs(Value - other.Value) < 1e-6 &&
42+
UnitType == other.UnitType;
43+
}
44+
45+
public static Dimension Parse(string dimension)
46+
{
47+
if (string.IsNullOrWhiteSpace(dimension))
48+
{
49+
throw new ArgumentException("Dimension cannot be null or empty.",
50+
nameof(dimension));
51+
}
52+
53+
var match = ValidDimensionRegex.Match(dimension);
54+
if (!match.Success)
55+
{
56+
throw new ArgumentException(
57+
"Invalid dimension format. Expected formats: '200px', '11in', etc.",
58+
nameof(dimension));
59+
}
60+
61+
var value = double.Parse(match.Groups[1].Value);
62+
var unitStr = match.Groups[3].Value.ToLower();
63+
64+
if (!TryParseUnit(unitStr, out var unit))
65+
{
66+
throw new ArgumentException($"Unknown unitType '{unitStr}'", nameof(dimension));
67+
}
68+
69+
return new Dimension(value, unit);
70+
}
71+
72+
private static bool TryParseUnit(string unitStr, out DimensionUnitType unitType)
73+
{
74+
foreach (DimensionUnitType type in Enum.GetValues(typeof(DimensionUnitType)))
75+
{
76+
if (type.GetDescription() == unitStr)
77+
{
78+
unitType = type;
79+
return true;
80+
}
81+
}
82+
unitType = default;
83+
84+
return false;
85+
}
86+
87+
public static Dimension FromPoints(double points)
88+
{
89+
return new Dimension(points, DimensionUnitType.Points);
90+
}
91+
92+
public static Dimension FromPixels(double pixels)
93+
{
94+
return new Dimension(pixels, DimensionUnitType.Pixels);
95+
}
96+
97+
public static Dimension FromInches(double inches)
98+
{
99+
return new Dimension(inches, DimensionUnitType.Inches);
100+
}
101+
102+
public static Dimension FromMillimeters(double millimeters)
103+
{
104+
return new Dimension(millimeters, DimensionUnitType.Millimeters);
105+
}
106+
107+
public static Dimension FromCentimeters(double centimeters)
108+
{
109+
return new Dimension(centimeters, DimensionUnitType.Centimeters);
110+
}
111+
112+
public static Dimension FromPicas(double picas)
113+
{
114+
return new Dimension(picas, DimensionUnitType.Picas);
115+
}
116+
117+
public override string ToString()
118+
{
119+
return $"{Value}{UnitType.GetDescription()}";
120+
}
121+
122+
public override bool Equals(object? obj)
123+
{
124+
return Equals(obj as Dimension);
125+
}
126+
127+
public override int GetHashCode()
128+
{
129+
return HashCode.Combine(Value, UnitType);
130+
}
131+
132+
public static bool operator ==(Dimension? left, Dimension? right)
133+
{
134+
return Equals(left, right);
135+
}
136+
137+
public static bool operator !=(Dimension? left, Dimension? right)
138+
{
139+
return !Equals(left, right);
140+
}
141+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2019-2025 Chris Mohan, Jaben Cargman
2+
// and GotenbergSharpApiClient Contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System.ComponentModel;
17+
18+
namespace Gotenberg.Sharp.API.Client.Domain.Dimensions
19+
{
20+
public enum DimensionUnitType
21+
{
22+
[Description("pt")] Points, // Points
23+
[Description("px")] Pixels, // Pixels
24+
[Description("in")] Inches, // Inches
25+
[Description("mm")] Millimeters, // Millimeters
26+
[Description("cm")] Centimeters, // Centimeters
27+
[Description("pc")] Picas // Picas
28+
}
29+
}

0 commit comments

Comments
 (0)