Use switch expressions

This commit is contained in:
Jason Nelson
2024-03-15 10:50:20 -07:00
committed by BobLd
parent dca11253a0
commit 9859c2672b
7 changed files with 67 additions and 130 deletions

View File

@@ -25,19 +25,13 @@
{ {
get get
{ {
switch (Value) return Value switch {
{ 0 => 0,
case 0: 90 => -0.5 * Math.PI,
return 0; 180 => -Math.PI,
case 90: 270 => -1.5 * Math.PI,
return -0.5 * Math.PI; _ => throw new InvalidOperationException($"Invalid value for rotation: {Value}.")
case 180: };
return -Math.PI;
case 270:
return -1.5 * Math.PI;
default:
throw new InvalidOperationException($"Invalid value for rotation: {Value}.");
}
} }
} }

View File

@@ -59,19 +59,11 @@
{ {
var token = trailerDictionary.Identifier[0]; var token = trailerDictionary.Identifier[0];
switch (token) documentIdBytes = token switch {
{ HexToken hex => hex.Bytes.ToArray(),
case HexToken hex: StringToken str => str.GetBytes(),
documentIdBytes = hex.Bytes.ToArray(); _ => OtherEncodings.StringAsLatin1Bytes(token.Data)
break; };
case StringToken str:
documentIdBytes = str.GetBytes();
break;
default:
documentIdBytes = OtherEncodings.StringAsLatin1Bytes(token.Data);
break;
}
} }
else else
{ {
@@ -847,23 +839,12 @@
var sumOfFirstSixteenBytesOfX = x.Take(16).Sum(v => (long)v); var sumOfFirstSixteenBytesOfX = x.Take(16).Sum(v => (long)v);
var mod3 = sumOfFirstSixteenBytesOfX % 3; var mod3 = sumOfFirstSixteenBytesOfX % 3;
HashAlgorithm nextHash = mod3 switch {
HashAlgorithm nextHash; 0 => SHA256.Create(),
switch (mod3) 1 => SHA384.Create(),
{ 2 => SHA512.Create(),
case 0: _ => throw new PdfDocumentEncryptedException("Invalid remainder from summing first sixteen bytes of this round's hash.")
nextHash = SHA256.Create(); };
break;
case 1:
nextHash = SHA384.Create();
break;
case 2:
nextHash = SHA512.Create();
break;
default:
throw new PdfDocumentEncryptedException("Invalid remainder from summing first sixteen bytes of this round's hash.");
}
input = nextHash.ComputeHash(x); input = nextHash.ComputeHash(x);
Array.Copy(input, key, 16); Array.Copy(input, key, 16);
Array.Copy(input, 16, iv, 0, 16); Array.Copy(input, 16, iv, 0, 16);

View File

@@ -99,33 +99,20 @@
/// </summary> /// </summary>
public static NameToken ToNameToken(this ColorSpace colorSpace) public static NameToken ToNameToken(this ColorSpace colorSpace)
{ {
switch (colorSpace) return colorSpace switch {
{ ColorSpace.DeviceGray => NameToken.Devicegray,
case ColorSpace.DeviceGray: ColorSpace.DeviceRGB => NameToken.Devicergb,
return NameToken.Devicegray; ColorSpace.DeviceCMYK => NameToken.Devicecmyk,
case ColorSpace.DeviceRGB: ColorSpace.CalGray => NameToken.Calgray,
return NameToken.Devicergb; ColorSpace.CalRGB => NameToken.Calrgb,
case ColorSpace.DeviceCMYK: ColorSpace.Lab => NameToken.Lab,
return NameToken.Devicecmyk; ColorSpace.ICCBased => NameToken.Iccbased,
case ColorSpace.CalGray: ColorSpace.Indexed => NameToken.Indexed,
return NameToken.Calgray; ColorSpace.Pattern => NameToken.Pattern,
case ColorSpace.CalRGB: ColorSpace.Separation => NameToken.Separation,
return NameToken.Calrgb; ColorSpace.DeviceN => NameToken.Devicen,
case ColorSpace.Lab: _ => throw new ArgumentException($"Unrecognized colorspace: {colorSpace}.")
return NameToken.Lab; };
case ColorSpace.ICCBased:
return NameToken.Iccbased;
case ColorSpace.Indexed:
return NameToken.Indexed;
case ColorSpace.Pattern:
return NameToken.Pattern;
case ColorSpace.Separation:
return NameToken.Separation;
case ColorSpace.DeviceN:
return NameToken.Devicen;
default:
throw new ArgumentException($"Unrecognized colorspace: {colorSpace}.");
}
} }
} }
} }

View File

@@ -28,20 +28,15 @@
{ {
public static RenderingIntent ToRenderingIntent(this string s) public static RenderingIntent ToRenderingIntent(this string s)
{ {
switch (s) return s switch {
{ "AbsoluteColorimetric" => RenderingIntent.AbsoluteColorimetric,
case "AbsoluteColorimetric": "RelativeColorimetric" => RenderingIntent.RelativeColorimetric,
return RenderingIntent.AbsoluteColorimetric; "Saturation" => RenderingIntent.Saturation,
case "RelativeColorimetric": "Perceptual" => RenderingIntent.Perceptual,
return RenderingIntent.RelativeColorimetric;
case "Saturation": // If the application does not recognise the name it uses RelativeColorimetric by default.
return RenderingIntent.Saturation; _ => RenderingIntent.RelativeColorimetric
case "Perceptual": };
return RenderingIntent.Perceptual;
default:
// If the application does not recognise the name it uses RelativeColorimetric by default.
return RenderingIntent.RelativeColorimetric;
}
} }
} }
} }

View File

@@ -25,21 +25,15 @@
return FontDetails.GetDefault(); return FontDetails.GetDefault();
} }
FontDetails WithWeightValues(bool isbold, int weight) => new FontDetails(null, isbold, weight, font.ItalicAngle != 0); FontDetails WithWeightValues(bool isBold, int weight) => new FontDetails(null, isBold, weight, font.ItalicAngle != 0);
switch (font.Weight?.ToLowerInvariant()) return (font.Weight?.ToLowerInvariant()) switch {
{ "light" => WithWeightValues(false, 300),
case "light": "semibold" => WithWeightValues(true, 600),
return WithWeightValues(false, 300); "bold" => WithWeightValues(true, FontDetails.BoldWeight),
case "semibold": "black" => WithWeightValues(true, 900),
return WithWeightValues(true, 600); _ => WithWeightValues(false, FontDetails.DefaultWeight)
case "bold": };
return WithWeightValues(true, FontDetails.BoldWeight);
case "black":
return WithWeightValues(true, 900);
default:
return WithWeightValues(false, FontDetails.DefaultWeight);
}
} }
public TransformationMatrix GetFontTransformationMatrix() => fontCollection.GetFirstTransformationMatrix(); public TransformationMatrix GetFontTransformationMatrix() => fontCollection.GetFirstTransformationMatrix();

View File

@@ -6,27 +6,17 @@
{ {
public static FontStretch ConvertToFontStretch(this NameToken name) public static FontStretch ConvertToFontStretch(this NameToken name)
{ {
switch (name.Data) return name.Data switch {
{ "UltraCondensed" => FontStretch.UltraCondensed,
case "UltraCondensed": "ExtraCondensed" => FontStretch.ExtraCondensed,
return FontStretch.UltraCondensed; "Condensed" => FontStretch.Condensed,
case "ExtraCondensed": "Normal" => FontStretch.Normal,
return FontStretch.ExtraCondensed; "SemiExpanded" => FontStretch.SemiExpanded,
case "Condensed": "Expanded" => FontStretch.Expanded,
return FontStretch.Condensed; "ExtraExpanded" => FontStretch.ExtraExpanded,
case "Normal": "UltraExpanded" => FontStretch.UltraExpanded,
return FontStretch.Normal; _ => FontStretch.Unknown
case "SemiExpanded": };
return FontStretch.SemiExpanded;
case "Expanded":
return FontStretch.Expanded;
case "ExtraExpanded":
return FontStretch.ExtraExpanded;
case "UltraExpanded":
return FontStretch.UltraExpanded;
default:
return FontStretch.Unknown;
}
} }
} }
} }

View File

@@ -57,17 +57,13 @@
// optional // optional
} }
switch (patternType) return patternType switch {
{ // Tiling
case 1: // Tiling 1 => CreateTilingPattern(patternStream, patternExtGState, matrix, scanner),
return CreateTilingPattern(patternStream, patternExtGState, matrix, scanner); // Shading
2 => CreateShadingPattern(patternDictionary, patternExtGState, matrix, scanner, resourceStore, filterProvider),
case 2: // Shading _ => throw new PdfDocumentFormatException($"Invalid Pattern type encountered in page resource dictionary: {patternType}.")
return CreateShadingPattern(patternDictionary, patternExtGState, matrix, scanner, resourceStore, filterProvider); };
default:
throw new PdfDocumentFormatException($"Invalid Pattern type encountered in page resource dictionary: {patternType}.");
}
} }
private static PatternColor CreateTilingPattern(StreamToken patternStream, DictionaryToken patternExtGState, private static PatternColor CreateTilingPattern(StreamToken patternStream, DictionaryToken patternExtGState,