diff --git a/Meta/Lagom/Contrib/MacPDF/AppDelegate.h b/Meta/Lagom/Contrib/MacPDF/AppDelegate.h new file mode 100644 index 0000000000..1063b120ca --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/AppDelegate.h @@ -0,0 +1,12 @@ +// +// AppDelegate.h +// SerenityPDF +// +// Created by Nico Weber on 7/22/23. +// + +#import + +@interface AppDelegate : NSObject + +@end diff --git a/Meta/Lagom/Contrib/MacPDF/AppDelegate.m b/Meta/Lagom/Contrib/MacPDF/AppDelegate.m new file mode 100644 index 0000000000..8fd4af0768 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/AppDelegate.m @@ -0,0 +1,37 @@ +// +// AppDelegate.m +// SerenityPDF +// +// Created by Nico Weber on 7/22/23. +// + +#import "AppDelegate.h" + +@interface AppDelegate () + +@property (strong) IBOutlet NSWindow* window; +@end + +@implementation AppDelegate + +- (void)applicationDidFinishLaunching:(NSNotification*)aNotification +{ + // Insert code here to initialize your application +} + +- (void)applicationWillTerminate:(NSNotification*)aNotification +{ + // Insert code here to tear down your application +} + +- (BOOL)applicationSupportsSecureRestorableState:(NSApplication*)app +{ + 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 new file mode 100644 index 0000000000..b359b798a2 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib @@ -0,0 +1,695 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h new file mode 100644 index 0000000000..27ac77c0d5 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h @@ -0,0 +1,15 @@ +// +// PDFView.h +// SerenityPDF +// +// Created by Nico Weber on 7/22/23. +// + +#pragma once + +#import + +@interface LagomPDFView : NSView +{ +} +@end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm new file mode 100644 index 0000000000..216da53d68 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm @@ -0,0 +1,79 @@ +// +// PDFView.m +// SerenityPDF +// +// Created by Nico Weber on 7/22/23. +// + +#import "LagomPDFView.h" + +// #define USING_AK_GLOBALLY 0 + +#include +#include +#include +#include +#include + +RefPtr s_file; + +static PDF::PDFErrorOr> load() +{ + auto source_root = DeprecatedString("/Users/thakis/src/serenity"); + Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root)); + + NSLog(@"before file"); + s_file = TRY(Core::MappedFile::map("/Users/thakis/src/hack/sample.pdf"sv)); + NSLog(@"got file"); + auto document = TRY(PDF::Document::create(s_file->bytes())); + TRY(document->initialize()); + return document; +} + +static PDF::PDFErrorOr> render(PDF::Document& document) +{ + NSLog(@"num pages %@", @(document.get_page_count())); + int page_index = 0; + auto page = TRY(document.get_page(page_index)); + auto page_size = Gfx::IntSize { 800, round_to(800 * page.media_box.height() / page.media_box.width()) }; + + auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); + + auto errors = PDF::Renderer::render(document, page, bitmap, PDF::RenderingPreferences {}); + if (errors.is_error()) { + for (auto const& error : errors.error().errors()) + NSLog(@"warning: %@", @(error.message().characters())); + } + + return bitmap; +} + +@implementation LagomPDFView + +- (void)drawRect:(NSRect)rect +{ + static auto doc_or = load(); + if (!doc_or.is_error()) { + auto doc = doc_or.value(); + auto bitmap_or = render(*doc); + if (!bitmap_or.is_error()) { + auto& bitmap_o = bitmap_or.value(); + auto& bitmap = bitmap_o.leak_ref(); + auto space = CGColorSpaceCreateDeviceRGB(); + CGBitmapInfo info = kCGBitmapByteOrder32Little | kCGImageAlphaFirst; + auto data = CGDataProviderCreateWithData( + &bitmap, bitmap.begin(), bitmap.size_in_bytes(), + [](void* p, void const*, size_t) { /* XXX adoptRef again */ }); + auto cgbmp = CGImageCreate(bitmap.width(), bitmap.height(), 8, + 32, bitmap.width() * 4, space, + info, data, nullptr, false, kCGRenderingIntentDefault); + auto* nsbmp = [[NSBitmapImageRep alloc] initWithCGImage:cgbmp]; + [nsbmp drawAtPoint:NSMakePoint(0, 0)]; + CGImageRelease(cgbmp); + } + } else { + NSLog(@"failed to load: %@", @(doc_or.error().message().characters())); + } +} + +@end diff --git a/Meta/Lagom/Contrib/MacPDF/SerenityPDF.entitlements b/Meta/Lagom/Contrib/MacPDF/SerenityPDF.entitlements new file mode 100644 index 0000000000..311b32bd20 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/SerenityPDF.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + + + diff --git a/Meta/Lagom/Contrib/MacPDF/main.m b/Meta/Lagom/Contrib/MacPDF/main.m new file mode 100644 index 0000000000..3eda97b856 --- /dev/null +++ b/Meta/Lagom/Contrib/MacPDF/main.m @@ -0,0 +1,16 @@ +// +// main.m +// SerenityPDF +// +// Created by Nico Weber on 7/22/23. +// + +#import + +int main(int argc, char const* argv[]) +{ + @autoreleasepool { + // Setup code that might create autoreleased objects goes here. + } + return NSApplicationMain(argc, argv); +}