#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading; using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; using Lumina.Plugins.Contracts.Core.Reading; using Lumina.Plugins.Pdf.Core.Pdf; using System.Collections.Generic; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Plugins.Pdf.Core; /// /// Book reader for the PDF format. /// /// /// See for how a PDF is decoded; this class only declares the supported format and delegates the decoding to the parser. /// internal sealed class PdfReader : IBookReader { /// /// Gets the file extensions supported by the reader. /// public IReadOnlyList SupportedExtensions => [".pdf"]; /// /// Gets the media library types the reader supports. /// public IReadOnlyList SupportedLibraryTypes => [LibraryType.EBook, LibraryType.Book]; /// /// Opens the PDF stored at , extracting its pages into , /// and returns its normalized reading document. When the PDF is rendered as page images, the page images are not rendered /// here: a page is rendered only when its resource is requested, so that opening a large book is instant. /// /// The file system path of the PDF. /// The directory into which the pages of the PDF are extracted. /// Whether the PDF is rendered as page images instead of extracting its text layer. This is used for scanned PDFs whose pages are only images and therefore yield no text. /// Cancellation token that can be used to stop the execution. /// The normalized reading document of the PDF. public Task OpenAsync(string path, string workingDirectory, bool shouldRenderPdfAsImages, CancellationToken cancellationToken) { return Task.FromResult(PdfDocumentParser.Parse(path, workingDirectory, shouldRenderPdfAsImages, cancellationToken)); } /// /// Produces the resource identified by of the PDF stored at , /// rendering the requested page to an image on demand. Only the page image whose resource is requested is rendered, /// so that the user does not wait for every page of a large book to be rendered before seeing the first one. /// /// The file system path of the PDF. /// The directory into which the pages of the PDF are extracted. /// The opaque resource key of the resource to produce, of the form page:{n}. /// Cancellation token that can be used to stop the execution. /// The binary data of the rendered page image. /// /// The PDFtoImage and SkiaSharp APIs used by are annotated with the supported /// platforms of the rendering engine, so this method must repeat that restriction or the CA1416 analyzer, which is a build error /// here, flags the call. The attributes must not be removed, even though Windows, Linux and macOS are the only platforms Lumina runs on. /// [SupportedOSPlatform("windows")] [SupportedOSPlatform("linux")] [SupportedOSPlatform("macos")] public Task GetResourceAsync(string path, string workingDirectory, string resourceKey, CancellationToken cancellationToken) { byte[] data = PdfDocumentParser.RenderResource(path, resourceKey, cancellationToken); return Task.FromResult(data); } }