Skip to content

Commit

Permalink
Enable nullable annotations (#803)
Browse files Browse the repository at this point in the history
* Enable nullable annotations

* Remove unused Jetbrain annotations

* Ensure system using statements are first

* Improve nullability annotations

* Annotate encryptionDictionary is non-null when IsEncrypted is true

* Disable nullable for PdfTokenScanner.Get

* Improve nullability annotations for ObjectLocationProvider.TryGetCached

* Revert changes to RGBWorkingSpace

* Update UglyToad.PdfPig.Package with new framework targets (fixes nightly builds)
  • Loading branch information
iamcarbon authored Mar 17, 2024
1 parent bf6c519 commit a412a23
Show file tree
Hide file tree
Showing 189 changed files with 1,392 additions and 2,384 deletions.
2 changes: 0 additions & 2 deletions src/UglyToad.PdfPig/AcroForms/AcroForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Core;
using Fields;
using Tokens;
using Util.JetBrains.Annotations;

/// <summary>
/// A collection of interactive fields for gathering data from a user through dropdowns, textboxes, checkboxes, etc.
Expand All @@ -22,7 +21,6 @@ public class AcroForm
/// <summary>
/// The raw PDF dictionary which is the root form object.
/// </summary>
[NotNull]
public DictionaryToken Dictionary { get; }

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/AcroForms/AcroFormExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public static IEnumerable<AcroFieldBase> GetFields(this AcroFieldBase fieldBase)
/// <summary>
/// Get string values of field.
/// </summary>
public static KeyValuePair<string, string> GetFieldValue(this AcroFieldBase fieldBase)
public static KeyValuePair<string?, string?> GetFieldValue(this AcroFieldBase fieldBase)
{
return fieldBase switch
{
AcroTextField textField => new(textField.Information.PartialName, textField.Value),
AcroCheckboxField checkboxField => new(checkboxField.Information.PartialName, checkboxField.IsChecked.ToString()),
AcroCheckboxField checkboxField => new(checkboxField.Information.PartialName, checkboxField.IsChecked.ToString())!,
_ => new(fieldBase.Information.PartialName, ""),
};
}
Expand Down
92 changes: 45 additions & 47 deletions src/UglyToad.PdfPig/AcroForms/AcroFormFactory.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/AcroForms/Fields/AcroChoiceOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AcroChoiceOption
/// <summary>
/// The value of the option when the form is exported.
/// </summary>
public string ExportValue { get; }
public string? ExportValue { get; }

/// <summary>
/// Whether the field defined an export value for this option.
Expand All @@ -33,7 +33,7 @@ public class AcroChoiceOption
/// <summary>
/// Create a new <see cref="AcroChoiceOption"/>.
/// </summary>
public AcroChoiceOption(int index, bool isSelected, string name, string exportValue = null)
public AcroChoiceOption(int index, bool isSelected, string name, string? exportValue = null)
{
Index = index;
IsSelected = isSelected;
Expand Down
13 changes: 6 additions & 7 deletions src/UglyToad.PdfPig/AcroForms/Fields/AcroComboBoxField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using Core;
using Tokens;
using Util.JetBrains.Annotations;

/// <inheritdoc />
/// <summary>
Expand All @@ -21,20 +20,17 @@ public class AcroComboBoxField : AcroFieldBase
/// <summary>
/// The options to be presented to the user.
/// </summary>
[NotNull]
public IReadOnlyList<AcroChoiceOption> Options { get; }

/// <summary>
/// The names of any currently selected options.
/// </summary>
[NotNull]
public IReadOnlyList<string> SelectedOptions { get; }

/// <summary>
/// For multiple select lists with duplicate names gives the indices of the selected options.
/// </summary>
[CanBeNull]
public IReadOnlyList<int> SelectedOptionIndices { get; }
public IReadOnlyList<int>? SelectedOptionIndices { get; }

/// <inheritdoc />
/// <summary>
Expand All @@ -49,10 +45,13 @@ public class AcroComboBoxField : AcroFieldBase
/// <param name="selectedOptions">The names of the selected options.</param>
/// <param name="pageNumber">The number of the page this field appears on.</param>
/// <param name="bounds">The location of this field on the page.</param>
public AcroComboBoxField(DictionaryToken dictionary, string fieldType, AcroChoiceFieldFlags fieldFlags,
public AcroComboBoxField(
DictionaryToken dictionary,
string fieldType,
AcroChoiceFieldFlags fieldFlags,
AcroFieldCommonInformation information, IReadOnlyList<AcroChoiceOption> options,
IReadOnlyList<string> selectedOptions,
IReadOnlyList<int> selectedOptionIndices,
IReadOnlyList<int>? selectedOptionIndices,
int? pageNumber,
PdfRectangle? bounds) :
base(dictionary, fieldType, (uint)fieldFlags, AcroFieldType.ComboBox, information,
Expand Down
6 changes: 1 addition & 5 deletions src/UglyToad.PdfPig/AcroForms/Fields/AcroFieldBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace UglyToad.PdfPig.AcroForms.Fields
{
using System;
using Core;
using System;
using Tokens;
using Util.JetBrains.Annotations;

/// <summary>
/// A field in an interactive <see cref="AcroForm"/>.
Expand All @@ -13,13 +12,11 @@ public abstract class AcroFieldBase
/// <summary>
/// The raw PDF dictionary for this field.
/// </summary>
[NotNull]
public DictionaryToken Dictionary { get; }

/// <summary>
/// The <see cref="string"/> representing the type of this field in PDF format.
/// </summary>
[NotNull]
public string RawFieldType { get; }

/// <summary>
Expand All @@ -35,7 +32,6 @@ public abstract class AcroFieldBase
/// <summary>
/// The optional information common to all types of field.
/// </summary>
[NotNull]
public AcroFieldCommonInformation Information { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace UglyToad.PdfPig.AcroForms.Fields
{
using Core;
using Util.JetBrains.Annotations;

/// <summary>
/// Information from the field dictionary which is common across all field types.
Expand All @@ -18,26 +17,23 @@ public class AcroFieldCommonInformation
/// The partial field name for this field. The fully qualified field name is the
/// period '.' joined name of all parents' partial names and this field's partial name.
/// </summary>
[CanBeNull]
public string PartialName { get; }
public string? PartialName { get; }

/// <summary>
/// The alternate field name to be used instead of the fully qualified field name where
/// the field is being identified on the user interface or by screen readers.
/// </summary>
[CanBeNull]
public string AlternateName { get; }
public string? AlternateName { get; }

/// <summary>
/// The mapping name used when exporting form field data from the document.
/// </summary>
[CanBeNull]
public string MappingName { get; }
public string? MappingName { get; }

/// <summary>
/// Create a new <see cref="AcroFieldCommonInformation"/>.
/// </summary>
public AcroFieldCommonInformation(IndirectReference? parent, string partialName, string alternateName, string mappingName)
public AcroFieldCommonInformation(IndirectReference? parent, string? partialName, string? alternateName, string? mappingName)
{
Parent = parent;
PartialName = partialName;
Expand All @@ -48,7 +44,7 @@ public AcroFieldCommonInformation(IndirectReference? parent, string partialName,
/// <inheritdoc />
public override string ToString()
{
string AppendIfNotNull(string val, string label, string result)
string AppendIfNotNull(string? val, string label, string result)
{
if (val is null)
{
Expand Down
8 changes: 2 additions & 6 deletions src/UglyToad.PdfPig/AcroForms/Fields/AcroListBoxField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using Core;
using Tokens;
using Util.JetBrains.Annotations;

/// <inheritdoc />
/// <summary>
Expand All @@ -20,20 +19,17 @@ public class AcroListBoxField : AcroFieldBase
/// <summary>
/// The options to be presented to the user.
/// </summary>
[NotNull]
public IReadOnlyList<AcroChoiceOption> Options { get; }

/// <summary>
/// The names of any currently selected options.
/// </summary>
[NotNull]
public IReadOnlyList<string> SelectedOptions { get; }

/// <summary>
/// For multiple select lists with duplicate names gives the indices of the selected options.
/// </summary>
[CanBeNull]
public IReadOnlyList<int> SelectedOptionIndices { get; }
public IReadOnlyList<int>? SelectedOptionIndices { get; }

/// <summary>
/// For scrollable list boxes gives the index of the first visible option.
Expand Down Expand Up @@ -62,7 +58,7 @@ public class AcroListBoxField : AcroFieldBase
public AcroListBoxField(DictionaryToken dictionary, string fieldType, AcroChoiceFieldFlags fieldFlags,
AcroFieldCommonInformation information, IReadOnlyList<AcroChoiceOption> options,
IReadOnlyList<string> selectedOptions,
IReadOnlyList<int> selectedOptionIndices,
IReadOnlyList<int>? selectedOptionIndices,
int? topIndex,
int? pageNumber,
PdfRectangle? bounds) :
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/AcroForms/Fields/AcroTextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AcroTextField : AcroFieldBase
/// The value of the text in this text field.
/// This can be <see langword="null"/> if no value has been set.
/// </summary>
public string Value { get; }
public string? Value { get; }

/// <summary>
/// The optional maximum length of the text field.
Expand Down Expand Up @@ -50,7 +50,7 @@ public class AcroTextField : AcroFieldBase
/// <param name="bounds">The location of this field on the page.</param>
public AcroTextField(DictionaryToken dictionary, string fieldType, AcroTextFieldFlags fieldFlags,
AcroFieldCommonInformation information,
string value,
string? value,
int? maxLength,
int? pageNumber,
PdfRectangle? bounds) :
Expand Down
15 changes: 8 additions & 7 deletions src/UglyToad.PdfPig/Actions/ActionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
namespace UglyToad.PdfPig.Actions
{
using System.Diagnostics.CodeAnalysis;
using Core;
using Logging;
using Outline;
using Outline.Destinations;
using Tokenization.Scanner;
using Tokens;
using Outline.Destinations;
using Util;

internal static class ActionProvider
{
/// <summary>
/// Get an action (A) from dictionary. If GoTo, GoToR or GoToE, also fetches the action destination.
/// </summary>
internal static bool TryGetAction(DictionaryToken dictionary,
internal static bool TryGetAction(
DictionaryToken dictionary,
NamedDestinations namedDestinations,
IPdfTokenScanner pdfScanner,
ILog log,
out PdfAction result)
[NotNullWhen(true)] out PdfAction? result)
{
result = null;

if (!dictionary.TryGet(NameToken.A, pdfScanner, out DictionaryToken actionDictionary))
if (!dictionary.TryGet(NameToken.A, pdfScanner, out DictionaryToken? actionDictionary))
{
return false;
}

if (!actionDictionary.TryGet(NameToken.S, pdfScanner, out NameToken actionType))
if (!actionDictionary.TryGet(NameToken.S, pdfScanner, out NameToken? actionType))
{
throw new PdfDocumentFormatException($"No action type (/S) specified for action: {actionDictionary}.");
}
Expand Down Expand Up @@ -81,7 +82,7 @@ internal static bool TryGetAction(DictionaryToken dictionary,
fileSpecification = null;
}

result = new GoToEAction(destination, fileSpecification);
result = new GoToEAction(destination, fileSpecification!);
return true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/UglyToad.PdfPig/Actions/UriAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace UglyToad.PdfPig.Actions
#nullable disable

namespace UglyToad.PdfPig.Actions
{
/// <summary>
/// Action to open a URI
Expand Down
15 changes: 8 additions & 7 deletions src/UglyToad.PdfPig/AdvancedPdfDocumentAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content;
using Core;
using Filters;
Expand Down Expand Up @@ -37,14 +38,14 @@ internal AdvancedPdfDocumentAccess(IPdfTokenScanner pdfScanner,
/// </summary>
/// <param name="embeddedFiles">The set of embedded files in this document.</param>
/// <returns><see langword="true"/> if this document contains more than zero embedded files, otherwise <see langword="false"/>.</returns>
public bool TryGetEmbeddedFiles(out IReadOnlyList<EmbeddedFile> embeddedFiles)
public bool TryGetEmbeddedFiles([NotNullWhen(true)] out IReadOnlyList<EmbeddedFile>? embeddedFiles)
{
GuardDisposed();

embeddedFiles = null;

if (!catalog.CatalogDictionary.TryGet(NameToken.Names, pdfScanner, out DictionaryToken namesDictionary)
|| !namesDictionary.TryGet(NameToken.EmbeddedFiles, pdfScanner, out DictionaryToken embeddedFileNamesDictionary))
if (!catalog.CatalogDictionary.TryGet(NameToken.Names, pdfScanner, out DictionaryToken? namesDictionary)
|| !namesDictionary.TryGet(NameToken.EmbeddedFiles, pdfScanner, out DictionaryToken? embeddedFileNamesDictionary))
{
return false;
}
Expand All @@ -60,15 +61,15 @@ public bool TryGetEmbeddedFiles(out IReadOnlyList<EmbeddedFile> embeddedFiles)

foreach (var keyValuePair in embeddedFileNames)
{
if (!DirectObjectFinder.TryGet(keyValuePair.Value, pdfScanner, out DictionaryToken fileDescriptorDictionaryToken)
|| !fileDescriptorDictionaryToken.TryGet(NameToken.Ef, pdfScanner, out DictionaryToken efDictionary)
|| !efDictionary.TryGet(NameToken.F, pdfScanner, out StreamToken fileStreamToken))
if (!DirectObjectFinder.TryGet(keyValuePair.Value, pdfScanner, out DictionaryToken? fileDescriptorDictionaryToken)
|| !fileDescriptorDictionaryToken.TryGet(NameToken.Ef, pdfScanner, out DictionaryToken? efDictionary)
|| !efDictionary.TryGet(NameToken.F, pdfScanner, out StreamToken? fileStreamToken))
{
continue;
}

var fileSpecification = string.Empty;
if (fileDescriptorDictionaryToken.TryGet(NameToken.F, pdfScanner, out IDataToken<string> fileSpecificationToken))
if (fileDescriptorDictionaryToken.TryGet(NameToken.F, pdfScanner, out IDataToken<string>? fileSpecificationToken))
{
fileSpecification = fileSpecificationToken.Data;
}
Expand Down
Loading

0 comments on commit a412a23

Please sign in to comment.