From 1346cb9aa422be8f6a590f31906a16589c47e3cd Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel <22495855+jkrumbiegel@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:28:29 +0200 Subject: [PATCH] Fix broken SVGs by not using `Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD)` (#3967) * Fix broken SVGs by not using `Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD)` * add changelog --- CHANGELOG.md | 1 + CairoMakie/src/infrastructure.jl | 9 ++++++--- CairoMakie/src/primitives.jl | 9 ++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 790ac9498c0..6433b0d463a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## [Unreleased] +- CairoMakie: Fix broken SVGs when using non-interpolated image primitives, for example Colorbars, with recent Cairo versions [#3967](https://github.com/MakieOrg/Makie.jl/pull/3967). - CairoMakie: Add argument `pdf_version` to restrict the PDF version when saving a figure as a PDF [#3845](https://github.com/MakieOrg/Makie.jl/pull/3845). - CairoMakie: Fix incorrect scaling factor for SVGs with Cairo_jll 1.18 [#3964](https://github.com/MakieOrg/Makie.jl/pull/3964). diff --git a/CairoMakie/src/infrastructure.jl b/CairoMakie/src/infrastructure.jl index 46e0e3bcc2d..1b54a8fd8e5 100644 --- a/CairoMakie/src/infrastructure.jl +++ b/CairoMakie/src/infrastructure.jl @@ -143,7 +143,7 @@ end # instead of the whole Scene # - Recognize when a screen is an image surface, and set scale to render the plot # at the scale of the device pixel -function draw_plot_as_image(scene::Scene, screen::Screen, primitive::Plot, scale::Number = 1) +function draw_plot_as_image(scene::Scene, screen::Screen{RT}, primitive::Plot, scale::Number = 1) where RT # you can provide `p.rasterize = scale::Int` or `p.rasterize = true`, both of which are numbers # Extract scene width in device indepentent units @@ -163,8 +163,11 @@ function draw_plot_as_image(scene::Scene, screen::Screen, primitive::Plot, scale # Cairo.scale(screen.context, w / scr.surface.width, h / scr.surface.height) Cairo.set_source_surface(screen.context, scr.surface, 0, 0) p = Cairo.get_source(scr.context) - # this is needed to avoid blurry edges - Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) + if RT !== SVG + # this is needed to avoid blurry edges in png renderings, however since Cairo 1.18 this + # setting seems to create broken SVGs + Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) + end # Set filter doesn't work!? Cairo.pattern_set_filter(p, Cairo.FILTER_BILINEAR) Cairo.fill(screen.context) diff --git a/CairoMakie/src/primitives.jl b/CairoMakie/src/primitives.jl index b594c79b11d..486afd9eea3 100644 --- a/CairoMakie/src/primitives.jl +++ b/CairoMakie/src/primitives.jl @@ -782,7 +782,7 @@ premultiplied_rgba(a::AbstractArray{<:Color}) = RGBA.(a) premultiplied_rgba(r::RGBA) = RGBA(r.r * r.alpha, r.g * r.alpha, r.b * r.alpha, r.alpha) premultiplied_rgba(c::Colorant) = premultiplied_rgba(RGBA(c)) -function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Union{Heatmap, Image})) +function draw_atomic(scene::Scene, screen::Screen{RT}, @nospecialize(primitive::Union{Heatmap, Image})) where RT ctx = screen.context image = primitive[3][] xs, ys = primitive[1][], primitive[2][] @@ -858,8 +858,11 @@ function draw_atomic(scene::Scene, screen::Screen, @nospecialize(primitive::Unio Cairo.scale(ctx, w / s.width, h / s.height) Cairo.set_source_surface(ctx, s, 0, 0) p = Cairo.get_source(ctx) - # this is needed to avoid blurry edges - Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) + if RT !== SVG + # this is needed to avoid blurry edges in png renderings, however since Cairo 1.18 this + # setting seems to create broken SVGs + Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) + end filt = interpolate ? Cairo.FILTER_BILINEAR : Cairo.FILTER_NEAREST Cairo.pattern_set_filter(p, filt) Cairo.fill(ctx)