From d04613d25231ce92ca6783c5b6cd1320ab324bee Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Sat, 26 Nov 2022 12:53:32 +0800 Subject: [PATCH] LibPDF: Fix path coordinates calculation Paths rendering was buggy because the map() function that translates points from user space to bitmap space applied the vertical flip conversion that the current transformation matrix already considers; Hence, all paths were upside down. The only exception was the "re" instruction, which manually adjusted the Y coordinate of its points to be flipped again (and had a FIXME saying that this should be unnecessary). This commit fixes the map() function that maps userspace points to bitmap coordinates. The "re" operator implementation has also been simplified creating a rectangle first and mapping *that* instead of mapping each point individually. --- Userland/Libraries/LibPDF/Renderer.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 85bf2c59dbf..8a97f0fcb3a 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -42,10 +42,18 @@ static Gfx::Path rect_path(float x, float y, float width, float height) return path; } +template +static void rect_path(Gfx::Path& path, Gfx::Rect rect) +{ + return rect_path(path, rect.x(), rect.y(), rect.width(), rect.height()); +} + template static Gfx::Path rect_path(Gfx::Rect rect) { - return rect_path(rect.x(), rect.y(), rect.width(), rect.height()); + Gfx::Path path; + rect_path(path, rect); + return path; } Renderer::Renderer(RefPtr document, Page const& page, RefPtr bitmap, RenderingPreferences rendering_preferences) @@ -229,14 +237,8 @@ RENDERER_HANDLER(path_close) RENDERER_HANDLER(path_append_rect) { - auto pos = map(args[0].to_float(), args[1].to_float()); - auto size = map(Gfx::FloatSize { args[2].to_float(), args[3].to_float() }); - - // FIXME: Why do we need to flip the y axis of rectangles here? The coordinates - // in the PDF file seem to be correct, with the same flipped-ness as - // everything else in a PDF file. - pos.set_y(m_bitmap->height() - pos.y() - size.height()); - rect_path(m_current_path, pos.x(), pos.y(), size.width(), size.height()); + auto rect = Gfx::FloatRect(args[0].to_float(), args[1].to_float(), args[2].to_float(), args[3].to_float()); + rect_path(m_current_path, map(rect)); return {}; } @@ -624,8 +626,7 @@ RENDERER_TODO(compatibility_end) template Gfx::Point Renderer::map(T x, T y) const { - auto mapped = state().ctm.map(Gfx::Point { x, y }); - return { mapped.x(), static_cast(m_bitmap->height()) - mapped.y() }; + return state().ctm.map(Gfx::Point { x, y }); } template