From 65a4c141ba6ee7ad42b6dbad36bcccf7d46b42c1 Mon Sep 17 00:00:00 2001 From: Kasper Frank Date: Fri, 7 May 2021 12:55:36 +0200 Subject: [PATCH] Consolidate DictionaryToken extension methods --- .../Filters/CcittFaxDecodeFilter.cs | 13 ++-- .../Filters/DecodeParameterResolver.cs | 9 +-- .../Filters/DefaultFilterProvider.cs | 7 +- .../Filters/FilterProviderWithLookup.cs | 7 +- src/UglyToad.PdfPig/PdfExtensions.cs | 43 ----------- .../Util/ColorSpaceDetailsParser.cs | 4 +- .../Util/DictionaryTokenExtensions.cs | 71 ++++++++++++++----- 7 files changed, 76 insertions(+), 78 deletions(-) diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs index bf677d9f..9fe84246 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using Tokens; + using UglyToad.PdfPig.Util; /// /// Decodes image data that has been encoded using either Group 3 or Group 4. @@ -21,9 +22,9 @@ { var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex); - var cols = decodeParms.GetInt(NameToken.Columns, 1728); - var rows = decodeParms.GetInt(NameToken.Rows, 0); - var height = streamDictionary.GetInt(NameToken.Height, NameToken.H, 0); + var cols = decodeParms.GetIntOrDefault(NameToken.Columns, 1728); + var rows = decodeParms.GetIntOrDefault(NameToken.Rows, 0); + var height = streamDictionary.GetIntOrDefault(NameToken.Height, NameToken.H, 0); if (rows > 0 && height > 0) { // PDFBOX-771, PDFBOX-3727: rows in DecodeParms sometimes contains an incorrect value @@ -35,8 +36,8 @@ rows = Math.Max(rows, height); } - var k = decodeParms.GetInt(NameToken.K, 0); - var encodedByteAlign = decodeParms.GetBoolean(NameToken.EncodedByteAlign, false); + var k = decodeParms.GetIntOrDefault(NameToken.K, 0); + var encodedByteAlign = decodeParms.GetBooleanOrDefault(NameToken.EncodedByteAlign, false); var compressionType = DetermineCompressionType(input, k); using (var stream = new CcittFaxDecoderStream(new MemoryStream(input.ToArray()), cols, compressionType, encodedByteAlign)) { @@ -45,7 +46,7 @@ ReadFromDecoderStream(stream, decompressed); // we expect black to be 1, if not invert the bitmap - var blackIsOne = decodeParms.GetBoolean(NameToken.BlackIs1, false); + var blackIsOne = decodeParms.GetBooleanOrDefault(NameToken.BlackIs1, false); if (!blackIsOne) { InvertBitmap(decompressed); diff --git a/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs b/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs index 973b45a9..296f053f 100644 --- a/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs +++ b/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs @@ -2,8 +2,9 @@ { using System; using System.Collections.Generic; - using Tokens; - + using Tokens; + using UglyToad.PdfPig.Util; + internal static class DecodeParameterResolver { public static DictionaryToken GetFilterParameters(DictionaryToken streamDictionary, int index) @@ -18,9 +19,9 @@ throw new ArgumentOutOfRangeException(nameof(index), "Index must be 0 or greater"); } - var filter = streamDictionary.GetDictionaryObject(NameToken.Filter, NameToken.F); + var filter = streamDictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F); - var parameters = streamDictionary.GetDictionaryObject(NameToken.DecodeParms, NameToken.Dp); + var parameters = streamDictionary.GetObjectOrDefault(NameToken.DecodeParms, NameToken.Dp); switch (filter) { diff --git a/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs b/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs index ffd41ad1..67b1f421 100644 --- a/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs +++ b/src/UglyToad.PdfPig/Filters/DefaultFilterProvider.cs @@ -4,8 +4,9 @@ using System.Collections.Generic; using System.Linq; using Core; - using Tokens; - + using Tokens; + using UglyToad.PdfPig.Util; + /// /// /// The default implementation of the . @@ -60,7 +61,7 @@ throw new ArgumentNullException(nameof(dictionary)); } - var token = dictionary.GetDictionaryObject(NameToken.Filter, NameToken.F); + var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F); if (token == null) { return EmptyArray.Instance; diff --git a/src/UglyToad.PdfPig/Filters/FilterProviderWithLookup.cs b/src/UglyToad.PdfPig/Filters/FilterProviderWithLookup.cs index efc95d85..9b1c334f 100644 --- a/src/UglyToad.PdfPig/Filters/FilterProviderWithLookup.cs +++ b/src/UglyToad.PdfPig/Filters/FilterProviderWithLookup.cs @@ -6,8 +6,9 @@ using Core; using Parser.Parts; using Tokenization.Scanner; - using Tokens; - + using Tokens; + using UglyToad.PdfPig.Util; + internal class FilterProviderWithLookup : ILookupFilterProvider { private readonly IFilterProvider inner; @@ -33,7 +34,7 @@ throw new ArgumentNullException(nameof(dictionary)); } - var token = dictionary.GetDictionaryObject(NameToken.Filter, NameToken.F); + var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F); if (token == null) { return EmptyArray.Instance; diff --git a/src/UglyToad.PdfPig/PdfExtensions.cs b/src/UglyToad.PdfPig/PdfExtensions.cs index cb9f527f..9d415455 100644 --- a/src/UglyToad.PdfPig/PdfExtensions.cs +++ b/src/UglyToad.PdfPig/PdfExtensions.cs @@ -47,49 +47,6 @@ return typedToken; } - internal static IToken GetDictionaryObject(this DictionaryToken dictionary, NameToken name) - { - if (dictionary.TryGet(name, out var token)) - { - return token; - } - - return null; - } - - internal static IToken GetDictionaryObject(this DictionaryToken dictionary, NameToken first, NameToken second) - { - if (dictionary.TryGet(first, out var token)) - { - return token; - } - - if (dictionary.TryGet(second, out token)) - { - return token; - } - - return null; - } - - internal static int GetInt(this DictionaryToken dictionary, NameToken name, int defaultValue) - { - var numericToken = dictionary.GetDictionaryObject(name) as NumericToken; - return numericToken?.Int ?? defaultValue; - } - - internal static int GetInt(this DictionaryToken dictionary, NameToken first, NameToken second, int defaultValue) - { - var numericToken = dictionary.GetDictionaryObject(first, second) as NumericToken; - return numericToken?.Int ?? defaultValue; - } - - internal static bool GetBoolean(this DictionaryToken dictionary, NameToken name, bool defaultValue) - { - var booleanToken = dictionary.GetDictionaryObject(name) as BooleanToken; - return booleanToken?.Data ?? defaultValue; - } - /// /// Get the decoded data from this stream. /// diff --git a/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs b/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs index 17e4130d..9c451d13 100644 --- a/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs +++ b/src/UglyToad.PdfPig/Util/ColorSpaceDetailsParser.cs @@ -75,9 +75,9 @@ ILookupFilterProvider filterProvider, bool cannotRecurse = false) { - if (imageDictionary.GetDictionaryObject(NameToken.ImageMask, NameToken.Im) != null) + if (imageDictionary.GetObjectOrDefault(NameToken.ImageMask, NameToken.Im) != null) { - var decodeRaw = imageDictionary.GetDictionaryObject(NameToken.Decode, NameToken.D) as ArrayToken + var decodeRaw = imageDictionary.GetObjectOrDefault(NameToken.Decode, NameToken.D) as ArrayToken ?? new ArrayToken(EmptyArray.Instance); var decode = decodeRaw.Data.OfType().Select(x => x.Data).ToArray(); return IndexedColorSpaceDetails.Stencil(decode); diff --git a/src/UglyToad.PdfPig/Util/DictionaryTokenExtensions.cs b/src/UglyToad.PdfPig/Util/DictionaryTokenExtensions.cs index e1117c8a..5a9d909c 100644 --- a/src/UglyToad.PdfPig/Util/DictionaryTokenExtensions.cs +++ b/src/UglyToad.PdfPig/Util/DictionaryTokenExtensions.cs @@ -9,6 +9,33 @@ internal static class DictionaryTokenExtensions { + [CanBeNull] + public static IToken GetObjectOrDefault(this DictionaryToken token, NameToken name) + { + if (token.TryGet(name, out var obj)) + { + return obj; + } + + return null; + } + + [CanBeNull] + public static IToken GetObjectOrDefault(this DictionaryToken token, NameToken first, NameToken second) + { + if (token.TryGet(first, out var obj)) + { + return obj; + } + + if (token.TryGet(second, out obj)) + { + return obj; + } + + return null; + } + public static int GetInt(this DictionaryToken token, NameToken name) { if (token == null) @@ -16,12 +43,9 @@ throw new ArgumentNullException(nameof(token)); } - if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric)) - { - throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {token}."); - } + var numeric = token.GetObjectOrDefault(name) as NumericToken; - return numeric.Int; + return numeric?.Int ?? throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {token}."); } public static int GetIntOrDefault(this DictionaryToken token, NameToken name, int defaultValue = 0) @@ -31,12 +55,21 @@ throw new ArgumentNullException(nameof(token)); } - if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric)) + var numeric = token.GetObjectOrDefault(name) as NumericToken; + + return numeric?.Int ?? defaultValue; + } + + public static int GetIntOrDefault(this DictionaryToken token, NameToken first, NameToken second, int defaultValue = 0) + { + if (token == null) { - return defaultValue; + throw new ArgumentNullException(nameof(token)); } - return numeric.Int; + var numeric = token.GetObjectOrDefault(first, second) as NumericToken; + + return numeric?.Int ?? default; } public static long? GetLongOrDefault(this DictionaryToken token, NameToken name) @@ -46,12 +79,21 @@ throw new ArgumentNullException(nameof(token)); } - if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric)) + var numeric = token.GetObjectOrDefault(name) as NumericToken; + + return numeric?.Long; + } + + public static bool GetBooleanOrDefault(this DictionaryToken token, NameToken name, bool defaultValue) + { + if (token == null) { - return null; + throw new ArgumentNullException(nameof(token)); } - return numeric.Long; + var boolean = token.GetObjectOrDefault(name) as BooleanToken; + + return boolean?.Data ?? defaultValue; } [CanBeNull] @@ -62,12 +104,7 @@ throw new ArgumentNullException(nameof(token)); } - if (!token.TryGet(name, out var nameToken) || !(nameToken is NameToken result)) - { - return null; - } - - return result; + return token.GetObjectOrDefault(name) as NameToken; } public static bool TryGetOptionalTokenDirect(this DictionaryToken token, NameToken name, IPdfTokenScanner scanner, out T result) where T : IToken