Skip to content

Commit

Permalink
Fix broken SVGs by not using `Cairo.pattern_set_extend(p, Cairo.EXTEN…
Browse files Browse the repository at this point in the history
…D_PAD)` (#3967)

* Fix broken SVGs by not using `Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD)`

* add changelog
  • Loading branch information
jkrumbiegel authored Jun 17, 2024
1 parent cae34cc commit 1346cb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
9 changes: 6 additions & 3 deletions CairoMakie/src/infrastructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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][]
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1346cb9

Please sign in to comment.