|
| 1 | +// |
| 2 | +// LagomPDFDocument.m |
| 3 | +// SerenityPDF |
| 4 | +// |
| 5 | +// Created by Nico Weber on 9/21/23. |
| 6 | +// |
| 7 | + |
| 8 | +#import "LagomPDFDocument.h" |
| 9 | + |
| 10 | +// Several AK types conflict with MacOS types. |
| 11 | +#define FixedPoint FixedPointMacOS |
| 12 | +#import <Cocoa/Cocoa.h> |
| 13 | +#undef FixedPoint |
| 14 | + |
| 15 | +#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h> |
| 16 | + |
| 17 | +#include <LibCore/File.h> |
| 18 | +#include <LibCore/MappedFile.h> |
| 19 | +#include <LibGfx/Bitmap.h> |
| 20 | +#include <LibPDF/Document.h> |
| 21 | +#include <LibPDF/Renderer.h> |
| 22 | + |
| 23 | +@interface LagomPDFDocument () |
| 24 | +{ |
| 25 | + NSData* _data; // Strong, _doc refers to it. |
| 26 | + RefPtr<PDF::Document> _doc; |
| 27 | +} |
| 28 | +@end |
| 29 | + |
| 30 | +@implementation LagomPDFDocument |
| 31 | + |
| 32 | +- (PDF::PDFErrorOr<NonnullRefPtr<PDF::Document>>)load:(NSData*)data |
| 33 | +{ |
| 34 | + auto document = TRY(PDF::Document::create(ReadonlyBytes { [data bytes], [data length] })); |
| 35 | + TRY(document->initialize()); |
| 36 | + return document; |
| 37 | +} |
| 38 | + |
| 39 | +- (NSString*)windowNibName |
| 40 | +{ |
| 41 | + // Override to return the nib file name of the document. |
| 42 | + // 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. |
| 43 | + return @"LagomPDFDocument"; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)windowControllerDidLoadNib:(NSWindowController*)aController |
| 47 | +{ |
| 48 | + [super windowControllerDidLoadNib:aController]; |
| 49 | + // Add any code here that needs to be executed once the windowController has loaded the document's window. |
| 50 | +} |
| 51 | + |
| 52 | +- (NSData*)dataOfType:(NSString*)typeName error:(NSError**)outError |
| 53 | +{ |
| 54 | + // 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. |
| 55 | + // Alternatively, you could remove this method and override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. |
| 56 | + if (outError) { |
| 57 | + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil]; |
| 58 | + } |
| 59 | + return nil; |
| 60 | +} |
| 61 | + |
| 62 | +- (BOOL)readFromData:(NSData*)data ofType:(NSString*)typeName error:(NSError**)outError |
| 63 | +{ |
| 64 | + // 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. |
| 65 | + // Alternatively, you could remove this method and override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. |
| 66 | + // If you do, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded. |
| 67 | + |
| 68 | + if (![[UTType typeWithIdentifier:typeName] conformsToType:UTTypePDF]) { |
| 69 | + if (outError) { |
| 70 | + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain |
| 71 | + code:unimpErr |
| 72 | + userInfo:nil]; |
| 73 | + } |
| 74 | + return NO; |
| 75 | + } |
| 76 | + |
| 77 | + if (auto doc_or = [self load:data]; !doc_or.is_error()) { |
| 78 | + _doc = doc_or.value(); |
| 79 | + _data = data; |
| 80 | + // [self pageChanged]; |
| 81 | + |
| 82 | + return YES; |
| 83 | + } else { |
| 84 | + NSLog(@"failed to load: %@", @(doc_or.error().message().characters())); |
| 85 | + if (outError) { |
| 86 | + *outError = [NSError errorWithDomain:NSOSStatusErrorDomain |
| 87 | + code:unimpErr |
| 88 | + userInfo:nil]; |
| 89 | + } |
| 90 | + return NO; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | ++ (BOOL)autosavesInPlace |
| 95 | +{ |
| 96 | + return YES; |
| 97 | +} |
| 98 | + |
| 99 | +@end |
0 commit comments