1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
- using System . Collections . ObjectModel ;
5
4
using System . Linq ;
6
5
using System . Management . Automation ;
7
6
using System . Management . Automation . Language ;
@@ -40,6 +39,8 @@ public class TransformToSKSizeIAttribute : ArgumentTransformationAttribute
40
39
public override object Transform ( EngineIntrinsics engineIntrinsics , object inputData )
41
40
{
42
41
int sideLength = 0 ;
42
+ SKSizeI ? result ;
43
+
43
44
switch ( inputData )
44
45
{
45
46
case SKSize sk :
@@ -72,75 +73,42 @@ public override object Transform(EngineIntrinsics engineIntrinsics, object input
72
73
{
73
74
sideLength = ( int ) ul ;
74
75
}
76
+
75
77
break ;
76
78
case decimal d :
77
79
if ( d <= int . MaxValue )
78
80
{
79
81
sideLength = ( int ) Math . Round ( d ) ;
80
82
}
83
+
81
84
break ;
82
85
case float f :
83
86
if ( f <= int . MaxValue )
84
87
{
85
88
sideLength = ( int ) Math . Round ( f ) ;
86
89
}
90
+
87
91
break ;
88
92
case double d :
89
93
if ( d <= int . MaxValue )
90
94
{
91
95
sideLength = ( int ) Math . Round ( d ) ;
92
96
}
97
+
93
98
break ;
94
99
case string s :
95
- if ( WCUtils . StandardImageSizes . ContainsKey ( s ) )
96
- {
97
- return WCUtils . StandardImageSizes [ s ] . Size ;
98
- }
99
- else
100
+ result = GetSizeFromString ( s ) ;
101
+ if ( result is not null )
100
102
{
101
- var matchWH = Regex . Match ( s , @"^(?<Width>[\d\.,]+)x(?<Height>[\d\.,]+)(px)?$" ) ;
102
- if ( matchWH . Success )
103
- {
104
- try
105
- {
106
- var width = int . Parse ( matchWH . Groups [ "Width" ] . Value ) ;
107
- var height = int . Parse ( matchWH . Groups [ "Height" ] . Value ) ;
108
-
109
- return new SKSizeI ( width , height ) ;
110
- }
111
- catch ( Exception e )
112
- {
113
- throw new ArgumentTransformationMetadataException (
114
- "Could not parse input string as a float value" , e ) ;
115
- }
116
- }
117
-
118
- var matchSide = Regex . Match ( s , @"^(?<SideLength>[\d\.,]+)(px)?$" ) ;
119
- if ( matchSide . Success )
120
- {
121
- sideLength = int . Parse ( matchSide . Groups [ "SideLength" ] . Value ) ;
122
- }
103
+ return result ;
123
104
}
124
105
125
106
break ;
126
107
case object o :
127
- IEnumerable properties ;
128
- if ( o is Hashtable ht )
129
- {
130
- properties = ht ;
131
- }
132
- else
108
+ result = GetSizeFromProperties ( o ) ;
109
+ if ( result is not null )
133
110
{
134
- properties = PSObject . AsPSObject ( o ) . Properties ;
135
- }
136
-
137
- if ( properties . GetValue ( "Width" ) != null && properties . GetValue ( "Height" ) != null )
138
- {
139
- // If these conversions fail, the exception will cause the transform to fail.
140
- var width = properties . GetValue ( "Width" ) . ConvertTo < int > ( ) ;
141
- var height = properties . GetValue ( "Height" ) . ConvertTo < int > ( ) ;
142
-
143
- return new SKSizeI ( width , height ) ;
111
+ return result ;
144
112
}
145
113
146
114
break ;
@@ -154,6 +122,71 @@ public override object Transform(EngineIntrinsics engineIntrinsics, object input
154
122
var errorMessage = $ "Unrecognisable input '{ inputData } ' for SKSize parameter. See the help documentation for the parameter for allowed values.";
155
123
throw new ArgumentTransformationMetadataException ( errorMessage ) ;
156
124
}
125
+
126
+ private SKSizeI ? GetSizeFromString ( string str )
127
+ {
128
+ if ( WCUtils . StandardImageSizes . ContainsKey ( str ) )
129
+ {
130
+ return WCUtils . StandardImageSizes [ str ] . Size ;
131
+ }
132
+ else
133
+ {
134
+ var matchWH = Regex . Match ( str , @"^(?<Width>[\d\.,]+)x(?<Height>[\d\.,]+)(px)?$" ) ;
135
+ if ( matchWH . Success )
136
+ {
137
+ try
138
+ {
139
+ var width = int . Parse ( matchWH . Groups [ "Width" ] . Value ) ;
140
+ var height = int . Parse ( matchWH . Groups [ "Height" ] . Value ) ;
141
+
142
+ return new SKSizeI ( width , height ) ;
143
+ }
144
+ catch ( Exception e )
145
+ {
146
+ throw new ArgumentTransformationMetadataException (
147
+ "Could not parse input string as an integer value" , e ) ;
148
+ }
149
+ }
150
+
151
+ var matchSide = Regex . Match ( str , @"^(?<SideLength>[\d\.,]+)(px)?$" ) ;
152
+ if ( matchSide . Success )
153
+ {
154
+ var sideLength = int . Parse ( matchSide . Groups [ "SideLength" ] . Value ) ;
155
+ return new SKSizeI ( sideLength , sideLength ) ;
156
+ }
157
+ }
158
+
159
+ return null ;
160
+ }
161
+
162
+ private SKSizeI ? GetSizeFromProperties ( object obj )
163
+ {
164
+ IEnumerable properties ;
165
+ if ( obj is Hashtable ht )
166
+ {
167
+ properties = ht ;
168
+ }
169
+ else
170
+ {
171
+ properties = PSObject . AsPSObject ( obj ) . Properties ;
172
+ }
173
+
174
+ if ( properties . GetValue ( "Width" ) is not null && properties . GetValue ( "Height" ) is not null )
175
+ {
176
+ // If these conversions fail, the exception will cause the transform to fail.
177
+ object ? width = properties . GetValue ( "Width" ) ;
178
+ object ? height = properties . GetValue ( "Height" ) ;
179
+
180
+ if ( width is null || height is null )
181
+ {
182
+ return null ;
183
+ }
184
+
185
+ return new SKSizeI ( width . ConvertTo < int > ( ) , height . ConvertTo < int > ( ) ) ;
186
+ }
187
+
188
+ return null ;
189
+ }
157
190
}
158
191
159
192
public class FontFamilyCompleter : IArgumentCompleter
@@ -210,21 +243,24 @@ private static SKTypeface CreateTypefaceFromObject(object input)
210
243
}
211
244
212
245
SKFontStyle style ;
213
- if ( properties . GetValue ( "FontWeight" ) != null
214
- || properties . GetValue ( "FontSlant" ) != null
215
- || properties . GetValue ( "FontWidth" ) != null )
246
+ if ( properties . GetValue ( "FontWeight" ) is not null
247
+ || properties . GetValue ( "FontSlant" ) is not null
248
+ || properties . GetValue ( "FontWidth" ) is not null )
216
249
{
217
- SKFontStyleWeight weight = properties . GetValue ( "FontWeight" ) == null
250
+ object ? weightValue = properties . GetValue ( "FontWeight" ) ;
251
+ SKFontStyleWeight weight = weightValue is null
218
252
? SKFontStyleWeight . Normal
219
- : properties . GetValue ( "FontWeight" ) . ConvertTo < SKFontStyleWeight > ( ) ;
253
+ : weightValue . ConvertTo < SKFontStyleWeight > ( ) ;
220
254
221
- SKFontStyleSlant slant = properties . GetValue ( "FontSlant" ) == null
255
+ object ? slantValue = properties . GetValue ( "FontSlant" ) ;
256
+ SKFontStyleSlant slant = slantValue is null
222
257
? SKFontStyleSlant . Upright
223
- : properties . GetValue ( "FontSlant" ) . ConvertTo < SKFontStyleSlant > ( ) ;
258
+ : slantValue . ConvertTo < SKFontStyleSlant > ( ) ;
224
259
225
- SKFontStyleWidth width = properties . GetValue ( "FontWidth" ) == null
260
+ object ? widthValue = properties . GetValue ( "FontWidth" ) ;
261
+ SKFontStyleWidth width = widthValue is null
226
262
? SKFontStyleWidth . Normal
227
- : properties . GetValue ( "FontWidth" ) . ConvertTo < SKFontStyleWidth > ( ) ;
263
+ : widthValue . ConvertTo < SKFontStyleWidth > ( ) ;
228
264
229
265
style = new SKFontStyle ( weight , width , slant ) ;
230
266
}
@@ -235,7 +271,7 @@ private static SKTypeface CreateTypefaceFromObject(object input)
235
271
: SKFontStyle . Normal ;
236
272
}
237
273
238
- string familyName = properties . GetValue ( "FamilyName" ) . ConvertTo < string > ( ) ;
274
+ string familyName = properties . GetValue ( "FamilyName" ) ? . ConvertTo < string > ( ) ?? string . Empty ;
239
275
return WCUtils . FontManager . MatchFamily ( familyName , style ) ;
240
276
}
241
277
@@ -301,21 +337,25 @@ private SKColor[] TransformObject(object input)
301
337
properties = PSObject . AsPSObject ( item ) . Properties ;
302
338
}
303
339
304
- byte red = properties . GetValue ( "red" ) == null
340
+ object ? redValue = properties . GetValue ( "red" ) ;
341
+ byte red = redValue is null
305
342
? ( byte ) 0
306
- : properties . GetValue ( "red" ) . ConvertTo < byte > ( ) ;
343
+ : redValue . ConvertTo < byte > ( ) ;
307
344
308
- byte green = properties . GetValue ( "green" ) == null
345
+ object ? greenValue = properties . GetValue ( "green" ) ;
346
+ byte green = greenValue is null
309
347
? ( byte ) 0
310
- : properties . GetValue ( "green" ) . ConvertTo < byte > ( ) ;
348
+ : greenValue . ConvertTo < byte > ( ) ;
311
349
312
- byte blue = properties . GetValue ( "blue" ) == null
350
+ object ? blueValue = properties . GetValue ( "blue" ) ;
351
+ byte blue = blueValue is null
313
352
? ( byte ) 0
314
- : properties . GetValue ( "blue" ) . ConvertTo < byte > ( ) ;
353
+ : blueValue . ConvertTo < byte > ( ) ;
315
354
316
- byte alpha = properties . GetValue ( "alpha" ) == null
355
+ object ? alphaValue = properties . GetValue ( "alpha" ) ;
356
+ byte alpha = alphaValue is null
317
357
? ( byte ) 255
318
- : properties . GetValue ( "alpha" ) . ConvertTo < byte > ( ) ;
358
+ : alphaValue . ConvertTo < byte > ( ) ;
319
359
320
360
colorList . Add ( new SKColor ( red , green , blue , alpha ) ) ;
321
361
}
0 commit comments