Skip to content

Commit 0a01a2c

Browse files
nicoawesomekling
authored andcommitted
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
1 parent 77b311f commit 0a01a2c

File tree

7 files changed

+181
-25
lines changed

7 files changed

+181
-25
lines changed

Meta/Lagom/Contrib/MacPDF/AppDelegate.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,4 @@ - (BOOL)applicationSupportsSecureRestorableState:(NSApplication*)app
2929
return YES;
3030
}
3131

32-
- (IBAction)openDocument:(id)sender
33-
{
34-
NSLog(@"open");
35-
}
36-
3732
@end

Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
5-
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
4+
<deployment identifier="macosx"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
66
</dependencies>
77
<objects>
88
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
@@ -12,11 +12,7 @@
1212
</customObject>
1313
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
1414
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
15-
<customObject id="Voe-Tx-rLC" customClass="AppDelegate">
16-
<connections>
17-
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
18-
</connections>
19-
</customObject>
15+
<customObject id="Voe-Tx-rLC" customClass="AppDelegate"/>
2016
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
2117
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
2218
<items>
@@ -680,16 +676,5 @@
680676
</items>
681677
<point key="canvasLocation" x="200" y="121"/>
682678
</menu>
683-
<window title="SerenityPDF" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
684-
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
685-
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
686-
<rect key="contentRect" x="335" y="390" width="705" height="1017"/>
687-
<rect key="screenRect" x="0.0" y="0.0" width="1728" height="1079"/>
688-
<view key="contentView" id="EiT-Mj-1SZ" customClass="LagomPDFView">
689-
<rect key="frame" x="0.0" y="0.0" width="705" height="1017"/>
690-
<autoresizingMask key="autoresizingMask"/>
691-
</view>
692-
<point key="canvasLocation" x="312.5" y="728.5"/>
693-
</window>
694679
</objects>
695680
</document>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDocumentTypes</key>
6+
<array>
7+
<dict>
8+
<key>CFBundleTypeName</key>
9+
<string>PDF</string>
10+
<key>CFBundleTypeRole</key>
11+
<string>Viewer</string>
12+
<key>LSHandlerRank</key>
13+
<string>Default</string>
14+
<key>LSItemContentTypes</key>
15+
<array>
16+
<string>com.adobe.pdf</string>
17+
</array>
18+
<key>NSDocumentClass</key>
19+
<string>LagomPDFDocument</string>
20+
</dict>
21+
</array>
22+
</dict>
23+
</plist>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// LagomPDFDocument.h
3+
// SerenityPDF
4+
//
5+
// Created by Nico Weber on 9/21/23.
6+
//
7+
8+
// Several AK types conflict with MacOS types.
9+
#define FixedPoint FixedPointMacOS
10+
#import <Cocoa/Cocoa.h>
11+
#undef FixedPoint
12+
13+
NS_ASSUME_NONNULL_BEGIN
14+
15+
@interface LagomPDFDocument : NSDocument
16+
17+
@end
18+
19+
NS_ASSUME_NONNULL_END
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
3+
<dependencies>
4+
<deployment identifier="macosx"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
7+
</dependencies>
8+
<objects>
9+
<customObject id="-2" userLabel="File's Owner" customClass="LagomPDFDocument">
10+
<connections>
11+
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
12+
</connections>
13+
</customObject>
14+
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
15+
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
16+
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="xOd-HO-29H" userLabel="Window">
17+
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
18+
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
19+
<rect key="contentRect" x="133" y="235" width="507" height="413"/>
20+
<rect key="screenRect" x="0.0" y="0.0" width="1728" height="1079"/>
21+
<value key="minSize" type="size" width="94" height="86"/>
22+
<view key="contentView" id="gIp-Ho-8D9" customClass="LagomPDFView">
23+
<rect key="frame" x="0.0" y="0.0" width="507" height="413"/>
24+
<autoresizingMask key="autoresizingMask"/>
25+
</view>
26+
<connections>
27+
<outlet property="delegate" destination="-2" id="0bl-1N-x8E"/>
28+
</connections>
29+
<point key="canvasLocation" x="80" y="126"/>
30+
</window>
31+
</objects>
32+
</document>

Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ @implementation LagomPDFView
6969
Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root));
7070

7171
_file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/pdf_reference_1-7.pdf"sv));
72+
// _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/DC-008-Translation-2023-E.pdf"sv));
73+
// _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/ISO_32000-2-2020_sponsored.pdf"sv));
74+
// _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/Text.pdf"sv));
7275
auto document = TRY(PDF::Document::create(_file->bytes()));
7376
TRY(document->initialize());
7477
return document;
@@ -87,7 +90,7 @@ - (void)drawRect:(NSRect)rect
8790
{
8891
static bool did_load = false;
8992
if (!did_load) {
90-
_page_index = 0;
93+
_page_index = 24 - 1;
9194
did_load = true;
9295
if (auto doc_or = [self load]; !doc_or.is_error()) {
9396
_doc = doc_or.value();

0 commit comments

Comments
 (0)