Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG arrowheads missing since 0.11 #3715

Open
deffi opened this issue Mar 18, 2024 · 13 comments
Open

SVG arrowheads missing since 0.11 #3715

deffi opened this issue Mar 18, 2024 · 13 comments
Labels
bug Something isn't working svg Anything about the file format for people who like to have fun.

Comments

@deffi
Copy link

deffi commented Mar 18, 2024

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

@deffi deffi added the bug Something isn't working label Mar 18, 2024
@laurmaedje
Copy link
Member

cc: @LaurenzV

@laurmaedje laurmaedje added the svg Anything about the file format for people who like to have fun. label Mar 18, 2024
@LaurenzV
Copy link
Sponsor 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.

@LaurenzV
Copy link
Sponsor Collaborator

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

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

@wenbin-liu
Copy link

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

@LaurenzV
Copy link
Sponsor 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.

@wenbin-liu
Copy link

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

@deffi
Copy link
Author

deffi commented May 21, 2024

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

@LaurenzV
Copy link
Sponsor Collaborator

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

@KronosTheLate
Copy link
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)`

@LaurenzV
Copy link
Sponsor 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.

@KronosTheLate
Copy link
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.

@LaurenzV
Copy link
Sponsor 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...

@deffi
Copy link
Author

deffi commented May 27, 2024

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-strokefill:#000000
  • stroke:context-strokestroke:none

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working svg Anything about the file format for people who like to have fun.
Projects
None yet
Development

No branches or pull requests

5 participants