1313// See the License for the specific language governing permissions and
1414// limitations under the License.
1515
16- using System . ComponentModel ;
1716using System . Globalization ;
18- using System . Reflection ;
19- using System . Runtime . CompilerServices ;
2017using System . Text . RegularExpressions ;
2118
2219namespace Gotenberg . Sharp . API . Client . Domain . Dimensions ;
@@ -54,9 +51,6 @@ namespace Gotenberg.Sharp.API.Client.Domain.Dimensions;
5451/// </summary>
5552public 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