From a0462f495ceb21bf2255d61e7c090ea82cdaa761 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 19 Jan 2024 20:01:23 -0800 Subject: [PATCH] LibPDF+MacPDF: Clip text, and add a debug option for disabling it --- Meta/Lagom/Contrib/MacPDF/MacPDFView.h | 1 + Meta/Lagom/Contrib/MacPDF/MacPDFView.mm | 12 ++++++++++++ Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h | 1 + Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm | 5 +++++ Meta/Lagom/Contrib/MacPDF/MainMenu.xib | 6 ++++++ Userland/Libraries/LibPDF/Renderer.cpp | 4 ++++ Userland/Libraries/LibPDF/Renderer.h | 1 + 7 files changed, 30 insertions(+) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.h b/Meta/Lagom/Contrib/MacPDF/MacPDFView.h index 1f176809ee1..dd0ed1065ea 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.h +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.h @@ -29,5 +29,6 @@ - (IBAction)toggleShowClippingPaths:(id)sender; - (IBAction)toggleClipImages:(id)sender; - (IBAction)toggleClipPaths:(id)sender; +- (IBAction)toggleClipText:(id)sender; @end diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm index 52450341044..1c072a8214e 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm @@ -177,6 +177,10 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) [item setState:_preferences.clip_paths ? NSControlStateValueOn : NSControlStateValueOff]; return _doc ? YES : NO; } + if ([item action] == @selector(toggleClipText:)) { + [item setState:_preferences.clip_text ? NSControlStateValueOn : NSControlStateValueOff]; + return _doc ? YES : NO; + } return NO; } @@ -204,6 +208,14 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) } } +- (IBAction)toggleClipText:(id)sender +{ + if (_doc) { + _preferences.clip_text = !_preferences.clip_text; + [self invalidateCachedBitmap]; + } +} + - (void)keyDown:(NSEvent*)event { // Calls moveLeft: or moveRight: below. diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h index a3387dc2d56..7b11f0a58fe 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h @@ -24,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN - (IBAction)toggleShowClippingPaths:(id)sender; - (IBAction)toggleClipImages:(id)sender; - (IBAction)toggleClipPaths:(id)sender; +- (IBAction)toggleClipText:(id)sender; - (IBAction)showGoToPageDialog:(id)sender; - (void)pdfDidInitialize; diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm index fa355c395fa..bf00df65135 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm @@ -154,6 +154,11 @@ [_pdfView toggleClipPaths:sender]; } +- (IBAction)toggleClipText:(id)sender +{ + [_pdfView toggleClipText:sender]; +} + - (IBAction)showGoToPageDialog:(id)sender { auto alert = [[NSAlert alloc] init]; diff --git a/Meta/Lagom/Contrib/MacPDF/MainMenu.xib b/Meta/Lagom/Contrib/MacPDF/MainMenu.xib index 9e833db67ef..f36878cef66 100644 --- a/Meta/Lagom/Contrib/MacPDF/MainMenu.xib +++ b/Meta/Lagom/Contrib/MacPDF/MainMenu.xib @@ -682,6 +682,12 @@ + + + + + + diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 8a18338fc3f..8830143de2d 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -1011,6 +1011,10 @@ PDFErrorOr Renderer::show_text(ByteString const& string) if (!text_state().font) return Error::rendering_unsupported_error("Can't draw text because an invalid font was in use"); + OwnPtr clip_raii; + if (m_rendering_preferences.clip_text) + clip_raii = make(*this); + auto start_position = Gfx::FloatPoint { 0.0f, 0.0f }; auto end_position = TRY(text_state().font->draw_string(m_painter, start_position, string, *this)); diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index a3c8d128581..407f40fb2a6 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -92,6 +92,7 @@ struct RenderingPreferences { bool clip_images { true }; bool clip_paths { true }; + bool clip_text { true }; unsigned hash() const {