Skip to content

SVG arrowheads missing since 0.11 #3715

Closed

Description

@deffi

Description

Since Typst 0.11, some arrow heads in SVG files are no longer rendered.

Example

SVG files (both from Inkscape):
1.svg: 1
2.svg: 2

arrows.typ:

1.svg: #image("1.svg", width: 10.521mm)
2.svg: #image("2.svg", width: 11.656mm)

When compiled with Typst 0.10:
grafik

When compiled with Typst 0.11:
grafik

All file as .zip: arrows.zip

Operating system

Windows, Linux

Activity

  1. added
    bug
    on Mar 18, 2024
  2. laurmaedje commented on Mar 18, 2024

    @laurmaedje
    Member
  3. added
    svg
    on Mar 18, 2024
  4. LaurenzV commented on Mar 18, 2024

    @LaurenzV
    Collaborator

    Interesting. The SVG uses a feature that I only only recently implemented in resvg, but I still can reproduce the bug on main, so looks like there is still some bug.

    As for why it worked in 0.10, I have no idea, in theory it shouldn't work in either of them... But I will take a look.

  5. LaurenzV commented on Mar 18, 2024

    @LaurenzV
    Collaborator

    I submitted a path for this upstream: linebender/resvg#725

    Thanks for the report! Should probably be fixed by the next release.

  6. wenbin-liu commented on Apr 18, 2024

    @wenbin-liu

    Is this bug fixed in resvg? Arrow missing for svg is quite disturbing.

  7. LaurenzV commented on Apr 18, 2024

    @LaurenzV
    Collaborator

    Yes, it's fixed in resvg. Keep in mind that you can work around this though by replacing instances of "context-fill" and "context-stroke" with the color you want in the SVG.

  8. wenbin-liu commented on Apr 18, 2024

    @wenbin-liu

    OK, thanks. Then we just need to wait for typst to update

  9. deffi commented on May 21, 2024

    @deffi
    Author

    It seems like Typst 0.11.1 still uses resvg 0.38.0, which does not yet incorporate the fix.

  10. LaurenzV commented on May 21, 2024

    @LaurenzV
    Collaborator

    Yes, indeed, 0.11.1 was just a patch release for some issues, but it didn't include this one.

  11. KronosTheLate commented on May 27, 2024

    @KronosTheLate
    Contributor

    Until then, if you are in a pinch, you can install the Julia programming language, and run the script below. I am currently using it to convert all my svg illustrations to png files for my masters thesis:

    using Rsvg
    using Cairo
    
    """
    	svg2png(filename_in, overwrite=false, dpi::Real=300)
    
    Load an SVG file `filename_in`, use Rsvg.jl to render the image 
    for cairo, then use Cairo.jl to write that image to a PNG file.
    """
    function svg2png(filename_in, overwrite=false, dpi::Real=300)
    	if !isfile(filename_in)
    		error("The given filename $filename_in returned `false` when passed to `isfile`. Are you sure that the file exists?")
    	end
    	filename_in_noext, input_extension = splitext(filename_in)
    	if input_extension != ".svg"
    		error("Extension of given filename $filename_in is $filename_in_ext, where \".svg\" was expected. Aborting.")
    	end
    	filename_out = filename_in_noext * ".png"
    	if isfile(filename_out) && !overwrite
    		error("Output file $filename_out already exists, and the second positional argument `overwrite` is set to false. Aborting.")
    	end
    	
    	r = Rsvg.handle_new_from_file(filename_in)
    	Rsvg.handle_set_dpi(r, float(dpi))  # Conversion to float due to unnecessary contraints on arguments to Rsvg.handle_set_dpi
    	d = Rsvg.handle_get_dimensions(r)
    	cs = Cairo.CairoImageSurface(d.width,d.height,Cairo.FORMAT_ARGB32)
    	c = Cairo.CairoContext(cs)
    	Rsvg.handle_render_cairo(c,r)
    	
    	Cairo.write_to_png(cs,filename_out)
    	return nothing
    end
    
    function convert_all_svgs(dir, overwrite=false, dpi::Real=300)
    	if !isdir(dir)
    		error("The given directory $dir returned false when passed to `isdir`. Are you sure the directory exists?")
    	end
    	map(readdir(dir, join=true)) do filename
    		if splitext(filename)[2]==".svg"
    			svg2png(filename, overwrite, dpi)
    		end
    	end
    end
    
    # With my svg files inside a folder "illustrations", I 
    # then just run `convert_all_svgs("illustrations", true)`
  12. LaurenzV commented on May 27, 2024

    @LaurenzV
    Collaborator

    I mean, as mentioned above, if all your arrows are in the same color, you could instead just write a script that replaces all instances of context-fill and context-stroke with black, then you don't need to convert the SVGs into bitmap images.

  13. KronosTheLate commented on May 27, 2024

    @KronosTheLate
    Contributor

    Opening the SVG in a text editor and doing that search-and-replace does indeed sound quite a lot easier 😅

    However, it would fail to preserve the colours of the true SVG, no? Sometimes, in the preview (vscode extension), the arrowheads were indeed filled as black, which was quite visually disruptive.

  14. LaurenzV commented on May 27, 2024

    @LaurenzV
    Collaborator

    Opening the SVG in a text editor and doing that search-and-replace does indeed sound quite a lot easier 😅

    Well, no, but if you have a script that converts SVGs into images, it is probably even easier to write a script that reads all SVGs and makes those two replacements, right? 😄 But your choice obviously!

    However, it would fail to preserve the colours of the true SVG, no?

    Yeah, that's true. If your arrows have different colors and it's important to preserve them, then I guess rasterizing is the only option for now...

  15. deffi commented on May 27, 2024

    @deffi
    Author

    You may not even need a script – there are tools that can do search and replace across multiple files: Eclipse, VS Code, sed (using the --in-place option), and probably many more. As @LaurenzV noted, this is only practical if all of the arrows are the same color.

    In my case, the following two replacements (applied to ≈80 files with Eclipse) worked perfectly:

    • fill:context-stroke → fill:#000000
    • stroke:context-stroke → stroke:none
  16. LaurenzV commented on Jun 6, 2024

    @LaurenzV
    Collaborator

    Has been fixed on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsvg

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions