mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:37:46 +00:00
MacPDF: Fewer leaks; cache bitmap; refactor a bit
This commit is contained in:
parent
2831654b00
commit
4d9233dacc
1 changed files with 45 additions and 32 deletions
|
@ -15,20 +15,13 @@
|
||||||
#include <LibPDF/Document.h>
|
#include <LibPDF/Document.h>
|
||||||
#include <LibPDF/Renderer.h>
|
#include <LibPDF/Renderer.h>
|
||||||
|
|
||||||
RefPtr<Core::MappedFile> s_file;
|
@interface LagomPDFView ()
|
||||||
|
|
||||||
static PDF::PDFErrorOr<NonnullRefPtr<PDF::Document>> load()
|
|
||||||
{
|
{
|
||||||
auto source_root = DeprecatedString("/Users/thakis/src/serenity");
|
RefPtr<Core::MappedFile> _file;
|
||||||
Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root));
|
RefPtr<PDF::Document> _doc;
|
||||||
|
NSBitmapImageRep* _rep;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
static PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render(PDF::Document& document)
|
static PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render(PDF::Document& document)
|
||||||
{
|
{
|
||||||
|
@ -48,32 +41,52 @@ static PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render(PDF::Document& documen
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation LagomPDFView
|
static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)rect
|
|
||||||
{
|
{
|
||||||
static auto doc_or = load();
|
auto& bitmap = bitmap_p.leak_ref();
|
||||||
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();
|
auto space = CGColorSpaceCreateDeviceRGB();
|
||||||
CGBitmapInfo info = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;
|
CGBitmapInfo info = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;
|
||||||
auto data = CGDataProviderCreateWithData(
|
auto data = CGDataProviderCreateWithData(
|
||||||
&bitmap, bitmap.begin(), bitmap.size_in_bytes(),
|
&bitmap, bitmap.begin(), bitmap.size_in_bytes(),
|
||||||
[](void* p, void const*, size_t) { /* XXX adoptRef again */ });
|
[](void* p, void const*, size_t) { (void)adopt_ref(*reinterpret_cast<Gfx::Bitmap*>(p)); });
|
||||||
auto cgbmp = CGImageCreate(bitmap.width(), bitmap.height(), 8,
|
auto cgbmp = CGImageCreate(bitmap.width(), bitmap.height(), 8,
|
||||||
32, bitmap.width() * 4, space,
|
32, bitmap.pitch(), space,
|
||||||
info, data, nullptr, false, kCGRenderingIntentDefault);
|
info, data, nullptr, false, kCGRenderingIntentDefault);
|
||||||
auto* nsbmp = [[NSBitmapImageRep alloc] initWithCGImage:cgbmp];
|
auto* bmp = [[NSBitmapImageRep alloc] initWithCGImage:cgbmp];
|
||||||
[nsbmp drawAtPoint:NSMakePoint(0, 0)];
|
CGColorSpaceRelease(space);
|
||||||
CGImageRelease(cgbmp);
|
CGImageRelease(cgbmp);
|
||||||
|
return bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@implementation LagomPDFView
|
||||||
|
|
||||||
|
- (PDF::PDFErrorOr<NonnullRefPtr<PDF::Document>>)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");
|
||||||
|
_file = TRY(Core::MappedFile::map("/Users/thakis/src/hack/sample.pdf"sv));
|
||||||
|
NSLog(@"got file");
|
||||||
|
auto document = TRY(PDF::Document::create(_file->bytes()));
|
||||||
|
TRY(document->initialize());
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)drawRect:(NSRect)rect
|
||||||
|
{
|
||||||
|
static bool did_load = false;
|
||||||
|
if (!did_load) {
|
||||||
|
did_load = true;
|
||||||
|
if (auto doc_or = [self load]; !doc_or.is_error()) {
|
||||||
|
_doc = doc_or.value();
|
||||||
|
if (auto bitmap_or = render(*_doc); !bitmap_or.is_error())
|
||||||
|
_rep = ns_from_gfx(bitmap_or.value());
|
||||||
} else {
|
} else {
|
||||||
NSLog(@"failed to load: %@", @(doc_or.error().message().characters()));
|
NSLog(@"failed to load: %@", @(doc_or.error().message().characters()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
[_rep drawAtPoint:NSMakePoint(0, 0)];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue