SVG Linear Gradient does not work in Firefox 104 - svg

In Firefox 104 on Windows 10, the following SVG file shows as blank, empty:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2769.2 1615.4">
<defs>
<style>
.fil1 {fill:url(#id0)}
</style>
<linearGradient id="id0" gradientUnits="userSpaceOnUse" x1="230.8" y1="2394.2" x2="2538.5" y2="2394.2">
<stop offset="0" style="stop-opacity:1; stop-color:#667DD1"/>
<stop offset="1" style="stop-opacity:1; stop-color:#8C9DDC"/>
</linearGradient>
</defs>
<g>
<polygon class="fil1" points="230.8,230.8 2538.5,230.8 2538.5,1384.6 230.8,1384.6 "/>
</g>
</svg>
Other browsers (e.g. Chrome) show the desired result: a rectangle with a gradient fill. The SVG appears to be empty in Firefox because Firefox does not show the fill.
Why does this work in other browsers, but not in Firefox?
In case it's of any interest: the example SVG presented here was exported from CorelDRAW 2021. I've removed extraneous markup from the original file.
A similar question already exists; why this new question?
This is a follow-on to the existing question "SVG Linear Gradient does not work in Firefox".
I originally—and, I acknowledge, incorrectly—posted this content as an "answer" to that existing question, because I could not post a code block as a comment to an answer for that existing question, and because I mistakenly believed that asking a new question was the wrong thing to do.
Here, in asking this question, I am following the advice of Robert Longson, the author of the answer to that original question, who offered me the following advice in a comment:
If it's not an answer to this question then you should delete it and ask a separate question. You can always include a link to this question in your new question so that it's not closed as a duplicate.
Done! Thanks for the advice, Robert!

Answering my own question...
In brief
User (my) error. I was inadvertently using UTF-16 character encoding instead of UTF-8.
The UTF-16 SVG file renders okay in Chrome, but not in Firefox.
In more complex SVG examples that include objects with single-color fills, everything except the gradient fills renders okay. The lack of gradient fills is a symptom of this character encoding issue.
In (embarrassing, to me) detail
Thanks to Robert Longson and ccprog for their comments on my question, which led me to this answer.
The snippet in my question works for me in situ, too, in Firefox 104 on Windows 10. I thought I'd tested it back in my now-deleted answer in the original question, but I've just opened that question in Firefox 104 on Windows 10 (where I can still see that now-deleted answer, although I understand others might not) and... it works there, too. I thought I'd tested it in that very context, but this tells me that I hadn't.
I'm very embarrassed about that, but I have to admit to it.
Mystified, I copied'n'pasted the working snippet from this question to a stand-alone .svg file. Again, that new .svg renders just fine in Firefox 104 on Windows 10.
So I compared that new .svg file with the .svg file that doesn't work. The comparison did not highlight any content differences, but did highlight that the two files had different character encodings. The comparison reported the file that does not work in Firefox (but works in Chrome) as "ISO-10646-UCS-2 BOM"; the new .svg file, which works in Firefox, as Window-1252.
Meanwhile, my text editor (I'm using jEdit) reports the character encoding of the two files as x-UTF-16LE-BOM and UTF-8, respectively.
My SVG files are typically UTF-8 (which, I understand, for printable characters with byte value 127 and below, matches Windows-1252; so I can understand
the comparison reporting that file as Windows-1252).
I currently have no idea how I changed the encoding of the problematic file from UTF-8 to UTF-16. I'll look into that. But, that's my problem.

Related

ImageMagick convert creates white output

I try to convert an svg to png with ImageMagick ImageMagick-6.8.9-Q8 under Windows 7 (64-bit)
I tried this code:
C:\>convert -monitor -define registry:temporary-path=D:\ sourceSVG.svg targetPNG.png
I use -define registry... because I dont have enough space in C:. There are no errors printed in the prompt but the output PNG is all white. There is not a single pixel in any other colour. The SVG seems to be correct, I could open it with IrfanView.
This is how the SVG looks (it has over 3500 polygons):
<svg height="40900" width="49500">
<polygon points="15968.0,3603.0 15924.0,4238.0 16061.0,4234.0 16140.0,4203.0 16177.0,3661.0 15968.0,3603.0" style="fill:#FFFFA5;" />
<polygon points="23738.0,5019.0 23738.0,5020.0 23739.0,5020.0 23739.0,5019.0 23738.0,5019.0" style="fill:#FFBD00;" />
</svg>
What could be the problem? Propably too much polygons right? But there wasn't any errors.. How can I fix this problem?
Edit: In order to find out where the problem lies a tried a couple of things:
I checked whether the problem is caused because of too many polygons. So I deleted all polygons but two. Still same problem
I checked whether the problem is cause because of the ".0" so I removed them to have Integers. Still same problem
I checked wether the problem is caused by a too big width/length. So I halved the width/length and made sure to delete all polygons wich lied beyond the new width/length ( ~24000 each). Still same problem. I halved them again (~12000 each) and one polygon was shown and the other one was missing (there were 2 polygons in the svg)

Arrows in SVG aren't rotated when rendered by browsers

I created an SVG file, and in inkscape it looks like this:
But when I render it by a browser, the arrows get screwed up:
This (above) is the actual svg (link), and in case it renders correctly in your browser, here is how I see it (this time it's a screenshot in png):
It's the same in the latest Firefox and Chrome.
This file was created in inkscape 0.48 on Windows, and when I re-open it in inkscape, it renders correctly. Is there a way to make the browser rotate the arrows?
There are bug reports of this for Chrome, Firefox, Inkscape, and Wikimedia. It turns out that some renderers get the arrow direction wrong when just one handle, the one at the beginning of the curve, has zero length. Currently, Firefox, Inkscape, and LibreOffice Write get it right, while Chrome gets it wrong.
To create an example of such a line, draw a line in Inkscape, then add a curved midpoint. Inkscape then makes both segments Bezier curves, but the end segments have zero length handles. If you then delete the midpoint, Inkscape will try to match the curve, and will create non-zero length handles for the endpoints.
Reported as bug in Firefox in 2015, and fixed
Reported as bug in Chrome in 2015, and not fixed
Reported as bug in Inkscape in 2006, blamed on user and closed as "out of date" in 2009
Reported as bug in Wikimedia in 2015, by me
Discussion of ambiguity in SVG spec
A fix that I have noticed in Inkscape is to first select the "edit paths by nodes" option, and select each endpoint and select the option to "make the selected nodes smooth" from the path editing toolbar.
I found the solution:
The problem was that for these lines Bezier curves were used, and even though the lines were straight, it caused the problem. Once I replaced the curves with "diagram connectors", the problem disappeared.
You're using degenerate bezier curves which display as straight lines. Neither Chrome nor Firefox prior to version 38 cope with these when determining marker angles.
This has been fixed in Firefox 38 by bug 1129854. I think there's an equivalent Chrome bug too.

get actual height of svg bezier curved path

This is more of a bezier question than an SVG question, but here goes...
given the following code:
<svg xmlns="http://www.w3.org/2000/svg" baseProfile="full" viewBox="0 0 400 400" baseProfile="full">
<path d="M0,0 C20,400 30,200 50,100" fill="#FF0000" stroke="#000000"/>
</svg>
Using [path].getBBox() returns me a height of 400, since the 1st control point of the curve is at 400 y, and this makes sense(ish).
But what I really need is the actual height of the object as rendered, not the box that contains all points used in its drawing.
I'm assuming I'm just going to have to figure it out myself? And if that's the case, does anybody have any suggestions for where to go to find such mathy things? I did some googling, but I'm not really coming up with anything (it's very possible I'm not phrasing the question correctly, some help in that regard would be super nice as well).
You are doing it right, it's a WebKit bug. If you try getBBox on Firefox or even Internet Explorer, it's fine.
If you need a workaround, give a look at the implementation done for SVG-edit.

SVG 'maskContentUnits' not rendering properly in mobile safari

I have an SVG I am building with Javascript. I load in a large SVG file, break it apart into pieces all drawn with paths, and place each element in my page. I'm only using these SVGs as masks for other images I am loading. Basically my structure is like the following.
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="26.750152587890625 10.117172241210938 197.24969482421875 348.1596221923828" width="197.25" height="348.16015625">
<mask id="designMask" maskContentUnits="objectBoundingBox">
<g id="CutContour1bg">
<path d="[my path coords]" style="fill:#FFFFFF;">
</g>
</mask>
<image href="http://myImage.jpg" style="mask: url(#designMask);" width="800px" height="800px" x="26.75" y="10.1171875">
</svg>
This renders the image being masked by my SVG perfect, in FF, IE9, Chrome, Safari 5.1 (desktop). In mobile safari however, the image does not render properly. I trace out coords of the mask, they are all correct. In FF I can see the SVG load (all black) then disappear as it becomes the mask. (I am waiting until the design is loaded, then wrap my <g> with <mask> since FF has an issue looking for the mask before the content is loaded.
This tells me the mask's position is exactly where it needs to be, but the maskContentUnits are not. They remain in the top left corner instead of the object's bounding box, like I'm telling it. I can barely see part of the image in the mask, so the mask units are correct, but I cannot get the maskContentUnits to work or be read in mobile safari.
Has anyone ever seen this issue, or any idea how to correct it? I hate having this work everywhere except mobile safari, as it is meant to mostly work on mobile... which defeats its not purpose haha.
Thanks!
I haven't found a way to make maskContentUnits work properly in mobile safari yet, I'm pretty sure it's just not recognized yet like other browsers. But I figured out a 'hack' to make the example work.
The issue is, the mask area resides in the top left corner of the browser, rather than of the svg object being used as the mask. So if you have an svg in the middle of your page, the image being masked will not follow the same positioning.
The way I found for it to work, is, I wrapped the svg inside a div with the same width as the svg, and modify the position of the div instead of the svg. This way the mask is technically still in the 'top left' corner, but of the div rather than the offset position of the svg.
If anyone finds a better way, to make maskContentUnits render proper in mobile safari, I'd like to hear it!

SVG to PNG Conversion with ImageMagick doesn't handle defs?

The answer is on the answer I flagged, but is in the comments. Basically, see here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=18816
Run this on Mac with Ports:
sudo port install ImageMagick +rsvg
Here's my original Question:
My test.svg file is here: https://gist.github.com/2727243
Imagemagick version: ImageMagick 6.7.6-9 2012-05-18 Q16 (just updated today)
I have a SVG file the does a defs -> symbol to define an arrow symbol. Later on in the document I xlink:use that symbol. This works fine in Chrome and the arrow comes up and is in its proper position:
However, if I convert the image via imagemagick like so:
convert test.svg test.png
It doesn't work. Here's what I get:
The "use" appears to be ignored completely, and instead the arrow is rendering at the def. At least, near as I can tell.
Am I formatting my SVG wrong for Imagemagick to handle this scenario? Or is this just something that Imagemagick doesn't support properly? I've been looking all over for a definitive answer and can't find one, one way or the other.
I also tried the SVG here: http://www.carto.net/svg/samples/symbol.svg and the symbols did NOT work. So I'm leaning toward Imagemagick being the culprit.
UPDATE: I tried changing the def to the following:
<symbol id="arrow" x="25">
In the browser, the arrow DOES move 25 to the right, but in Imagemagick it sticks to (0,0). This was leading me to believe that it's less about the symbol definition and more about it not translating it properly. However, even if I try a straight transform on the "use" and, even better, remove the "use" entirely, the arrow still appears.
Update: This seams to be a bug in the default SVG renderar in ImageMagick that ignores x and y on use tags (and also ignores transform attributes on g tags). Try to use the rsvg backend instead.
Does it work if you change:
<use xlink:href="#arrow" x="20" y="50"></use>
To:
<g transform="translate(20 50)">
<use xlink:href="#arrow"></use>
</g>
Can you give more detail as to why you have to use IM? If it turns out that IM is useless in this case, surely you would have to use something else? I second Inkscape for this.
That said, perhaps you can still use IM if you're willing to do some XML manipulation. I presume the contents of a symbol could be copied to their <use> locations individually; it's a messy workaround if you have lots of uses of the same symbol, but if you want to stick with IM it may be your only way forward. I'm working on the theory that it's the symbol system that is confusing IM - if the path is 'inline' it might be ok.

Resources