Skip to content

Commit

Permalink
error when surface type and extension are not matching
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeBouton committed Mar 6, 2020
1 parent dae3220 commit d4b04d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/rendermodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ function Base.write(filename::String, c::CairoSurface)
if ext == "png"
write_to_png(c, filename)
elseif ext == "svg" || ext == "pdf"
write_stream(filename, c)
typ = ccall((:cairo_surface_get_type, Cairo.libcairo), Cint, (Ptr{Nothing},), c.ptr)
if ( typ == Cairo.CAIRO_SURFACE_TYPE_PDF && ext == "pdf" ) ||
( typ == Cairo.CAIRO_SURFACE_TYPE_SVG && ext == "svg" )
write_stream(filename, c)
else
surf_type = typ == Cairo.CAIRO_SURFACE_TYPE_PDF ? "CairoPDFSurface" : "CairoSVGSurface"
throw(ErrorException("""
AutoViz WriteError: Surface type mismatch! You tried to write a $surf_type to a .$ext file.
To solve this problem either change the file extension to match the surface type or change the surface type passed to the render function using the keyword `surface=`.
"""))
end
else

This comment has been minimized.

Copy link
@mattuntergassmair

mattuntergassmair Mar 7, 2020

Collaborator

Niiiiice

throw("AutoViz write error: this file extension is not supported, supported extensions are 'png', 'pdf', 'svg'.")
end
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ end
c = render([roadway], camera=camera,
surface=AutoViz.CairoRGBSurface(AutoViz.canvas_width(camera), AutoViz.canvas_height(camera)))
write("out.png", c)

# try to write svg surface to pdf
c = render([roadway], camera=camera)
@test_throws ErrorException write("out.pdf", c)

# try to write pdf surface to svg
c = render([roadway], camera=camera,
surface=AutoViz.CairoPDFSurface(IOBuffer(), AutoViz.canvas_width(camera), AutoViz.canvas_height(camera)))
@test_throws ErrorException write("out.svg", c)

# png should always work
c = render([roadway], camera=camera)
write("out2.png", c)
end

@testset "vehicle rendering" begin
Expand Down

0 comments on commit d4b04d5

Please sign in to comment.