diff --git a/readme.md b/readme.md index adf8e36..1ed3a72 100644 --- a/readme.md +++ b/readme.md @@ -52,6 +52,27 @@ public static void Initialize() `VerifyImageMagick.RegisterComparers` (provided by https://github.com/VerifyTests/Verify.ImageMagick) allows minor image changes to be ignored. +### Outputs + +By default each pdf is split into a png per page and the text of each page (in the info). `Initialize` accepts a `DocNetOutputs` flags enum to choose which of these are produced globally: + + * `Png`: render each page to a png. + * `Text`: extract the text of each page into the info. + * `All` (default): both. + +Omitted outputs are not rendered/extracted at all. The source pdf snapshot is not affected (use `VerifierSettings.ExcludeTargets("pdf")` for that). + + + +```cs +[ModuleInitializer] +public static void Initialize() => + VerifyDocNet.Initialize(DocNetOutputs.Text); +``` +snippet source | anchor + + + ### Verify a file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7b738ea..a14812b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;NU1608;NU1109 - 3.6.0 + 3.7.0 1.0.0 preview false diff --git a/src/StaticSettingsTests/ModuleInitializer.cs b/src/StaticSettingsTests/ModuleInitializer.cs new file mode 100644 index 0000000..00fcd16 --- /dev/null +++ b/src/StaticSettingsTests/ModuleInitializer.cs @@ -0,0 +1,10 @@ +public static class ModuleInitializer +{ + #region InitializeOutputs + + [ModuleInitializer] + public static void Initialize() => + VerifyDocNet.Initialize(DocNetOutputs.Text); + + #endregion +} diff --git a/src/StaticSettingsTests/StaticSettingsTests.csproj b/src/StaticSettingsTests/StaticSettingsTests.csproj new file mode 100644 index 0000000..11fd57c --- /dev/null +++ b/src/StaticSettingsTests/StaticSettingsTests.csproj @@ -0,0 +1,19 @@ + + + net10.0 + Exe + + + + + + + + + + + + + + + diff --git a/src/StaticSettingsTests/TextOnlyTests.VerifyPdf#pdf.verified.pdf b/src/StaticSettingsTests/TextOnlyTests.VerifyPdf#pdf.verified.pdf new file mode 100644 index 0000000..f2036d9 Binary files /dev/null and b/src/StaticSettingsTests/TextOnlyTests.VerifyPdf#pdf.verified.pdf differ diff --git a/src/StaticSettingsTests/TextOnlyTests.VerifyPdf.verified.txt b/src/StaticSettingsTests/TextOnlyTests.VerifyPdf.verified.txt new file mode 100644 index 0000000..a3766c3 --- /dev/null +++ b/src/StaticSettingsTests/TextOnlyTests.VerifyPdf.verified.txt @@ -0,0 +1,29 @@ +{ + Version: 1.4, + PageCount: 2, + Pages: [ + { + Text: + A Simple PDF File + This is a small demonstration .pdf file - + just for use in the Virtual Mechanics tutorials. More text. And more + text. And more text. And more text. And more text. + And more text. And more text. And more text. And more text. And more + text. And more text. Boring, zzzzz. And more text. And more text. And + more text. And more text. And more text. And more text. And more text. + And more text. And more text. + And more text. And more text. And more text. And more text. And more + text. And more text. And more text. Even more. Continued on page 2 ... + }, + { + Index: 1, + Text: + Simple PDF File 2 + ...continued from page 1. Yet more text. And more text. And more text. + And more text. And more text. And more text. And more text. And more + text. Oh, how boring typing this stuff. But not as boring as watching + paint dry. And more text. And more text. And more text. And more text. + Boring. More, a little more text. The end, and just as well. + } + ] +} \ No newline at end of file diff --git a/src/StaticSettingsTests/TextOnlyTests.cs b/src/StaticSettingsTests/TextOnlyTests.cs new file mode 100644 index 0000000..91fd60c --- /dev/null +++ b/src/StaticSettingsTests/TextOnlyTests.cs @@ -0,0 +1,6 @@ +public class TextOnlyTests +{ + [Test] + public Task VerifyPdf() => + VerifyFile("sample.pdf"); +} diff --git a/src/Verify.DocNet.slnx b/src/Verify.DocNet.slnx index 3c64655..09cf009 100644 --- a/src/Verify.DocNet.slnx +++ b/src/Verify.DocNet.slnx @@ -10,6 +10,7 @@ + \ No newline at end of file diff --git a/src/Verify.DocNet/DocNetOutputs.cs b/src/Verify.DocNet/DocNetOutputs.cs new file mode 100644 index 0000000..bbefe94 --- /dev/null +++ b/src/Verify.DocNet/DocNetOutputs.cs @@ -0,0 +1,21 @@ +namespace VerifyTests; + +/// +/// Controls which outputs a pdf is split into. Passed to . +/// The source pdf snapshot is not controlled by this (use VerifierSettings.ExcludeTargets("pdf")). +/// +[Flags] +public enum DocNetOutputs +{ + /// No outputs. Only the source document and info are emitted. + None = 0, + + /// Render each page to a png. When omitted, pages are not rendered. + Png = 1, + + /// Extract the text of each page into the info. When omitted, text is not extracted. + Text = 2, + + /// All outputs. + All = Png | Text +} diff --git a/src/Verify.DocNet/Verify.DocNet.csproj b/src/Verify.DocNet/Verify.DocNet.csproj index ccea32f..0385782 100644 --- a/src/Verify.DocNet/Verify.DocNet.csproj +++ b/src/Verify.DocNet/Verify.DocNet.csproj @@ -1,6 +1,6 @@ - net6.0;net7.0;net8.0;net9.0;net10.0 + net8.0;net9.0;net10.0 diff --git a/src/Verify.DocNet/VerifyDocNet.cs b/src/Verify.DocNet/VerifyDocNet.cs index 1d7ca67..87f1793 100644 --- a/src/Verify.DocNet/VerifyDocNet.cs +++ b/src/Verify.DocNet/VerifyDocNet.cs @@ -4,7 +4,10 @@ public static partial class VerifyDocNet { public static bool Initialized { get; private set; } - public static void Initialize() + static DocNetOutputs outputs = DocNetOutputs.All; + + /// The outputs each pdf is split into. Defaults to . + public static void Initialize(DocNetOutputs outputs = DocNetOutputs.All) { if (Initialized) { @@ -12,6 +15,7 @@ public static void Initialize() } Initialized = true; + VerifyDocNet.outputs = outputs; InnerVerifier.ThrowIfVerifyHasBeenRun(); VerifierSettings.RegisterStreamConverter("pdf", Convert); diff --git a/src/Verify.DocNet/VerifyDocNet_Pdf.cs b/src/Verify.DocNet/VerifyDocNet_Pdf.cs index bbc9831..8209921 100644 --- a/src/Verify.DocNet/VerifyDocNet_Pdf.cs +++ b/src/Verify.DocNet/VerifyDocNet_Pdf.cs @@ -69,12 +69,21 @@ static ConversionResult Convert(string? name, IDocReader document, IReadOnlyDict } var preserveTransparency = settings.GetPreserveTransparency(); + var includePng = outputs.HasFlag(DocNetOutputs.Png); + var includeText = outputs.HasFlag(DocNetOutputs.Text); var targets = new List(); var pages = new List(); for (var index = start; index < pagesToInclude; index++) { using var reader = document.GetPageReader(index); + pages.Add(new() { Index = index, Text = includeText ? reader.GetText() : null }); + + if (!includePng) + { + continue; + } + var rawBytes = preserveTransparency ? reader.GetImage() : reader.GetImage(transparencyRemover); @@ -85,8 +94,6 @@ static ConversionResult Convert(string? name, IDocReader document, IReadOnlyDict var stream = new MemoryStream(); PngEncoder.WriteBgraAsPng(rawBytes, width, height, stream); targets.Add(new("png", stream, name)); - - pages.Add(new() { Index = index, Text = reader.GetText() }); } var info = new PdfInfo