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
{
switch (Value)
{
case 0:
return 0;
case 90:
return -0.5 * Math.PI;
case 180:
return -Math.PI;
case 270:
return -1.5 * Math.PI;
default:
throw new InvalidOperationException($"Invalid value for rotation: {Value}.");
}
return Value switch {
0 => 0,
90 => -0.5 * Math.PI,
180 => -Math.PI,
270 => -1.5 * Math.PI,
_ => throw new InvalidOperationException($"Invalid value for rotation: {Value}.")
};
}
}

View File

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

View File

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

View File

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

View File

@ -25,21 +25,15 @@
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())
{
case "light":
return WithWeightValues(false, 300);
case "semibold":
return WithWeightValues(true, 600);
case "bold":
return WithWeightValues(true, FontDetails.BoldWeight);
case "black":
return WithWeightValues(true, 900);
default:
return WithWeightValues(false, FontDetails.DefaultWeight);
}
return (font.Weight?.ToLowerInvariant()) switch {
"light" => WithWeightValues(false, 300),
"semibold" => WithWeightValues(true, 600),
"bold" => WithWeightValues(true, FontDetails.BoldWeight),
"black" => WithWeightValues(true, 900),
_ => WithWeightValues(false, FontDetails.DefaultWeight)
};
}
public TransformationMatrix GetFontTransformationMatrix() => fontCollection.GetFirstTransformationMatrix();

View File

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

View File

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