From 0a01a2c82bcd2cb35a48fbb22d79efa312caed5f Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 21 Sep 2023 14:04:52 -0400 Subject: [PATCH] MacPDF: Start converting to document architecture Cmd-O now produces an Open... dialog, and opening a PDF file reads it (and then creates the old LagomPDFView object, which then reads a hardcoded PDF again and displays that. One thing at a time.) "Open Recent>" is also populated and seems to work. See the guide: https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/Introduction/Introduction.html --- Meta/Lagom/Contrib/MacPDF/AppDelegate.m | 5 - .../Contrib/MacPDF/Base.lproj/MainMenu.xib | 23 +---- Meta/Lagom/Contrib/MacPDF/Info.plist | 23 +++++ Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h | 19 ++++ Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm | 99 +++++++++++++++++++ .../Lagom/Contrib/MacPDF/LagomPDFDocument.xib | 32 ++++++ Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm | 5 +- 7 files changed, 181 insertions(+), 25 deletions(-) create mode 100644 Meta/Lagom/Contrib/MacPDF/Info.plist create mode 100644 Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h create mode 100644 Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm create mode 100644 Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib diff --git a/Meta/Lagom/Contrib/MacPDF/AppDelegate.m b/Meta/Lagom/Contrib/MacPDF/AppDelegate.m index 8fd4af0768..4531875f62 100644 --- a/Meta/Lagom/Contrib/MacPDF/AppDelegate.m +++ b/Meta/Lagom/Contrib/MacPDF/AppDelegate.m @@ -29,9 +29,4 @@ return YES; } -- (IBAction)openDocument:(id)sender -{ - NSLog(@"open"); -} - @end diff --git a/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib b/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib index b359b798a2..937e7cca86 100644 --- a/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib +++ b/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib @@ -1,8 +1,8 @@ - + - - + + @@ -12,11 +12,7 @@ - - - - - + @@ -680,16 +676,5 @@ - - - - - - - - - - - diff --git a/Meta/Lagom/Contrib/MacPDF/Info.plist b/Meta/Lagom/Contrib/MacPDF/Info.plist new file mode 100644 index 0000000000..75e16c7e03 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/Info.plist @@ -0,0 +1,23 @@ + + + + + CFBundleDocumentTypes + + + CFBundleTypeName + PDF + CFBundleTypeRole + Viewer + LSHandlerRank + Default + LSItemContentTypes + + com.adobe.pdf + + NSDocumentClass + LagomPDFDocument + + + + diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h new file mode 100644 index 0000000000..592ef77c0d --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h @@ -0,0 +1,19 @@ +// +// LagomPDFDocument.h +// SerenityPDF +// +// Created by Nico Weber on 9/21/23. +// + +// Several AK types conflict with MacOS types. +#define FixedPoint FixedPointMacOS +#import +#undef FixedPoint + +NS_ASSUME_NONNULL_BEGIN + +@interface LagomPDFDocument : NSDocument + +@end + +NS_ASSUME_NONNULL_END diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm new file mode 100644 index 0000000000..510754ee82 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm @@ -0,0 +1,99 @@ +// +// LagomPDFDocument.m +// SerenityPDF +// +// Created by Nico Weber on 9/21/23. +// + +#import "LagomPDFDocument.h" + +// Several AK types conflict with MacOS types. +#define FixedPoint FixedPointMacOS +#import +#undef FixedPoint + +#import + +#include +#include +#include +#include +#include + +@interface LagomPDFDocument () +{ + NSData* _data; // Strong, _doc refers to it. + RefPtr _doc; +} +@end + +@implementation LagomPDFDocument + +- (PDF::PDFErrorOr>)load:(NSData*)data +{ + auto document = TRY(PDF::Document::create(ReadonlyBytes { [data bytes], [data length] })); + TRY(document->initialize()); + return document; +} + +- (NSString*)windowNibName +{ + // Override to return the nib file name of the document. + // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. + return @"LagomPDFDocument"; +} + +- (void)windowControllerDidLoadNib:(NSWindowController*)aController +{ + [super windowControllerDidLoadNib:aController]; + // Add any code here that needs to be executed once the windowController has loaded the document's window. +} + +- (NSData*)dataOfType:(NSString*)typeName error:(NSError**)outError +{ + // Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error if you return nil. + // Alternatively, you could remove this method and override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. + if (outError) { + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil]; + } + return nil; +} + +- (BOOL)readFromData:(NSData*)data ofType:(NSString*)typeName error:(NSError**)outError +{ + // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error if you return NO. + // Alternatively, you could remove this method and override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. + // If you do, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded. + + if (![[UTType typeWithIdentifier:typeName] conformsToType:UTTypePDF]) { + if (outError) { + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain + code:unimpErr + userInfo:nil]; + } + return NO; + } + + if (auto doc_or = [self load:data]; !doc_or.is_error()) { + _doc = doc_or.value(); + _data = data; + // [self pageChanged]; + + return YES; + } else { + NSLog(@"failed to load: %@", @(doc_or.error().message().characters())); + if (outError) { + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain + code:unimpErr + userInfo:nil]; + } + return NO; + } +} + ++ (BOOL)autosavesInPlace +{ + return YES; +} + +@end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib new file mode 100644 index 0000000000..c8df7b7d7c --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm index 567487bd7c..4106864f16 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm @@ -69,6 +69,9 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root)); _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/pdf_reference_1-7.pdf"sv)); + // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/DC-008-Translation-2023-E.pdf"sv)); + // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/ISO_32000-2-2020_sponsored.pdf"sv)); + // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/Text.pdf"sv)); auto document = TRY(PDF::Document::create(_file->bytes())); TRY(document->initialize()); return document; @@ -87,7 +90,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) { static bool did_load = false; if (!did_load) { - _page_index = 0; + _page_index = 24 - 1; did_load = true; if (auto doc_or = [self load]; !doc_or.is_error()) { _doc = doc_or.value();