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

feat: [7877] Allow loading SVG with viewBox only #7878

Closed

Conversation

zero41120
Copy link
Contributor

Description of change
  • Update width/height SVGResource's error handler.
  • Instead of reporting 0 and abort, fallback to parse the base64 and get viewBox's width and height
  • If viewBox is also not available, then abort.
  • Added 2 unit test
    • Get size from SVGResource.parseWidthHeightFromViewBox(svgString)
    • Get scaled size from SVGResource.parseWidthHeightFromViewBox(svgString, size)
      image
Pre-Merge Checklist
  • Tests and/or benchmarks are included
  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

@zero41120 zero41120 changed the title feat: 7877 Allow loading SVG with viewBox only feat: [7877] Allow loading SVG with viewBox only Oct 15, 2021
* @return {PIXI.ISize} scaled viewBox size if parse success, return (0, 0)
* otherwise.
*/
static parseWidthHeightFromViewBox(svg: string, scaleTo?: ISize): ISize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this private?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this name is pretty verbose, why not parseViewBoxSize?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About unit test, I had something similar to load svg inline.
However, it will not really test this particular issue since it is a Firefox issue.
When I invoke the unit test framework, it is running on chrome, by default chrome will never use this.
Any suggestion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood that this is only specific to Firefox, that's fine.

try
{
const parser = new DOMParser();
const svgDocument = parser.parseFromString(svg, 'image/svg+xml');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only problem with this is parsing SVG with DOMParser is that it isn't supported in IE9. We have not yet dropped support for this browser, so we need to find another approach.

https://caniuse.com/mdn-api_domparser_parsefromstring_svg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I check on MDN it says fully supported on IE9. I don't really have access to IE9 to verify which one.

Quick google shows something about it works on IE9. Do you have recommended method to verify it on IE9?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See IE10 for SVG only. But application/xml does work.

Screen Shot 2021-10-18 at 1 09 51 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of parsing, would you suggest a simple RegExp to grab the viewBox value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would work

Copy link
Collaborator

@ivanpopelyshev ivanpopelyshev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone is taking reponsibility for svg sizes? Nice!

@bigtimebuddy
Copy link
Member

@zero41120 thanks so much for this contribution and apologies it's taken so long to get a resolution on. I had a chat with @GoodBoyDigital about this and I'm going to close this PR.

This is to workaround a browser specific issue, which is to infer width/height from viewBox on Firefox. Firefox already helpfully provides a warning for this case. The easiest fix, that doesn't rely on any code changes, is to add width and height to your SVG to prevent the warning. We don't think Pixi should be handling invalid data in this case, especially when it adds a lot of complexity to the SVGResource. It's better to nudge developers to do the right thing with their assets and make sure they are valid (even if one browser has more strict requirements).

If someone needs to process lots of SVGs that are formatted this way (without width/height), I'd suggest a node.js script or Webpack plugin or some other build-environment solution which can parse the SVG correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants