Make DictionaryTokenExtensions and ArrayTokenExtensions public

This commit is contained in:
BobLd 2024-06-08 09:41:12 +01:00
parent affc1ed8b5
commit 531e00e9c9
2 changed files with 129 additions and 37 deletions

View File

@ -257,9 +257,11 @@
"UglyToad.PdfPig.Structure", "UglyToad.PdfPig.Structure",
"UglyToad.PdfPig.Tokenization.Scanner.IPdfTokenScanner", "UglyToad.PdfPig.Tokenization.Scanner.IPdfTokenScanner",
"UglyToad.PdfPig.Util.Adler32Checksum", "UglyToad.PdfPig.Util.Adler32Checksum",
"UglyToad.PdfPig.Util.ArrayTokenExtensions",
"UglyToad.PdfPig.Util.IWordExtractor", "UglyToad.PdfPig.Util.IWordExtractor",
"UglyToad.PdfPig.Util.DefaultWordExtractor", "UglyToad.PdfPig.Util.DefaultWordExtractor",
"UglyToad.PdfPig.Util.DateFormatHelper", "UglyToad.PdfPig.Util.DateFormatHelper",
"UglyToad.PdfPig.Util.DictionaryTokenExtensions",
"UglyToad.PdfPig.Util.WhitespaceSizeStatistics", "UglyToad.PdfPig.Util.WhitespaceSizeStatistics",
"UglyToad.PdfPig.Writer.ITokenWriter", "UglyToad.PdfPig.Writer.ITokenWriter",
"UglyToad.PdfPig.Writer.PdfAStandard", "UglyToad.PdfPig.Writer.PdfAStandard",

View File

@ -7,11 +7,20 @@
using Tokenization.Scanner; using Tokenization.Scanner;
using Tokens; using Tokens;
internal static class DictionaryTokenExtensions /// <summary>
/// <see cref="DictionaryToken"/> extensions.
/// </summary>
public static class DictionaryTokenExtensions
{ {
public static IToken? GetObjectOrDefault(this DictionaryToken token, NameToken name) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <returns>The token, if it is found. <c>null</c> otherwise.</returns>
public static IToken? GetObjectOrDefault(this DictionaryToken dictionaryToken, NameToken name)
{ {
if (token.TryGet(name, out var obj)) if (dictionaryToken.TryGet(name, out var obj))
{ {
return obj; return obj;
} }
@ -19,14 +28,21 @@
return null; return null;
} }
public static IToken? GetObjectOrDefault(this DictionaryToken token, NameToken first, NameToken second) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="first">The name of the entry to retrieve first.</param>
/// <param name="second">The name of the entry to retrieve if the first one is not found.</param>
/// <returns>The token, if it is found. <c>null</c> otherwise.</returns>
public static IToken? GetObjectOrDefault(this DictionaryToken dictionaryToken, NameToken first, NameToken second)
{ {
if (token.TryGet(first, out var obj)) if (dictionaryToken.TryGet(first, out var obj))
{ {
return obj; return obj;
} }
if (token.TryGet(second, out obj)) if (dictionaryToken.TryGet(second, out obj))
{ {
return obj; return obj;
} }
@ -34,81 +50,129 @@
return null; return null;
} }
public static int GetInt(this DictionaryToken token, NameToken name) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <returns>The <see langword="int"/> token.</returns>
public static int GetInt(this DictionaryToken dictionaryToken, NameToken name)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
var numeric = token.GetObjectOrDefault(name) as NumericToken; var numeric = dictionaryToken.GetObjectOrDefault(name) as NumericToken;
return numeric?.Int ?? throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {token}."); return numeric?.Int ?? throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {dictionaryToken}.");
} }
public static int GetIntOrDefault(this DictionaryToken token, NameToken name, int defaultValue = 0) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <param name="defaultValue">The default value to return if entry is not found.</param>
/// <returns>The <see langword="int"/> token, if it is found. The default value otherwise.</returns>
public static int GetIntOrDefault(this DictionaryToken dictionaryToken, NameToken name, int defaultValue = 0)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
var numeric = token.GetObjectOrDefault(name) as NumericToken; var numeric = dictionaryToken.GetObjectOrDefault(name) as NumericToken;
return numeric?.Int ?? defaultValue; return numeric?.Int ?? defaultValue;
} }
public static int GetIntOrDefault(this DictionaryToken token, NameToken first, NameToken second, int defaultValue = 0) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="first">The name of the entry to retrieve first.</param>
/// <param name="second">The name of the entry to retrieve if the first one is not found.</param>
/// <param name="defaultValue">The default value to return if entry is not found.</param>
/// <returns>The <see langword="int"/> token, if it is found. The default value otherwise.</returns>
public static int GetIntOrDefault(this DictionaryToken dictionaryToken, NameToken first, NameToken second, int defaultValue = 0)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
var numeric = token.GetObjectOrDefault(first, second) as NumericToken; var numeric = dictionaryToken.GetObjectOrDefault(first, second) as NumericToken;
return numeric?.Int ?? default; return numeric?.Int ?? default;
} }
public static long? GetLongOrDefault(this DictionaryToken token, NameToken name) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <returns>The token, if it is found. <c>null</c> otherwise.</returns>
public static long? GetLongOrDefault(this DictionaryToken dictionaryToken, NameToken name)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
var numeric = token.GetObjectOrDefault(name) as NumericToken; var numeric = dictionaryToken.GetObjectOrDefault(name) as NumericToken;
return numeric?.Long; return numeric?.Long;
} }
public static bool GetBooleanOrDefault(this DictionaryToken token, NameToken name, bool defaultValue) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <param name="defaultValue">The default value to return if entry is not found.</param>
/// <returns>The <see langword="bool"/> token, if it is found. The default value otherwise.</returns>
public static bool GetBooleanOrDefault(this DictionaryToken dictionaryToken, NameToken name, bool defaultValue)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
var boolean = token.GetObjectOrDefault(name) as BooleanToken; var boolean = dictionaryToken.GetObjectOrDefault(name) as BooleanToken;
return boolean?.Data ?? defaultValue; return boolean?.Data ?? defaultValue;
} }
public static NameToken? GetNameOrDefault(this DictionaryToken token, NameToken name) /// <summary>
/// Get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <returns>The token, if it is found. <c>null</c> otherwise.</returns>
public static NameToken? GetNameOrDefault(this DictionaryToken dictionaryToken, NameToken name)
{ {
if (token is null) if (dictionaryToken is null)
{ {
throw new ArgumentNullException(nameof(token)); throw new ArgumentNullException(nameof(dictionaryToken));
} }
return token.GetObjectOrDefault(name) as NameToken; return dictionaryToken.GetObjectOrDefault(name) as NameToken;
} }
public static bool TryGetOptionalTokenDirect<T>(this DictionaryToken token, NameToken name, IPdfTokenScanner scanner, [NotNullWhen(true)] out T? result) /// <summary>
/// Try and get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <param name="scanner">The Pdf token scanner</param>
/// <param name="result">The entry.</param>
/// <returns><see langword="true"/> if the token is found, <see langword="false"/> otherwise.</returns>
public static bool TryGetOptionalTokenDirect<T>(this DictionaryToken dictionaryToken, NameToken name, IPdfTokenScanner scanner, [NotNullWhen(true)] out T? result)
where T : class, IToken where T : class, IToken
{ {
result = null; result = null;
if (token.TryGet(name, out var appearancesToken) && DirectObjectFinder.TryGet(appearancesToken, scanner, out T? innerResult)) if (dictionaryToken.TryGet(name, out var appearancesToken) && DirectObjectFinder.TryGet(appearancesToken, scanner, out T? innerResult))
{ {
result = innerResult; result = innerResult;
return true; return true;
@ -117,16 +181,24 @@
return false; return false;
} }
public static bool TryGetOptionalStringDirect(this DictionaryToken token, NameToken name, IPdfTokenScanner scanner, [NotNullWhen(true)] out string? result) /// <summary>
/// Try and get the entry with a given name.
/// </summary>
/// <param name="dictionaryToken">The <see cref="DictionaryToken"/>.</param>
/// <param name="name">The name of the entry to retrieve.</param>
/// <param name="scanner">The Pdf token scanner.</param>
/// <param name="result">The entry.</param>
/// <returns><see langword="true"/> if the token is found, <see langword="false"/> otherwise.</returns>
public static bool TryGetOptionalStringDirect(this DictionaryToken dictionaryToken, NameToken name, IPdfTokenScanner scanner, [NotNullWhen(true)] out string? result)
{ {
result = null; result = null;
if (token.TryGetOptionalTokenDirect(name, scanner, out StringToken? stringToken)) if (dictionaryToken.TryGetOptionalTokenDirect(name, scanner, out StringToken? stringToken))
{ {
result = stringToken.Data; result = stringToken.Data;
return true; return true;
} }
if (token.TryGetOptionalTokenDirect(name, scanner, out HexToken? hexToken)) if (dictionaryToken.TryGetOptionalTokenDirect(name, scanner, out HexToken? hexToken))
{ {
result = hexToken.Data; result = hexToken.Data;
return true; return true;
@ -136,8 +208,16 @@
} }
} }
internal static class ArrayTokenExtensions /// <summary>
/// <see cref="ArrayTokenExtensions"/> extensions.
/// </summary>
public static class ArrayTokenExtensions
{ {
/// <summary>
/// Get the numeric value at the given index.
/// </summary>
/// <param name="array">The <see cref="ArrayToken"/>.</param>
/// <param name="index">The index.</param>
public static NumericToken GetNumeric(this ArrayToken array, int index) public static NumericToken GetNumeric(this ArrayToken array, int index)
{ {
if (array is null) if (array is null)
@ -158,6 +238,11 @@
throw new PdfDocumentFormatException($"The array did not contain a number at index {index}. Array was: {array}."); throw new PdfDocumentFormatException($"The array did not contain a number at index {index}. Array was: {array}.");
} }
/// <summary>
/// Converts the <see cref="ArrayToken"/> into a <see cref="PdfRectangle"/>.
/// </summary>
/// <param name="array">The <see cref="ArrayToken"/>.</param>
/// <param name="tokenScanner">The Pdf token scanner.</param>
public static PdfRectangle ToRectangle(this ArrayToken array, IPdfTokenScanner tokenScanner) public static PdfRectangle ToRectangle(this ArrayToken array, IPdfTokenScanner tokenScanner)
{ {
if (array is null) if (array is null)
@ -177,6 +262,11 @@
DirectObjectFinder.Get<NumericToken>(array[3], tokenScanner).Double); DirectObjectFinder.Get<NumericToken>(array[3], tokenScanner).Double);
} }
/// <summary>
/// Converts the <see cref="ArrayToken"/> into a <see cref="PdfRectangle"/>.
/// </summary>
/// <param name="array">The <see cref="ArrayToken"/>.</param>
/// <param name="tokenScanner">The Pdf token scanner.</param>
public static PdfRectangle ToIntRectangle(this ArrayToken array, IPdfTokenScanner tokenScanner) public static PdfRectangle ToIntRectangle(this ArrayToken array, IPdfTokenScanner tokenScanner)
{ {
if (array is null) if (array is null)