Skip to content

Commit

Permalink
CairoMakie: Allow restricting PDF version
Browse files Browse the repository at this point in the history
  • Loading branch information
barucden committed May 9, 2024
1 parent bbf8e78 commit 21ec306
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
20 changes: 20 additions & 0 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ function get_render_type(surface::Cairo.CairoSurface)
typ == Cairo.CAIRO_SURFACE_TYPE_IMAGE && return IMAGE
return IMAGE # By default assume that the render type is IMAGE
end

const CAIRO_PDF_VERSION_1_4 = 0
const CAIRO_PDF_VERSION_1_5 = 1
const CAIRO_PDF_VERSION_1_6 = 2
const CAIRO_PDF_VERSION_1_7 = 3

function restrict_pdf_version!(surface::Cairo.CairoSurface, pdf_version::String)
@assert surface.ptr != C_NULL
versions = Dict("1.4" => CAIRO_PDF_VERSION_1_4,
"1.5" => CAIRO_PDF_VERSION_1_5,
"1.6" => CAIRO_PDF_VERSION_1_6,
"1.7" => CAIRO_PDF_VERSION_1_7)
if !haskey(versions, pdf_version)
versionlist = join(sort(collect(keys(versions))), ", ")
throw(ArgumentError("PDF version must be one of $versionlist (received '$pdf_version')"))
end
cairo_pdf_version = versions[pdf_version]
ccall((:cairo_pdf_surface_restrict_to_version, Cairo.libcairo), Nothing,
(Ptr{UInt8},Int32), surface.ptr, cairo_pdf_version)
end
11 changes: 11 additions & 0 deletions CairoMakie/src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct ScreenConfig
antialias::Symbol
visible::Bool
start_renderloop::Bool # Only used to satisfy the interface for record using `Screen(...; start_renderloop=false)` for GLMakie
pdf_version::String
end

function device_scaling_factor(rendertype, sc::ScreenConfig)
Expand Down Expand Up @@ -213,6 +214,11 @@ function apply_config!(screen::Screen, config::ScreenConfig)
aa = to_cairo_antialias(config.antialias)
Cairo.set_antialias(context, aa)
set_miter_limit(context, 2.0)

if get_render_type(surface) === PDF
restrict_pdf_version!(surface, config.pdf_version)
end

screen.antialias = aa
screen.device_scaling_factor = dsf
screen.config = config
Expand Down Expand Up @@ -285,6 +291,11 @@ function Screen(scene::Scene, config::ScreenConfig, surface::Cairo.CairoSurface)
aa = to_cairo_antialias(config.antialias)
Cairo.set_antialias(ctx, aa)
set_miter_limit(ctx, 2.0)

if get_render_type(surface) === PDF
restrict_pdf_version!(surface, config.pdf_version)
end

return Screen{get_render_type(surface)}(scene, surface, ctx, dsf, aa, config.visible, config)
end

Expand Down
16 changes: 16 additions & 0 deletions CairoMakie/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ functions = [:volume, :volume!, :uv_mesh]
missing_images, scores = ReferenceTests.record_comparison(recording_dir)
ReferenceTests.test_comparison(scores; threshold = 0.05)
end

@testset "restrict PDF version" begin
filename = "figure.pdf"
for version in ["1.4", "1.5", "1.6", "1.7"]
fig = Figure()
try
save(filename, fig, pdf_version=version)
open(filename) do f
magic_number = String(read(f, sizeof("%PDF-X.Y")))
@test magic_number == "%PDF-$version"
end
finally
rm(filename)
end
end
end
3 changes: 2 additions & 1 deletion src/theming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ const MAKIE_DEFAULT_THEME = Attributes(
pt_per_unit = 0.75,
antialias = :best,
visible = true,
start_renderloop = false
start_renderloop = false,
pdf_version = "1.7"
),

GLMakie = Attributes(
Expand Down

0 comments on commit 21ec306

Please sign in to comment.